xfrin.py.in 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. #!@PYTHON@
  2. # Copyright (C) 2011 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. import sys; sys.path.append ('@@PYTHONPATH@@')
  17. import os
  18. import signal
  19. import isc
  20. import asyncore
  21. import struct
  22. import threading
  23. import socket
  24. import random
  25. from optparse import OptionParser, OptionValueError
  26. from isc.config.ccsession import *
  27. from isc.notify import notify_out
  28. import isc.util.process
  29. import isc.net.parse
  30. try:
  31. from pydnspp import *
  32. except ImportError as e:
  33. # C++ loadable module may not be installed; even so the xfrin process
  34. # must keep running, so we warn about it and move forward.
  35. sys.stderr.write('[b10-xfrin] failed to import DNS module: %s\n' % str(e))
  36. isc.util.process.rename()
  37. # If B10_FROM_BUILD is set in the environment, we use data files
  38. # from a directory relative to that, otherwise we use the ones
  39. # installed on the system
  40. if "B10_FROM_BUILD" in os.environ:
  41. SPECFILE_PATH = os.environ["B10_FROM_BUILD"] + "/src/bin/xfrin"
  42. AUTH_SPECFILE_PATH = os.environ["B10_FROM_BUILD"] + "/src/bin/auth"
  43. else:
  44. PREFIX = "@prefix@"
  45. DATAROOTDIR = "@datarootdir@"
  46. SPECFILE_PATH = "@datadir@/@PACKAGE@".replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  47. AUTH_SPECFILE_PATH = SPECFILE_PATH
  48. SPECFILE_LOCATION = SPECFILE_PATH + "/xfrin.spec"
  49. AUTH_SPECFILE_LOCATION = AUTH_SPECFILE_PATH + "/auth.spec"
  50. XFROUT_MODULE_NAME = 'Xfrout'
  51. ZONE_MANAGER_MODULE_NAME = 'Zonemgr'
  52. REFRESH_FROM_ZONEMGR = 'refresh_from_zonemgr'
  53. ZONE_XFRIN_FAILED = 'zone_xfrin_failed'
  54. DEFAULT_MASTER_PORT = 53
  55. __version__ = 'BIND10'
  56. # define xfrin rcode
  57. XFRIN_OK = 0
  58. XFRIN_FAIL = 1
  59. def log_error(msg):
  60. sys.stderr.write("[b10-xfrin] %s\n" % str(msg))
  61. class XfrinException(Exception):
  62. pass
  63. class XfrinConfigException(Exception):
  64. """This exception is raised if there is an error in the given
  65. configuration (part), for instance when the zone's master
  66. address is not a valid IP address, when the zone does not
  67. have a name, or when multiple settings are given for the same
  68. zone."""
  69. pass
  70. def _check_zone_name(zone_name_str):
  71. """Checks if the given zone name is a valid domain name, and returns
  72. it as a Name object. Raises an XfrinException if it is not."""
  73. try:
  74. return Name(zone_name_str)
  75. except (EmptyLabel, TooLongLabel, BadLabelType, BadEscape,
  76. TooLongName, IncompleteName) as ne:
  77. raise XfrinConfigException("bad zone name: " + zone_name_str + " (" + str(ne) + ")")
  78. def _check_zone_class(zone_class_str):
  79. """If the given argument is a string: checks if the given class is
  80. a valid one, and returns an RRClass object if so.
  81. Raises XfrinConfigException if not.
  82. If it is None, this function returns the default RRClass.IN()"""
  83. if zone_class_str is None:
  84. return RRClass.IN()
  85. try:
  86. return RRClass(zone_class_str)
  87. except InvalidRRClass as irce:
  88. raise XfrinConfigException("bad zone class: " + zone_class_str + " (" + str(irce) + ")")
  89. class XfrinConnection(asyncore.dispatcher):
  90. '''Do xfrin in this class. '''
  91. def __init__(self,
  92. sock_map, zone_name, rrclass, db_file, shutdown_event,
  93. master_addrinfo, tsig_key_str = None, verbose = False,
  94. idle_timeout = 60):
  95. ''' idle_timeout: max idle time for read data from socket.
  96. db_file: specify the data source file.
  97. check_soa: when it's true, check soa first before sending xfr query
  98. '''
  99. asyncore.dispatcher.__init__(self, map=sock_map)
  100. self.create_socket(master_addrinfo[0], master_addrinfo[1])
  101. self._zone_name = zone_name
  102. self._sock_map = sock_map
  103. self._rrclass = rrclass
  104. self._db_file = db_file
  105. self._soa_rr_count = 0
  106. self._idle_timeout = idle_timeout
  107. self.setblocking(1)
  108. self._shutdown_event = shutdown_event
  109. self._verbose = verbose
  110. self._master_address = master_addrinfo[2]
  111. self._tsig_ctx = None
  112. if tsig_key_str is not None:
  113. self._tsig_ctx = TSIGContext(TSIGKey(tsig_key_str))
  114. def connect_to_master(self):
  115. '''Connect to master in TCP.'''
  116. try:
  117. self.connect(self._master_address)
  118. return True
  119. except socket.error as e:
  120. self.log_msg('Failed to connect:(%s), %s' % (self._master_address,
  121. str(e)))
  122. return False
  123. def _create_query(self, query_type):
  124. '''Create dns query message. '''
  125. msg = Message(Message.RENDER)
  126. query_id = random.randint(0, 0xFFFF)
  127. self._query_id = query_id
  128. msg.set_qid(query_id)
  129. msg.set_opcode(Opcode.QUERY())
  130. msg.set_rcode(Rcode.NOERROR())
  131. query_question = Question(Name(self._zone_name), self._rrclass, query_type)
  132. msg.add_question(query_question)
  133. return msg
  134. def _send_data(self, data):
  135. size = len(data)
  136. total_count = 0
  137. while total_count < size:
  138. count = self.send(data[total_count:])
  139. total_count += count
  140. def _send_query(self, query_type):
  141. '''Send query message over TCP. '''
  142. msg = self._create_query(query_type)
  143. render = MessageRenderer()
  144. # XXX Currently, python wrapper doesn't accept 'None' parameter in this case,
  145. # we should remove the if statement and use a universal interface later.
  146. if self._tsig_ctx is not None:
  147. msg.to_wire(render, self._tsig_ctx)
  148. else:
  149. msg.to_wire(render)
  150. header_len = struct.pack('H', socket.htons(render.get_length()))
  151. self._send_data(header_len)
  152. self._send_data(render.get_data())
  153. def _asyncore_loop(self):
  154. '''
  155. This method is a trivial wrapper for asyncore.loop(). It's extracted from
  156. _get_request_response so that we can test the rest of the code without
  157. involving actual communication with a remote server.'''
  158. asyncore.loop(self._idle_timeout, map=self._sock_map, count=1)
  159. def _get_request_response(self, size):
  160. recv_size = 0
  161. data = b''
  162. while recv_size < size:
  163. self._recv_time_out = True
  164. self._need_recv_size = size - recv_size
  165. self._asyncore_loop()
  166. if self._recv_time_out:
  167. raise XfrinException('receive data from socket time out.')
  168. recv_size += self._recvd_size
  169. data += self._recvd_data
  170. return data
  171. def _check_soa_serial(self):
  172. ''' Compare the soa serial, if soa serial in master is less than
  173. the soa serial in local, Finish xfrin.
  174. False: soa serial in master is less or equal to the local one.
  175. True: soa serial in master is bigger
  176. '''
  177. self._send_query(RRType("SOA"))
  178. data_len = self._get_request_response(2)
  179. msg_len = socket.htons(struct.unpack('H', data_len)[0])
  180. soa_response = self._get_request_response(msg_len)
  181. msg = Message(Message.PARSE)
  182. msg.from_wire(soa_response)
  183. # perform some minimal level validation. It's an open issue how
  184. # strict we should be (see the comment in _check_response_header())
  185. self._check_response_header(msg)
  186. # TODO, need select soa record from data source then compare the two
  187. # serial, current just return OK, since this function hasn't been used
  188. # now.
  189. return XFRIN_OK
  190. def do_xfrin(self, check_soa, ixfr_first = False):
  191. '''Do xfr by sending xfr request and parsing response. '''
  192. try:
  193. ret = XFRIN_OK
  194. if check_soa:
  195. logstr = 'SOA check for \'%s\' ' % self._zone_name
  196. ret = self._check_soa_serial()
  197. logstr = 'transfer of \'%s\': AXFR ' % self._zone_name
  198. if ret == XFRIN_OK:
  199. self.log_msg(logstr + 'started')
  200. # TODO: .AXFR() RRType.AXFR()
  201. self._send_query(RRType(252))
  202. isc.datasrc.sqlite3_ds.load(self._db_file, self._zone_name,
  203. self._handle_xfrin_response)
  204. self.log_msg(logstr + 'succeeded')
  205. except XfrinException as e:
  206. self.log_msg(e)
  207. self.log_msg(logstr + 'failed')
  208. ret = XFRIN_FAIL
  209. #TODO, recover data source.
  210. except isc.datasrc.sqlite3_ds.Sqlite3DSError as e:
  211. self.log_msg(e)
  212. self.log_msg(logstr + 'failed')
  213. ret = XFRIN_FAIL
  214. except UserWarning as e:
  215. # XXX: this is an exception from our C++ library via the
  216. # Boost.Python binding. It would be better to have more more
  217. # specific exceptions, but at this moment this is the finest
  218. # granularity.
  219. self.log_msg(e)
  220. self.log_msg(logstr + 'failed')
  221. ret = XFRIN_FAIL
  222. finally:
  223. self.close()
  224. return ret
  225. def _check_response_header(self, msg):
  226. '''Perform minimal validation on responses'''
  227. # It's not clear how strict we should be about response validation.
  228. # BIND 9 ignores some cases where it would normally be considered a
  229. # bogus response. For example, it accepts a response even if its
  230. # opcode doesn't match that of the corresponding request.
  231. # According to an original developer of BIND 9 some of the missing
  232. # checks are deliberate to be kind to old implementations that would
  233. # cause interoperability trouble with stricter checks.
  234. msg_rcode = msg.get_rcode()
  235. if msg_rcode != Rcode.NOERROR():
  236. raise XfrinException('error response: %s' % msg_rcode.to_text())
  237. if not msg.get_header_flag(Message.HEADERFLAG_QR):
  238. raise XfrinException('response is not a response ')
  239. if msg.get_qid() != self._query_id:
  240. raise XfrinException('bad query id')
  241. def _check_response_status(self, msg):
  242. '''Check validation of xfr response. '''
  243. self._check_response_header(msg)
  244. if msg.get_rr_count(Message.SECTION_ANSWER) == 0:
  245. raise XfrinException('answer section is empty')
  246. if msg.get_rr_count(Message.SECTION_QUESTION) > 1:
  247. raise XfrinException('query section count greater than 1')
  248. def _handle_answer_section(self, answer_section):
  249. '''Return a generator for the reponse in one tcp package to a zone transfer.'''
  250. for rrset in answer_section:
  251. rrset_name = rrset.get_name().to_text()
  252. rrset_ttl = int(rrset.get_ttl().to_text())
  253. rrset_class = rrset.get_class().to_text()
  254. rrset_type = rrset.get_type().to_text()
  255. for rdata in rrset.get_rdata():
  256. # Count the soa record count
  257. if rrset.get_type() == RRType("SOA"):
  258. self._soa_rr_count += 1
  259. # XXX: the current DNS message parser can't preserve the
  260. # RR order or separete the beginning and ending SOA RRs.
  261. # As a short term workaround, we simply ignore the second
  262. # SOA, and ignore the erroneous case where the transfer
  263. # session doesn't end with an SOA.
  264. if (self._soa_rr_count == 2):
  265. # Avoid inserting soa record twice
  266. break
  267. rdata_text = rdata.to_text()
  268. yield (rrset_name, rrset_ttl, rrset_class, rrset_type,
  269. rdata_text)
  270. def _handle_xfrin_response(self):
  271. '''Return a generator for the response to a zone transfer. '''
  272. while True:
  273. data_len = self._get_request_response(2)
  274. msg_len = socket.htons(struct.unpack('H', data_len)[0])
  275. recvdata = self._get_request_response(msg_len)
  276. msg = Message(Message.PARSE)
  277. msg.from_wire(recvdata)
  278. self._check_response_status(msg)
  279. answer_section = msg.get_section(Message.SECTION_ANSWER)
  280. for rr in self._handle_answer_section(answer_section):
  281. yield rr
  282. if self._soa_rr_count == 2:
  283. break
  284. if self._shutdown_event.is_set():
  285. raise XfrinException('xfrin is forced to stop')
  286. def handle_read(self):
  287. '''Read query's response from socket. '''
  288. self._recvd_data = self.recv(self._need_recv_size)
  289. self._recvd_size = len(self._recvd_data)
  290. self._recv_time_out = False
  291. def writable(self):
  292. '''Ignore the writable socket. '''
  293. return False
  294. def log_info(self, msg, type='info'):
  295. # Overwrite the log function, log nothing
  296. pass
  297. def log_msg(self, msg):
  298. if self._verbose:
  299. sys.stdout.write('[b10-xfrin] %s\n' % str(msg))
  300. def process_xfrin(server, xfrin_recorder, zone_name, rrclass, db_file,
  301. shutdown_event, master_addrinfo, check_soa, verbose,
  302. tsig_key_str):
  303. xfrin_recorder.increment(zone_name)
  304. sock_map = {}
  305. conn = XfrinConnection(sock_map, zone_name, rrclass, db_file,
  306. shutdown_event, master_addrinfo,
  307. tsig_key_str, verbose)
  308. ret = XFRIN_FAIL
  309. if conn.connect_to_master():
  310. ret = conn.do_xfrin(check_soa)
  311. # Publish the zone transfer result news, so zonemgr can reset the
  312. # zone timer, and xfrout can notify the zone's slaves if the result
  313. # is success.
  314. server.publish_xfrin_news(zone_name, rrclass, ret)
  315. xfrin_recorder.decrement(zone_name)
  316. class XfrinRecorder:
  317. def __init__(self):
  318. self._lock = threading.Lock()
  319. self._zones = []
  320. def increment(self, zone_name):
  321. self._lock.acquire()
  322. self._zones.append(zone_name)
  323. self._lock.release()
  324. def decrement(self, zone_name):
  325. self._lock.acquire()
  326. if zone_name in self._zones:
  327. self._zones.remove(zone_name)
  328. self._lock.release()
  329. def xfrin_in_progress(self, zone_name):
  330. self._lock.acquire()
  331. ret = zone_name in self._zones
  332. self._lock.release()
  333. return ret
  334. def count(self):
  335. self._lock.acquire()
  336. ret = len(self._zones)
  337. self._lock.release()
  338. return ret
  339. class ZoneInfo:
  340. def __init__(self, config_data, module_cc=None):
  341. """Creates a zone_info with the config data element as
  342. specified by the 'zones' list in xfrin.spec. Module_cc is
  343. needed to get the defaults from the specification"""
  344. self.name_str = config_data.get('name')
  345. self.class_str = config_data.get('class') or \
  346. module_cc.get_default_value("zones/class")
  347. if self.name_str is None:
  348. raise XfrinConfigException("Configuration zones list "
  349. "element does not contain "
  350. "'name' attribute")
  351. self.master_addr_str = config_data.get('master_addr')
  352. self.master_port_str = config_data.get('master_port') or \
  353. str(module_cc.get_default_value("zones/master_port"))
  354. try:
  355. self.name = Name(self.name_str)
  356. except (EmptyLabel, TooLongLabel, BadLabelType, BadEscape,
  357. TooLongName, IncompleteName) as ne:
  358. errmsg = "bad zone name: " + self.name_str + " (" + str(ne) + ")"
  359. log_error(errmsg)
  360. raise XfrinConfigException(errmsg)
  361. try:
  362. self.master_addr = isc.net.parse.addr_parse(self.master_addr_str)
  363. self.master_port = isc.net.parse.port_parse(self.master_port_str)
  364. except ValueError:
  365. errmsg = "bad format for zone's master: " + str(config_data)
  366. log_error(errmsg)
  367. raise XfrinConfigException(errmsg)
  368. try:
  369. self.rrclass = RRClass(self.class_str)
  370. except InvalidRRClass:
  371. errmsg = "invalid class: " + self.class_str
  372. log_error(errmsg)
  373. raise XfrinConfigException(errmsg)
  374. self.tsig_key_str = config_data.get('tsig_key') or None
  375. if self.tsig_key_str is not None:
  376. try:
  377. tsig_key = TSIGKey(self.tsig_key_str)
  378. except InvalidParameter as ipe:
  379. errmsg = "bad TSIG key string: " + self.tsig_key_str
  380. log_error(errmsg)
  381. raise XfrinConfigException(errmsg)
  382. def get_master_addr_info(self):
  383. return (self.master_addr.family, socket.SOCK_STREAM,
  384. (self.master_addr_str, self.master_port))
  385. class Xfrin:
  386. def __init__(self, verbose = False):
  387. self._max_transfers_in = 10
  388. self._zones = {}
  389. self._cc_setup()
  390. self.recorder = XfrinRecorder()
  391. self._shutdown_event = threading.Event()
  392. self._verbose = verbose
  393. def _cc_setup(self):
  394. '''This method is used only as part of initialization, but is
  395. implemented separately for convenience of unit tests; by letting
  396. the test code override this method we can test most of this class
  397. without requiring a command channel.'''
  398. # Create one session for sending command to other modules, because the
  399. # listening session will block the send operation.
  400. self._send_cc_session = isc.cc.Session()
  401. self._module_cc = isc.config.ModuleCCSession(SPECFILE_LOCATION,
  402. self.config_handler,
  403. self.command_handler)
  404. self._module_cc.start()
  405. config_data = self._module_cc.get_full_config()
  406. self.config_handler(config_data)
  407. def _cc_check_command(self):
  408. '''This is a straightforward wrapper for cc.check_command,
  409. but provided as a separate method for the convenience
  410. of unit tests.'''
  411. self._module_cc.check_command(False)
  412. def _get_zone_info(self, name, rrclass):
  413. """Returns the ZoneInfo object containing the configured data
  414. for the given zone name. If the zone name did not have any
  415. data, returns None"""
  416. key = (name.to_text(), rrclass.to_text())
  417. if key in self._zones:
  418. return self._zones[key]
  419. else:
  420. return None
  421. def _get_all_zone_info(self):
  422. """Returns the structure used to store ZoneInfo objects. This
  423. method can be used (together with _set_all_zone_info()) to
  424. revert to the previous zone info configuration when one
  425. of the new config items turns out to be bad"""
  426. return self._zones
  427. def _add_zone_info(self, zone_info):
  428. """Add the zone info. Raises a XfrinConfigException if a zone
  429. with the same name and class is already configured"""
  430. key = (zone_info.name.to_text(), zone_info.class_str)
  431. if key in self._zones:
  432. raise XfrinConfigException("zone " + str(key) +
  433. " configured multiple times")
  434. self._zones[key] = zone_info
  435. def _set_all_zone_info(self, zones):
  436. self._zones = zones
  437. def _clear_zone_info(self):
  438. self._zones = {}
  439. def config_handler(self, new_config):
  440. self._max_transfers_in = new_config.get("transfers_in") or self._max_transfers_in
  441. if 'zones' in new_config:
  442. zones_backup = self._get_all_zone_info()
  443. self._clear_zone_info()
  444. for zone_config in new_config.get('zones'):
  445. try:
  446. zone_info = ZoneInfo(zone_config, self._module_cc)
  447. self._add_zone_info(zone_info)
  448. except XfrinConfigException as xce:
  449. self._set_all_zone_info(zones_backup)
  450. return create_answer(1, str(xce))
  451. return create_answer(0)
  452. def shutdown(self):
  453. ''' shutdown the xfrin process. the thread which is doing xfrin should be
  454. terminated.
  455. '''
  456. self._shutdown_event.set()
  457. main_thread = threading.currentThread()
  458. for th in threading.enumerate():
  459. if th is main_thread:
  460. continue
  461. th.join()
  462. def command_handler(self, command, args):
  463. answer = create_answer(0)
  464. try:
  465. if command == 'shutdown':
  466. self._shutdown_event.set()
  467. elif command == 'notify' or command == REFRESH_FROM_ZONEMGR:
  468. # Xfrin receives the refresh/notify command from zone manager.
  469. # notify command maybe has the parameters which
  470. # specify the notifyfrom address and port, according the RFC1996, zone
  471. # transfer should starts first from the notifyfrom, but now, let 'TODO' it.
  472. # (using the value now, while we can only set one master address, would be
  473. # a security hole. Once we add the ability to have multiple master addresses,
  474. # we should check if it matches one of them, and then use it.)
  475. (zone_name, rrclass) = self._parse_zone_name_and_class(args)
  476. zone_info = self._get_zone_info(zone_name, rrclass)
  477. if zone_info is None:
  478. # TODO what to do? no info known about zone. defaults?
  479. errmsg = "Got notification to retransfer unknown zone " + zone_name.to_text()
  480. log_error(errmsg)
  481. answer = create_answer(1, errmsg)
  482. else:
  483. master_addr = zone_info.get_master_addr_info()
  484. ret = self.xfrin_start(zone_name,
  485. rrclass,
  486. self._get_db_file(),
  487. master_addr,
  488. zone_info.tsig_key_str,
  489. True)
  490. answer = create_answer(ret[0], ret[1])
  491. elif command == 'retransfer' or command == 'refresh':
  492. # Xfrin receives the retransfer/refresh from cmdctl(sent by bindctl).
  493. # If the command has specified master address, do transfer from the
  494. # master address, or else do transfer from the configured masters.
  495. (zone_name, rrclass) = self._parse_zone_name_and_class(args)
  496. master_addr = self._parse_master_and_port(args)
  497. zone_info = self._get_zone_info(zone_name, rrclass)
  498. tsig_key_str = None
  499. if zone_info:
  500. tsig_key_str = zone_info.tsig_key_str
  501. db_file = args.get('db_file') or self._get_db_file()
  502. ret = self.xfrin_start(zone_name,
  503. rrclass,
  504. db_file,
  505. master_addr,
  506. tsig_key_str,
  507. (False if command == 'retransfer' else True))
  508. answer = create_answer(ret[0], ret[1])
  509. else:
  510. answer = create_answer(1, 'unknown command: ' + command)
  511. except XfrinException as err:
  512. log_error('error happened for command: %s, %s' % (command, str(err)) )
  513. answer = create_answer(1, str(err))
  514. return answer
  515. def _parse_zone_name_and_class(self, args):
  516. zone_name_str = args.get('zone_name')
  517. if not zone_name_str:
  518. raise XfrinException('zone name should be provided')
  519. rrclass = args.get('zone_class')
  520. if not rrclass:
  521. rrclass = RRClass.IN()
  522. else:
  523. try:
  524. rrclass = RRClass(rrclass)
  525. except InvalidRRClass as e:
  526. raise XfrinException('invalid RRClass: ' + rrclass)
  527. return _check_zone_name(zone_name_str), rrclass
  528. def _parse_master_and_port(self, args):
  529. # check if we have configured info about this zone, in case
  530. # port or master are not specified
  531. zone_name = _check_zone_name(args.get('zone_name'))
  532. zone_class = _check_zone_class(args.get('class'))
  533. zone_info = self._get_zone_info(zone_name, zone_class)
  534. master = args.get('master')
  535. if master is None:
  536. if zone_info is not None:
  537. master = zone_info.master_addr_str
  538. else:
  539. raise XfrinException("Master address not given or "
  540. "configured for " + zone_name.to_text())
  541. port = args.get('port')
  542. if port is None:
  543. if zone_info is not None:
  544. port = zone_info.master_port_str
  545. else:
  546. port = DEFAULT_MASTER_PORT
  547. return build_addr_info(master, port)
  548. def _get_db_file(self):
  549. #TODO, the db file path should be got in auth server's configuration
  550. # if we need access to this configuration more often, we
  551. # should add it on start, and not remove it here
  552. # (or, if we have writable ds, we might not need this in
  553. # the first place)
  554. self._module_cc.add_remote_config(AUTH_SPECFILE_LOCATION)
  555. db_file, is_default = self._module_cc.get_remote_config_value("Auth", "database_file")
  556. if is_default and "B10_FROM_BUILD" in os.environ:
  557. # this too should be unnecessary, but currently the
  558. # 'from build' override isn't stored in the config
  559. # (and we don't have writable datasources yet)
  560. db_file = os.environ["B10_FROM_BUILD"] + os.sep + "bind10_zones.sqlite3"
  561. self._module_cc.remove_remote_config(AUTH_SPECFILE_LOCATION)
  562. return db_file
  563. def publish_xfrin_news(self, zone_name, zone_class, xfr_result):
  564. '''Send command to xfrout/zone manager module.
  565. If xfrin has finished successfully for one zone, tell the good
  566. news(command: zone_new_data_ready) to zone manager and xfrout.
  567. if xfrin failed, just tell the bad news to zone manager, so that
  568. it can reset the refresh timer for that zone. '''
  569. param = {'zone_name': zone_name, 'zone_class': zone_class.to_text()}
  570. if xfr_result == XFRIN_OK:
  571. msg = create_command(notify_out.ZONE_NEW_DATA_READY_CMD, param)
  572. # catch the exception, in case msgq has been killed.
  573. try:
  574. seq = self._send_cc_session.group_sendmsg(msg,
  575. XFROUT_MODULE_NAME)
  576. try:
  577. answer, env = self._send_cc_session.group_recvmsg(False,
  578. seq)
  579. except isc.cc.session.SessionTimeout:
  580. pass # for now we just ignore the failure
  581. seq = self._send_cc_session.group_sendmsg(msg, ZONE_MANAGER_MODULE_NAME)
  582. try:
  583. answer, env = self._send_cc_session.group_recvmsg(False,
  584. seq)
  585. except isc.cc.session.SessionTimeout:
  586. pass # for now we just ignore the failure
  587. except socket.error as err:
  588. log_error("Fail to send message to %s and %s, msgq may has been killed"
  589. % (XFROUT_MODULE_NAME, ZONE_MANAGER_MODULE_NAME))
  590. else:
  591. msg = create_command(ZONE_XFRIN_FAILED, param)
  592. # catch the exception, in case msgq has been killed.
  593. try:
  594. seq = self._send_cc_session.group_sendmsg(msg, ZONE_MANAGER_MODULE_NAME)
  595. try:
  596. answer, env = self._send_cc_session.group_recvmsg(False,
  597. seq)
  598. except isc.cc.session.SessionTimeout:
  599. pass # for now we just ignore the failure
  600. except socket.error as err:
  601. log_error("Fail to send message to %s, msgq may has been killed"
  602. % ZONE_MANAGER_MODULE_NAME)
  603. def startup(self):
  604. while not self._shutdown_event.is_set():
  605. self._cc_check_command()
  606. def xfrin_start(self, zone_name, rrclass, db_file, master_addrinfo, tsig_key_str,
  607. check_soa = True):
  608. if "pydnspp" not in sys.modules:
  609. return (1, "xfrin failed, can't load dns message python library: 'pydnspp'")
  610. # check max_transfer_in, else return quota error
  611. if self.recorder.count() >= self._max_transfers_in:
  612. return (1, 'xfrin quota error')
  613. if self.recorder.xfrin_in_progress(zone_name):
  614. return (1, 'zone xfrin is in progress')
  615. xfrin_thread = threading.Thread(target = process_xfrin,
  616. args = (self,
  617. self.recorder,
  618. zone_name.to_text(),
  619. rrclass,
  620. db_file,
  621. self._shutdown_event,
  622. master_addrinfo, check_soa,
  623. self._verbose,
  624. tsig_key_str))
  625. xfrin_thread.start()
  626. return (0, 'zone xfrin is started')
  627. xfrind = None
  628. def signal_handler(signal, frame):
  629. if xfrind:
  630. xfrind.shutdown()
  631. sys.exit(0)
  632. def set_signal_handler():
  633. signal.signal(signal.SIGTERM, signal_handler)
  634. signal.signal(signal.SIGINT, signal_handler)
  635. def build_addr_info(addrstr, portstr):
  636. """
  637. Return tuple (family, socktype, sockaddr) for given address and port.
  638. IPv4 and IPv6 are the only supported addresses now, so sockaddr will be
  639. (address, port). The socktype is socket.SOCK_STREAM for now.
  640. """
  641. try:
  642. port = isc.net.parse.port_parse(portstr)
  643. addr = isc.net.parse.addr_parse(addrstr)
  644. return (addr.family, socket.SOCK_STREAM, (addrstr, port))
  645. except ValueError as err:
  646. raise XfrinException("failed to resolve master address/port=%s/%s: %s" %
  647. (addrstr, portstr, str(err)))
  648. def set_cmd_options(parser):
  649. parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
  650. help="display more about what is going on")
  651. def main(xfrin_class, use_signal = True):
  652. """The main loop of the Xfrin daemon.
  653. @param xfrin_class: A class of the Xfrin object. This is normally Xfrin,
  654. but can be a subclass of it for customization.
  655. @param use_signal: True if this process should catch signals. This is
  656. normally True, but may be disabled when this function is called in a
  657. testing context."""
  658. global xfrind
  659. try:
  660. parser = OptionParser(version = __version__)
  661. set_cmd_options(parser)
  662. (options, args) = parser.parse_args()
  663. if use_signal:
  664. set_signal_handler()
  665. xfrind = xfrin_class(verbose = options.verbose)
  666. xfrind.startup()
  667. except KeyboardInterrupt:
  668. log_error("exit b10-xfrin")
  669. except isc.cc.session.SessionError as e:
  670. log_error(str(e))
  671. log_error('Error happened! is the command channel daemon running?')
  672. except Exception as e:
  673. log_error(str(e))
  674. if xfrind:
  675. xfrind.shutdown()
  676. if __name__ == '__main__':
  677. main(Xfrin)