bindcmd.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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 BindCmdParser
  24. from bindctl import command_sets
  25. from xml.dom import minidom
  26. import isc.config
  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. import errno
  41. try:
  42. from collections import OrderedDict
  43. except ImportError:
  44. from bindctl.mycollections import OrderedDict
  45. # if we have readline support, use that, otherwise use normal stdio
  46. try:
  47. import readline
  48. # Only consider spaces as word boundaries; identifiers can contain
  49. # '/' and '[]', and configuration item names can in theory use any
  50. # printable character. See the discussion in tickets #1345 and
  51. # #2254 for more information.
  52. readline.set_completer_delims(' ')
  53. my_readline = readline.get_line_buffer
  54. except ImportError:
  55. my_readline = sys.stdin.readline
  56. # Used for tab-completion of 'identifiers' (i.e. config values)
  57. # If a command parameter has this name, the tab completion hints
  58. # are derived from config data
  59. CFGITEM_IDENTIFIER_PARAM = 'identifier'
  60. CSV_FILE_NAME = 'default_user.csv'
  61. CONFIG_MODULE_NAME = 'config'
  62. CONST_BINDCTL_HELP = """
  63. usage: <module name> <command name> [param1 = value1 [, param2 = value2]]
  64. Type Tab character to get the hint of module/command/parameters.
  65. Type \"help(? h)\" for help on bindctl.
  66. Type \"<module_name> help\" for help on the specific module.
  67. Type \"<module_name> <command_name> help\" for help on the specific command.
  68. \nAvailable module names: """
  69. class ValidatedHTTPSConnection(http.client.HTTPSConnection):
  70. '''Overrides HTTPSConnection to support certification
  71. validation. '''
  72. def __init__(self, host, ca_certs):
  73. http.client.HTTPSConnection.__init__(self, host)
  74. self.ca_certs = ca_certs
  75. def connect(self):
  76. ''' Overrides the connect() so that we do
  77. certificate validation. '''
  78. sock = socket.create_connection((self.host, self.port),
  79. self.timeout)
  80. if self._tunnel_host:
  81. self.sock = sock
  82. self._tunnel()
  83. req_cert = ssl.CERT_NONE
  84. if self.ca_certs:
  85. req_cert = ssl.CERT_REQUIRED
  86. self.sock = ssl.wrap_socket(sock, self.key_file,
  87. self.cert_file,
  88. cert_reqs=req_cert,
  89. ca_certs=self.ca_certs)
  90. class BindCmdInterpreter(Cmd):
  91. """simple bindctl example."""
  92. def __init__(self, server_port='localhost:8080', pem_file=None,
  93. csv_file_dir=None):
  94. Cmd.__init__(self)
  95. self.location = ""
  96. self.prompt_end = '> '
  97. if sys.stdin.isatty():
  98. self.prompt = self.prompt_end
  99. else:
  100. self.prompt = ""
  101. self.ruler = '-'
  102. self.modules = OrderedDict()
  103. self.add_module_info(ModuleInfo("help", desc = "Get help for bindctl."))
  104. self.server_port = server_port
  105. self.conn = ValidatedHTTPSConnection(self.server_port,
  106. ca_certs=pem_file)
  107. self.session_id = self._get_session_id()
  108. self.config_data = None
  109. if csv_file_dir is not None:
  110. self.csv_file_dir = csv_file_dir
  111. else:
  112. self.csv_file_dir = pwd.getpwnam(getpass.getuser()).pw_dir + \
  113. os.sep + '.bind10' + os.sep
  114. def _print(self, *args):
  115. '''Simple wrapper around calls to print that can be overridden in
  116. unit tests.'''
  117. print(*args)
  118. def _get_session_id(self):
  119. '''Generate one session id for the connection. '''
  120. rand = os.urandom(16)
  121. now = time.time()
  122. session_id = sha1(("%s%s%s" %(rand, now,
  123. socket.gethostname())).encode())
  124. digest = session_id.hexdigest()
  125. return digest
  126. def run(self):
  127. '''Parse commands from user and send them to cmdctl.'''
  128. # Show helper warning about a well known issue. We only do this
  129. # when stdin is attached to a terminal, because otherwise it doesn't
  130. # matter and is just noisy, and could even be harmful if the output
  131. # is processed by a script that expects a specific format.
  132. if my_readline == sys.stdin.readline and sys.stdin.isatty():
  133. sys.stdout.write("""\
  134. WARNING: Python readline module isn't available, so the command line editor
  135. (including command history management) does not work. See BIND 10
  136. guide for more details.\n\n""")
  137. try:
  138. if not self.login_to_cmdctl():
  139. return 1
  140. self.cmdloop()
  141. self._print('\nExit from bindctl')
  142. return 0
  143. except FailToLogin as err:
  144. # error already printed when this was raised, ignoring
  145. return 1
  146. except KeyboardInterrupt:
  147. self._print('\nExit from bindctl')
  148. return 0
  149. except socket.error as err:
  150. self._print('Failed to send request, the connection is closed')
  151. return 1
  152. except http.client.CannotSendRequest:
  153. self._print('Can not send request, the connection is busy')
  154. return 1
  155. def _get_saved_user_info(self, dir, file_name):
  156. ''' Read all the available username and password pairs saved in
  157. file(path is "dir + file_name"), Return value is one list of elements
  158. ['name', 'password'], If get information failed, empty list will be
  159. returned.'''
  160. if (not dir) or (not os.path.exists(dir)):
  161. return []
  162. try:
  163. csvfile = None
  164. users = []
  165. csvfile = open(dir + file_name)
  166. users_info = csv.reader(csvfile)
  167. for row in users_info:
  168. users.append([row[0], row[1]])
  169. except (IOError, IndexError) as err:
  170. self._print("Error reading saved username and password "
  171. "from %s%s: %s" % (dir, file_name, err))
  172. finally:
  173. if csvfile:
  174. csvfile.close()
  175. return users
  176. def _save_user_info(self, username, passwd, dir, file_name):
  177. ''' Save username and password in file "dir + file_name"
  178. If it's saved properly, return True, or else return False. '''
  179. try:
  180. if not os.path.exists(dir):
  181. os.mkdir(dir, 0o700)
  182. csvfilepath = dir + file_name
  183. csvfile = open(csvfilepath, 'w')
  184. os.chmod(csvfilepath, 0o600)
  185. writer = csv.writer(csvfile)
  186. writer.writerow([username, passwd])
  187. csvfile.close()
  188. except IOError as err:
  189. self._print("Error saving user information:", err)
  190. self._print("user info file name: %s%s" % (dir, file_name))
  191. return False
  192. return True
  193. def __print_check_ssl_msg(self):
  194. self._print("Please check the logs of b10-cmdctl, there may "
  195. "be a problem accepting SSL connections, such "
  196. "as a permission problem on the server "
  197. "certificate file.")
  198. def _try_login(self, username, password):
  199. '''
  200. Attempts to log into cmdctl by sending a POST with the given
  201. username and password. On success of the POST (not the login,
  202. but the network operation), it returns a tuple (response, data).
  203. On failure, it raises a FailToLogin exception and prints some
  204. information on the failure. This call is essentially 'private',
  205. but made 'protected' for easier testing.
  206. '''
  207. param = {'username': username, 'password' : password}
  208. try:
  209. response = self.send_POST('/login', param)
  210. data = response.read().decode()
  211. # return here (will raise error after try block)
  212. return (response, data)
  213. except ssl.SSLError as err:
  214. self._print("SSL error while sending login information: ", err)
  215. if err.errno == ssl.SSL_ERROR_EOF:
  216. self.__print_check_ssl_msg()
  217. except socket.error as err:
  218. self._print("Socket error while sending login information: ", err)
  219. # An SSL setup error can also bubble up as a plain CONNRESET...
  220. # (on some systems it usually does)
  221. if err.errno == errno.ECONNRESET:
  222. self.__print_check_ssl_msg()
  223. pass
  224. raise FailToLogin()
  225. def _have_users(self):
  226. '''
  227. Checks if cmdctl knows of any users by making a POST to it. On
  228. success of the POST, it returns True if cmdctl has users
  229. configured, and False otherwise. On failure, a FailToLogin
  230. exception is raised, and some information on the failure is
  231. printed.
  232. '''
  233. try:
  234. response = self.send_POST('/users-exist')
  235. if response.status == http.client.OK:
  236. return json.loads(response.read().decode())
  237. # if not OK, fall through to raise error
  238. self._print("Failure in cmdctl when checking if users already exist")
  239. except ssl.SSLError as err:
  240. self._print("SSL error checking if users exist: ", err)
  241. if err.errno == ssl.SSL_ERROR_EOF:
  242. self.__print_check_ssl_msg()
  243. except socket.error as err:
  244. self._print("Socket error checking if users exist: ", err)
  245. # An SSL setup error can also bubble up as a plain CONNRESET...
  246. # (on some systems it usually does)
  247. if err.errno == errno.ECONNRESET:
  248. self.__print_check_ssl_msg()
  249. pass
  250. raise FailToLogin()
  251. def login_to_cmdctl(self):
  252. '''Login to cmdctl with the username and password given by
  253. the user. After the login is sucessful, the username and
  254. password will be saved in 'default_user.csv', when run the next
  255. time, username and password saved in 'default_user.csv' will be
  256. used first.
  257. '''
  258. # First, check that valid users exist. If not, ask the person at
  259. # the tty to configure one using b10-cmdctl-usermgr.
  260. if not self._have_users():
  261. self._print('There are no existing users. Please configure '
  262. 'a user account using b10-cmdctl-usermgr.')
  263. return False
  264. # Look at existing username/password combinations and try to log in
  265. users = self._get_saved_user_info(self.csv_file_dir, CSV_FILE_NAME)
  266. for row in users:
  267. response, data = self._try_login(row[0], row[1])
  268. if response.status == http.client.OK:
  269. # Is interactive?
  270. if sys.stdin.isatty():
  271. self._print(data + ' login as ' + row[0])
  272. return True
  273. # No valid logins were found, prompt the user for a username/password
  274. count = 0
  275. self._print('No stored password file found, please see sections '
  276. '"Configuration specification for b10-cmdctl" and "bindctl '
  277. 'command-line options" of the BIND 10 guide.')
  278. while True:
  279. count = count + 1
  280. if count > 3:
  281. self._print("Too many authentication failures")
  282. return False
  283. username = input("Username: ")
  284. passwd = getpass.getpass()
  285. response, data = self._try_login(username, passwd)
  286. self._print(data)
  287. if response.status == http.client.OK:
  288. self._save_user_info(username, passwd, self.csv_file_dir,
  289. CSV_FILE_NAME)
  290. return True
  291. def _update_commands(self):
  292. '''Update the commands of all modules. '''
  293. for module_name in self.config_data.get_config_item_list():
  294. self._prepare_module_commands(self.config_data.get_module_spec(module_name))
  295. def _send_message(self, url, body):
  296. headers = {"cookie" : self.session_id}
  297. self.conn.request('GET', url, body, headers)
  298. res = self.conn.getresponse()
  299. return res.status, res.read()
  300. def send_GET(self, url, body = None):
  301. '''Send GET request to cmdctl, session id is send with the name
  302. 'cookie' in header.
  303. '''
  304. status, reply_msg = self._send_message(url, body)
  305. if status == http.client.UNAUTHORIZED:
  306. if self.login_to_cmdctl():
  307. # successful, so try send again
  308. status, reply_msg = self._send_message(url, body)
  309. if reply_msg:
  310. return json.loads(reply_msg.decode())
  311. else:
  312. return {}
  313. def send_POST(self, url, post_param=None):
  314. '''Send POST request to cmdctl, session id is send with the name
  315. 'cookie' in header.
  316. Format: /module_name/command_name
  317. parameters of command is encoded as a map
  318. '''
  319. param = None
  320. if post_param is not None and len(post_param) != 0:
  321. param = json.dumps(post_param)
  322. headers = {"cookie" : self.session_id}
  323. self.conn.request('POST', url, param, headers)
  324. return self.conn.getresponse()
  325. def _update_all_modules_info(self):
  326. ''' Get all modules' information from cmdctl, including
  327. specification file and configuration data. This function
  328. should be called before interpreting command line or complete-key
  329. is entered. This may not be the best way to keep bindctl
  330. and cmdctl share same modules information, but it works.'''
  331. if self.config_data is not None:
  332. self.config_data.update_specs_and_config()
  333. else:
  334. self.config_data = isc.config.UIModuleCCSession(self)
  335. self._update_commands()
  336. def precmd(self, line):
  337. if line != 'EOF':
  338. self._update_all_modules_info()
  339. return line
  340. def postcmd(self, stop, line):
  341. '''Update the prompt after every command, but only if we
  342. have a tty as output'''
  343. if sys.stdin.isatty():
  344. self.prompt = self.location + self.prompt_end
  345. return stop
  346. def _prepare_module_commands(self, module_spec):
  347. '''Prepare the module commands'''
  348. module = ModuleInfo(name = module_spec.get_module_name(),
  349. desc = module_spec.get_module_description())
  350. for command in module_spec.get_commands_spec():
  351. cmd = CommandInfo(name = command["command_name"],
  352. desc = command["command_description"])
  353. for arg in command["command_args"]:
  354. param = ParamInfo(name = arg["item_name"],
  355. type = arg["item_type"],
  356. optional = bool(arg["item_optional"]),
  357. param_spec = arg)
  358. if ("item_default" in arg):
  359. param.default = arg["item_default"]
  360. if ("item_description" in arg):
  361. param.desc = arg["item_description"]
  362. cmd.add_param(param)
  363. module.add_command(cmd)
  364. self.add_module_info(module)
  365. def _validate_cmd(self, cmd):
  366. '''validate the parameters and merge some parameters together,
  367. merge algorithm is based on the command line syntax, later, if
  368. a better command line syntax come out, this function should be
  369. updated first.
  370. '''
  371. if not cmd.module in self.modules:
  372. raise CmdUnknownModuleSyntaxError(cmd.module)
  373. module_info = self.modules[cmd.module]
  374. if not module_info.has_command_with_name(cmd.command):
  375. raise CmdUnknownCmdSyntaxError(cmd.module, cmd.command)
  376. command_info = module_info.get_command_with_name(cmd.command)
  377. manda_params = command_info.get_mandatory_param_names()
  378. all_params = command_info.get_param_names()
  379. # If help is entered, don't do further parameter validation.
  380. for val in cmd.params.keys():
  381. if val == "help":
  382. return
  383. params = cmd.params.copy()
  384. if not params and manda_params:
  385. raise CmdMissParamSyntaxError(cmd.module, cmd.command, manda_params[0])
  386. elif params and not all_params:
  387. raise CmdUnknownParamSyntaxError(cmd.module, cmd.command,
  388. list(params.keys())[0])
  389. elif params:
  390. param_name = None
  391. param_count = len(params)
  392. for name in params:
  393. # either the name of the parameter must be known, or
  394. # the 'name' must be an integer (ie. the position of
  395. # an unnamed argument
  396. if type(name) == int:
  397. # lump all extraneous arguments together as one big final one
  398. # todo: check if last param type is a string?
  399. while (param_count > 2 and
  400. param_count > len(command_info.params) - 1):
  401. params[param_count - 2] += " " + params[param_count - 1]
  402. del(params[param_count - 1])
  403. param_count = len(params)
  404. cmd.params = params.copy()
  405. # (-1, help is always in the all_params list)
  406. if name >= len(all_params) - 1:
  407. # add to last known param
  408. if param_name:
  409. cmd.params[param_name] += cmd.params[name]
  410. else:
  411. raise CmdUnknownParamSyntaxError(cmd.module, cmd.command, cmd.params[name])
  412. else:
  413. # replace the numbered items by named items
  414. param_name = command_info.get_param_name_by_position(name, param_count)
  415. cmd.params[param_name] = cmd.params[name]
  416. del cmd.params[name]
  417. elif not name in all_params:
  418. raise CmdUnknownParamSyntaxError(cmd.module, cmd.command, name)
  419. param_nr = 0
  420. for name in manda_params:
  421. if not name in params and not param_nr in params:
  422. raise CmdMissParamSyntaxError(cmd.module, cmd.command, name)
  423. param_nr += 1
  424. # Convert parameter value according parameter spec file.
  425. # Ignore check for commands belongs to module 'config' or 'execute
  426. if cmd.module != CONFIG_MODULE_NAME and\
  427. cmd.module != command_sets.EXECUTE_MODULE_NAME:
  428. for param_name in cmd.params:
  429. param_spec = command_info.get_param_with_name(param_name).param_spec
  430. try:
  431. cmd.params[param_name] = isc.config.config_data.convert_type(param_spec, cmd.params[param_name])
  432. except isc.cc.data.DataTypeError as e:
  433. raise isc.cc.data.DataTypeError('Invalid parameter value for \"%s\", the type should be \"%s\" \n'
  434. % (param_name, param_spec['item_type']) + str(e))
  435. def _handle_cmd(self, cmd):
  436. '''Handle a command entered by the user'''
  437. if cmd.command == "help" or ("help" in cmd.params.keys()):
  438. self._handle_help(cmd)
  439. elif cmd.module == CONFIG_MODULE_NAME:
  440. self.apply_config_cmd(cmd)
  441. elif cmd.module == command_sets.EXECUTE_MODULE_NAME:
  442. self.apply_execute_cmd(cmd)
  443. else:
  444. self.apply_cmd(cmd)
  445. def add_module_info(self, module_info):
  446. '''Add the information about one module'''
  447. self.modules[module_info.name] = module_info
  448. def get_module_names(self):
  449. '''Return the names of all known modules'''
  450. return list(self.modules.keys())
  451. #override methods in cmd
  452. def default(self, line):
  453. self._parse_cmd(line)
  454. def emptyline(self):
  455. pass
  456. def do_help(self, name):
  457. self._print(CONST_BINDCTL_HELP)
  458. for k in self.modules.values():
  459. n = k.get_name()
  460. if len(n) >= CONST_BINDCTL_HELP_INDENT_WIDTH:
  461. self._print(" %s" % n)
  462. self._print(textwrap.fill(k.get_desc(),
  463. initial_indent=" ",
  464. subsequent_indent=" " +
  465. " " * CONST_BINDCTL_HELP_INDENT_WIDTH,
  466. width=70))
  467. else:
  468. self._print(textwrap.fill("%s%s%s" %
  469. (k.get_name(),
  470. " "*(CONST_BINDCTL_HELP_INDENT_WIDTH -
  471. len(k.get_name())),
  472. k.get_desc()),
  473. initial_indent=" ",
  474. subsequent_indent=" " +
  475. " " * CONST_BINDCTL_HELP_INDENT_WIDTH,
  476. width=70))
  477. def onecmd(self, line):
  478. if line == 'EOF' or line.lower() == "quit":
  479. self.conn.close()
  480. return True
  481. if line == 'h':
  482. line = 'help'
  483. Cmd.onecmd(self, line)
  484. def _get_identifier_startswith(self, id_text):
  485. """Return the tab-completion hints for identifiers starting with
  486. id_text.
  487. Parameters:
  488. id_text (string): the currently entered identifier part, which
  489. is to be completed.
  490. """
  491. # Strip starting "/" from id_text
  492. if id_text.startswith('/'):
  493. id_text = id_text[1:]
  494. # Get all items from the given module (up to the first /)
  495. list = self.config_data.get_config_item_list(
  496. id_text.rpartition("/")[0], recurse=True)
  497. # filter out all possibilities that don't match currently entered
  498. # text part
  499. hints = [val for val in list if val.startswith(id_text)]
  500. return hints
  501. def _cmd_has_identifier_param(self, cmd):
  502. """
  503. Returns True if the given (parsed) command is known and has a
  504. parameter which points to a config data identifier
  505. Parameters:
  506. cmd (cmdparse.BindCmdParser): command context, including given params
  507. """
  508. if cmd.module not in self.modules:
  509. return False
  510. command = self.modules[cmd.module].get_command_with_name(cmd.command)
  511. return command.has_param_with_name(CFGITEM_IDENTIFIER_PARAM)
  512. def complete(self, text, state):
  513. """
  514. Returns tab-completion hints. See the python documentation of the
  515. readline and Cmd modules for more information.
  516. The first time this is called (within one 'completer' action), it
  517. has state 0, and a list of possible completions is made. This list
  518. is stored; complete() will then be called with increasing values of
  519. state, until it returns None. For each call it returns the state'th
  520. element of the hints it collected in the first call.
  521. The hints list contents depend on which part of the full command
  522. line; if no module is given yet, it will list all modules. If a
  523. module is given, but no command, it will complete with module
  524. commands. If both have been given, it will create the hints based on
  525. the command parameters.
  526. If module and command have already been specified, and the command
  527. has a parameter 'identifier', the configuration data is used to
  528. create the hints list.
  529. Parameters:
  530. text (string): The text entered so far in the 'current' part of
  531. the command (module, command, parameters)
  532. state (int): state used in the readline tab-completion logic;
  533. 0 on first call, increasing by one until there are
  534. no (more) hints to return.
  535. Returns the string value of the hints list with index 'state',
  536. or None if no (more) hints are available.
  537. """
  538. if state == 0:
  539. self._update_all_modules_info()
  540. text = text.strip()
  541. hints = []
  542. cur_line = my_readline()
  543. try:
  544. cmd = BindCmdParser(cur_line)
  545. if not cmd.params and text:
  546. hints = self._get_command_startswith(cmd.module, text)
  547. elif self._cmd_has_identifier_param(cmd):
  548. # If the command has an argument that is a configuration
  549. # identifier (currently, this is only a subset of
  550. # the config commands), then don't tab-complete with
  551. # hints derived from command parameters, but from
  552. # possible configuration identifiers.
  553. #
  554. # This solves the issue reported in #2254, where
  555. # there were hints such as 'argument' and 'identifier'.
  556. #
  557. # Since they are replaced, the tab-completion no longer
  558. # adds 'help' as an option (but it still works)
  559. #
  560. # Also, currently, tab-completion does not work
  561. # together with 'config go' (it does not take 'current
  562. # position' into account). But config go currently has
  563. # problems by itself, unrelated to completion.
  564. hints = self._get_identifier_startswith(text)
  565. else:
  566. hints = self._get_param_startswith(cmd.module, cmd.command,
  567. text)
  568. except CmdModuleNameFormatError:
  569. if not text:
  570. hints = self.get_module_names()
  571. except CmdMissCommandNameFormatError as e:
  572. if not text.strip(): # command name is empty
  573. hints = self.modules[e.module].get_command_names()
  574. else:
  575. hints = self._get_module_startswith(text)
  576. except CmdCommandNameFormatError as e:
  577. if e.module in self.modules:
  578. hints = self._get_command_startswith(e.module, text)
  579. except CmdParamFormatError as e:
  580. hints = self._get_param_startswith(e.module, e.command, text)
  581. except BindCtlException:
  582. hints = []
  583. self.hint = hints
  584. if state < len(self.hint):
  585. return self.hint[state]
  586. else:
  587. return None
  588. def _get_module_startswith(self, text):
  589. return [module
  590. for module in self.modules
  591. if module.startswith(text)]
  592. def _get_command_startswith(self, module, text):
  593. if module in self.modules:
  594. return [command
  595. for command in self.modules[module].get_command_names()
  596. if command.startswith(text)]
  597. return []
  598. def _get_param_startswith(self, module, command, text):
  599. if module in self.modules:
  600. module_info = self.modules[module]
  601. if command in module_info.get_command_names():
  602. cmd_info = module_info.get_command_with_name(command)
  603. params = cmd_info.get_param_names()
  604. hint = []
  605. if text:
  606. hint = [val for val in params if val.startswith(text)]
  607. else:
  608. hint = list(params)
  609. if len(hint) == 1 and hint[0] != "help":
  610. hint[0] = hint[0] + " ="
  611. return hint
  612. return []
  613. def _parse_cmd(self, line):
  614. try:
  615. cmd = BindCmdParser(line)
  616. self._validate_cmd(cmd)
  617. self._handle_cmd(cmd)
  618. except (IOError, http.client.HTTPException) as err:
  619. self._print('Error: ', err)
  620. except BindCtlException as err:
  621. self._print("Error! ", err)
  622. self._print_correct_usage(err)
  623. except isc.cc.data.DataTypeError as err:
  624. self._print("Error! ", err)
  625. except isc.cc.data.DataTypeError as dte:
  626. self._print("Error: " + str(dte))
  627. except isc.cc.data.DataNotFoundError as dnfe:
  628. self._print("Error: " + str(dnfe))
  629. except isc.cc.data.DataAlreadyPresentError as dape:
  630. self._print("Error: " + str(dape))
  631. except KeyError as ke:
  632. self._print("Error: missing " + str(ke))
  633. def _print_correct_usage(self, ept):
  634. if isinstance(ept, CmdUnknownModuleSyntaxError):
  635. self.do_help(None)
  636. elif isinstance(ept, CmdUnknownCmdSyntaxError):
  637. self.modules[ept.module].module_help()
  638. elif isinstance(ept, CmdMissParamSyntaxError) or \
  639. isinstance(ept, CmdUnknownParamSyntaxError):
  640. self.modules[ept.module].command_help(ept.command)
  641. def _append_space_to_hint(self):
  642. """Append one space at the end of complete hint."""
  643. self.hint = [(val + " ") for val in self.hint]
  644. def _handle_help(self, cmd):
  645. if cmd.command == "help":
  646. self.modules[cmd.module].module_help()
  647. else:
  648. self.modules[cmd.module].command_help(cmd.command)
  649. def apply_config_cmd(self, cmd):
  650. '''Handles a configuration command.
  651. Raises a DataTypeError if a wrong value is set.
  652. Raises a DataNotFoundError if a wrong identifier is used.
  653. Raises a KeyError if the command was not complete
  654. '''
  655. identifier = self.location
  656. if 'identifier' in cmd.params:
  657. if not identifier.endswith("/"):
  658. identifier += "/"
  659. if cmd.params['identifier'].startswith("/"):
  660. identifier = cmd.params['identifier']
  661. else:
  662. if cmd.params['identifier'].startswith('['):
  663. identifier = identifier[:-1]
  664. identifier += cmd.params['identifier']
  665. # Check if the module is known; for unknown modules
  666. # we currently deny setting preferences, as we have
  667. # no way yet to determine if they are ok.
  668. module_name = identifier.split('/')[1]
  669. if module_name != "" and (self.config_data is None or \
  670. not self.config_data.have_specification(module_name)):
  671. self._print("Error: Module '" + module_name +
  672. "' unknown or not running")
  673. return
  674. if cmd.command == "show":
  675. # check if we have the 'all' argument
  676. show_all = False
  677. if 'argument' in cmd.params:
  678. if cmd.params['argument'] == 'all':
  679. show_all = True
  680. elif 'identifier' not in cmd.params:
  681. # no 'all', no identifier, assume this is the
  682. #identifier
  683. identifier += cmd.params['argument']
  684. else:
  685. self._print("Error: unknown argument " +
  686. cmd.params['argument'] +
  687. ", or multiple identifiers given")
  688. return
  689. values = self.config_data.get_value_maps(identifier, show_all)
  690. for value_map in values:
  691. line = value_map['name']
  692. if value_map['type'] in [ 'module', 'map' ]:
  693. line += "/"
  694. elif value_map['type'] == 'list' \
  695. and value_map['value'] != []:
  696. # do not print content of non-empty lists if
  697. # we have more data to show
  698. line += "/"
  699. else:
  700. # if type is named_set, don't print value if None
  701. # (it is either {} meaning empty, or None, meaning
  702. # there actually is data, but not to be shown with
  703. # the current command
  704. if value_map['type'] == 'named_set' and\
  705. value_map['value'] is None:
  706. line += "/\t"
  707. else:
  708. line += "\t" + json.dumps(value_map['value'])
  709. line += "\t" + value_map['type']
  710. line += "\t"
  711. if value_map['default']:
  712. line += "(default)"
  713. if value_map['modified']:
  714. line += "(modified)"
  715. self._print(line)
  716. elif cmd.command == "show_json":
  717. if identifier == "":
  718. self._print("Need at least the module to show the "
  719. "configuration in JSON format")
  720. else:
  721. data, default = self.config_data.get_value(identifier)
  722. self._print(json.dumps(data))
  723. elif cmd.command == "add":
  724. self.config_data.add_value(identifier,
  725. cmd.params.get('value_or_name'),
  726. cmd.params.get('value_for_set'))
  727. elif cmd.command == "remove":
  728. if 'value' in cmd.params:
  729. self.config_data.remove_value(identifier, cmd.params['value'])
  730. else:
  731. self.config_data.remove_value(identifier, None)
  732. elif cmd.command == "set":
  733. if 'identifier' not in cmd.params:
  734. self._print("Error: missing identifier or value")
  735. else:
  736. parsed_value = None
  737. try:
  738. parsed_value = json.loads(cmd.params['value'])
  739. except Exception as exc:
  740. # ok could be an unquoted string, interpret as such
  741. parsed_value = cmd.params['value']
  742. self.config_data.set_value(identifier, parsed_value)
  743. elif cmd.command == "unset":
  744. self.config_data.unset(identifier)
  745. elif cmd.command == "revert":
  746. self.config_data.clear_local_changes()
  747. elif cmd.command == "commit":
  748. try:
  749. self.config_data.commit()
  750. except isc.config.ModuleCCSessionError as mcse:
  751. self._print(str(mcse))
  752. elif cmd.command == "diff":
  753. self._print(self.config_data.get_local_changes())
  754. elif cmd.command == "go":
  755. self.go(identifier)
  756. def go(self, identifier):
  757. '''Handles the config go command, change the 'current' location
  758. within the configuration tree. '..' will be interpreted as
  759. 'up one level'.'''
  760. id_parts = isc.cc.data.split_identifier(identifier)
  761. new_location = ""
  762. for id_part in id_parts:
  763. if (id_part == ".."):
  764. # go 'up' one level
  765. new_location, a, b = new_location.rpartition("/")
  766. else:
  767. new_location += "/" + id_part
  768. # check if exists, if not, revert and error
  769. v,d = self.config_data.get_value(new_location)
  770. if v is None:
  771. self._print("Error: " + identifier + " not found")
  772. return
  773. self.location = new_location
  774. def apply_execute_cmd(self, command):
  775. '''Handles the 'execute' command, which executes a number of
  776. (preset) statements. The command set to execute is either
  777. read from a file (e.g. 'execute file <file>'.) or one
  778. of the sets as defined in command_sets.py'''
  779. if command.command == 'file':
  780. try:
  781. with open(command.params['filename']) as command_file:
  782. commands = command_file.readlines()
  783. except IOError as ioe:
  784. self._print("Error: " + str(ioe))
  785. return
  786. elif command_sets.has_command_set(command.command):
  787. commands = command_sets.get_commands(command.command)
  788. else:
  789. # Should not be reachable; parser should've caught this
  790. raise Exception("Unknown execute command type " + command.command)
  791. # We have our set of commands now, depending on whether 'show' was
  792. # specified, show or execute them
  793. if 'show' in command.params and command.params['show'] == 'show':
  794. self.__show_execute_commands(commands)
  795. else:
  796. self.__apply_execute_commands(commands)
  797. def __show_execute_commands(self, commands):
  798. '''Prints the command list without executing them'''
  799. for line in commands:
  800. self._print(line.strip())
  801. def __apply_execute_commands(self, commands):
  802. '''Applies the configuration commands from the given iterator.
  803. This is the method that catches, comments, echo statements, and
  804. other directives. All commands not filtered by this method are
  805. interpreted as if they are directly entered in an active session.
  806. Lines starting with any of the following characters are not
  807. passed directly:
  808. # - These are comments
  809. ! - These are directives
  810. !echo: print the rest of the line
  811. !verbose on/off: print the commands themselves too
  812. Unknown directives are ignored (with a warning)
  813. The execution is stopped if there are any errors.
  814. '''
  815. verbose = False
  816. try:
  817. for line in commands:
  818. line = line.strip()
  819. if verbose:
  820. self._print(line)
  821. if line.startswith('#') or len(line) == 0:
  822. continue
  823. elif line.startswith('!'):
  824. if re.match('^!echo ', line, re.I) and len(line) > 6:
  825. self._print(line[6:])
  826. elif re.match('^!verbose\s+on\s*$', line, re.I):
  827. verbose = True
  828. elif re.match('^!verbose\s+off$', line, re.I):
  829. verbose = False
  830. else:
  831. self._print("Warning: ignoring unknown directive: " +
  832. line)
  833. else:
  834. cmd = BindCmdParser(line)
  835. self._validate_cmd(cmd)
  836. self._handle_cmd(cmd)
  837. except (isc.config.ModuleCCSessionError,
  838. IOError, http.client.HTTPException,
  839. BindCtlException, isc.cc.data.DataTypeError,
  840. isc.cc.data.DataNotFoundError,
  841. isc.cc.data.DataAlreadyPresentError,
  842. KeyError) as err:
  843. self._print('Error: ', err)
  844. self._print()
  845. self._print('Depending on the contents of the script, and which')
  846. self._print('commands it has called, there can be committed and')
  847. self._print('local changes. It is advised to check your settings')
  848. self._print(', and revert local changes with "config revert".')
  849. def apply_cmd(self, cmd):
  850. '''Handles a general module command'''
  851. url = '/' + cmd.module + '/' + cmd.command
  852. cmd_params = None
  853. if (len(cmd.params) != 0):
  854. cmd_params = json.dumps(cmd.params)
  855. reply = self.send_POST(url, cmd.params)
  856. data = reply.read().decode()
  857. # The reply is a string containing JSON data,
  858. # parse it, then prettyprint
  859. if data != "" and data != "{}":
  860. self._print(json.dumps(json.loads(data), sort_keys=True,
  861. indent=4))