bindcmd.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. import command_sets
  25. from xml.dom import minidom
  26. import isc
  27. import isc.cc.data
  28. import http.client
  29. import json
  30. import inspect
  31. import pprint
  32. import ssl, socket
  33. import os, time, random, re
  34. import getpass
  35. from hashlib import sha1
  36. import csv
  37. import pwd
  38. import getpass
  39. import copy
  40. try:
  41. from collections import OrderedDict
  42. except ImportError:
  43. from bindctl.mycollections import OrderedDict
  44. # if we have readline support, use that, otherwise use normal stdio
  45. try:
  46. import readline
  47. # This is a fix for the problem described in
  48. # http://bind10.isc.org/ticket/1345
  49. # If '-' is seen as a word-boundary, the final completion-step
  50. # (as handled by the cmd module, and hence outside our reach) can
  51. # mistakenly add data twice, resulting in wrong completion results
  52. # The solution is to remove it.
  53. delims = readline.get_completer_delims()
  54. delims = delims.replace('-', '')
  55. readline.set_completer_delims(delims)
  56. my_readline = readline.get_line_buffer
  57. except ImportError:
  58. my_readline = sys.stdin.readline
  59. CSV_FILE_NAME = 'default_user.csv'
  60. CONFIG_MODULE_NAME = 'config'
  61. CONST_BINDCTL_HELP = """
  62. usage: <module name> <command name> [param1 = value1 [, param2 = value2]]
  63. Type Tab character to get the hint of module/command/parameters.
  64. Type \"help(? h)\" for help on bindctl.
  65. Type \"<module_name> help\" for help on the specific module.
  66. Type \"<module_name> <command_name> help\" for help on the specific command.
  67. \nAvailable module names: """
  68. class ValidatedHTTPSConnection(http.client.HTTPSConnection):
  69. '''Overrides HTTPSConnection to support certification
  70. validation. '''
  71. def __init__(self, host, ca_certs):
  72. http.client.HTTPSConnection.__init__(self, host)
  73. self.ca_certs = ca_certs
  74. def connect(self):
  75. ''' Overrides the connect() so that we do
  76. certificate validation. '''
  77. sock = socket.create_connection((self.host, self.port),
  78. self.timeout)
  79. if self._tunnel_host:
  80. self.sock = sock
  81. self._tunnel()
  82. req_cert = ssl.CERT_NONE
  83. if self.ca_certs:
  84. req_cert = ssl.CERT_REQUIRED
  85. self.sock = ssl.wrap_socket(sock, self.key_file,
  86. self.cert_file,
  87. cert_reqs=req_cert,
  88. ca_certs=self.ca_certs)
  89. class BindCmdInterpreter(Cmd):
  90. """simple bindctl example."""
  91. def __init__(self, server_port='localhost:8080', pem_file=None,
  92. csv_file_dir=None):
  93. Cmd.__init__(self)
  94. self.location = ""
  95. self.prompt_end = '> '
  96. if sys.stdin.isatty():
  97. self.prompt = self.prompt_end
  98. else:
  99. self.prompt = ""
  100. self.ruler = '-'
  101. self.modules = OrderedDict()
  102. self.add_module_info(ModuleInfo("help", desc = "Get help for bindctl."))
  103. self.server_port = server_port
  104. self.conn = ValidatedHTTPSConnection(self.server_port,
  105. ca_certs=pem_file)
  106. self.session_id = self._get_session_id()
  107. self.config_data = None
  108. if csv_file_dir is not None:
  109. self.csv_file_dir = csv_file_dir
  110. else:
  111. self.csv_file_dir = pwd.getpwnam(getpass.getuser()).pw_dir + \
  112. os.sep + '.bind10' + os.sep
  113. def _get_session_id(self):
  114. '''Generate one session id for the connection. '''
  115. rand = os.urandom(16)
  116. now = time.time()
  117. session_id = sha1(("%s%s%s" %(rand, now,
  118. socket.gethostname())).encode())
  119. digest = session_id.hexdigest()
  120. return digest
  121. def run(self):
  122. '''Parse commands from user and send them to cmdctl. '''
  123. try:
  124. if not self.login_to_cmdctl():
  125. return 1
  126. self.cmdloop()
  127. print('\nExit from bindctl')
  128. return 0
  129. except FailToLogin as err:
  130. # error already printed when this was raised, ignoring
  131. return 1
  132. except KeyboardInterrupt:
  133. print('\nExit from bindctl')
  134. return 0
  135. except socket.error as err:
  136. print('Failed to send request, the connection is closed')
  137. return 1
  138. except http.client.CannotSendRequest:
  139. print('Can not send request, the connection is busy')
  140. return 1
  141. def _get_saved_user_info(self, dir, file_name):
  142. ''' Read all the available username and password pairs saved in
  143. file(path is "dir + file_name"), Return value is one list of elements
  144. ['name', 'password'], If get information failed, empty list will be
  145. returned.'''
  146. if (not dir) or (not os.path.exists(dir)):
  147. return []
  148. try:
  149. csvfile = None
  150. users = []
  151. csvfile = open(dir + file_name)
  152. users_info = csv.reader(csvfile)
  153. for row in users_info:
  154. users.append([row[0], row[1]])
  155. except (IOError, IndexError) as err:
  156. print("Error reading saved username and password from %s%s: %s" % (dir, file_name, err))
  157. finally:
  158. if csvfile:
  159. csvfile.close()
  160. return users
  161. def _save_user_info(self, username, passwd, dir, file_name):
  162. ''' Save username and password in file "dir + file_name"
  163. If it's saved properly, return True, or else return False. '''
  164. try:
  165. if not os.path.exists(dir):
  166. os.mkdir(dir, 0o700)
  167. csvfilepath = dir + file_name
  168. csvfile = open(csvfilepath, 'w')
  169. os.chmod(csvfilepath, 0o600)
  170. writer = csv.writer(csvfile)
  171. writer.writerow([username, passwd])
  172. csvfile.close()
  173. except IOError as err:
  174. print("Error saving user information:", err)
  175. print("user info file name: %s%s" % (dir, file_name))
  176. return False
  177. return True
  178. def login_to_cmdctl(self):
  179. '''Login to cmdctl with the username and password inputted
  180. from user. After the login is sucessful, the username and
  181. password will be saved in 'default_user.csv', when run the next
  182. time, username and password saved in 'default_user.csv' will be
  183. used first.
  184. '''
  185. users = self._get_saved_user_info(self.csv_file_dir, CSV_FILE_NAME)
  186. for row in users:
  187. param = {'username': row[0], 'password' : row[1]}
  188. try:
  189. response = self.send_POST('/login', param)
  190. data = response.read().decode()
  191. except socket.error as err:
  192. print("Socket error while sending login information:", err)
  193. raise FailToLogin()
  194. if response.status == http.client.OK:
  195. # Is interactive?
  196. if sys.stdin.isatty():
  197. print(data + ' login as ' + row[0])
  198. return True
  199. count = 0
  200. print("[TEMP MESSAGE]: username :root password :bind10")
  201. while True:
  202. count = count + 1
  203. if count > 3:
  204. print("Too many authentication failures")
  205. return False
  206. username = input("Username:")
  207. passwd = getpass.getpass()
  208. param = {'username': username, 'password' : passwd}
  209. try:
  210. response = self.send_POST('/login', param)
  211. data = response.read().decode()
  212. print(data)
  213. except socket.error as err:
  214. print("Socket error while sending login information:", err)
  215. raise FailToLogin()
  216. if response.status == http.client.OK:
  217. self._save_user_info(username, passwd, self.csv_file_dir,
  218. CSV_FILE_NAME)
  219. return True
  220. def _update_commands(self):
  221. '''Update the commands of all modules. '''
  222. for module_name in self.config_data.get_config_item_list():
  223. self._prepare_module_commands(self.config_data.get_module_spec(module_name))
  224. def _send_message(self, url, body):
  225. headers = {"cookie" : self.session_id}
  226. self.conn.request('GET', url, body, headers)
  227. res = self.conn.getresponse()
  228. return res.status, res.read()
  229. def send_GET(self, url, body = None):
  230. '''Send GET request to cmdctl, session id is send with the name
  231. 'cookie' in header.
  232. '''
  233. status, reply_msg = self._send_message(url, body)
  234. if status == http.client.UNAUTHORIZED:
  235. if self.login_to_cmdctl():
  236. # successful, so try send again
  237. status, reply_msg = self._send_message(url, body)
  238. if reply_msg:
  239. return json.loads(reply_msg.decode())
  240. else:
  241. return {}
  242. def send_POST(self, url, post_param = None):
  243. '''Send POST request to cmdctl, session id is send with the name
  244. 'cookie' in header.
  245. Format: /module_name/command_name
  246. parameters of command is encoded as a map
  247. '''
  248. param = None
  249. if (len(post_param) != 0):
  250. param = json.dumps(post_param)
  251. headers = {"cookie" : self.session_id}
  252. self.conn.request('POST', url, param, headers)
  253. return self.conn.getresponse()
  254. def _update_all_modules_info(self):
  255. ''' Get all modules' information from cmdctl, including
  256. specification file and configuration data. This function
  257. should be called before interpreting command line or complete-key
  258. is entered. This may not be the best way to keep bindctl
  259. and cmdctl share same modules information, but it works.'''
  260. if self.config_data is not None:
  261. self.config_data.update_specs_and_config()
  262. else:
  263. self.config_data = isc.config.UIModuleCCSession(self)
  264. self._update_commands()
  265. def precmd(self, line):
  266. if line != 'EOF':
  267. self._update_all_modules_info()
  268. return line
  269. def postcmd(self, stop, line):
  270. '''Update the prompt after every command, but only if we
  271. have a tty as output'''
  272. if sys.stdin.isatty():
  273. self.prompt = self.location + self.prompt_end
  274. return stop
  275. def _prepare_module_commands(self, module_spec):
  276. '''Prepare the module commands'''
  277. module = ModuleInfo(name = module_spec.get_module_name(),
  278. desc = module_spec.get_module_description())
  279. for command in module_spec.get_commands_spec():
  280. cmd = CommandInfo(name = command["command_name"],
  281. desc = command["command_description"])
  282. for arg in command["command_args"]:
  283. param = ParamInfo(name = arg["item_name"],
  284. type = arg["item_type"],
  285. optional = bool(arg["item_optional"]),
  286. param_spec = arg)
  287. if ("item_default" in arg):
  288. param.default = arg["item_default"]
  289. if ("item_description" in arg):
  290. param.desc = arg["item_description"]
  291. cmd.add_param(param)
  292. module.add_command(cmd)
  293. self.add_module_info(module)
  294. def _validate_cmd(self, cmd):
  295. '''validate the parameters and merge some parameters together,
  296. merge algorithm is based on the command line syntax, later, if
  297. a better command line syntax come out, this function should be
  298. updated first.
  299. '''
  300. if not cmd.module in self.modules:
  301. raise CmdUnknownModuleSyntaxError(cmd.module)
  302. module_info = self.modules[cmd.module]
  303. if not module_info.has_command_with_name(cmd.command):
  304. raise CmdUnknownCmdSyntaxError(cmd.module, cmd.command)
  305. command_info = module_info.get_command_with_name(cmd.command)
  306. manda_params = command_info.get_mandatory_param_names()
  307. all_params = command_info.get_param_names()
  308. # If help is entered, don't do further parameter validation.
  309. for val in cmd.params.keys():
  310. if val == "help":
  311. return
  312. params = cmd.params.copy()
  313. if not params and manda_params:
  314. raise CmdMissParamSyntaxError(cmd.module, cmd.command, manda_params[0])
  315. elif params and not all_params:
  316. raise CmdUnknownParamSyntaxError(cmd.module, cmd.command,
  317. list(params.keys())[0])
  318. elif params:
  319. param_name = None
  320. param_count = len(params)
  321. for name in params:
  322. # either the name of the parameter must be known, or
  323. # the 'name' must be an integer (ie. the position of
  324. # an unnamed argument
  325. if type(name) == int:
  326. # lump all extraneous arguments together as one big final one
  327. # todo: check if last param type is a string?
  328. while (param_count > 2 and
  329. param_count > len(command_info.params) - 1):
  330. params[param_count - 2] += " " + params[param_count - 1]
  331. del(params[param_count - 1])
  332. param_count = len(params)
  333. cmd.params = params.copy()
  334. # (-1, help is always in the all_params list)
  335. if name >= len(all_params) - 1:
  336. # add to last known param
  337. if param_name:
  338. cmd.params[param_name] += cmd.params[name]
  339. else:
  340. raise CmdUnknownParamSyntaxError(cmd.module, cmd.command, cmd.params[name])
  341. else:
  342. # replace the numbered items by named items
  343. param_name = command_info.get_param_name_by_position(name, param_count)
  344. cmd.params[param_name] = cmd.params[name]
  345. del cmd.params[name]
  346. elif not name in all_params:
  347. raise CmdUnknownParamSyntaxError(cmd.module, cmd.command, name)
  348. param_nr = 0
  349. for name in manda_params:
  350. if not name in params and not param_nr in params:
  351. raise CmdMissParamSyntaxError(cmd.module, cmd.command, name)
  352. param_nr += 1
  353. # Convert parameter value according parameter spec file.
  354. # Ignore check for commands belongs to module 'config' or 'execute
  355. if cmd.module != CONFIG_MODULE_NAME and\
  356. cmd.module != command_sets.EXECUTE_MODULE_NAME:
  357. for param_name in cmd.params:
  358. param_spec = command_info.get_param_with_name(param_name).param_spec
  359. try:
  360. cmd.params[param_name] = isc.config.config_data.convert_type(param_spec, cmd.params[param_name])
  361. except isc.cc.data.DataTypeError as e:
  362. raise isc.cc.data.DataTypeError('Invalid parameter value for \"%s\", the type should be \"%s\" \n'
  363. % (param_name, param_spec['item_type']) + str(e))
  364. def _handle_cmd(self, cmd):
  365. '''Handle a command entered by the user'''
  366. if cmd.command == "help" or ("help" in cmd.params.keys()):
  367. self._handle_help(cmd)
  368. elif cmd.module == CONFIG_MODULE_NAME:
  369. self.apply_config_cmd(cmd)
  370. elif cmd.module == command_sets.EXECUTE_MODULE_NAME:
  371. self.apply_execute_cmd(cmd)
  372. else:
  373. self.apply_cmd(cmd)
  374. def add_module_info(self, module_info):
  375. '''Add the information about one module'''
  376. self.modules[module_info.name] = module_info
  377. def get_module_names(self):
  378. '''Return the names of all known modules'''
  379. return list(self.modules.keys())
  380. #override methods in cmd
  381. def default(self, line):
  382. self._parse_cmd(line)
  383. def emptyline(self):
  384. pass
  385. def do_help(self, name):
  386. print(CONST_BINDCTL_HELP)
  387. for k in self.modules.values():
  388. n = k.get_name()
  389. if len(n) >= CONST_BINDCTL_HELP_INDENT_WIDTH:
  390. print(" %s" % n)
  391. print(textwrap.fill(k.get_desc(),
  392. initial_indent=" ",
  393. subsequent_indent=" " +
  394. " " * CONST_BINDCTL_HELP_INDENT_WIDTH,
  395. width=70))
  396. else:
  397. print(textwrap.fill("%s%s%s" %
  398. (k.get_name(),
  399. " "*(CONST_BINDCTL_HELP_INDENT_WIDTH - len(k.get_name())),
  400. k.get_desc()),
  401. initial_indent=" ",
  402. subsequent_indent=" " +
  403. " " * CONST_BINDCTL_HELP_INDENT_WIDTH,
  404. width=70))
  405. def onecmd(self, line):
  406. if line == 'EOF' or line.lower() == "quit":
  407. self.conn.close()
  408. return True
  409. if line == 'h':
  410. line = 'help'
  411. Cmd.onecmd(self, line)
  412. def remove_prefix(self, list, prefix):
  413. """Removes the prefix already entered, and all elements from the
  414. list that don't match it"""
  415. if prefix.startswith('/'):
  416. prefix = prefix[1:]
  417. new_list = []
  418. for val in list:
  419. if val.startswith(prefix):
  420. new_val = val[len(prefix):]
  421. if new_val.startswith("/"):
  422. new_val = new_val[1:]
  423. new_list.append(new_val)
  424. return new_list
  425. def complete(self, text, state):
  426. if 0 == state:
  427. self._update_all_modules_info()
  428. text = text.strip()
  429. hints = []
  430. cur_line = my_readline()
  431. try:
  432. cmd = BindCmdParse(cur_line)
  433. if not cmd.params and text:
  434. hints = self._get_command_startswith(cmd.module, text)
  435. else:
  436. hints = self._get_param_startswith(cmd.module, cmd.command,
  437. text)
  438. if cmd.module == CONFIG_MODULE_NAME:
  439. # grm text has been stripped of slashes...
  440. my_text = self.location + "/" + cur_line.rpartition(" ")[2]
  441. list = self.config_data.get_config_item_list(my_text.rpartition("/")[0], True)
  442. hints.extend([val for val in list if val.startswith(my_text[1:])])
  443. # remove the common prefix from the hints so we don't get it twice
  444. hints = self.remove_prefix(hints, my_text.rpartition("/")[0])
  445. except CmdModuleNameFormatError:
  446. if not text:
  447. hints = self.get_module_names()
  448. except CmdMissCommandNameFormatError as e:
  449. if not text.strip(): # command name is empty
  450. hints = self.modules[e.module].get_command_names()
  451. else:
  452. hints = self._get_module_startswith(text)
  453. except CmdCommandNameFormatError as e:
  454. if e.module in self.modules:
  455. hints = self._get_command_startswith(e.module, text)
  456. except CmdParamFormatError as e:
  457. hints = self._get_param_startswith(e.module, e.command, text)
  458. except BindCtlException:
  459. hints = []
  460. self.hint = hints
  461. if state < len(self.hint):
  462. return self.hint[state]
  463. else:
  464. return None
  465. def _get_module_startswith(self, text):
  466. return [module
  467. for module in self.modules
  468. if module.startswith(text)]
  469. def _get_command_startswith(self, module, text):
  470. if module in self.modules:
  471. return [command
  472. for command in self.modules[module].get_command_names()
  473. if command.startswith(text)]
  474. return []
  475. def _get_param_startswith(self, module, command, text):
  476. if module in self.modules:
  477. module_info = self.modules[module]
  478. if command in module_info.get_command_names():
  479. cmd_info = module_info.get_command_with_name(command)
  480. params = cmd_info.get_param_names()
  481. hint = []
  482. if text:
  483. hint = [val for val in params if val.startswith(text)]
  484. else:
  485. hint = list(params)
  486. if len(hint) == 1 and hint[0] != "help":
  487. hint[0] = hint[0] + " ="
  488. return hint
  489. return []
  490. def _parse_cmd(self, line):
  491. try:
  492. cmd = BindCmdParse(line)
  493. self._validate_cmd(cmd)
  494. self._handle_cmd(cmd)
  495. except (IOError, http.client.HTTPException) as err:
  496. print('Error: ', err)
  497. except BindCtlException as err:
  498. print("Error! ", err)
  499. self._print_correct_usage(err)
  500. except isc.cc.data.DataTypeError as err:
  501. print("Error! ", err)
  502. except isc.cc.data.DataTypeError as dte:
  503. print("Error: " + str(dte))
  504. except isc.cc.data.DataNotFoundError as dnfe:
  505. print("Error: " + str(dnfe))
  506. except isc.cc.data.DataAlreadyPresentError as dape:
  507. print("Error: " + str(dape))
  508. except KeyError as ke:
  509. print("Error: missing " + str(ke))
  510. def _print_correct_usage(self, ept):
  511. if isinstance(ept, CmdUnknownModuleSyntaxError):
  512. self.do_help(None)
  513. elif isinstance(ept, CmdUnknownCmdSyntaxError):
  514. self.modules[ept.module].module_help()
  515. elif isinstance(ept, CmdMissParamSyntaxError) or \
  516. isinstance(ept, CmdUnknownParamSyntaxError):
  517. self.modules[ept.module].command_help(ept.command)
  518. def _append_space_to_hint(self):
  519. """Append one space at the end of complete hint."""
  520. self.hint = [(val + " ") for val in self.hint]
  521. def _handle_help(self, cmd):
  522. if cmd.command == "help":
  523. self.modules[cmd.module].module_help()
  524. else:
  525. self.modules[cmd.module].command_help(cmd.command)
  526. def apply_config_cmd(self, cmd):
  527. '''Handles a configuration command.
  528. Raises a DataTypeError if a wrong value is set.
  529. Raises a DataNotFoundError if a wrong identifier is used.
  530. Raises a KeyError if the command was not complete
  531. '''
  532. identifier = self.location
  533. if 'identifier' in cmd.params:
  534. if not identifier.endswith("/"):
  535. identifier += "/"
  536. if cmd.params['identifier'].startswith("/"):
  537. identifier = cmd.params['identifier']
  538. else:
  539. if cmd.params['identifier'].startswith('['):
  540. identifier = identifier[:-1]
  541. identifier += cmd.params['identifier']
  542. # Check if the module is known; for unknown modules
  543. # we currently deny setting preferences, as we have
  544. # no way yet to determine if they are ok.
  545. module_name = identifier.split('/')[1]
  546. if module_name != "" and (self.config_data is None or \
  547. not self.config_data.have_specification(module_name)):
  548. print("Error: Module '" + module_name + "' unknown or not running")
  549. return
  550. if cmd.command == "show":
  551. # check if we have the 'all' argument
  552. show_all = False
  553. if 'argument' in cmd.params:
  554. if cmd.params['argument'] == 'all':
  555. show_all = True
  556. elif 'identifier' not in cmd.params:
  557. # no 'all', no identifier, assume this is the
  558. #identifier
  559. identifier += cmd.params['argument']
  560. else:
  561. print("Error: unknown argument " + cmd.params['argument'] + ", or multiple identifiers given")
  562. return
  563. values = self.config_data.get_value_maps(identifier, show_all)
  564. for value_map in values:
  565. line = value_map['name']
  566. if value_map['type'] in [ 'module', 'map' ]:
  567. line += "/"
  568. elif value_map['type'] == 'list' \
  569. and value_map['value'] != []:
  570. # do not print content of non-empty lists if
  571. # we have more data to show
  572. line += "/"
  573. else:
  574. # if type is named_set, don't print value if None
  575. # (it is either {} meaning empty, or None, meaning
  576. # there actually is data, but not to be shown with
  577. # the current command
  578. if value_map['type'] == 'named_set' and\
  579. value_map['value'] is None:
  580. line += "/\t"
  581. else:
  582. line += "\t" + json.dumps(value_map['value'])
  583. line += "\t" + value_map['type']
  584. line += "\t"
  585. if value_map['default']:
  586. line += "(default)"
  587. if value_map['modified']:
  588. line += "(modified)"
  589. print(line)
  590. elif cmd.command == "show_json":
  591. if identifier == "":
  592. print("Need at least the module to show the configuration in JSON format")
  593. else:
  594. data, default = self.config_data.get_value(identifier)
  595. print(json.dumps(data))
  596. elif cmd.command == "add":
  597. self.config_data.add_value(identifier,
  598. cmd.params.get('value_or_name'),
  599. cmd.params.get('value_for_set'))
  600. elif cmd.command == "remove":
  601. if 'value' in cmd.params:
  602. self.config_data.remove_value(identifier, cmd.params['value'])
  603. else:
  604. self.config_data.remove_value(identifier, None)
  605. elif cmd.command == "set":
  606. if 'identifier' not in cmd.params:
  607. print("Error: missing identifier or value")
  608. else:
  609. parsed_value = None
  610. try:
  611. parsed_value = json.loads(cmd.params['value'])
  612. except Exception as exc:
  613. # ok could be an unquoted string, interpret as such
  614. parsed_value = cmd.params['value']
  615. self.config_data.set_value(identifier, parsed_value)
  616. elif cmd.command == "unset":
  617. self.config_data.unset(identifier)
  618. elif cmd.command == "revert":
  619. self.config_data.clear_local_changes()
  620. elif cmd.command == "commit":
  621. try:
  622. self.config_data.commit()
  623. except isc.config.ModuleCCSessionError as mcse:
  624. print(str(mcse))
  625. elif cmd.command == "diff":
  626. print(self.config_data.get_local_changes())
  627. elif cmd.command == "go":
  628. self.go(identifier)
  629. def go(self, identifier):
  630. '''Handles the config go command, change the 'current' location
  631. within the configuration tree. '..' will be interpreted as
  632. 'up one level'.'''
  633. id_parts = isc.cc.data.split_identifier(identifier)
  634. new_location = ""
  635. for id_part in id_parts:
  636. if (id_part == ".."):
  637. # go 'up' one level
  638. new_location, a, b = new_location.rpartition("/")
  639. else:
  640. new_location += "/" + id_part
  641. # check if exists, if not, revert and error
  642. v,d = self.config_data.get_value(new_location)
  643. if v is None:
  644. print("Error: " + identifier + " not found")
  645. return
  646. self.location = new_location
  647. def apply_execute_cmd(self, command):
  648. '''Handles the 'execute' command, which executes a number of
  649. (preset) statements. Currently only 'file' commands are supported,
  650. e.g. 'execute file <file>'.'''
  651. if command.command == 'file':
  652. try:
  653. command_file = open(command.params['filename'])
  654. # copy them into a list for consistency with the built-in
  655. # sets of commands
  656. commands = []
  657. for line in command_file:
  658. commands.append(line)
  659. command_file.close()
  660. except IOError as ioe:
  661. print("Error: " + str(ioe))
  662. return
  663. elif command_sets.has_command_set(command.command):
  664. commands = command_sets.get_commands(command.command)
  665. else:
  666. # Should not be reachable; parser should've caught this
  667. raise Exception("Unknown execute command type " + command.command)
  668. # We have our set of commands now, depending on whether 'show' was
  669. # specified, show or execute them
  670. if 'show' in command.params and command.params['show'] == 'show':
  671. self.__show_execute_commands(commands)
  672. else:
  673. self.__apply_execute_commands(commands)
  674. def __show_execute_commands(self, commands):
  675. '''Prints the command list without executing them'''
  676. for line in commands:
  677. print(line.strip())
  678. def __apply_execute_commands(self, commands):
  679. '''Applies the configuration commands from the given iterator.
  680. This is the method that catches, comments, echo statements, and
  681. other directives. All commands not filtered by this method are
  682. interpreted as if they are directly entered in an active session.
  683. Lines starting with any of the following characters are not
  684. passed directly:
  685. # - These are comments
  686. ! - These are directives
  687. !echo: print the rest of the line
  688. !verbose on/off: print the commands themselves too
  689. Unknown directives are ignored (with a warning)
  690. The execution is stopped if there are any errors.
  691. '''
  692. verbose = False
  693. # TODO: revert local changes on failure
  694. # make sure it's a copy
  695. local_changes_backup =\
  696. copy.deepcopy(self.config_data.get_local_changes())
  697. try:
  698. for line in commands:
  699. line = line.strip()
  700. if verbose:
  701. print(line)
  702. if line.startswith('#'):
  703. continue
  704. elif line.startswith('!'):
  705. if line.startswith('!echo ') and len(line) > 6:
  706. print(line[6:])
  707. elif line.startswith('!verbose on'):
  708. verbose = True
  709. elif line.startswith('!verbose off'):
  710. verbose = False
  711. else:
  712. print("Warning: ignoring unknown directive: " + line)
  713. else:
  714. cmd = BindCmdParse(line)
  715. self._validate_cmd(cmd)
  716. self._handle_cmd(cmd)
  717. except (isc.config.ModuleCCSessionError,
  718. IOError, http.client.HTTPException,
  719. BindCtlException, isc.cc.data.DataTypeError,
  720. isc.cc.data.DataNotFoundError,
  721. isc.cc.data.DataAlreadyPresentError,
  722. KeyError) as err:
  723. print('Error: ', err)
  724. # revert changes
  725. self.config_data.set_local_changes(local_changes_backup)
  726. def apply_cmd(self, cmd):
  727. '''Handles a general module command'''
  728. url = '/' + cmd.module + '/' + cmd.command
  729. cmd_params = None
  730. if (len(cmd.params) != 0):
  731. cmd_params = json.dumps(cmd.params)
  732. reply = self.send_POST(url, cmd.params)
  733. data = reply.read().decode()
  734. # The reply is a string containing JSON data,
  735. # parse it, then prettyprint
  736. if data != "" and data != "{}":
  737. print(json.dumps(json.loads(data), sort_keys=True, indent=4))