notify_out.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. # Copyright (C) 2010 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. import select
  16. import sys
  17. import random
  18. import socket
  19. import threading
  20. import time
  21. import errno
  22. from isc.datasrc import sqlite3_ds
  23. from isc.datasrc import DataSourceClient
  24. from isc.net import addr
  25. import isc
  26. from isc.log_messages.notify_out_messages import *
  27. logger = isc.log.Logger("notify_out")
  28. # there used to be a printed message if this import failed, but if
  29. # we can't import we should not start anyway, and logging an error
  30. # is a bad idea since the logging system is most likely not
  31. # initialized yet. see trac ticket #1103
  32. from isc.dns import *
  33. ZONE_NEW_DATA_READY_CMD = 'zone_new_data_ready'
  34. _MAX_NOTIFY_NUM = 30
  35. _MAX_NOTIFY_TRY_NUM = 5
  36. _EVENT_NONE = 0
  37. _EVENT_READ = 1
  38. _EVENT_TIMEOUT = 2
  39. _NOTIFY_TIMEOUT = 1
  40. # define the rcode for parsing notify reply message
  41. _REPLY_OK = 0
  42. _BAD_QUERY_ID = 1
  43. _BAD_QUERY_NAME = 2
  44. _BAD_OPCODE = 3
  45. _BAD_QR = 4
  46. _BAD_REPLY_PACKET = 5
  47. SOCK_DATA = b's'
  48. # borrowed from xfrin.py @ #1298. We should eventually unify it.
  49. def format_zone_str(zone_name, zone_class):
  50. """Helper function to format a zone name and class as a string of
  51. the form '<name>/<class>'.
  52. Parameters:
  53. zone_name (isc.dns.Name) name to format
  54. zone_class (isc.dns.RRClass) class to format
  55. """
  56. return zone_name.to_text() + '/' + str(zone_class)
  57. class NotifyOutDataSourceError(Exception):
  58. """An exception raised when data source error happens within notify out.
  59. This exception is expected to be caught within the notify_out module.
  60. """
  61. pass
  62. class ZoneNotifyInfo:
  63. '''This class keeps track of notify-out information for one zone.'''
  64. def __init__(self, zone_name_, class_):
  65. self._notify_current = None
  66. self._slave_index = 0
  67. self._sock = None
  68. self.notify_slaves = []
  69. self.zone_name = zone_name_
  70. self.zone_class = class_
  71. self.notify_msg_id = 0
  72. # Absolute time for next notify reply. When the zone is preparing for
  73. # sending notify message, notify_timeout_ is set to now, that means
  74. # the first sending is triggered by the 'Timeout' mechanism.
  75. self.notify_timeout = None
  76. self.notify_try_num = 0 # Notify times sending to one target.
  77. def set_next_notify_target(self):
  78. if self._slave_index < (len(self.notify_slaves) - 1):
  79. self._slave_index += 1
  80. self._notify_current = self.notify_slaves[self._slave_index]
  81. else:
  82. self._notify_current = None
  83. def prepare_notify_out(self):
  84. '''Set notify timeout time to now'''
  85. self.notify_timeout = time.time()
  86. self.notify_try_num = 0
  87. self._slave_index = 0
  88. if len(self.notify_slaves) > 0:
  89. self._notify_current = self.notify_slaves[0]
  90. def finish_notify_out(self):
  91. if self._sock:
  92. self._sock.close()
  93. self._sock = None
  94. self.notify_timeout = None
  95. def create_socket(self, dest_addr):
  96. self._sock = socket.socket(addr.IPAddr(dest_addr).family,
  97. socket.SOCK_DGRAM)
  98. return self._sock
  99. def get_socket(self):
  100. return self._sock
  101. def get_current_notify_target(self):
  102. return self._notify_current
  103. class NotifyOut:
  104. '''This class is used to handle notify logic for all zones(sending
  105. notify message to its slaves). notify service can be started by
  106. calling dispatcher(), and it can be stoped by calling shutdown()
  107. in another thread. '''
  108. def __init__(self, datasrc_file, verbose=True):
  109. self._notify_infos = {} # key is (zone_name, zone_class)
  110. self._waiting_zones = []
  111. self._notifying_zones = []
  112. self._serving = False
  113. self._read_sock, self._write_sock = socket.socketpair()
  114. self._read_sock.setblocking(False)
  115. self.notify_num = 0 # the count of in progress notifies
  116. self._verbose = verbose
  117. self._lock = threading.Lock()
  118. self._db_file = datasrc_file
  119. self._init_notify_out(datasrc_file)
  120. # Use nonblock event to eliminate busy loop
  121. # If there are no notifying zones, clear the event bit and wait.
  122. self._nonblock_event = threading.Event()
  123. def _init_notify_out(self, datasrc_file):
  124. '''Get all the zones name and its notify target's address.
  125. TODO, currently the zones are got by going through the zone
  126. table in database. There should be a better way to get them
  127. and also the setting 'also_notify', and there should be one
  128. mechanism to cover the changed datasrc.
  129. '''
  130. self._db_file = datasrc_file
  131. for zone_name, zone_class in sqlite3_ds.get_zones_info(datasrc_file):
  132. zone_id = (zone_name, zone_class)
  133. self._notify_infos[zone_id] = ZoneNotifyInfo(zone_name, zone_class)
  134. slaves = self._get_notify_slaves_from_ns(Name(zone_name),
  135. RRClass(zone_class))
  136. for item in slaves:
  137. self._notify_infos[zone_id].notify_slaves.append((item, 53))
  138. def send_notify(self, zone_name, zone_class='IN'):
  139. '''Send notify to one zone's slaves, this function is
  140. the only interface for class NotifyOut which can be called
  141. by other object.
  142. Internally, the function only set the zone's notify-reply
  143. timeout to now, then notify message will be sent out. '''
  144. if zone_name[len(zone_name) - 1] != '.':
  145. zone_name += '.'
  146. zone_id = (zone_name, zone_class)
  147. if zone_id not in self._notify_infos:
  148. return
  149. # Has no slave servers, skip it.
  150. if (len(self._notify_infos[zone_id].notify_slaves) <= 0):
  151. return
  152. with self._lock:
  153. if (self.notify_num >= _MAX_NOTIFY_NUM) or (zone_id in self._notifying_zones):
  154. if zone_id not in self._waiting_zones:
  155. self._waiting_zones.append(zone_id)
  156. else:
  157. self._notify_infos[zone_id].prepare_notify_out()
  158. self.notify_num += 1
  159. self._notifying_zones.append(zone_id)
  160. if not self._nonblock_event.isSet():
  161. self._nonblock_event.set()
  162. def _dispatcher(self, started_event):
  163. started_event.set() # Let the master know we are alive already
  164. while self._serving:
  165. replied_zones, not_replied_zones = self._wait_for_notify_reply()
  166. for name_ in replied_zones:
  167. self._zone_notify_handler(replied_zones[name_], _EVENT_READ)
  168. for name_ in not_replied_zones:
  169. if not_replied_zones[name_].notify_timeout <= time.time():
  170. self._zone_notify_handler(not_replied_zones[name_], _EVENT_TIMEOUT)
  171. def dispatcher(self, daemon=False):
  172. """Spawns a thread that will handle notify related events.
  173. If one zone get the notify reply before timeout, call the
  174. handle to process the reply. If one zone can't get the notify
  175. before timeout, call the handler to resend notify or notify
  176. next slave.
  177. The thread can be stopped by calling shutdown().
  178. Returns the thread object to anyone interested.
  179. """
  180. if self._serving:
  181. raise RuntimeError(
  182. 'Dispatcher already running, tried to start twice')
  183. # Prepare for launch
  184. self._serving = True
  185. started_event = threading.Event()
  186. # Start
  187. self._thread = threading.Thread(target=self._dispatcher,
  188. args=[started_event])
  189. if daemon:
  190. self._thread.daemon = daemon
  191. self._thread.start()
  192. # Wait for it to get started
  193. started_event.wait()
  194. # Return it to anyone listening
  195. return self._thread
  196. def shutdown(self):
  197. """Stop the dispatcher() thread. Blocks until the thread stopped."""
  198. if not self._serving:
  199. raise RuntimeError('Tried to stop while not running')
  200. # Ask it to stop
  201. self._serving = False
  202. if not self._nonblock_event.isSet():
  203. # set self._nonblock_event to stop waiting for new notifying zones.
  204. self._nonblock_event.set()
  205. self._write_sock.send(SOCK_DATA) # make self._read_sock be readable.
  206. # Wait for it
  207. self._thread.join()
  208. # Clean up
  209. self._write_sock = None
  210. self._read_sock = None
  211. self._thread = None
  212. def _get_rdata_data(self, rr):
  213. return rr[7].strip()
  214. def _get_notify_slaves_from_ns(self, zone_name, zone_class):
  215. '''Get all NS records, then remove the primary master from ns rrset,
  216. then use the name in NS record rdata part to get the a/aaaa records
  217. in the same zone. the targets listed in a/aaaa record rdata are treated
  218. as the notify slaves.
  219. Note: this is the simplest way to get the address of slaves,
  220. but not correct, it can't handle the delegation slaves, or the CNAME
  221. and DNAME logic.
  222. TODO. the function should be provided by one library.
  223. '''
  224. # Prepare data source client. This should eventually be moved to
  225. # an earlier stage of initialization and also support multiple
  226. # data sources.
  227. datasrc_config = '{ "database_file": "' + self._db_file + '"}'
  228. try:
  229. result, finder = DataSourceClient('sqlite3',
  230. datasrc_config).find_zone(
  231. zone_name)
  232. except isc.datasrc.Error as ex:
  233. logger.error(NOTIFY_OUT_DATASRC_ACCESS_FAILURE, ex)
  234. return []
  235. if result is not DataSourceClient.SUCCESS:
  236. logger.error(NOTIFY_OUT_DATASRC_ZONE_NOT_FOUND,
  237. format_zone_str(zone_name, zone_class))
  238. return []
  239. result, ns_rrset, _ = finder.find(zone_name, RRType.NS())
  240. if result is not finder.SUCCESS or ns_rrset is None:
  241. logger.warn(NOTIFY_OUT_ZONE_NO_NS,
  242. format_zone_str(zone_name, zone_class))
  243. return []
  244. result, soa_rrset, _ = finder.find(zone_name, RRType.SOA())
  245. if result is not finder.SUCCESS or soa_rrset is None or \
  246. soa_rrset.get_rdata_count() != 1:
  247. logger.warn(NOTIFY_OUT_ZONE_BAD_SOA,
  248. format_zone_str(zone_name, zone_class))
  249. return [] # broken zone anyway, stop here.
  250. soa_mname = Name(soa_rrset.get_rdata()[0].to_text().split(' ')[0])
  251. addrs = []
  252. for ns_rdata in ns_rrset.get_rdata():
  253. ns_name = Name(ns_rdata.to_text())
  254. if soa_mname == ns_name:
  255. continue
  256. result, rrset, _ = finder.find(ns_name, RRType.A())
  257. if result is finder.SUCCESS and rrset is not None:
  258. addrs.extend([a.to_text() for a in rrset.get_rdata()])
  259. result, rrset, _ = finder.find(ns_name, RRType.AAAA())
  260. if result is finder.SUCCESS and rrset is not None:
  261. addrs.extend([aaaa.to_text() for aaaa in rrset.get_rdata()])
  262. return addrs
  263. def _prepare_select_info(self):
  264. '''
  265. Prepare the information for select(), returned
  266. value is one tuple
  267. (block_timeout, valid_socks, notifying_zones)
  268. block_timeout: the timeout for select()
  269. valid_socks: sockets list for waiting ready reading.
  270. notifying_zones: the zones which have been triggered
  271. for notify.
  272. '''
  273. valid_socks = []
  274. notifying_zones = {}
  275. min_timeout = None
  276. for info in self._notify_infos:
  277. sock = self._notify_infos[info].get_socket()
  278. if sock:
  279. valid_socks.append(sock)
  280. # If a non null timeout is specified notify has been scheduled
  281. # (in which case socket is still None) or sent (with a valid
  282. # socket). In either case we need add the zone to notifying_zones
  283. # so that we can invoke the appropriate event for the zone after
  284. # select.
  285. tmp_timeout = self._notify_infos[info].notify_timeout
  286. if tmp_timeout is not None:
  287. notifying_zones[info] = self._notify_infos[info]
  288. if min_timeout is not None:
  289. if tmp_timeout < min_timeout:
  290. min_timeout = tmp_timeout
  291. else:
  292. min_timeout = tmp_timeout
  293. block_timeout = None
  294. if min_timeout is not None:
  295. block_timeout = min_timeout - time.time()
  296. if block_timeout < 0:
  297. block_timeout = 0
  298. return (block_timeout, valid_socks, notifying_zones)
  299. def _wait_for_notify_reply(self):
  300. '''
  301. Receive notify replies in specified time. returned value
  302. is one tuple:(replied_zones, not_replied_zones). ({}, {}) is
  303. returned if shutdown() was called.
  304. replied_zones: the zones which receive notify reply.
  305. not_replied_zones: the zones which haven't got notify reply.
  306. '''
  307. (block_timeout, valid_socks, notifying_zones) = \
  308. self._prepare_select_info()
  309. # This is None only during some tests
  310. if self._read_sock is not None:
  311. valid_socks.append(self._read_sock)
  312. # Currently, there is no notifying zones, waiting for zones to send notify
  313. if block_timeout is None:
  314. self._nonblock_event.clear()
  315. self._nonblock_event.wait()
  316. # has new notifying zone, check immediately
  317. block_timeout = 0
  318. try:
  319. r_fds, w, e = select.select(valid_socks, [], [], block_timeout)
  320. except select.error as err:
  321. if err.args[0] != errno.EINTR:
  322. return {}, {}
  323. if self._read_sock in r_fds: # user has called shutdown()
  324. try:
  325. # Noone should write anything else than shutdown
  326. assert self._read_sock.recv(len(SOCK_DATA)) == SOCK_DATA
  327. return {}, {}
  328. except socket.error as e: # Workaround around rare linux bug
  329. if e.errno != errno.EAGAIN and e.errno != errno.EWOULDBLOCK:
  330. raise
  331. not_replied_zones = {}
  332. replied_zones = {}
  333. for info in notifying_zones:
  334. if notifying_zones[info].get_socket() in r_fds:
  335. replied_zones[info] = notifying_zones[info]
  336. else:
  337. not_replied_zones[info] = notifying_zones[info]
  338. return replied_zones, not_replied_zones
  339. def _zone_notify_handler(self, zone_notify_info, event_type):
  340. '''Notify handler for one zone. The first notify message is
  341. always triggered by the event "_EVENT_TIMEOUT" since when
  342. one zone prepares to notify its slaves, its notify_timeout
  343. is set to now, which is used to trigger sending notify
  344. message when dispatcher() scanning zones. '''
  345. tgt = zone_notify_info.get_current_notify_target()
  346. if event_type == _EVENT_READ:
  347. reply = self._get_notify_reply(zone_notify_info.get_socket(), tgt)
  348. if reply is not None:
  349. if self._handle_notify_reply(zone_notify_info, reply, tgt):
  350. self._notify_next_target(zone_notify_info)
  351. elif event_type == _EVENT_TIMEOUT and zone_notify_info.notify_try_num > 0:
  352. logger.info(NOTIFY_OUT_TIMEOUT, tgt[0], tgt[1])
  353. tgt = zone_notify_info.get_current_notify_target()
  354. if tgt:
  355. zone_notify_info.notify_try_num += 1
  356. if zone_notify_info.notify_try_num > _MAX_NOTIFY_TRY_NUM:
  357. logger.warn(NOTIFY_OUT_RETRY_EXCEEDED, tgt[0], tgt[1],
  358. _MAX_NOTIFY_TRY_NUM)
  359. self._notify_next_target(zone_notify_info)
  360. else:
  361. # set exponential backoff according rfc1996 section 3.6
  362. retry_timeout = _NOTIFY_TIMEOUT * pow(2, zone_notify_info.notify_try_num)
  363. zone_notify_info.notify_timeout = time.time() + retry_timeout
  364. self._send_notify_message_udp(zone_notify_info, tgt)
  365. def _notify_next_target(self, zone_notify_info):
  366. '''Notify next address for the same zone. If all the targets
  367. has been notified, notify the first zone in waiting list. '''
  368. zone_notify_info.notify_try_num = 0
  369. zone_notify_info.set_next_notify_target()
  370. tgt = zone_notify_info.get_current_notify_target()
  371. if not tgt:
  372. zone_notify_info.finish_notify_out()
  373. with self._lock:
  374. self.notify_num -= 1
  375. self._notifying_zones.remove((zone_notify_info.zone_name,
  376. zone_notify_info.zone_class))
  377. # trigger notify out for waiting zones
  378. if len(self._waiting_zones) > 0:
  379. zone_id = self._waiting_zones.pop(0)
  380. self._notify_infos[zone_id].prepare_notify_out()
  381. self.notify_num += 1
  382. self._notifying_zones.append(zone_id)
  383. if not self._nonblock_event.isSet():
  384. self._nonblock_event.set()
  385. def _send_notify_message_udp(self, zone_notify_info, addrinfo):
  386. msg, qid = self._create_notify_message(
  387. Name(zone_notify_info.zone_name),
  388. RRClass(zone_notify_info.zone_class))
  389. render = MessageRenderer()
  390. render.set_length_limit(512)
  391. msg.to_wire(render)
  392. zone_notify_info.notify_msg_id = qid
  393. try:
  394. sock = zone_notify_info.create_socket(addrinfo[0])
  395. sock.sendto(render.get_data(), 0, addrinfo)
  396. logger.info(NOTIFY_OUT_SENDING_NOTIFY, addrinfo[0],
  397. addrinfo[1])
  398. except (socket.error, addr.InvalidAddress) as err:
  399. logger.error(NOTIFY_OUT_SOCKET_ERROR, addrinfo[0],
  400. addrinfo[1], err)
  401. return False
  402. except addr.InvalidAddress as iae:
  403. logger.error(NOTIFY_OUT_INVALID_ADDRESS, addrinfo[0],
  404. addrinfo[1], iae)
  405. return False
  406. return True
  407. def _create_notify_message(self, zone_name, zone_class):
  408. msg = Message(Message.RENDER)
  409. qid = random.randint(0, 0xFFFF)
  410. msg.set_qid(qid)
  411. msg.set_opcode(Opcode.NOTIFY())
  412. msg.set_rcode(Rcode.NOERROR())
  413. msg.set_header_flag(Message.HEADERFLAG_AA)
  414. msg.add_question(Question(zone_name, zone_class, RRType.SOA()))
  415. msg.add_rrset(Message.SECTION_ANSWER, self._get_zone_soa(zone_name,
  416. zone_class))
  417. return msg, qid
  418. def _get_zone_soa(self, zone_name, zone_class):
  419. # We create (and soon drop) the data source client here because
  420. # clients should be thread specific. We could let the main thread
  421. # loop (_dispatcher) create and retain the client in order to avoid
  422. # the overhead when we generalize the interface (and we may also
  423. # revisit the design of notify_out more substantially anyway).
  424. datasrc_config = '{ "database_file": "' + self._db_file + '"}'
  425. result, finder = DataSourceClient('sqlite3',
  426. datasrc_config).find_zone(zone_name)
  427. if result is not DataSourceClient.SUCCESS:
  428. raise NotifyOutDataSourceError('_get_zone_soa: Zone ' +
  429. zone_name.to_text() + '/' +
  430. zone_class.to_text() + ' not found')
  431. result, soa_rrset, _ = finder.find(zone_name, RRType.SOA())
  432. if result is not finder.SUCCESS or soa_rrset is None or \
  433. soa_rrset.get_rdata_count() != 1:
  434. raise NotifyOutDataSourceError('_get_zone_soa: Zone ' +
  435. zone_name.to_text() + '/' +
  436. zone_class.to_text() +
  437. ' is broken: no valid SOA found')
  438. return soa_rrset
  439. def _handle_notify_reply(self, zone_notify_info, msg_data, from_addr):
  440. '''Parse the notify reply message.
  441. rcode will not checked here, If we get the response
  442. from the slave, it means the slaves has got the notify.'''
  443. msg = Message(Message.PARSE)
  444. try:
  445. msg.from_wire(msg_data)
  446. if not msg.get_header_flag(Message.HEADERFLAG_QR):
  447. logger.warn(NOTIFY_OUT_REPLY_QR_NOT_SET, from_addr[0],
  448. from_addr[1])
  449. return _BAD_QR
  450. if msg.get_qid() != zone_notify_info.notify_msg_id:
  451. logger.warn(NOTIFY_OUT_REPLY_BAD_QID, from_addr[0],
  452. from_addr[1], msg.get_qid(),
  453. zone_notify_info.notify_msg_id)
  454. return _BAD_QUERY_ID
  455. question = msg.get_question()[0]
  456. if question.get_name() != Name(zone_notify_info.zone_name):
  457. logger.warn(NOTIFY_OUT_REPLY_BAD_QUERY_NAME, from_addr[0],
  458. from_addr[1], question.get_name().to_text(),
  459. Name(zone_notify_info.zone_name).to_text())
  460. return _BAD_QUERY_NAME
  461. if msg.get_opcode() != Opcode.NOTIFY():
  462. logger.warn(NOTIFY_OUT_REPLY_BAD_OPCODE, from_addr[0],
  463. from_addr[1], msg.get_opcode().to_text())
  464. return _BAD_OPCODE
  465. except Exception as err:
  466. # We don't care what exception, just report it?
  467. logger.error(NOTIFY_OUT_REPLY_UNCAUGHT_EXCEPTION, err)
  468. return _BAD_REPLY_PACKET
  469. return _REPLY_OK
  470. def _get_notify_reply(self, sock, tgt_addr):
  471. try:
  472. msg, addr = sock.recvfrom(512)
  473. except socket.error as err:
  474. logger.error(NOTIFY_OUT_SOCKET_RECV_ERROR, tgt_addr[0],
  475. tgt_addr[1], err)
  476. return None
  477. return msg