zonemgr.py.in 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. #!@PYTHON@
  2. # Copyright (C) 2010 Internet Systems Consortium.
  3. # Copyright (C) 2010 CZ NIC
  4. #
  5. # Permission to use, copy, modify, and distribute this software for any
  6. # purpose with or without fee is hereby granted, provided that the above
  7. # copyright notice and this permission notice appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  10. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  11. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  12. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  13. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  14. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  15. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  16. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. """\
  18. This file implements the Secondary Manager program.
  19. The secondary manager is one of the co-operating processes
  20. of BIND10, which keeps track of timers and other information
  21. necessary for BIND10 to act as a slave.
  22. """
  23. import sys; sys.path.append ('@@PYTHONPATH@@')
  24. import os
  25. import time
  26. import signal
  27. import isc
  28. import random
  29. import threading
  30. import select
  31. import socket
  32. import errno
  33. from isc.datasrc import sqlite3_ds
  34. from optparse import OptionParser, OptionValueError
  35. from isc.config.ccsession import *
  36. import isc.utils.process
  37. isc.utils.process.rename()
  38. # If B10_FROM_BUILD is set in the environment, we use data files
  39. # from a directory relative to that, otherwise we use the ones
  40. # installed on the system
  41. if "B10_FROM_BUILD" in os.environ:
  42. SPECFILE_PATH = os.environ["B10_FROM_BUILD"] + "/src/bin/zonemgr"
  43. AUTH_SPECFILE_PATH = os.environ["B10_FROM_BUILD"] + "/src/bin/auth"
  44. else:
  45. PREFIX = "@prefix@"
  46. DATAROOTDIR = "@datarootdir@"
  47. SPECFILE_PATH = "@datadir@/@PACKAGE@".replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  48. AUTH_SPECFILE_PATH = SPECFILE_PATH
  49. SPECFILE_LOCATION = SPECFILE_PATH + "/zonemgr.spec"
  50. AUTH_SPECFILE_LOCATION = AUTH_SPECFILE_PATH + "/auth.spec"
  51. __version__ = "BIND10"
  52. # define module name
  53. XFRIN_MODULE_NAME = 'Xfrin'
  54. AUTH_MODULE_NAME = 'Auth'
  55. # define command name
  56. ZONE_XFRIN_FAILED_COMMAND = 'zone_xfrin_failed'
  57. ZONE_XFRIN_SUCCESS_COMMAND = 'zone_new_data_ready'
  58. ZONE_REFRESH_COMMAND = 'refresh_from_zonemgr'
  59. ZONE_NOTIFY_COMMAND = 'notify'
  60. # define zone state
  61. ZONE_OK = 0
  62. ZONE_REFRESHING = 1
  63. ZONE_EXPIRED = 2
  64. # smallest refresh timeout
  65. LOWERBOUND_REFRESH = 10
  66. # smallest retry timeout
  67. LOWERBOUND_RETRY = 5
  68. # max zone transfer timeout
  69. MAX_TRANSFER_TIMEOUT = 14400
  70. # offsets of fields in the SOA RDATA
  71. REFRESH_OFFSET = 3
  72. RETRY_OFFSET = 4
  73. EXPIRED_OFFSET = 5
  74. # verbose mode
  75. VERBOSE_MODE = False
  76. def log_msg(msg):
  77. if VERBOSE_MODE:
  78. sys.stdout.write("[b10-zonemgr] %s\n" % str(msg))
  79. class ZonemgrException(Exception):
  80. pass
  81. class ZonemgrRefresh:
  82. """This class will maintain and manage zone refresh info.
  83. It also provides methods to keep track of zone timers and
  84. do zone refresh.
  85. """
  86. def __init__(self, cc, db_file, slave_socket):
  87. self._cc = cc
  88. self._socket = slave_socket
  89. self._db_file = db_file
  90. self._zonemgr_refresh_info = {}
  91. self._build_zonemgr_refresh_info()
  92. def _random_jitter(self, max, jitter):
  93. """Imposes some random jitters for refresh and
  94. retry timers to avoid many zones need to do refresh
  95. at the same time.
  96. The value should be between (max - jitter) and max.
  97. """
  98. if 0 == jitter:
  99. return max
  100. return random.uniform(max - jitter, max)
  101. def _get_current_time(self):
  102. return time.time()
  103. def _set_zone_timer(self, zone_name_class, max, jitter):
  104. """Set zone next refresh time."""
  105. self._set_zone_next_refresh_time(zone_name_class, self._get_current_time() + \
  106. self._random_jitter(max, jitter))
  107. def _set_zone_refresh_timer(self, zone_name_class):
  108. """Set zone next refresh time after zone refresh success.
  109. now + refresh*3/4 <= next_refresh_time <= now + refresh
  110. """
  111. zone_refresh_time = float(self._get_zone_soa_rdata(zone_name_class).split(" ")[REFRESH_OFFSET])
  112. zone_refresh_time = max(LOWERBOUND_REFRESH, zone_refresh_time)
  113. self._set_zone_timer(zone_name_class, zone_refresh_time, (1 * zone_refresh_time) / 4)
  114. def _set_zone_retry_timer(self, zone_name_class):
  115. """Set zone next refresh time after zone refresh fail.
  116. now + retry*3/4 <= next_refresh_time <= now + retry
  117. """
  118. zone_retry_time = float(self._get_zone_soa_rdata(zone_name_class).split(" ")[RETRY_OFFSET])
  119. zone_retry_time = max(LOWERBOUND_RETRY, zone_retry_time)
  120. self._set_zone_timer(zone_name_class, zone_retry_time, (1 * zone_retry_time) / 4)
  121. def _set_zone_notify_timer(self, zone_name_class):
  122. """Set zone next refresh time after receiving notify
  123. next_refresh_time = now
  124. """
  125. self._set_zone_timer(zone_name_class, 0, 0)
  126. def _zone_not_exist(self, zone_name_class):
  127. """ Zone doesn't belong to zonemgr"""
  128. if zone_name_class in self._zonemgr_refresh_info.keys():
  129. return False
  130. return True
  131. def zone_refresh_success(self, zone_name_class):
  132. """Update zone info after zone refresh success"""
  133. if (self._zone_not_exist(zone_name_class)):
  134. raise ZonemgrException("[b10-zonemgr] Zone (%s, %s) doesn't "
  135. "belong to zonemgr" % zone_name_class)
  136. return
  137. self.zonemgr_reload_zone(zone_name_class)
  138. self._set_zone_refresh_timer(zone_name_class)
  139. self._set_zone_state(zone_name_class, ZONE_OK)
  140. self._set_zone_last_refresh_time(zone_name_class, self._get_current_time())
  141. def zone_refresh_fail(self, zone_name_class):
  142. """Update zone info after zone refresh fail"""
  143. if (self._zone_not_exist(zone_name_class)):
  144. raise ZonemgrException("[b10-zonemgr] Zone (%s, %s) doesn't "
  145. "belong to zonemgr" % zone_name_class)
  146. return
  147. # Is zone expired?
  148. if (self._zone_is_expired(zone_name_class)):
  149. self._set_zone_state(zone_name_class, ZONE_EXPIRED)
  150. else:
  151. self._set_zone_state(zone_name_class, ZONE_OK)
  152. self._set_zone_retry_timer(zone_name_class)
  153. def zone_handle_notify(self, zone_name_class, master):
  154. """Handle zone notify"""
  155. if (self._zone_not_exist(zone_name_class)):
  156. raise ZonemgrException("[b10-zonemgr] Notified zone (%s, %s) "
  157. "doesn't belong to zonemgr" % zone_name_class)
  158. return
  159. self._set_zone_notifier_master(zone_name_class, master)
  160. self._set_zone_notify_timer(zone_name_class)
  161. def zonemgr_reload_zone(self, zone_name_class):
  162. """ Reload a zone."""
  163. zone_soa = sqlite3_ds.get_zone_soa(str(zone_name_class[0]), self._db_file)
  164. self._zonemgr_refresh_info[zone_name_class]["zone_soa_rdata"] = zone_soa[7]
  165. def zonemgr_add_zone(self, zone_name_class):
  166. """ Add a zone into zone manager."""
  167. zone_info = {}
  168. zone_soa = sqlite3_ds.get_zone_soa(str(zone_name_class[0]), self._db_file)
  169. if not zone_soa:
  170. raise ZonemgrException("[b10-zonemgr] zone (%s, %s) doesn't have soa." % zone_name_class)
  171. zone_info["zone_soa_rdata"] = zone_soa[7]
  172. zone_info["zone_state"] = ZONE_OK
  173. zone_info["last_refresh_time"] = self._get_current_time()
  174. zone_info["next_refresh_time"] = self._get_current_time() + \
  175. float(zone_soa[7].split(" ")[REFRESH_OFFSET])
  176. self._zonemgr_refresh_info[zone_name_class] = zone_info
  177. def _build_zonemgr_refresh_info(self):
  178. """ Build zonemgr refresh info map."""
  179. log_msg("Start loading zone into zonemgr.")
  180. for zone_name, zone_class in sqlite3_ds.get_zones_info(self._db_file):
  181. zone_name_class = (zone_name, zone_class)
  182. self.zonemgr_add_zone(zone_name_class)
  183. log_msg("Finish loading zone into zonemgr.")
  184. def _zone_is_expired(self, zone_name_class):
  185. """Judge whether a zone is expired or not."""
  186. zone_expired_time = float(self._get_zone_soa_rdata(zone_name_class).split(" ")[EXPIRED_OFFSET])
  187. zone_last_refresh_time = self._get_zone_last_refresh_time(zone_name_class)
  188. if (ZONE_EXPIRED == self._get_zone_state(zone_name_class) or
  189. zone_last_refresh_time + zone_expired_time <= self._get_current_time()):
  190. return True
  191. return False
  192. def _get_zone_soa_rdata(self, zone_name_class):
  193. return self._zonemgr_refresh_info[zone_name_class]["zone_soa_rdata"]
  194. def _get_zone_last_refresh_time(self, zone_name_class):
  195. return self._zonemgr_refresh_info[zone_name_class]["last_refresh_time"]
  196. def _set_zone_last_refresh_time(self, zone_name_class, time):
  197. self._zonemgr_refresh_info[zone_name_class]["last_refresh_time"] = time
  198. def _get_zone_notifier_master(self, zone_name_class):
  199. if ("notify_master" in self._zonemgr_refresh_info[zone_name_class].keys()):
  200. return self._zonemgr_refresh_info[zone_name_class]["notify_master"]
  201. return None
  202. def _set_zone_notifier_master(self, zone_name_class, master_addr):
  203. self._zonemgr_refresh_info[zone_name_class]["notify_master"] = master_addr
  204. def _clear_zone_notifier_master(self, zone_name_class):
  205. if ("notify_master" in self._zonemgr_refresh_info[zone_name_class].keys()):
  206. del self._zonemgr_refresh_info[zone_name_class]["notify_master"]
  207. def _get_zone_state(self, zone_name_class):
  208. return self._zonemgr_refresh_info[zone_name_class]["zone_state"]
  209. def _set_zone_state(self, zone_name_class, zone_state):
  210. self._zonemgr_refresh_info[zone_name_class]["zone_state"] = zone_state
  211. def _get_zone_refresh_timeout(self, zone_name_class):
  212. return self._zonemgr_refresh_info[zone_name_class]["refresh_timeout"]
  213. def _set_zone_refresh_timeout(self, zone_name_class, time):
  214. self._zonemgr_refresh_info[zone_name_class]["refresh_timeout"] = time
  215. def _get_zone_next_refresh_time(self, zone_name_class):
  216. return self._zonemgr_refresh_info[zone_name_class]["next_refresh_time"]
  217. def _set_zone_next_refresh_time(self, zone_name_class, time):
  218. self._zonemgr_refresh_info[zone_name_class]["next_refresh_time"] = time
  219. def _send_command(self, module_name, command_name, params):
  220. """Send command between modules."""
  221. msg = create_command(command_name, params)
  222. try:
  223. self._cc.group_sendmsg(msg, module_name)
  224. except socket.error:
  225. sys.stderr.write("[b10-zonemgr] Failed to send to module %s, the session has been closed." % module_name)
  226. def _find_need_do_refresh_zone(self):
  227. """Find the first zone need do refresh, if no zone need
  228. do refresh, return the zone with minimum next_refresh_time.
  229. """
  230. zone_need_refresh = None
  231. for zone_name_class in self._zonemgr_refresh_info.keys():
  232. zone_state = self._get_zone_state(zone_name_class)
  233. # If hasn't received refresh response but are within refresh timeout, skip the zone
  234. if (ZONE_REFRESHING == zone_state and
  235. (self._get_zone_refresh_timeout(zone_name_class) > self._get_current_time())):
  236. continue
  237. # Get the zone with minimum next_refresh_time
  238. if ((zone_need_refresh is None) or
  239. (self._get_zone_next_refresh_time(zone_name_class) <
  240. self._get_zone_next_refresh_time(zone_need_refresh))):
  241. zone_need_refresh = zone_name_class
  242. # Find the zone need do refresh
  243. if (self._get_zone_next_refresh_time(zone_need_refresh) < self._get_current_time()):
  244. break
  245. return zone_need_refresh
  246. def _do_refresh(self, zone_name_class):
  247. """Do zone refresh."""
  248. log_msg("Do refresh for zone (%s, %s)." % zone_name_class)
  249. self._set_zone_state(zone_name_class, ZONE_REFRESHING)
  250. self._set_zone_refresh_timeout(zone_name_class, self._get_current_time() + MAX_TRANSFER_TIMEOUT)
  251. notify_master = self._get_zone_notifier_master(zone_name_class)
  252. # If the zone has notify master, send notify command to xfrin module
  253. if notify_master:
  254. param = {"zone_name" : zone_name_class[0],
  255. "zone_class" : zone_name_class[1],
  256. "master" : notify_master
  257. }
  258. self._send_command(XFRIN_MODULE_NAME, ZONE_NOTIFY_COMMAND, param)
  259. self._clear_zone_notifier_master(zone_name_class)
  260. # Send refresh command to xfrin module
  261. else:
  262. param = {"zone_name" : zone_name_class[0],
  263. "zone_class" : zone_name_class[1]
  264. }
  265. self._send_command(XFRIN_MODULE_NAME, ZONE_REFRESH_COMMAND, param)
  266. def _zone_mgr_is_empty(self):
  267. """Does zone manager has no zone?"""
  268. if not len(self._zonemgr_refresh_info):
  269. return True
  270. return False
  271. def run_timer(self):
  272. """Keep track of zone timers."""
  273. while True:
  274. # Zonemgr has no zone.
  275. if self._zone_mgr_is_empty():
  276. time.sleep(LOWERBOUND_RETRY) # A better time?
  277. continue
  278. zone_need_refresh = self._find_need_do_refresh_zone()
  279. # If don't get zone with minimum next refresh time, set timer timeout = LOWERBOUND_REFRESH
  280. if not zone_need_refresh:
  281. timeout = LOWERBOUND_RETRY
  282. else:
  283. timeout = self._get_zone_next_refresh_time(zone_need_refresh) - self._get_current_time()
  284. if (timeout < 0):
  285. self._do_refresh(zone_need_refresh)
  286. continue
  287. """ Wait for the socket notification for a maximum time of timeout
  288. in seconds (as float)."""
  289. try:
  290. (rlist, wlist, xlist) = select.select([self._socket], [], [], timeout)
  291. if rlist:
  292. self._socket.recv(32)
  293. except ValueError as e:
  294. raise ZonemgrException("[b10-zonemgr] Socket has been closed\n")
  295. break
  296. except select.error as e:
  297. if e.args[0] == errno.EINTR:
  298. (rlist, wlist, xlist) = ([], [], [])
  299. else:
  300. raise ZonemgrException("[b10-zonemgr] Error with select(): %s\n" % e)
  301. break
  302. class Zonemgr:
  303. """Zone manager class."""
  304. def __init__(self):
  305. self._setup_session()
  306. self._db_file = self.get_db_file()
  307. # Create socket pair for communicating between main thread and zonemgr timer thread
  308. self._master_socket, self._slave_socket = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)
  309. self._zone_refresh= ZonemgrRefresh(self._cc, self._db_file, self._slave_socket)
  310. self._start_zone_refresh_timer()
  311. self._lock = threading.Lock()
  312. self._shutdown_event = threading.Event()
  313. def _start_zone_refresh_timer(self):
  314. """Start a new thread to keep track of zone timers"""
  315. listener = threading.Thread(target = self._zone_refresh.run_timer, args = ())
  316. listener.setDaemon(True)
  317. listener.start()
  318. def _setup_session(self):
  319. """Setup two sessions for zonemgr, one(self._module_cc) is used for receiving
  320. commands and config data sent from other modules, another one (self._cc)
  321. is used to send commands to proper modules."""
  322. self._cc = isc.cc.Session()
  323. self._module_cc = isc.config.ModuleCCSession(SPECFILE_LOCATION,
  324. self.config_handler,
  325. self.command_handler)
  326. self._module_cc.add_remote_config(AUTH_SPECFILE_LOCATION)
  327. self._config_data = self._module_cc.get_full_config()
  328. self._module_cc.start()
  329. def get_db_file(self):
  330. db_file, is_default = self._module_cc.get_remote_config_value(AUTH_MODULE_NAME, "database_file")
  331. # this too should be unnecessary, but currently the
  332. # 'from build' override isn't stored in the config
  333. # (and we don't have indirect python access to datasources yet)
  334. if is_default and "B10_FROM_BUILD" in os.environ:
  335. db_file = os.environ["B10_FROM_BUILD"] + "/bind10_zones.sqlite3"
  336. return db_file
  337. def shutdown(self):
  338. """Shutdown the zonemgr process. the thread which is keeping track of zone
  339. timers should be terminated.
  340. """
  341. self._slave_socket.close()
  342. self._master_socket.close()
  343. self._shutdown_event.set()
  344. main_thread = threading.currentThread()
  345. for th in threading.enumerate():
  346. if th is main_thread:
  347. continue
  348. th.join()
  349. def config_handler(self, new_config):
  350. """Update config data."""
  351. answer = create_answer(0)
  352. for key in new_config:
  353. if key not in self._config_data:
  354. answer = create_answer(1, "Unknown config data: " + str(key))
  355. continue
  356. self._config_data[key] = new_config[key]
  357. return answer
  358. def _parse_cmd_params(self, args, command):
  359. zone_name = args.get("zone_name")
  360. if not zone_name:
  361. raise ZonemgrException("zone name should be provided")
  362. zone_class = args.get("zone_class")
  363. if not zone_class:
  364. raise ZonemgrException("zone class should be provided")
  365. if (command != ZONE_NOTIFY_COMMAND):
  366. return (zone_name, zone_class)
  367. master_str = args.get("master")
  368. if not master_str:
  369. raise ZonemgrException("master address should be provided")
  370. return ((zone_name, zone_class), master_str)
  371. def command_handler(self, command, args):
  372. """Handle command receivd from command channel.
  373. ZONE_NOTIFY_COMMAND is issued by Auth process; ZONE_XFRIN_SUCCESS_COMMAND
  374. and ZONE_XFRIN_FAILED_COMMAND are issued by Xfrin process; shutdown is issued
  375. by a user or Boss process. """
  376. answer = create_answer(0)
  377. if command == ZONE_NOTIFY_COMMAND:
  378. """ Handle Auth notify command"""
  379. # master is the source sender of the notify message.
  380. zone_name_class, master = self._parse_cmd_params(args, command)
  381. log_msg("Received notify command for zone (%s, %s)." % zone_name_class)
  382. with self._lock:
  383. self._zone_refresh.zone_handle_notify(zone_name_class, master)
  384. # Send notification to zonemgr timer thread
  385. self._master_socket.send(b" ")
  386. elif command == ZONE_XFRIN_SUCCESS_COMMAND:
  387. """ Handle xfrin success command"""
  388. zone_name_class = self._parse_cmd_params(args, command)
  389. with self._lock:
  390. self._zone_refresh.zone_refresh_success(zone_name_class)
  391. self._master_socket.send(b" ")
  392. elif command == ZONE_XFRIN_FAILED_COMMAND:
  393. """ Handle xfrin fail command"""
  394. zone_name_class = self._parse_cmd_params(args, command)
  395. with self._lock:
  396. self._zone_refresh.zone_refresh_fail(zone_name_class)
  397. self._master_socket.send(b" ")
  398. elif command == "shutdown":
  399. self.shutdown()
  400. else:
  401. answer = create_answer(1, "Unknown command:" + str(command))
  402. return answer
  403. def run(self):
  404. while not self._shutdown_event.is_set():
  405. self._module_cc.check_command(False)
  406. zonemgrd = None
  407. def signal_handler(signal, frame):
  408. if zonemgrd:
  409. zonemgrd.shutdown()
  410. sys.exit(0)
  411. def set_signal_handler():
  412. signal.signal(signal.SIGTERM, signal_handler)
  413. signal.signal(signal.SIGINT, signal_handler)
  414. def set_cmd_options(parser):
  415. parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
  416. help="display more about what is going on")
  417. if '__main__' == __name__:
  418. try:
  419. parser = OptionParser()
  420. set_cmd_options(parser)
  421. (options, args) = parser.parse_args()
  422. VERBOSE_MODE = options.verbose
  423. set_signal_handler()
  424. zonemgrd = Zonemgr()
  425. zonemgrd.run()
  426. except KeyboardInterrupt:
  427. sys.stderr.write("[b10-zonemgr] exit zonemgr process\n")
  428. except isc.cc.session.SessionError as e:
  429. sys.stderr.write("[b10-zonemgr] Error creating zonemgr, "
  430. "is the command channel daemon running?\n")
  431. except isc.cc.session.SessionTimeout as e:
  432. sys.stderr.write("[b10-zonemgr] Error creating zonemgr, "
  433. "is the configuration manager running?\n")
  434. except isc.config.ModuleCCSessionError as e:
  435. sys.stderr.write("[b10-zonemgr] exit zonemgr process: %s\n" % str(e))
  436. if zonemgrd:
  437. zonemgrd.shutdown()