cmdctl.py.in 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #!@PYTHON@
  2. # Copyright (C) 2010 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. ''' cmdctl module is the configuration entry point for all commands from bindctl
  17. or some other web tools client of bind10. cmdctl is pure https server which provi-
  18. des RESTful API. When command client connecting with cmdctl, it should first login
  19. with legal username and password.
  20. When cmdctl starting up, it will collect command specification and
  21. configuration specification/data of other available modules from configmanager, then
  22. wait for receiving request from client, parse the request and resend the request to
  23. the proper module. When getting the request result from the module, send back the
  24. resut to client.
  25. '''
  26. import sys; sys.path.append ('@@PYTHONPATH@@')
  27. import os
  28. import socketserver
  29. import http.server
  30. import urllib.parse
  31. import json
  32. import re
  33. import ssl, socket
  34. import isc
  35. import pprint
  36. import select
  37. import csv
  38. import random
  39. import time
  40. import signal
  41. from isc.config import ccsession
  42. import isc.util.process
  43. import isc.net.parse
  44. from optparse import OptionParser, OptionValueError
  45. from hashlib import sha1
  46. from isc.util import socketserver_mixin
  47. from isc.log_messages.cmdctl_messages import *
  48. isc.log.init("b10-cmdctl", buffer=True)
  49. logger = isc.log.Logger("cmdctl")
  50. # Debug level for communication with BIND10
  51. DBG_CMDCTL_MESSAGING = logger.DBGLVL_COMMAND
  52. try:
  53. import threading
  54. except ImportError:
  55. import dummy_threading as threading
  56. isc.util.process.rename()
  57. __version__ = 'BIND10'
  58. URL_PATTERN = re.compile('/([\w]+)(?:/([\w]+))?/?')
  59. CONFIG_DATA_URL = 'config_data'
  60. MODULE_SPEC_URL = 'module_spec'
  61. # If B10_FROM_BUILD is set in the environment, we use data files
  62. # from a directory relative to that, otherwise we use the ones
  63. # installed on the system
  64. if "B10_FROM_BUILD" in os.environ:
  65. SPECFILE_PATH = os.environ["B10_FROM_BUILD"] + "/src/bin/cmdctl"
  66. else:
  67. PREFIX = "@prefix@"
  68. DATAROOTDIR = "@datarootdir@"
  69. SPECFILE_PATH = "@datadir@/@PACKAGE@".replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  70. SPECFILE_LOCATION = SPECFILE_PATH + os.sep + "cmdctl.spec"
  71. class CmdctlException(Exception):
  72. pass
  73. def check_file(file_name):
  74. # TODO: Check contents of certificate file
  75. if not os.path.exists(file_name):
  76. raise CmdctlException("'%s' does not exist" % file_name)
  77. if not os.path.isfile(file_name):
  78. raise CmdctlException("'%s' is not a file" % file_name)
  79. if not os.access(file_name, os.R_OK):
  80. raise CmdctlException("'%s' is not readable" % file_name)
  81. class SecureHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
  82. '''https connection request handler.
  83. Currently only GET and POST are supported. '''
  84. def do_GET(self):
  85. '''The client should send its session id in header with
  86. the name 'cookie'
  87. '''
  88. self.session_id = self.headers.get('cookie')
  89. rcode, reply = http.client.OK, []
  90. if self._is_session_valid():
  91. if self._is_user_logged_in():
  92. rcode, reply = self._handle_get_request()
  93. else:
  94. rcode, reply = http.client.UNAUTHORIZED, ["please login"]
  95. else:
  96. rcode = http.client.BAD_REQUEST
  97. self.send_response(rcode)
  98. self.end_headers()
  99. self.wfile.write(json.dumps(reply).encode())
  100. def _handle_get_request(self):
  101. '''Currently only support the following three url GET request '''
  102. id, module = self._parse_request_path()
  103. return self.server.get_reply_data_for_GET(id, module)
  104. def _is_session_valid(self):
  105. return self.session_id
  106. def _is_user_logged_in(self):
  107. login_time = self.server.user_sessions.get(self.session_id)
  108. if not login_time:
  109. return False
  110. idle_time = time.time() - login_time
  111. if idle_time > self.server.idle_timeout:
  112. return False
  113. # Update idle time
  114. self.server.user_sessions[self.session_id] = time.time()
  115. return True
  116. def _parse_request_path(self):
  117. '''Parse the url, the legal url should like /ldh or /ldh/ldh '''
  118. groups = URL_PATTERN.match(self.path)
  119. if not groups:
  120. return (None, None)
  121. else:
  122. return (groups.group(1), groups.group(2))
  123. def do_POST(self):
  124. '''Process POST request. '''
  125. '''Process user login and send command to proper module
  126. The client should send its session id in header with
  127. the name 'cookie'
  128. '''
  129. self.session_id = self.headers.get('cookie')
  130. rcode, reply = http.client.OK, []
  131. if self._is_session_valid():
  132. if self.path == '/login':
  133. rcode, reply = self._handle_login()
  134. elif self._is_user_logged_in():
  135. rcode, reply = self._handle_post_request()
  136. else:
  137. rcode, reply = http.client.UNAUTHORIZED, ["please login"]
  138. else:
  139. rcode, reply = http.client.BAD_REQUEST, ["session isn't valid"]
  140. self.send_response(rcode)
  141. self.end_headers()
  142. self.wfile.write(json.dumps(reply).encode())
  143. def _handle_login(self):
  144. if self._is_user_logged_in():
  145. return http.client.OK, ["user has already login"]
  146. is_user_valid, error_info = self._check_user_name_and_pwd()
  147. if is_user_valid:
  148. self.server.save_user_session_id(self.session_id)
  149. return http.client.OK, ["login success "]
  150. else:
  151. return http.client.UNAUTHORIZED, error_info
  152. def _check_user_name_and_pwd(self):
  153. '''Check user name and its password '''
  154. length = self.headers.get('Content-Length')
  155. if not length:
  156. return False, ["invalid username or password"]
  157. try:
  158. user_info = json.loads((self.rfile.read(int(length))).decode())
  159. except:
  160. return False, ["invalid username or password"]
  161. user_name = user_info.get('username')
  162. if not user_name:
  163. return False, ["need user name"]
  164. if not self.server.get_user_info(user_name):
  165. logger.info(CMDCTL_NO_SUCH_USER, user_name)
  166. return False, ["username or password error"]
  167. user_pwd = user_info.get('password')
  168. if not user_pwd:
  169. return False, ["need password"]
  170. local_info = self.server.get_user_info(user_name)
  171. pwd_hashval = sha1((user_pwd + local_info[1]).encode())
  172. if pwd_hashval.hexdigest() != local_info[0]:
  173. logger.info(CMDCTL_BAD_PASSWORD, user_name)
  174. return False, ["username or password error"]
  175. return True, None
  176. def _handle_post_request(self):
  177. '''Handle all the post request from client. '''
  178. mod, cmd = self._parse_request_path()
  179. if (not mod) or (not cmd):
  180. return http.client.BAD_REQUEST, ['malformed url']
  181. param = None
  182. len = self.headers.get('Content-Length')
  183. if len:
  184. try:
  185. post_str = str(self.rfile.read(int(len)).decode())
  186. param = json.loads(post_str)
  187. except:
  188. pass
  189. rcode, reply = self.server.send_command_to_module(mod, cmd, param)
  190. ret = http.client.OK
  191. if rcode != 0:
  192. ret = http.client.BAD_REQUEST
  193. return ret, reply
  194. def log_request(self, code='-', size='-'):
  195. '''Rewrite the log request function, log nothing.'''
  196. pass
  197. class CommandControl():
  198. '''Get all modules' config data/specification from configmanager.
  199. receive command from client and resend it to proper module.
  200. '''
  201. def __init__(self, httpserver, verbose = False):
  202. ''' httpserver: the http server which use the object of
  203. CommandControl to communicate with other modules. '''
  204. self._verbose = verbose
  205. self._httpserver = httpserver
  206. self._lock = threading.Lock()
  207. self._setup_session()
  208. self.modules_spec = self._get_modules_specification()
  209. self._config_data = self._get_config_data_from_config_manager()
  210. self._serving = True
  211. self._start_msg_handle_thread()
  212. def _setup_session(self):
  213. '''Setup the session for receving the commands
  214. sent from other modules. There are two sessions
  215. for cmdctl, one(self.module_cc) is used for receiving
  216. commands sent from other modules, another one (self._cc)
  217. is used to send the command from Bindctl or other tools
  218. to proper modules.'''
  219. self._cc = isc.cc.Session()
  220. self._module_cc = isc.config.ModuleCCSession(SPECFILE_LOCATION,
  221. self.config_handler,
  222. self.command_handler)
  223. self._module_name = self._module_cc.get_module_spec().get_module_name()
  224. self._cmdctl_config_data = self._module_cc.get_full_config()
  225. self._module_cc.start()
  226. def _accounts_file_check(self, filepath):
  227. ''' Check whether the accounts file is valid, each row
  228. should be a list with 3 items.'''
  229. csvfile = None
  230. errstr = None
  231. try:
  232. csvfile = open(filepath)
  233. reader = csv.reader(csvfile)
  234. for row in reader:
  235. a = (row[0], row[1], row[2])
  236. except (IOError, IndexError) as e:
  237. errstr = 'Invalid accounts file: ' + str(e)
  238. finally:
  239. if csvfile:
  240. csvfile.close()
  241. return errstr
  242. def _config_data_check(self, new_config):
  243. ''' Check whether the new config data is valid or
  244. not. '''
  245. errstr = None
  246. for key in new_config:
  247. if key == 'version':
  248. continue
  249. elif key in ['key_file', 'cert_file']:
  250. # TODO: we only check whether the file exist, is a
  251. # file, and is readable; but further check need to be done:
  252. # eg. whether the private/certificate is valid.
  253. path = new_config[key]
  254. try:
  255. check_file(path)
  256. except CmdctlException as cce:
  257. errstr = str(cce)
  258. elif key == 'accounts_file':
  259. errstr = self._accounts_file_check(new_config[key])
  260. else:
  261. errstr = 'unknown config item: ' + key
  262. if errstr != None:
  263. logger.error(CMDCTL_BAD_CONFIG_DATA, errstr);
  264. return ccsession.create_answer(1, errstr)
  265. return ccsession.create_answer(0)
  266. def config_handler(self, new_config):
  267. answer = self._config_data_check(new_config)
  268. rcode, val = ccsession.parse_answer(answer)
  269. if rcode != 0:
  270. return answer
  271. with self._lock:
  272. for key in new_config:
  273. if key in self._cmdctl_config_data:
  274. self._cmdctl_config_data[key] = new_config[key]
  275. return answer
  276. def command_handler(self, command, args):
  277. answer = ccsession.create_answer(0)
  278. if command == ccsession.COMMAND_MODULE_SPECIFICATION_UPDATE:
  279. # The 'value' of a specification update can be either
  280. # a specification, or None. In the first case, simply
  281. # set it. If it is None, delete the module if it is
  282. # known.
  283. with self._lock:
  284. if args[1] is None:
  285. if args[0] in self.modules_spec:
  286. del self.modules_spec[args[0]]
  287. else:
  288. answer = ccsession.create_answer(1,
  289. 'No such module: ' +
  290. args[0])
  291. else:
  292. self.modules_spec[args[0]] = args[1]
  293. elif command == ccsession.COMMAND_SHUTDOWN:
  294. #When cmdctl get 'shutdown' command from b10-init,
  295. #shutdown the outer httpserver.
  296. self._module_cc.send_stopping()
  297. self._httpserver.shutdown()
  298. self._serving = False
  299. elif command == 'print_settings':
  300. answer = ccsession.create_answer(0, self._cmdctl_config_data)
  301. else:
  302. answer = ccsession.create_answer(1, 'unknown command: ' + command)
  303. return answer
  304. def _start_msg_handle_thread(self):
  305. ''' Start one thread to handle received message from msgq.'''
  306. td = threading.Thread(target=self._handle_msg_from_msgq)
  307. td.daemon = True
  308. td.start()
  309. def _handle_msg_from_msgq(self):
  310. '''Process all the received commands with module session. '''
  311. while self._serving:
  312. self._module_cc.check_command(False)
  313. def _parse_command_result(self, rcode, reply):
  314. '''Ignore the error reason when command rcode isn't 0, '''
  315. if rcode != 0:
  316. return {}
  317. return reply
  318. def _get_config_data_from_config_manager(self):
  319. '''Get config data for all modules from configmanager '''
  320. rcode, reply = self.send_command('ConfigManager', ccsession.COMMAND_GET_CONFIG)
  321. return self._parse_command_result(rcode, reply)
  322. def _update_config_data(self, module_name, command_name):
  323. '''Get lastest config data for all modules from configmanager '''
  324. if module_name == 'ConfigManager' and command_name == ccsession.COMMAND_SET_CONFIG:
  325. data = self._get_config_data_from_config_manager()
  326. with self._lock:
  327. self._config_data = data
  328. def get_config_data(self):
  329. with self._lock:
  330. data = self._config_data
  331. return data
  332. def get_modules_spec(self):
  333. with self._lock:
  334. spec = self.modules_spec
  335. return spec
  336. def _get_modules_specification(self):
  337. '''Get all the modules' specification files. '''
  338. rcode, reply = self.send_command('ConfigManager', ccsession.COMMAND_GET_MODULE_SPEC)
  339. return self._parse_command_result(rcode, reply)
  340. def send_command_with_check(self, module_name, command_name, params = None):
  341. '''Before send the command to modules, check if module_name, command_name
  342. parameters are legal according the spec file of the module.
  343. Return rcode, dict. TODO, the rcode should be defined properly.
  344. rcode = 0: dict is the correct returned value.
  345. rcode > 0: dict is : { 'error' : 'error reason' }
  346. '''
  347. # core module ConfigManager does not have a specification file
  348. if module_name == 'ConfigManager':
  349. return self.send_command(module_name, command_name, params)
  350. specs = self.get_modules_spec()
  351. if module_name not in specs.keys():
  352. return 1, {'error' : 'unknown module'}
  353. spec_obj = isc.config.module_spec.ModuleSpec(specs[module_name], False)
  354. errors = []
  355. if not spec_obj.validate_command(command_name, params, errors):
  356. return 1, {'error': errors[0]}
  357. return self.send_command(module_name, command_name, params)
  358. def send_command(self, module_name, command_name, params = None):
  359. '''Send the command from bindctl to proper module. '''
  360. errstr = 'unknown error'
  361. answer = None
  362. logger.debug(DBG_CMDCTL_MESSAGING, CMDCTL_SEND_COMMAND,
  363. command_name, module_name)
  364. if module_name == self._module_name:
  365. # Process the command sent to cmdctl directly.
  366. answer = self.command_handler(command_name, params)
  367. else:
  368. # FIXME: Due to the fact that we use a separate session
  369. # from the module one (due to threads and blocking), and
  370. # because the plain cc session does not have the high-level
  371. # rpc-call method, we use the low-level way and create the
  372. # command ourself.
  373. msg = ccsession.create_command(command_name, params)
  374. seq = self._cc.group_sendmsg(msg, module_name, want_answer=True)
  375. logger.debug(DBG_CMDCTL_MESSAGING, CMDCTL_COMMAND_SENT,
  376. command_name, module_name)
  377. #TODO, it may be blocked, msqg need to add a new interface waiting in timeout.
  378. try:
  379. answer, env = self._cc.group_recvmsg(False, seq)
  380. except isc.cc.session.SessionTimeout:
  381. errstr = "Module '%s' not responding" % module_name
  382. if answer:
  383. try:
  384. rcode, arg = ccsession.parse_answer(answer)
  385. if rcode == 0:
  386. self._update_config_data(module_name, command_name)
  387. if arg != None:
  388. return rcode, arg
  389. else:
  390. return rcode, {}
  391. else:
  392. errstr = str(answer['result'][1])
  393. except ccsession.ModuleCCSessionError as mcse:
  394. errstr = str("Error in ccsession answer:") + str(mcse)
  395. logger.error(CMDCTL_COMMAND_ERROR, command_name, module_name, errstr)
  396. return 1, {'error': errstr}
  397. def get_cmdctl_config_data(self):
  398. ''' If running in source code tree, use keyfile, certificate
  399. and user accounts file in source code. '''
  400. if "B10_FROM_SOURCE" in os.environ:
  401. sysconf_path = os.environ["B10_FROM_SOURCE"] + "/src/bin/cmdctl/"
  402. accountsfile = sysconf_path + "cmdctl-accounts.csv"
  403. keyfile = sysconf_path + "cmdctl-keyfile.pem"
  404. certfile = sysconf_path + "cmdctl-certfile.pem"
  405. return (keyfile, certfile, accountsfile)
  406. with self._lock:
  407. keyfile = self._cmdctl_config_data.get('key_file')
  408. certfile = self._cmdctl_config_data.get('cert_file')
  409. accountsfile = self._cmdctl_config_data.get('accounts_file')
  410. return (keyfile, certfile, accountsfile)
  411. class SecureHTTPServer(socketserver_mixin.NoPollMixIn,
  412. socketserver.ThreadingMixIn,
  413. http.server.HTTPServer):
  414. '''Make the server address can be reused.'''
  415. allow_reuse_address = True
  416. def __init__(self, server_address, RequestHandlerClass,
  417. CommandControlClass,
  418. idle_timeout = 1200, verbose = False):
  419. '''idle_timeout: the max idle time for login'''
  420. socketserver_mixin.NoPollMixIn.__init__(self)
  421. try:
  422. http.server.HTTPServer.__init__(self, server_address, RequestHandlerClass)
  423. logger.debug(DBG_CMDCTL_MESSAGING, CMDCTL_STARTED,
  424. server_address[0], server_address[1])
  425. except socket.error as err:
  426. raise CmdctlException("Error creating server, because: %s \n" % str(err))
  427. self.user_sessions = {}
  428. self.idle_timeout = idle_timeout
  429. self.cmdctl = CommandControlClass(self, verbose)
  430. self._verbose = verbose
  431. self._lock = threading.Lock()
  432. self._user_infos = {}
  433. self._accounts_file = None
  434. def _create_user_info(self, accounts_file):
  435. '''Read all user's name and its' salt, hashed password
  436. from accounts file.'''
  437. if (self._accounts_file == accounts_file) and (len(self._user_infos) > 0):
  438. return
  439. with self._lock:
  440. self._user_infos = {}
  441. csvfile = None
  442. try:
  443. csvfile = open(accounts_file)
  444. reader = csv.reader(csvfile)
  445. for row in reader:
  446. self._user_infos[row[0]] = [row[1], row[2]]
  447. except (IOError, IndexError) as e:
  448. logger.error(CMDCTL_USER_DATABASE_READ_ERROR,
  449. accounts_file, e)
  450. finally:
  451. if csvfile:
  452. csvfile.close()
  453. self._accounts_file = accounts_file
  454. if len(self._user_infos) == 0:
  455. logger.error(CMDCTL_NO_USER_ENTRIES_READ)
  456. def get_user_info(self, username):
  457. '''Get user's salt and hashed string. If the user
  458. doesn't exist, return None, or else, the list
  459. [salt, hashed password] will be returned.'''
  460. with self._lock:
  461. info = self._user_infos.get(username)
  462. return info
  463. def save_user_session_id(self, session_id):
  464. ''' Record user's id and login time. '''
  465. self.user_sessions[session_id] = time.time()
  466. def _check_key_and_cert(self, key, cert):
  467. check_file(key)
  468. check_file(cert);
  469. def _wrap_socket_in_ssl_context(self, sock, key, cert):
  470. try:
  471. self._check_key_and_cert(key, cert)
  472. ssl_sock = ssl.wrap_socket(sock,
  473. server_side=True,
  474. certfile=cert,
  475. keyfile=key,
  476. ssl_version=ssl.PROTOCOL_SSLv23)
  477. # Return here (if control leaves this blocks it will raise an
  478. # error)
  479. return ssl_sock
  480. except ssl.SSLError as err:
  481. logger.error(CMDCTL_SSL_SETUP_FAILURE_USER_DENIED, err)
  482. except (CmdctlException, IOError) as cce:
  483. logger.error(CMDCTL_SSL_SETUP_FAILURE_READING_CERT, cce)
  484. self.close_request(sock)
  485. # raise socket error to finish the request
  486. raise socket.error
  487. def get_request(self):
  488. '''Get client request socket and wrap it in SSL context. '''
  489. key, cert, account_file = self.cmdctl.get_cmdctl_config_data()
  490. self._create_user_info(account_file)
  491. newsocket, fromaddr = self.socket.accept()
  492. ssl_sock = self._wrap_socket_in_ssl_context(newsocket, key, cert)
  493. return (ssl_sock, fromaddr)
  494. def get_reply_data_for_GET(self, id, module):
  495. '''Currently only support the following three url GET request '''
  496. rcode, reply = http.client.NO_CONTENT, []
  497. if not module:
  498. if id == CONFIG_DATA_URL:
  499. rcode, reply = http.client.OK, self.cmdctl.get_config_data()
  500. elif id == MODULE_SPEC_URL:
  501. rcode, reply = http.client.OK, self.cmdctl.get_modules_spec()
  502. return rcode, reply
  503. def send_command_to_module(self, module_name, command_name, params):
  504. return self.cmdctl.send_command_with_check(module_name, command_name, params)
  505. httpd = None
  506. def signal_handler(signal, frame):
  507. if httpd:
  508. httpd.shutdown()
  509. sys.exit(0)
  510. def set_signal_handler():
  511. signal.signal(signal.SIGTERM, signal_handler)
  512. signal.signal(signal.SIGINT, signal_handler)
  513. def run(addr = 'localhost', port = 8080, idle_timeout = 1200, verbose = False):
  514. ''' Start cmdctl as one https server. '''
  515. httpd = SecureHTTPServer((addr, port), SecureHTTPRequestHandler,
  516. CommandControl, idle_timeout, verbose)
  517. httpd.serve_forever()
  518. def check_port(option, opt_str, value, parser):
  519. try:
  520. parser.values.port = isc.net.parse.port_parse(value)
  521. except ValueError as e:
  522. raise OptionValueError(str(e))
  523. def check_addr(option, opt_str, value, parser):
  524. try:
  525. isc.net.parse.addr_parse(value)
  526. parser.values.addr = value
  527. except ValueError as e:
  528. raise OptionValueError(str(e))
  529. def set_cmd_options(parser):
  530. parser.add_option('-p', '--port', dest = 'port', type = 'int',
  531. action = 'callback', callback=check_port,
  532. default = '8080', help = 'port cmdctl will use')
  533. parser.add_option('-a', '--address', dest = 'addr', type = 'string',
  534. action = 'callback', callback=check_addr,
  535. default = '127.0.0.1', help = 'IP address cmdctl will use')
  536. parser.add_option('-i', '--idle-timeout', dest = 'idle_timeout', type = 'int',
  537. default = '1200', help = 'login idle time out')
  538. parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
  539. help="display more about what is going on")
  540. if __name__ == '__main__':
  541. set_signal_handler()
  542. parser = OptionParser(version = __version__)
  543. set_cmd_options(parser)
  544. (options, args) = parser.parse_args()
  545. result = 1 # in case of failure
  546. try:
  547. if options.verbose:
  548. logger.set_severity("DEBUG", 99)
  549. run(options.addr, options.port, options.idle_timeout, options.verbose)
  550. result = 0
  551. except isc.cc.SessionError as err:
  552. logger.fatal(CMDCTL_CC_SESSION_ERROR, err)
  553. except isc.cc.SessionTimeout:
  554. logger.fatal(CMDCTL_CC_SESSION_TIMEOUT)
  555. except KeyboardInterrupt:
  556. logger.info(CMDCTL_STOPPED_BY_KEYBOARD)
  557. except CmdctlException as err:
  558. logger.fatal(CMDCTL_UNCAUGHT_EXCEPTION, err);
  559. if httpd:
  560. httpd.shutdown()
  561. logger.info(CMDCTL_EXITING)
  562. sys.exit(result)