cmdctl.py.in 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 optparse import OptionParser, OptionValueError
  42. from hashlib import sha1
  43. try:
  44. import threading
  45. except ImportError:
  46. import dummy_threading as threading
  47. __version__ = 'BIND10'
  48. URL_PATTERN = re.compile('/([\w]+)(?:/([\w]+))?/?')
  49. # If B10_FROM_SOURCE is set in the environment, we use data files
  50. # from a directory relative to that, otherwise we use the ones
  51. # installed on the system
  52. if "B10_FROM_SOURCE" in os.environ:
  53. SPECFILE_PATH = os.environ["B10_FROM_SOURCE"] + "/src/bin/cmdctl"
  54. SYSCONF_PATH = os.environ["B10_FROM_SOURCE"] + "/src/bin/cmdctl"
  55. else:
  56. PREFIX = "@prefix@"
  57. DATAROOTDIR = "@datarootdir@"
  58. SPECFILE_PATH = "@datadir@/@PACKAGE@".replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  59. SYSCONF_PATH = "@sysconfdir@/@PACKAGE@".replace("${prefix}", PREFIX)
  60. SPECFILE_LOCATION = SPECFILE_PATH + "/cmdctl.spec"
  61. USER_INFO_FILE = SYSCONF_PATH + "/cmdctl-accounts.csv"
  62. PRIVATE_KEY_FILE = SYSCONF_PATH + "/cmdctl-keyfile.pem"
  63. CERTIFICATE_FILE = SYSCONF_PATH + "/cmdctl-certfile.pem"
  64. class SecureHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
  65. '''https connection request handler.
  66. Currently only GET and POST are supported.
  67. '''
  68. def do_GET(self):
  69. '''The client should send its session id in header with
  70. the name 'cookie'
  71. '''
  72. self.session_id = self.headers.get('cookie')
  73. rcode, reply = http.client.OK, []
  74. if self._is_session_valid():
  75. if self._is_user_logged_in():
  76. rcode, reply = self._handle_get_request()
  77. else:
  78. rcode, reply = http.client.UNAUTHORIZED, ["please login"]
  79. else:
  80. rcode = http.client.BAD_REQUEST
  81. self.send_response(rcode)
  82. self.end_headers()
  83. self.wfile.write(json.dumps(reply).encode())
  84. def _handle_get_request(self):
  85. '''Currently only support the following three url GET request '''
  86. id, module = self._parse_request_path()
  87. return self.server.get_reply_data_for_GET(id, module)
  88. def _is_session_valid(self):
  89. return self.session_id
  90. def _is_user_logged_in(self):
  91. login_time = self.server.user_sessions.get(self.session_id)
  92. if not login_time:
  93. return False
  94. idle_time = time.time() - login_time
  95. if idle_time > self.server.idle_timeout:
  96. return False
  97. # Update idle time
  98. self.server.user_sessions[self.session_id] = time.time()
  99. return True
  100. def _parse_request_path(self):
  101. '''Parse the url, the legal url should like /ldh or /ldh/ldh '''
  102. groups = URL_PATTERN.match(self.path)
  103. if not groups:
  104. return (None, None)
  105. else:
  106. return (groups.group(1), groups.group(2))
  107. def do_POST(self):
  108. '''Process POST request. '''
  109. '''Process user login and send command to proper module
  110. The client should send its session id in header with
  111. the name 'cookie'
  112. '''
  113. self.session_id = self.headers.get('cookie')
  114. rcode, reply = http.client.OK, []
  115. if self._is_session_valid():
  116. if self.path == '/login':
  117. rcode, reply = self._handle_login()
  118. elif self._is_user_logged_in():
  119. rcode, reply = self._handle_post_request()
  120. else:
  121. rcode, reply = http.client.UNAUTHORIZED, ["please login"]
  122. else:
  123. rcode, reply = http.client.BAD_REQUEST, ["session isn't valid"]
  124. self.send_response(rcode)
  125. self.end_headers()
  126. self.wfile.write(json.dumps(reply).encode())
  127. def _handle_login(self):
  128. if self._is_user_logged_in():
  129. return http.client.OK, ["user has already login"]
  130. is_user_valid, error_info = self._check_user_name_and_pwd()
  131. if is_user_valid:
  132. self.server.save_user_session_id(self.session_id)
  133. return http.client.OK, ["login success "]
  134. else:
  135. return http.client.UNAUTHORIZED, error_info
  136. def _check_user_name_and_pwd(self):
  137. '''Check user name and its password '''
  138. length = self.headers.get('Content-Length')
  139. if not length:
  140. return False, ["invalid username or password"]
  141. try:
  142. user_info = json.loads((self.rfile.read(int(length))).decode())
  143. except:
  144. return False, ["invalid username or password"]
  145. user_name = user_info.get('username')
  146. if not user_name:
  147. return False, ["need user name"]
  148. if not self.server.user_infos.get(user_name):
  149. return False, ["user doesn't exist"]
  150. user_pwd = user_info.get('password')
  151. if not user_pwd:
  152. return False, ["need password"]
  153. local_info = self.server.user_infos.get(user_name)
  154. pwd_hashval = sha1((user_pwd + local_info[1]).encode())
  155. if pwd_hashval.hexdigest() != local_info[0]:
  156. return False, ["password doesn't match"]
  157. return True, None
  158. def _handle_post_request(self):
  159. '''Handle all the post request from client. '''
  160. mod, cmd = self._parse_request_path()
  161. if (not mod) or (not cmd):
  162. return http.client.BAD_REQUEST, ['malformed url']
  163. param = None
  164. len = self.headers.get('Content-Length')
  165. if len:
  166. try:
  167. post_str = str(self.rfile.read(int(len)).decode())
  168. param = json.loads(post_str)
  169. except:
  170. pass
  171. rcode, reply = self.server.send_command_to_module(mod, cmd, param)
  172. print('b10-cmdctl finish send message \'%s\' to module %s' % (cmd, mod))
  173. ret = http.client.OK
  174. if rcode != 0:
  175. ret = http.client.BAD_REQUEST
  176. return ret, reply
  177. def log_request(self, code='-', size='-'):
  178. '''Rewrite the log request function, log nothing.'''
  179. pass
  180. class CommandControl():
  181. '''Get all modules' config data/specification from configmanager.
  182. receive command from client and resend it to proper module.
  183. '''
  184. def __init__(self):
  185. self.cc = isc.cc.Session()
  186. self.cc.group_subscribe('Cmd-Ctrld')
  187. #self.cc.group_subscribe('Boss', 'Cmd-Ctrld')
  188. self.command_spec = self.get_cmd_specification()
  189. self.config_spec = self.get_data_specification()
  190. self.config_data = self.get_config_data()
  191. def _parse_command_result(self, rcode, reply):
  192. '''Ignore the error reason when command rcode isn't 0, '''
  193. if rcode != 0:
  194. return {}
  195. return reply
  196. def get_cmd_specification(self):
  197. rcode, reply = self.send_command('ConfigManager', isc.config.ccsession.COMMAND_GET_COMMANDS_SPEC)
  198. return self._parse_command_result(rcode, reply)
  199. def get_config_data(self):
  200. '''Get config data for all modules from configmanager '''
  201. rcode, reply = self.send_command('ConfigManager', isc.config.ccsession.COMMAND_GET_CONFIG)
  202. return self._parse_command_result(rcode, reply)
  203. def update_config_data(self, module_name, command_name):
  204. '''Get lastest config data for all modules from configmanager '''
  205. if module_name == 'ConfigManager' and command_name == isc.config.ccsession.COMMAND_SET_CONFIG:
  206. self.config_data = self.get_config_data()
  207. def get_data_specification(self):
  208. rcode, reply = self.send_command('ConfigManager', isc.config.ccsession.COMMAND_GET_MODULE_SPEC)
  209. return self._parse_command_result(rcode, reply)
  210. def handle_recv_msg(self):
  211. '''Handle received message, if 'shutdown' is received, return False'''
  212. (message, env) = self.cc.group_recvmsg(True)
  213. command, arg = isc.config.ccsession.parse_command(message)
  214. while command:
  215. if command == isc.config.ccsession.COMMAND_COMMANDS_UPDATE:
  216. self.command_spec[arg[0]] = arg[1]
  217. elif command == isc.config.ccsession.COMMAND_SPECIFICATION_UPDATE:
  218. self.config_spec[arg[0]] = arg[1]
  219. elif command == "shutdown":
  220. return False
  221. (message, env) = self.cc.group_recvmsg(True)
  222. command, arg = isc.config.ccsession.parse_command(message)
  223. return True
  224. def send_command_with_check(self, module_name, command_name, params = None):
  225. '''Before send the command to modules, check if module_name, command_name
  226. parameters are legal according the spec file of the module.
  227. Return rcode, dict.
  228. rcode = 0: dict is the correct returned value.
  229. rcode > 0: dict is : { 'error' : 'error reason' }
  230. TODO. add check for parameters.
  231. '''
  232. # core module ConfigManager does not have a specification file
  233. if module_name == 'ConfigManager':
  234. return self.send_command(module_name, command_name, params)
  235. if module_name not in self.command_spec.keys():
  236. return 1, {'error' : 'unknown module'}
  237. cmd_valid = False
  238. commands = self.command_spec[module_name]
  239. for cmd in commands:
  240. if cmd['command_name'] == command_name:
  241. cmd_valid = True
  242. break
  243. if not cmd_valid:
  244. return 1, {'error' : 'unknown command'}
  245. return self.send_command(module_name, command_name, params)
  246. def send_command(self, module_name, command_name, params = None):
  247. '''Send the command from bindctl to proper module. '''
  248. errstr = 'no error'
  249. print('b10-cmdctl send command \'%s\' to %s' %(command_name, module_name))
  250. try:
  251. msg = isc.config.ccsession.create_command(command_name, params)
  252. seq = self.cc.group_sendmsg(msg, module_name)
  253. #TODO, it may be blocked, msqg need to add a new interface waiting in timeout.
  254. answer, env = self.cc.group_recvmsg(False, seq)
  255. if answer:
  256. try:
  257. rcode, arg = isc.config.ccsession.parse_answer(answer)
  258. if rcode == 0:
  259. self.update_config_data(module_name, command_name)
  260. if arg != None:
  261. return rcode, arg
  262. else:
  263. return rcode, {}
  264. else:
  265. # todo: exception
  266. errstr = str(answer['result'][1])
  267. except isc.config.ccsession.ModuleCCSessionError as mcse:
  268. errstr = str("Error in ccsession answer:") + str(mcse)
  269. print(answer)
  270. except Exception as e:
  271. errstr = str(e)
  272. print(e, ':b10-cmdctl fail send command \'%s\' to %s' % (command_name, module_name))
  273. return 1, {'error': errstr}
  274. class SecureHTTPServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
  275. '''Make the server address can be reused.'''
  276. allow_reuse_address = True
  277. def __init__(self, server_address, RequestHandlerClass, idle_timeout = 1200):
  278. '''idle_timeout: the max idle time for login'''
  279. http.server.HTTPServer.__init__(self, server_address, RequestHandlerClass)
  280. self.user_sessions = {}
  281. self.idle_timeout = idle_timeout
  282. self.cmdctrl = CommandControl()
  283. self.__is_shut_down = threading.Event()
  284. self.__serving = False
  285. self.user_infos = {}
  286. self._read_user_info()
  287. def _read_user_info(self):
  288. '''Read all user's name and its' password from csv file.'''
  289. csvfile = None
  290. try:
  291. csvfile = open(USER_INFO_FILE)
  292. reader = csv.reader(csvfile)
  293. for row in reader:
  294. self.user_infos[row[0]] = [row[1], row[2]]
  295. except Exception as e:
  296. print("Fail to read user information ", e)
  297. finally:
  298. if csvfile:
  299. csvfile.close()
  300. def save_user_session_id(self, session_id):
  301. # Record user's id and login time.
  302. self.user_sessions[session_id] = time.time()
  303. def get_request(self):
  304. '''Get client request socket and wrap it in SSL context. '''
  305. newsocket, fromaddr = self.socket.accept()
  306. try:
  307. connstream = ssl.wrap_socket(newsocket,
  308. server_side = True,
  309. certfile = CERTIFICATE_FILE,
  310. keyfile = PRIVATE_KEY_FILE,
  311. ssl_version = ssl.PROTOCOL_SSLv23)
  312. return (connstream, fromaddr)
  313. except ssl.SSLError as e :
  314. print("cmdctl: deny client's invalid connection", e)
  315. self.close_request(newsocket)
  316. # raise socket error to finish the request
  317. raise socket.error
  318. def get_reply_data_for_GET(self, id, module):
  319. '''Currently only support the following three url GET request '''
  320. rcode, reply = http.client.NO_CONTENT, []
  321. if not module:
  322. if id == 'command_spec':
  323. rcode, reply = http.client.OK, self.cmdctrl.command_spec
  324. elif id == 'config_data':
  325. rcode, reply = http.client.OK, self.cmdctrl.config_data
  326. elif id == 'config_spec':
  327. rcode, reply = http.client.OK, self.cmdctrl.config_spec
  328. return rcode, reply
  329. def serve_forever(self, poll_interval = 0.5):
  330. '''Start cmdctl as one tcp server. '''
  331. self.__serving = True
  332. self.__is_shut_down.clear()
  333. while self.__serving:
  334. if not self.cmdctrl.handle_recv_msg():
  335. break
  336. r, w, e = select.select([self], [], [], poll_interval)
  337. if r:
  338. self._handle_request_noblock()
  339. self.__is_shut_down.set()
  340. def shutdown(self):
  341. self.__serving = False
  342. self.__is_shut_down.wait()
  343. def send_command_to_module(self, module_name, command_name, params):
  344. return self.cmdctrl.send_command_with_check(module_name, command_name, params)
  345. httpd = None
  346. def signal_handler(signal, frame):
  347. if httpd:
  348. httpd.shutdown()
  349. sys.exit(0)
  350. def set_signal_handler():
  351. signal.signal(signal.SIGTERM, signal_handler)
  352. signal.signal(signal.SIGINT, signal_handler)
  353. def run(addr = 'localhost', port = 8080, idle_timeout = 1200):
  354. ''' Start cmdctl as one https server. '''
  355. print("b10-cmdctl module is starting on :%s port:%d" %(addr, port))
  356. httpd = SecureHTTPServer((addr, port), SecureHTTPRequestHandler, idle_timeout)
  357. httpd.serve_forever()
  358. def check_port(option, opt_str, value, parser):
  359. if (value < 0) or (value > 65535):
  360. raise OptionValueError('%s requires a port number (0-65535)' % opt_str)
  361. parser.values.port = value
  362. def check_addr(option, opt_str, value, parser):
  363. ipstr = value
  364. ip_family = socket.AF_INET
  365. if (ipstr.find(':') != -1):
  366. ip_family = socket.AF_INET6
  367. try:
  368. socket.inet_pton(ip_family, ipstr)
  369. except:
  370. raise OptionValueError("%s invalid ip address" % ipstr)
  371. parser.values.addr = value
  372. def set_cmd_options(parser):
  373. parser.add_option('-p', '--port', dest = 'port', type = 'int',
  374. action = 'callback', callback=check_port,
  375. default = '8080', help = 'port cmdctl will use')
  376. parser.add_option('-a', '--address', dest = 'addr', type = 'string',
  377. action = 'callback', callback=check_addr,
  378. default = '127.0.0.1', help = 'IP address cmdctl will use')
  379. parser.add_option('-i', '--idle-timeout', dest = 'idle_timeout', type = 'int',
  380. default = '1200', help = 'login idle time out')
  381. if __name__ == '__main__':
  382. try:
  383. parser = OptionParser(version = __version__)
  384. set_cmd_options(parser)
  385. (options, args) = parser.parse_args()
  386. set_signal_handler()
  387. run(options.addr, options.port, options.idle_timeout)
  388. except isc.cc.SessionError as se:
  389. print("[b10-cmdctl] Error creating b10-cmdctl, "
  390. "is the command channel daemon running?")
  391. except KeyboardInterrupt:
  392. print("exit http server")
  393. if httpd:
  394. httpd.shutdown()