b10-cmdctl.py.in 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 http.server
  29. import urllib.parse
  30. import json
  31. import re
  32. import ssl, socket
  33. import isc
  34. import pprint
  35. import select
  36. import csv
  37. import random
  38. from hashlib import sha1
  39. try:
  40. import threading
  41. except ImportError:
  42. import dummy_threading as threading
  43. URL_PATTERN = re.compile('/([\w]+)(?:/([\w]+))?/?')
  44. # If B10_FROM_SOURCE is set in the environment, we use data files
  45. # from a directory relative to that, otherwise we use the ones
  46. # installed on the system
  47. if "B10_FROM_SOURCE" in os.environ:
  48. SPECFILE_PATH = os.environ["B10_FROM_SOURCE"] + "/src/bin/cmdctl"
  49. else:
  50. PREFIX = "@prefix@"
  51. DATAROOTDIR = "@datarootdir@"
  52. SPECFILE_PATH = "@datadir@/@PACKAGE@".replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  53. SPECFILE_LOCATION = SPECFILE_PATH + "/cmdctl.spec"
  54. USER_INFO_FILE = SPECFILE_PATH + "/passwd.csv"
  55. CERTIFICATE_FILE = SPECFILE_PATH + "/b10-cmdctl.pem"
  56. class SecureHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
  57. '''https connection request handler.
  58. Currently only GET and POST are supported.
  59. '''
  60. def do_GET(self):
  61. '''The client should send its session id in header with
  62. the name 'cookie'
  63. '''
  64. self.session_id = self.headers.get('cookie')
  65. rcode, reply = http.client.OK, []
  66. if self._is_session_valid():
  67. if self._is_user_logged_in():
  68. rcode, reply = self._handle_get_request()
  69. else:
  70. rcode, reply = http.client.UNAUTHORIZED, ["please login"]
  71. else:
  72. rcode = http.client.BAD_REQUEST
  73. self.send_response(rcode)
  74. self.end_headers()
  75. self.wfile.write(json.dumps(reply).encode())
  76. def _handle_get_request(self):
  77. '''Currently only support the following three url GET request '''
  78. id, module = self._parse_request_path()
  79. return self.server.get_reply_data_for_GET(id, module)
  80. def _is_session_valid(self):
  81. return self.session_id
  82. def _is_user_logged_in(self):
  83. return self.session_id in self.server.user_sessions
  84. def _parse_request_path(self):
  85. '''Parse the url, the legal url should like /ldh or /ldh/ldh '''
  86. groups = URL_PATTERN.match(self.path)
  87. if not groups:
  88. return (None, None)
  89. else:
  90. return (groups.group(1), groups.group(2))
  91. def do_POST(self):
  92. '''Process user login and send command to proper module
  93. The client should send its session id in header with
  94. the name 'cookie'
  95. '''
  96. self.session_id = self.headers.get('cookie')
  97. rcode, reply = http.client.OK, []
  98. if self._is_session_valid():
  99. if self.path == '/login':
  100. rcode, reply = self._handle_login()
  101. else:
  102. rcode, reply = self._handle_post_request()
  103. else:
  104. rcode, reply = http.client.BAD_REQUEST, ["session isn't valid"]
  105. self.send_response(rcode)
  106. self.end_headers()
  107. self.wfile.write(json.dumps(reply).encode())
  108. def _handle_login(self):
  109. if self._is_user_logged_in():
  110. return http.client.OK, ["user has already login"]
  111. is_user_valid, error_info = self._check_user_name_and_pwd()
  112. if is_user_valid:
  113. self.server.user_sessions.append(self.session_id)
  114. return http.client.OK, ["login success "]
  115. else:
  116. return http.client.UNAUTHORIZED, error_info
  117. def _check_user_name_and_pwd(self):
  118. length = self.headers.get('Content-Length')
  119. if not length:
  120. return False, ["invalid username or password"]
  121. user_info = json.loads((self.rfile.read(int(length))).decode())
  122. if not user_info:
  123. return False, ["invalid username or password"]
  124. user_name = user_info.get('username')
  125. if not user_name:
  126. return False, ["need user name"]
  127. if not self.server.user_infos.get(user_name):
  128. return False, ["user doesn't exist"]
  129. user_pwd = user_info.get('password')
  130. if not user_pwd:
  131. return False, ["need password"]
  132. local_info = self.server.user_infos.get(user_name)
  133. pwd_hashval = sha1((user_pwd + local_info[1]).encode())
  134. if pwd_hashval.hexdigest() != local_info[0]:
  135. return False, ["password doesn't match"]
  136. return True, None
  137. def _handle_post_request(self):
  138. mod, cmd = self._parse_request_path()
  139. param = None
  140. len = self.headers.get('Content-Length')
  141. rcode = http.client.OK
  142. reply = None
  143. if len:
  144. post_str = str(self.rfile.read(int(len)).decode())
  145. print("command parameter:%s" % post_str)
  146. param = json.loads(post_str)
  147. # TODO, need return some proper return code.
  148. # currently always OK.
  149. reply = self.server.send_command_to_module(mod, cmd, param)
  150. print('b10-cmdctl finish send message \'%s\' to module %s' % (cmd, mod))
  151. return rcode, reply
  152. class CommandControl():
  153. '''Get all modules' config data/specification from configmanager.
  154. receive command from client and resend it to proper module.
  155. '''
  156. def __init__(self):
  157. self.cc = isc.cc.Session()
  158. self.cc.group_subscribe('Cmd-Ctrld')
  159. #self.cc.group_subscribe('Boss', 'Cmd-Ctrld')
  160. self.command_spec = self.get_cmd_specification()
  161. self.config_spec = self.get_data_specification()
  162. self.config_data = self.get_config_data()
  163. def get_cmd_specification(self):
  164. return self.send_command('ConfigManager', 'get_commands_spec')
  165. def get_config_data(self):
  166. return self.send_command('ConfigManager', 'get_config')
  167. def update_config_data(self, module_name, command_name):
  168. if module_name == 'ConfigManager' and command_name == 'set_config':
  169. self.config_data = self.get_config_data()
  170. def get_data_specification(self):
  171. return self.send_command('ConfigManager', 'get_module_spec')
  172. def handle_recv_msg(self):
  173. # Handle received message, if 'shutdown' is received, return False
  174. (message, env) = self.cc.group_recvmsg(True)
  175. while message:
  176. if 'commands_update' in message:
  177. self.command_spec[message['commands_update'][0]] = message['commands_update'][1]
  178. elif 'specification_update' in message:
  179. msgvalue = message['specification_update']
  180. self.config_spec[msgvalue[0]] = msgvalue[1]
  181. elif 'command' in message and message['command'][0] == 'shutdown':
  182. return False;
  183. (message, env) = self.cc.group_recvmsg(True)
  184. return True
  185. def send_command(self, module_name, command_name, params = None):
  186. content = [command_name]
  187. if params:
  188. content.append(params)
  189. msg = {'command' : content}
  190. print('b10-cmdctl send command \'%s\' to %s' %(command_name, module_name))
  191. try:
  192. self.cc.group_sendmsg(msg, module_name)
  193. #TODO, it may be blocked, msqg need to add a new interface
  194. # wait in timeout.
  195. answer, env = self.cc.group_recvmsg(False)
  196. if answer and 'result' in answer.keys() and type(answer['result']) == list:
  197. # TODO: with the new cc implementation, replace "1" by 1
  198. if answer['result'][0] == 1:
  199. # todo: exception
  200. print("Error: " + str(answer['result'][1]))
  201. return {}
  202. else:
  203. self.update_config_data(module_name, command_name)
  204. if len(answer['result']) > 1:
  205. return answer['result'][1]
  206. return {}
  207. else:
  208. print("Error: unexpected answer from %s" % module_name)
  209. print(answer)
  210. except Exception as e:
  211. print(e)
  212. print('b10-cmdctl fail send command \'%s\' to %s' % (command_name, module_name))
  213. return {}
  214. class SecureHTTPServer(http.server.HTTPServer):
  215. '''Make the server address can be reused.'''
  216. allow_reuse_address = True
  217. def __init__(self, server_address, RequestHandlerClass):
  218. http.server.HTTPServer.__init__(self, server_address, RequestHandlerClass)
  219. self.user_sessions = []
  220. self.cmdctrl = CommandControl()
  221. self.__is_shut_down = threading.Event()
  222. self.__serving = False
  223. self.user_infos = {}
  224. self._read_user_info()
  225. def _read_user_info(self):
  226. # Get all username and password information
  227. csvfile = None
  228. try:
  229. csvfile = open(USER_INFO_FILE)
  230. reader = csv.reader(csvfile)
  231. for row in reader:
  232. self.user_infos[row[0]] = [row[1], row[2]]
  233. except Exception as e:
  234. print("Fail to read user information ", e)
  235. exit(1)
  236. finally:
  237. if csvfile:
  238. csvfile.close()
  239. def get_request(self):
  240. newsocket, fromaddr = self.socket.accept()
  241. try:
  242. connstream = ssl.wrap_socket(newsocket,
  243. server_side = True,
  244. certfile = CERTIFICATE_FILE,
  245. keyfile = CERTIFICATE_FILE,
  246. ssl_version = ssl.PROTOCOL_SSLv23)
  247. return (connstream, fromaddr)
  248. except ssl.SSLError as e :
  249. print("cmdctl: deny client's invalid connection", e)
  250. self.close_request(newsocket)
  251. # raise socket error to finish the request
  252. raise socket.error
  253. def get_reply_data_for_GET(self, id, module):
  254. '''Currently only support the following three url GET request '''
  255. rcode, reply = http.client.NO_CONTENT, []
  256. if not module:
  257. rcode = http.client.OK
  258. if id == 'command_spec':
  259. reply = self.cmdctrl.command_spec
  260. elif id == 'config_data':
  261. reply = self.cmdctrl.config_data
  262. elif id == 'config_spec':
  263. reply = self.cmdctrl.config_spec
  264. return rcode, reply
  265. def serve_forever(self, poll_interval = 0.5):
  266. self.__serving = True
  267. self.__is_shut_down.clear()
  268. while self.__serving:
  269. if not self.cmdctrl.handle_recv_msg():
  270. break
  271. r, w, e = select.select([self], [], [], poll_interval)
  272. if r:
  273. self._handle_request_noblock()
  274. self.__is_shut_down.set()
  275. def shutdown(self):
  276. self.__serving = False
  277. self.__is_shut_down.wait()
  278. def send_command_to_module(self, module_name, command_name, params):
  279. return self.cmdctrl.send_command(module_name, command_name, params)
  280. def run(server_class = SecureHTTPServer, addr = 'localhost', port = 8080):
  281. ''' Start cmdctl as one https server. '''
  282. print("b10-cmdctl module is starting on :%s port:%d" %(addr, port))
  283. httpd = server_class((addr, port), SecureHTTPRequestHandler)
  284. httpd.serve_forever()
  285. if __name__ == '__main__':
  286. try:
  287. run()
  288. except isc.cc.SessionError as se:
  289. print("[b10-cmdctl] Error creating b10-cmdctl, "
  290. "is the command channel daemon running?")
  291. except KeyboardInterrupt:
  292. print("exit http server")