notify_out.py 21 KB

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