xfrin.py.in 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  1. #!@PYTHON@
  2. # Copyright (C) 2009-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. import time
  26. from functools import reduce
  27. from optparse import OptionParser, OptionValueError
  28. from isc.config.ccsession import *
  29. from isc.notify import notify_out
  30. import isc.util.process
  31. from isc.datasrc import DataSourceClient, ZoneFinder
  32. import isc.net.parse
  33. from isc.xfrin.diff import Diff
  34. from isc.log_messages.xfrin_messages import *
  35. isc.log.init("b10-xfrin")
  36. logger = isc.log.Logger("xfrin")
  37. # Pending system-wide debug level definitions, the ones we
  38. # use here are hardcoded for now
  39. DBG_PROCESS = logger.DBGLVL_TRACE_BASIC
  40. DBG_COMMANDS = logger.DBGLVL_TRACE_DETAIL
  41. try:
  42. from pydnspp import *
  43. except ImportError as e:
  44. # C++ loadable module may not be installed; even so the xfrin process
  45. # must keep running, so we warn about it and move forward.
  46. logger.error(XFRIN_IMPORT_DNS, str(e))
  47. isc.util.process.rename()
  48. # If B10_FROM_BUILD is set in the environment, we use data files
  49. # from a directory relative to that, otherwise we use the ones
  50. # installed on the system
  51. if "B10_FROM_BUILD" in os.environ:
  52. SPECFILE_PATH = os.environ["B10_FROM_BUILD"] + "/src/bin/xfrin"
  53. AUTH_SPECFILE_PATH = os.environ["B10_FROM_BUILD"] + "/src/bin/auth"
  54. else:
  55. PREFIX = "@prefix@"
  56. DATAROOTDIR = "@datarootdir@"
  57. SPECFILE_PATH = "@datadir@/@PACKAGE@".replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  58. AUTH_SPECFILE_PATH = SPECFILE_PATH
  59. SPECFILE_LOCATION = SPECFILE_PATH + "/xfrin.spec"
  60. AUTH_SPECFILE_LOCATION = AUTH_SPECFILE_PATH + "/auth.spec"
  61. XFROUT_MODULE_NAME = 'Xfrout'
  62. ZONE_MANAGER_MODULE_NAME = 'Zonemgr'
  63. REFRESH_FROM_ZONEMGR = 'refresh_from_zonemgr'
  64. ZONE_XFRIN_FAILED = 'zone_xfrin_failed'
  65. # Constants for debug levels.
  66. DBG_XFRIN_TRACE = logger.DBGLVL_TRACE_BASIC
  67. # These two default are currently hard-coded. For config this isn't
  68. # necessary, but we need these defaults for optional command arguments
  69. # (TODO: have similar support to get default values for command
  70. # arguments as we do for config options)
  71. DEFAULT_MASTER_PORT = 53
  72. DEFAULT_ZONE_CLASS = RRClass.IN()
  73. __version__ = 'BIND10'
  74. # Internal result codes of an xfr session
  75. XFRIN_OK = 0 # normal success
  76. XFRIN_FAIL = 1 # general failure (internal/external)
  77. class XfrinException(Exception):
  78. pass
  79. class XfrinProtocolError(Exception):
  80. '''An exception raised for errors encountered in xfrin protocol handling.
  81. '''
  82. pass
  83. class XfrinZoneUptodate(Exception):
  84. '''TBD
  85. '''
  86. pass
  87. class XfrinZoneInfoException(Exception):
  88. """This exception is raised if there is an error in the given
  89. configuration (part), or when a command does not have a required
  90. argument or has bad arguments, for instance when the zone's master
  91. address is not a valid IP address, when the zone does not
  92. have a name, or when multiple settings are given for the same
  93. zone."""
  94. pass
  95. def _check_zone_name(zone_name_str):
  96. """Checks if the given zone name is a valid domain name, and returns
  97. it as a Name object. Raises an XfrinException if it is not."""
  98. try:
  99. # In the _zones dict, part of the key is the zone name,
  100. # but due to a limitation in the Name class, we
  101. # cannot directly use it as a dict key, and we use to_text()
  102. #
  103. # Downcase the name here for that reason.
  104. return Name(zone_name_str, True)
  105. except (EmptyLabel, TooLongLabel, BadLabelType, BadEscape,
  106. TooLongName, IncompleteName) as ne:
  107. raise XfrinZoneInfoException("bad zone name: " + zone_name_str + " (" + str(ne) + ")")
  108. def _check_zone_class(zone_class_str):
  109. """If the given argument is a string: checks if the given class is
  110. a valid one, and returns an RRClass object if so.
  111. Raises XfrinZoneInfoException if not.
  112. If it is None, this function returns the default RRClass.IN()"""
  113. if zone_class_str is None:
  114. return DEFAULT_ZONE_CLASS
  115. try:
  116. return RRClass(zone_class_str)
  117. except InvalidRRClass as irce:
  118. raise XfrinZoneInfoException("bad zone class: " + zone_class_str + " (" + str(irce) + ")")
  119. def format_zone_str(zone_name, zone_class):
  120. """Helper function to format a zone name and class as a string of
  121. the form '<name>/<class>'.
  122. Parameters:
  123. zone_name (isc.dns.Name) name to format
  124. zone_class (isc.dns.RRClass) class to format
  125. """
  126. return zone_name.to_text(True) + '/' + str(zone_class)
  127. def format_addrinfo(addrinfo):
  128. """Helper function to format the addrinfo as a string of the form
  129. <addr>:<port> (for IPv4) or [<addr>]:port (for IPv6). For unix domain
  130. sockets, and unknown address families, it returns a basic string
  131. conversion of the third element of the passed tuple.
  132. Parameters:
  133. addrinfo: a 3-tuple consisting of address family, socket type, and,
  134. depending on the family, either a 2-tuple with the address
  135. and port, or a filename
  136. """
  137. try:
  138. if addrinfo[0] == socket.AF_INET:
  139. return str(addrinfo[2][0]) + ":" + str(addrinfo[2][1])
  140. elif addrinfo[0] == socket.AF_INET6:
  141. return "[" + str(addrinfo[2][0]) + "]:" + str(addrinfo[2][1])
  142. else:
  143. return str(addrinfo[2])
  144. except IndexError:
  145. raise TypeError("addrinfo argument to format_addrinfo() does not "
  146. "appear to be consisting of (family, socktype, (addr, port))")
  147. def get_soa_serial(soa_rdata):
  148. '''Extract the serial field of SOA RDATA and return it as a Serial object.
  149. We don't have to be very efficient here, so we first dump the entire RDATA
  150. as a string and convert the first corresponding field. This should be
  151. sufficient in practice, but may not always work when the MNAME or RNAME
  152. contains an (escaped) space character in their labels. Ideally there
  153. should be a more direct and convenient way to get access to the SOA
  154. fields.
  155. '''
  156. return Serial(int(soa_rdata.to_text().split()[2]))
  157. class XfrinState:
  158. '''
  159. The states of the incomding *XFR state machine.
  160. We (will) handle both IXFR and AXFR with a single integrated state
  161. machine because they cannot be distinguished immediately - an AXFR
  162. response to an IXFR request can only be detected when the first two (2)
  163. response RRs have already been received.
  164. The following diagram summarizes the state transition. After sending
  165. the query, xfrin starts the process with the InitialSOA state (all
  166. IXFR/AXFR response begins with an SOA). When it reaches IXFREnd
  167. or AXFREnd, the process successfully completes.
  168. (AXFR or
  169. (recv SOA) AXFR-style IXFR) (SOA, add)
  170. InitialSOA------->FirstData------------->AXFR--------->AXFREnd
  171. | | | ^ (post xfr
  172. |(IXFR && | | | checks, then
  173. | recv SOA | +--+ commit)
  174. | not new) | (non SOA, add)
  175. V |
  176. IXFRUptodate | (non SOA, delete)
  177. (pure IXFR,| +-------+
  178. keep handling)| (Delete SOA) V |
  179. + ->IXFRDeleteSOA------>IXFRDelete--+
  180. ^ |
  181. (see SOA, not end, | (see SOA)|
  182. commit, keep handling) | |
  183. | V
  184. +---------IXFRAdd<----------+IXFRAddSOA
  185. (non SOA, add)| ^ | (Add SOA)
  186. ----------+ |
  187. |(see SOA w/ end serial, commit changes)
  188. V
  189. IXFREnd
  190. Note that changes are committed for every "difference sequence"
  191. (i.e. changes for one SOA update). This means when an IXFR response
  192. contains multiple difference sequences and something goes wrong
  193. after several commits, these changes have been published and visible
  194. to clients even if the IXFR session is subsequently aborted.
  195. It is not clear if this is valid in terms of the protocol specification.
  196. Section 4 of RFC 1995 states:
  197. An IXFR client, should only replace an older version with a newer
  198. version after all the differences have been successfully processed.
  199. If this "replacement" is for the changes of one difference sequence
  200. and "all the differences" mean the changes for that sequence, this
  201. implementation strictly follows what RFC states. If this is for
  202. the entire IXFR response (that may contain multiple sequences),
  203. we should implement it with one big transaction and one final commit
  204. at the very end.
  205. For now, we implement it with multiple smaller commits for two
  206. reasons. First, this is what BIND 9 does, and we generally port
  207. the implementation logic here. BIND 9 has been supporting IXFR
  208. for many years, so the fact that it still behaves this way
  209. probably means it at least doesn't cause a severe operational
  210. problem in practice. Second, especially because BIND 10 would
  211. often uses a database backend, a larger transaction could cause an
  212. undesirable effects, e.g. suspending normal lookups for a longer
  213. period depending on the characteristics of the database. Even if
  214. we find something wrong in a later sequeunce and abort the
  215. session, we can start another incremental update from what has
  216. been validated, or we can switch to AXFR to replace the zone
  217. completely.
  218. This implementation uses the state design pattern, where each state
  219. is represented as a subclass of the base XfrinState class. Each concrete
  220. subclass of XfrinState is assumed to define two methods: handle_rr() and
  221. finish_message(). These methods handle specific part of XFR protocols
  222. and (if necessary) perform the state transition.
  223. Conceptually, XfrinState and its subclasses are a "friend" of
  224. XfrinConnection and are assumed to be allowed to access its internal
  225. information (even though Python does not have a strict access control
  226. between different classes).
  227. The XfrinState and its subclasses are designed to be stateless, and
  228. can be used as singleton objects. For now, however, we always instantiate
  229. a new object for every state transition, partly because the introduction
  230. of singleton will make a code bit complicated, and partly because
  231. the overhead of object instantiotion wouldn't be significant for xfrin.
  232. '''
  233. def set_xfrstate(self, conn, new_state):
  234. '''Set the XfrConnection to a given new state.
  235. As a "friend" class, this method intentionally gets access to the
  236. connection's "private" method.
  237. '''
  238. conn._XfrinConnection__set_xfrstate(new_state)
  239. def handle_rr(self, conn):
  240. '''Handle one RR of an XFR response message.
  241. Depending on the state, the RR is generally added or deleted in the
  242. corresponding data source, or in some special cases indicates
  243. a specifi transition, such as starting a new IXFR difference
  244. sequence or completing the session.
  245. All subclass has their specific behaviors for this method, so
  246. there is no default definition. If the base class version
  247. is called, it's a bug of the caller, and it's notified via
  248. an XfrinException exception.
  249. This method returns a boolean value: True if the given RR was
  250. fully handled and the caller should go to the next RR; False
  251. if the caller needs to call this method with the (possibly) new
  252. state for the same RR again.
  253. '''
  254. raise XfrinException("Internal bug: " +
  255. "XfrinState.handle_rr() called directly")
  256. def finish_message(self, conn):
  257. '''Perform any final processing after handling all RRs of a response.
  258. This method then returns a boolean indicating whether to continue
  259. receiving the message. Unless it's in the end of the entire XFR
  260. session, we should continue, so this default method simply returns
  261. True.
  262. '''
  263. return True
  264. class XfrinInitialSOA(XfrinState):
  265. def handle_rr(self, conn, rr):
  266. if rr.get_type() != RRType.SOA():
  267. raise XfrinProtocolError('First RR in zone transfer must be SOA ('
  268. + rr.get_type().to_text() + ' received)')
  269. conn._end_serial = get_soa_serial(rr.get_rdata()[0])
  270. if conn._request_type == RRType.IXFR() and \
  271. conn._end_serial <= conn._request_serial:
  272. logger.info(XFRIN_IXFR_UPTODATE, conn.zone_str(),
  273. conn._request_serial, conn._end_serial)
  274. self.set_xfrstate(conn, XfrinIXFRUptodate())
  275. else:
  276. self.set_xfrstate(conn, XfrinFirstData())
  277. return True
  278. class XfrinFirstData(XfrinState):
  279. def handle_rr(self, conn, rr):
  280. '''Handle the first RR after initial SOA in an XFR session.
  281. This state happens exactly once in an XFR session, where
  282. we decide whether it's incremental update ("real" IXFR) or
  283. non incremental update (AXFR or AXFR-style IXFR).
  284. If we initiated IXFR and the transfer begins with two SOAs
  285. (the serial of the second one being equal to our serial),
  286. it's incremental; otherwise it's non incremental.
  287. This method always return False (unlike many other handle_rr()
  288. methods) because this first RR must be examined again in the
  289. determined update context.
  290. Note that in the non incremental case the RR should normally be
  291. something other SOA, but it's still possible it's an SOA with a
  292. different serial than ours. The only possible interpretation at
  293. this point is that it's non incremental update that only consists
  294. of the SOA RR. It will result in broken zone (for example, it
  295. wouldn't even contain an apex NS) and should be rejected at post
  296. XFR processing, but in terms of the XFR session processing we
  297. accept it and move forward.
  298. Note further that, in the half-broken SOA-only transfer case,
  299. these two SOAs are supposed to be the same as stated in Section 2.2
  300. of RFC 5936. We don't check that condition here, either; we'll
  301. leave whether and how to deal with that situation to the end of
  302. the processing of non incremental update. See also a related
  303. discussion at the IETF dnsext wg:
  304. http://www.ietf.org/mail-archive/web/dnsext/current/msg07908.html
  305. '''
  306. if conn._request_type == RRType.IXFR() and \
  307. rr.get_type() == RRType.SOA() and \
  308. conn._request_serial == get_soa_serial(rr.get_rdata()[0]):
  309. logger.debug(DBG_XFRIN_TRACE, XFRIN_GOT_INCREMENTAL_RESP,
  310. conn.zone_str())
  311. self.set_xfrstate(conn, XfrinIXFRDeleteSOA())
  312. else:
  313. logger.debug(DBG_XFRIN_TRACE, XFRIN_GOT_NONINCREMENTAL_RESP,
  314. conn.zone_str())
  315. # We are now going to add RRs to the new zone. We need create
  316. # a Diff object. It will be used throughtout the XFR session.
  317. conn._diff = Diff(conn._datasrc_client, conn._zone_name, True)
  318. self.set_xfrstate(conn, XfrinAXFR())
  319. return False
  320. class XfrinIXFRDeleteSOA(XfrinState):
  321. def handle_rr(self, conn, rr):
  322. if rr.get_type() != RRType.SOA():
  323. # this shouldn't happen; should this occur it means an internal
  324. # bug.
  325. raise XfrinException(rr.get_type().to_text() +
  326. ' RR is given in IXFRDeleteSOA state')
  327. # This is the beginning state of one difference sequence (changes
  328. # for one SOA update). We need to create a new Diff object now.
  329. # Note also that we (unconditionally) enable journaling here. The
  330. # Diff constructor may internally disable it, however, if the
  331. # underlying data source doesn't support journaling.
  332. conn._diff = Diff(conn._datasrc_client, conn._zone_name, False, True)
  333. conn._diff.delete_data(rr)
  334. self.set_xfrstate(conn, XfrinIXFRDelete())
  335. conn.get_transfer_stats().ixfr_deletion_count += 1
  336. return True
  337. class XfrinIXFRDelete(XfrinState):
  338. def handle_rr(self, conn, rr):
  339. if rr.get_type() == RRType.SOA():
  340. # This is the only place where current_serial is set
  341. conn._current_serial = get_soa_serial(rr.get_rdata()[0])
  342. self.set_xfrstate(conn, XfrinIXFRAddSOA())
  343. return False
  344. conn._diff.delete_data(rr)
  345. conn.get_transfer_stats().ixfr_deletion_count += 1
  346. return True
  347. class XfrinIXFRAddSOA(XfrinState):
  348. def handle_rr(self, conn, rr):
  349. if rr.get_type() != RRType.SOA():
  350. # this shouldn't happen; should this occur it means an internal
  351. # bug.
  352. raise XfrinException(rr.get_type().to_text() +
  353. ' RR is given in IXFRAddSOA state')
  354. conn._diff.add_data(rr)
  355. self.set_xfrstate(conn, XfrinIXFRAdd())
  356. conn.get_transfer_stats().ixfr_addition_count += 1
  357. return True
  358. class XfrinIXFRAdd(XfrinState):
  359. def handle_rr(self, conn, rr):
  360. if rr.get_type() == RRType.SOA():
  361. # This SOA marks the end of a difference sequence
  362. conn.get_transfer_stats().ixfr_changeset_count += 1
  363. soa_serial = get_soa_serial(rr.get_rdata()[0])
  364. if soa_serial == conn._end_serial:
  365. conn._diff.commit()
  366. self.set_xfrstate(conn, XfrinIXFREnd())
  367. return True
  368. elif soa_serial != conn._current_serial:
  369. raise XfrinProtocolError('IXFR out of sync: expected ' +
  370. 'serial ' +
  371. str(conn._current_serial) +
  372. ', got ' + str(soa_serial))
  373. else:
  374. conn._diff.commit()
  375. self.set_xfrstate(conn, XfrinIXFRDeleteSOA())
  376. return False
  377. conn._diff.add_data(rr)
  378. conn.get_transfer_stats().ixfr_addition_count += 1
  379. return True
  380. class XfrinIXFREnd(XfrinState):
  381. def handle_rr(self, conn, rr):
  382. raise XfrinProtocolError('Extra data after the end of IXFR diffs: ' +
  383. rr.to_text())
  384. def finish_message(self, conn):
  385. '''Final processing after processing an entire IXFR session.
  386. There will be more actions here, but for now we simply return False,
  387. indicating there will be no more message to receive.
  388. '''
  389. return False
  390. class XfrinIXFRUptodate(XfrinState):
  391. def handle_rr(self, conn, rr):
  392. raise XfrinProtocolError('Extra data after single IXFR response ' +
  393. rr.to_text())
  394. def finish_message(self, conn):
  395. raise XfrinZoneUptodate
  396. class XfrinAXFR(XfrinState):
  397. def handle_rr(self, conn, rr):
  398. """
  399. Handle the RR by putting it into the zone.
  400. """
  401. conn._diff.add_data(rr)
  402. if rr.get_type() == RRType.SOA():
  403. # SOA means end. Don't commit it yet - we need to perform
  404. # post-transfer checks
  405. soa_serial = get_soa_serial(rr.get_rdata()[0])
  406. if conn._end_serial != soa_serial:
  407. logger.warn(XFRIN_AXFR_INCONSISTENT_SOA, conn.zone_str(),
  408. conn._end_serial, soa_serial)
  409. self.set_xfrstate(conn, XfrinAXFREnd())
  410. conn.get_transfer_stats().axfr_rr_count += 1
  411. # Yes, we've eaten this RR.
  412. return True
  413. class XfrinAXFREnd(XfrinState):
  414. def handle_rr(self, conn, rr):
  415. raise XfrinProtocolError('Extra data after the end of AXFR: ' +
  416. rr.to_text())
  417. def finish_message(self, conn):
  418. """
  419. Final processing after processing an entire AXFR session.
  420. In this process all the AXFR changes are committed to the
  421. data source.
  422. There might be more actions here, but for now we simply return False,
  423. indicating there will be no more message to receive.
  424. """
  425. conn._diff.commit()
  426. return False
  427. class XfrinTransferStats:
  428. """
  429. This class keeps a record of transfer data for logging purposes.
  430. It records number of messages, rrs, and bytes transfered, as well
  431. as the start and end time. The start time is set upon instantiation of
  432. this class. The end time is set the first time finalize(),
  433. get_running_time(), or get_bytes_per_second() is called. The end time is
  434. set only once; subsequent calls to any of these methods does not modify
  435. it further.
  436. All _count instance variables can be directly set as needed by the
  437. class collecting these results.
  438. """
  439. def __init__(self):
  440. self.message_count = 0
  441. self.axfr_rr_count = 0
  442. self.byte_count = 0
  443. self.ixfr_changeset_count = 0;
  444. self.ixfr_deletion_count = 0;
  445. self.ixfr_addition_count = 0;
  446. self._start_time = time.time()
  447. self._end_time = None
  448. def finalize(self):
  449. """Sets the end time to time.time() if not done already."""
  450. if self._end_time is None:
  451. self._end_time = time.time()
  452. def get_running_time(self):
  453. """Calls finalize(), then returns the difference between creation
  454. and finalization time"""
  455. self.finalize()
  456. return self._end_time - self._start_time
  457. def get_bytes_per_second(self):
  458. """Returns the number of bytes per second, based on the result of
  459. get_running_time() and the value of bytes_count."""
  460. runtime = self.get_running_time()
  461. if runtime > 0.0:
  462. return float(self.byte_count) / runtime
  463. else:
  464. # This should never happen, but if some clock is so
  465. # off or reset in the meantime, we do need to return
  466. # *something* (and not raise an error)
  467. if self.byte_count == 0:
  468. return 0.0
  469. else:
  470. return float("inf")
  471. class XfrinConnection(asyncore.dispatcher):
  472. '''Do xfrin in this class. '''
  473. def __init__(self,
  474. sock_map, zone_name, rrclass, datasrc_client,
  475. shutdown_event, master_addrinfo, db_file, tsig_key=None,
  476. idle_timeout=60):
  477. '''Constructor of the XfirnConnection class.
  478. db_file: SQLite3 DB file. Unforutnately we still need this for
  479. temporary workaround in _get_zone_soa(). This should be
  480. removed when we eliminate the need for the workaround.
  481. idle_timeout: max idle time for read data from socket.
  482. datasrc_client: the data source client object used for the XFR session.
  483. This will eventually replace db_file completely.
  484. '''
  485. asyncore.dispatcher.__init__(self, map=sock_map)
  486. # The XFR state. Conceptually this is purely private, so we emphasize
  487. # the fact by the double underscore. Other classes are assumed to
  488. # get access to this via get_xfrstate(), and only XfrinState classes
  489. # are assumed to be allowed to modify it via __set_xfrstate().
  490. self.__state = None
  491. # Requested transfer type (RRType.AXFR or RRType.IXFR). The actual
  492. # transfer type may differ due to IXFR->AXFR fallback:
  493. self._request_type = None
  494. # Zone parameters
  495. self._zone_name = zone_name
  496. self._rrclass = rrclass
  497. # Data source handler
  498. self._db_file = db_file
  499. self._datasrc_client = datasrc_client
  500. self._zone_soa = self._get_zone_soa()
  501. self._sock_map = sock_map
  502. self._soa_rr_count = 0
  503. self._idle_timeout = idle_timeout
  504. self._shutdown_event = shutdown_event
  505. self._master_addrinfo = master_addrinfo
  506. self._tsig_key = tsig_key
  507. self._tsig_ctx = None
  508. # tsig_ctx_creator is introduced to allow tests to use a mock class for
  509. # easier tests (in normal case we always use the default)
  510. self._tsig_ctx_creator = lambda key : TSIGContext(key)
  511. # keep a record of this specific transfer to log on success
  512. # (time, rr/s, etc)
  513. self._transfer_stats = XfrinTransferStats()
  514. def init_socket(self):
  515. '''Initialize the underlyig socket.
  516. This is essentially a part of __init__() and is expected to be
  517. called immediately after the constructor. It's separated from
  518. the constructor because otherwise we might not be able to close
  519. it if the constructor raises an exception after opening the socket.
  520. '''
  521. self.create_socket(self._master_addrinfo[0], self._master_addrinfo[1])
  522. self.setblocking(1)
  523. def _get_zone_soa(self):
  524. '''Retrieve the current SOA RR of the zone to be transferred.
  525. It will be used for various purposes in subsequent xfr protocol
  526. processing. It is validly possible that the zone is currently
  527. empty and therefore doesn't have an SOA, so this method doesn't
  528. consider it an error and returns None in such a case. It may or
  529. may not result in failure in the actual processing depending on
  530. how the SOA is used.
  531. When the zone has an SOA RR, this method makes sure that it's
  532. valid, i.e., it has exactly one RDATA; if it is not the case
  533. this method returns None.
  534. If the underlying data source doesn't even know the zone, this method
  535. tries to provide backward compatible behavior where xfrin is
  536. responsible for creating zone in the corresponding DB table.
  537. For a longer term we should deprecate this behavior by introducing
  538. more generic zone management framework, but at the moment we try
  539. to not surprise existing users. (Note also that the part of
  540. providing the compatible behavior uses the old data source API.
  541. We'll deprecate this API in a near future, too).
  542. '''
  543. # get the zone finder. this must be SUCCESS (not even
  544. # PARTIALMATCH) because we are specifying the zone origin name.
  545. result, finder = self._datasrc_client.find_zone(self._zone_name)
  546. if result != DataSourceClient.SUCCESS:
  547. # The data source doesn't know the zone. For now, we provide
  548. # backward compatibility and creates a new one ourselves.
  549. isc.datasrc.sqlite3_ds.load(self._db_file,
  550. self._zone_name.to_text(),
  551. lambda : [])
  552. logger.warn(XFRIN_ZONE_CREATED, self.zone_str())
  553. # try again
  554. result, finder = self._datasrc_client.find_zone(self._zone_name)
  555. if result != DataSourceClient.SUCCESS:
  556. return None
  557. result, soa_rrset, _ = finder.find(self._zone_name, RRType.SOA())
  558. if result != ZoneFinder.SUCCESS:
  559. logger.info(XFRIN_ZONE_NO_SOA, self.zone_str())
  560. return None
  561. if soa_rrset.get_rdata_count() != 1:
  562. logger.warn(XFRIN_ZONE_MULTIPLE_SOA, self.zone_str(),
  563. soa_rrset.get_rdata_count())
  564. return None
  565. return soa_rrset
  566. def __set_xfrstate(self, new_state):
  567. self.__state = new_state
  568. def get_xfrstate(self):
  569. return self.__state
  570. def get_transfer_stats(self):
  571. """Returns the transfer stats object, used to measure transfer time,
  572. and number of messages/records/bytes transfered."""
  573. return self._transfer_stats
  574. def zone_str(self):
  575. '''A convenience function for logging to include zone name and class'''
  576. return format_zone_str(self._zone_name, self._rrclass)
  577. def connect_to_master(self):
  578. '''Connect to master in TCP.'''
  579. try:
  580. self.connect(self._master_addrinfo[2])
  581. return True
  582. except socket.error as e:
  583. logger.error(XFRIN_CONNECT_MASTER, self._master_addrinfo[2],
  584. str(e))
  585. return False
  586. def _create_query(self, query_type):
  587. '''Create an XFR-related query message.
  588. query_type is either SOA, AXFR or IXFR. An IXFR query needs the
  589. zone's current SOA record. If it's not known, it raises an
  590. XfrinException exception. Note that this may not necessarily a
  591. broken configuration; for the first attempt of transfer the secondary
  592. may not have any boot-strap zone information, in which case IXFR
  593. simply won't work. The xfrin should then fall back to AXFR.
  594. _request_serial is recorded for later use.
  595. '''
  596. msg = Message(Message.RENDER)
  597. query_id = random.randint(0, 0xFFFF)
  598. self._query_id = query_id
  599. msg.set_qid(query_id)
  600. msg.set_opcode(Opcode.QUERY())
  601. msg.set_rcode(Rcode.NOERROR())
  602. msg.add_question(Question(self._zone_name, self._rrclass, query_type))
  603. # Remember our serial, if known
  604. self._request_serial = get_soa_serial(self._zone_soa.get_rdata()[0]) \
  605. if self._zone_soa is not None else None
  606. # Set the authority section with our SOA for IXFR
  607. if query_type == RRType.IXFR():
  608. if self._zone_soa is None:
  609. # (incremental) IXFR doesn't work without known SOA
  610. raise XfrinException('Failed to create IXFR query due to no ' +
  611. 'SOA for ' + self.zone_str())
  612. msg.add_rrset(Message.SECTION_AUTHORITY, self._zone_soa)
  613. return msg
  614. def _send_data(self, data):
  615. size = len(data)
  616. total_count = 0
  617. while total_count < size:
  618. count = self.send(data[total_count:])
  619. total_count += count
  620. def _send_query(self, query_type):
  621. '''Send query message over TCP. '''
  622. msg = self._create_query(query_type)
  623. render = MessageRenderer()
  624. # XXX Currently, python wrapper doesn't accept 'None' parameter in this case,
  625. # we should remove the if statement and use a universal interface later.
  626. if self._tsig_key is not None:
  627. self._tsig_ctx = self._tsig_ctx_creator(self._tsig_key)
  628. msg.to_wire(render, self._tsig_ctx)
  629. else:
  630. msg.to_wire(render)
  631. header_len = struct.pack('H', socket.htons(render.get_length()))
  632. self._send_data(header_len)
  633. self._send_data(render.get_data())
  634. def _asyncore_loop(self):
  635. '''
  636. This method is a trivial wrapper for asyncore.loop(). It's extracted from
  637. _get_request_response so that we can test the rest of the code without
  638. involving actual communication with a remote server.'''
  639. asyncore.loop(self._idle_timeout, map=self._sock_map, count=1)
  640. def _get_request_response(self, size):
  641. recv_size = 0
  642. data = b''
  643. while recv_size < size:
  644. self._recv_time_out = True
  645. self._need_recv_size = size - recv_size
  646. self._asyncore_loop()
  647. if self._recv_time_out:
  648. raise XfrinException('receive data from socket time out.')
  649. recv_size += self._recvd_size
  650. data += self._recvd_data
  651. return data
  652. def _check_response_tsig(self, msg, response_data):
  653. tsig_record = msg.get_tsig_record()
  654. if self._tsig_ctx is not None:
  655. tsig_error = self._tsig_ctx.verify(tsig_record, response_data)
  656. if tsig_error != TSIGError.NOERROR:
  657. raise XfrinProtocolError('TSIG verify fail: %s' %
  658. str(tsig_error))
  659. elif tsig_record is not None:
  660. # If the response includes a TSIG while we didn't sign the query,
  661. # we treat it as an error. RFC doesn't say anything about this
  662. # case, but it clearly states the server must not sign a response
  663. # to an unsigned request. Although we could be flexible, no sane
  664. # implementation would return such a response, and since this is
  665. # part of security mechanism, it's probably better to be more
  666. # strict.
  667. raise XfrinProtocolError('Unexpected TSIG in response')
  668. def __parse_soa_response(self, msg, response_data):
  669. '''Parse a response to SOA query and extract the SOA from answer.
  670. This is a subroutine of _check_soa_serial(). This method also
  671. validates message, and rejects bogus responses with XfrinProtocolError.
  672. If everything is okay, it returns the SOA RR from the answer section
  673. of the response.
  674. '''
  675. # Check TSIG integrity and validate the header. Unlike AXFR/IXFR,
  676. # we should be more strict for SOA queries and check the AA flag, too.
  677. self._check_response_tsig(msg, response_data)
  678. self._check_response_header(msg)
  679. if not msg.get_header_flag(Message.HEADERFLAG_AA):
  680. raise XfrinProtocolError('non-authoritative answer to SOA query')
  681. # Validate the question section
  682. n_question = msg.get_rr_count(Message.SECTION_QUESTION)
  683. if n_question != 1:
  684. raise XfrinProtocolError('Invalid response to SOA query: ' +
  685. '(' + str(n_question) + ' questions, 1 ' +
  686. 'expected)')
  687. resp_question = msg.get_question()[0]
  688. if resp_question.get_name() != self._zone_name or \
  689. resp_question.get_class() != self._rrclass or \
  690. resp_question.get_type() != RRType.SOA():
  691. raise XfrinProtocolError('Invalid response to SOA query: '
  692. 'question mismatch: ' +
  693. str(resp_question))
  694. # Look into the answer section for SOA
  695. soa = None
  696. for rr in msg.get_section(Message.SECTION_ANSWER):
  697. if rr.get_type() == RRType.SOA():
  698. if soa is not None:
  699. raise XfrinProtocolError('SOA response had multiple SOAs')
  700. soa = rr
  701. # There should not be a CNAME record at top of zone.
  702. if rr.get_type() == RRType.CNAME():
  703. raise XfrinProtocolError('SOA query resulted in CNAME')
  704. # If SOA is not found, try to figure out the reason then report it.
  705. if soa is None:
  706. # See if we have any SOA records in the authority section.
  707. for rr in msg.get_section(Message.SECTION_AUTHORITY):
  708. if rr.get_type() == RRType.NS():
  709. raise XfrinProtocolError('SOA query resulted in referral')
  710. if rr.get_type() == RRType.SOA():
  711. raise XfrinProtocolError('SOA query resulted in NODATA')
  712. raise XfrinProtocolError('No SOA record found in response to ' +
  713. 'SOA query')
  714. # Check if the SOA is really what we asked for
  715. if soa.get_name() != self._zone_name or \
  716. soa.get_class() != self._rrclass:
  717. raise XfrinProtocolError("SOA response doesn't match query: " +
  718. str(soa))
  719. # All okay, return it
  720. return soa
  721. def _check_soa_serial(self):
  722. '''Send SOA query and compare the local and remote serials.
  723. If we know our local serial and the remote serial isn't newer
  724. than ours, we abort the session with XfrinZoneUptodate.
  725. On success it returns XFRIN_OK for testing. The caller won't use it.
  726. '''
  727. self._send_query(RRType.SOA())
  728. data_len = self._get_request_response(2)
  729. msg_len = socket.htons(struct.unpack('H', data_len)[0])
  730. soa_response = self._get_request_response(msg_len)
  731. msg = Message(Message.PARSE)
  732. msg.from_wire(soa_response, Message.PRESERVE_ORDER)
  733. # Validate/parse the rest of the response, and extract the SOA
  734. # from the answer section
  735. soa = self.__parse_soa_response(msg, soa_response)
  736. # Compare the two serials. If ours is 'new', abort with ZoneUptodate.
  737. primary_serial = get_soa_serial(soa.get_rdata()[0])
  738. if self._request_serial is not None and \
  739. self._request_serial >= primary_serial:
  740. if self._request_serial != primary_serial:
  741. logger.info(XFRIN_ZONE_SERIAL_AHEAD, primary_serial,
  742. self.zone_str(),
  743. format_addrinfo(self._master_addrinfo),
  744. self._request_serial)
  745. raise XfrinZoneUptodate
  746. return XFRIN_OK
  747. def do_xfrin(self, check_soa, request_type=RRType.AXFR()):
  748. '''Do an xfr session by sending xfr request and parsing responses.'''
  749. try:
  750. ret = XFRIN_OK
  751. self._request_type = request_type
  752. # Right now RRType.[IA]XFR().to_text() is 'TYPExxx', so we need
  753. # to hardcode here.
  754. req_str = 'IXFR' if request_type == RRType.IXFR() else 'AXFR'
  755. if check_soa:
  756. self._check_soa_serial()
  757. logger.info(XFRIN_XFR_TRANSFER_STARTED, req_str, self.zone_str())
  758. self._send_query(self._request_type)
  759. self.__state = XfrinInitialSOA()
  760. self._handle_xfrin_responses()
  761. # Depending what data was found, we log different status reports
  762. # (In case of an AXFR-style IXFR, print the 'AXFR' message)
  763. if self._transfer_stats.axfr_rr_count == 0:
  764. logger.info(XFRIN_IXFR_TRANSFER_SUCCESS,
  765. self.zone_str(),
  766. self._transfer_stats.message_count,
  767. self._transfer_stats.ixfr_changeset_count,
  768. self._transfer_stats.ixfr_deletion_count,
  769. self._transfer_stats.ixfr_addition_count,
  770. self._transfer_stats.byte_count,
  771. "%.3f" % self._transfer_stats.get_running_time(),
  772. "%.f" % self._transfer_stats.get_bytes_per_second()
  773. )
  774. else:
  775. logger.info(XFRIN_TRANSFER_SUCCESS,
  776. req_str,
  777. self.zone_str(),
  778. self._transfer_stats.message_count,
  779. self._transfer_stats.axfr_rr_count,
  780. self._transfer_stats.byte_count,
  781. "%.3f" % self._transfer_stats.get_running_time(),
  782. "%.f" % self._transfer_stats.get_bytes_per_second()
  783. )
  784. except XfrinZoneUptodate:
  785. # Eventually we'll probably have to treat this case as a trigger
  786. # of trying another primary server, etc, but for now we treat it
  787. # as "success".
  788. pass
  789. except XfrinProtocolError as e:
  790. logger.info(XFRIN_XFR_TRANSFER_PROTOCOL_ERROR, req_str,
  791. self.zone_str(),
  792. format_addrinfo(self._master_addrinfo), str(e))
  793. ret = XFRIN_FAIL
  794. except XfrinException as e:
  795. logger.error(XFRIN_XFR_TRANSFER_FAILURE, req_str,
  796. self.zone_str(),
  797. format_addrinfo(self._master_addrinfo), str(e))
  798. ret = XFRIN_FAIL
  799. except Exception as e:
  800. # Catching all possible exceptions like this is generally not a
  801. # good practice, but handling an xfr session could result in
  802. # so many types of exceptions, including ones from the DNS library
  803. # or from the data source library. Eventually we'd introduce a
  804. # hierarchy for exception classes from a base "ISC exception" and
  805. # catch it here, but until then we need broadest coverage so that
  806. # we won't miss anything.
  807. logger.error(XFRIN_XFR_OTHER_FAILURE, req_str,
  808. self.zone_str(), str(e))
  809. ret = XFRIN_FAIL
  810. finally:
  811. # Make sure any remaining transaction in the diff is closed
  812. # (if not yet - possible in case of xfr-level exception) as soon
  813. # as possible
  814. self._diff = None
  815. return ret
  816. def _check_response_header(self, msg):
  817. '''Perform minimal validation on responses'''
  818. # It's not clear how strict we should be about response validation.
  819. # BIND 9 ignores some cases where it would normally be considered a
  820. # bogus response. For example, it accepts a response even if its
  821. # opcode doesn't match that of the corresponding request.
  822. # According to an original developer of BIND 9 some of the missing
  823. # checks are deliberate to be kind to old implementations that would
  824. # cause interoperability trouble with stricter checks.
  825. msg_rcode = msg.get_rcode()
  826. if msg_rcode != Rcode.NOERROR():
  827. raise XfrinProtocolError('error response: %s' %
  828. msg_rcode.to_text())
  829. if not msg.get_header_flag(Message.HEADERFLAG_QR):
  830. raise XfrinProtocolError('response is not a response')
  831. if msg.get_qid() != self._query_id:
  832. raise XfrinProtocolError('bad query id')
  833. def _check_response_status(self, msg):
  834. '''Check validation of xfr response. '''
  835. self._check_response_header(msg)
  836. if msg.get_rr_count(Message.SECTION_QUESTION) > 1:
  837. raise XfrinProtocolError('query section count greater than 1')
  838. def _handle_xfrin_responses(self):
  839. read_next_msg = True
  840. while read_next_msg:
  841. data_len = self._get_request_response(2)
  842. msg_len = socket.htons(struct.unpack('H', data_len)[0])
  843. self._transfer_stats.byte_count += msg_len + 2
  844. recvdata = self._get_request_response(msg_len)
  845. msg = Message(Message.PARSE)
  846. msg.from_wire(recvdata, Message.PRESERVE_ORDER)
  847. self._transfer_stats.message_count += 1
  848. # TSIG related checks, including an unexpected signed response
  849. self._check_response_tsig(msg, recvdata)
  850. # Perform response status validation
  851. self._check_response_status(msg)
  852. for rr in msg.get_section(Message.SECTION_ANSWER):
  853. rr_handled = False
  854. while not rr_handled:
  855. rr_handled = self.__state.handle_rr(self, rr)
  856. read_next_msg = self.__state.finish_message(self)
  857. if self._shutdown_event.is_set():
  858. raise XfrinException('xfrin is forced to stop')
  859. def handle_read(self):
  860. '''Read query's response from socket. '''
  861. self._recvd_data = self.recv(self._need_recv_size)
  862. self._recvd_size = len(self._recvd_data)
  863. self._recv_time_out = False
  864. def writable(self):
  865. '''Ignore the writable socket. '''
  866. return False
  867. def __process_xfrin(server, zone_name, rrclass, db_file,
  868. shutdown_event, master_addrinfo, check_soa, tsig_key,
  869. request_type, conn_class):
  870. conn = None
  871. exception = None
  872. ret = XFRIN_FAIL
  873. try:
  874. # Create a data source client used in this XFR session. Right now we
  875. # still assume an sqlite3-based data source, and use both the old and new
  876. # data source APIs. We also need to use a mock client for tests.
  877. # For a temporary workaround to deal with these situations, we skip the
  878. # creation when the given file is none (the test case). Eventually
  879. # this code will be much cleaner.
  880. datasrc_client = None
  881. if db_file is not None:
  882. # temporary hardcoded sqlite initialization. Once we decide on
  883. # the config specification, we need to update this (TODO)
  884. # this may depend on #1207, or any followup ticket created for #1207
  885. datasrc_type = "sqlite3"
  886. datasrc_config = "{ \"database_file\": \"" + db_file + "\"}"
  887. datasrc_client = DataSourceClient(datasrc_type, datasrc_config)
  888. # Create a TCP connection for the XFR session and perform the operation.
  889. sock_map = {}
  890. # In case we were asked to do IXFR and that one fails, we try again with
  891. # AXFR. But only if we could actually connect to the server.
  892. #
  893. # So we start with retry as True, which is set to false on each attempt.
  894. # In the case of connected but failed IXFR, we set it to true once again.
  895. retry = True
  896. while retry:
  897. retry = False
  898. conn = conn_class(sock_map, zone_name, rrclass, datasrc_client,
  899. shutdown_event, master_addrinfo, db_file,
  900. tsig_key)
  901. conn.init_socket()
  902. ret = XFRIN_FAIL
  903. if conn.connect_to_master():
  904. ret = conn.do_xfrin(check_soa, request_type)
  905. if ret == XFRIN_FAIL and request_type == RRType.IXFR():
  906. # IXFR failed for some reason. It might mean the server can't
  907. # handle it, or we don't have the zone or we are out of sync or
  908. # whatever else. So we retry with with AXFR, as it may succeed
  909. # in many such cases.
  910. retry = True
  911. request_type = RRType.AXFR()
  912. logger.warn(XFRIN_XFR_TRANSFER_FALLBACK, conn.zone_str())
  913. conn.close()
  914. conn = None
  915. except Exception as ex:
  916. # If exception happens, just remember it here so that we can re-raise
  917. # after cleaning up things. We don't log it here because we want
  918. # eliminate smallest possibility of having an exception in logging
  919. # itself.
  920. exception = ex
  921. # asyncore.dispatcher requires explicit close() unless its lifetime
  922. # from born to destruction is closed within asyncore.loop, which is not
  923. # the case for us. We always close() here, whether or not do_xfrin
  924. # succeeds, and even when we see an unexpected exception.
  925. if conn is not None:
  926. conn.close()
  927. # Publish the zone transfer result news, so zonemgr can reset the
  928. # zone timer, and xfrout can notify the zone's slaves if the result
  929. # is success.
  930. server.publish_xfrin_news(zone_name, rrclass, ret)
  931. if exception is not None:
  932. raise exception
  933. def process_xfrin(server, xfrin_recorder, zone_name, rrclass, db_file,
  934. shutdown_event, master_addrinfo, check_soa, tsig_key,
  935. request_type, conn_class=XfrinConnection):
  936. # Even if it should be rare, the main process of xfrin session can
  937. # raise an exception. In order to make sure the lock in xfrin_recorder
  938. # is released in any cases, we delegate the main part to the helper
  939. # function in the try block, catch any exceptions, then release the lock.
  940. xfrin_recorder.increment(zone_name)
  941. exception = None
  942. try:
  943. __process_xfrin(server, zone_name, rrclass, db_file,
  944. shutdown_event, master_addrinfo, check_soa, tsig_key,
  945. request_type, conn_class)
  946. except Exception as ex:
  947. # don't log it until we complete decrement().
  948. exception = ex
  949. xfrin_recorder.decrement(zone_name)
  950. if exception is not None:
  951. typestr = "AXFR" if request_type == RRType.AXFR() else "IXFR"
  952. logger.error(XFRIN_XFR_PROCESS_FAILURE, typestr, zone_name.to_text(),
  953. str(rrclass), str(exception))
  954. class XfrinRecorder:
  955. def __init__(self):
  956. self._lock = threading.Lock()
  957. self._zones = []
  958. def increment(self, zone_name):
  959. self._lock.acquire()
  960. self._zones.append(zone_name)
  961. self._lock.release()
  962. def decrement(self, zone_name):
  963. self._lock.acquire()
  964. if zone_name in self._zones:
  965. self._zones.remove(zone_name)
  966. self._lock.release()
  967. def xfrin_in_progress(self, zone_name):
  968. self._lock.acquire()
  969. ret = zone_name in self._zones
  970. self._lock.release()
  971. return ret
  972. def count(self):
  973. self._lock.acquire()
  974. ret = len(self._zones)
  975. self._lock.release()
  976. return ret
  977. class ZoneInfo:
  978. def __init__(self, config_data, module_cc):
  979. """Creates a zone_info with the config data element as
  980. specified by the 'zones' list in xfrin.spec. Module_cc is
  981. needed to get the defaults from the specification"""
  982. self._module_cc = module_cc
  983. self.set_name(config_data.get('name'))
  984. self.set_master_addr(config_data.get('master_addr'))
  985. self.set_master_port(config_data.get('master_port'))
  986. self.set_zone_class(config_data.get('class'))
  987. self.set_tsig_key(config_data.get('tsig_key'))
  988. self.set_use_ixfr(config_data.get('use_ixfr'))
  989. def set_name(self, name_str):
  990. """Set the name for this zone given a name string.
  991. Raises XfrinZoneInfoException if name_str is None or if it
  992. cannot be parsed."""
  993. if name_str is None:
  994. raise XfrinZoneInfoException("Configuration zones list "
  995. "element does not contain "
  996. "'name' attribute")
  997. else:
  998. self.name = _check_zone_name(name_str)
  999. def set_master_addr(self, master_addr_str):
  1000. """Set the master address for this zone given an IP address
  1001. string. Raises XfrinZoneInfoException if master_addr_str is
  1002. None or if it cannot be parsed."""
  1003. if master_addr_str is None:
  1004. raise XfrinZoneInfoException("master address missing from config data")
  1005. else:
  1006. try:
  1007. self.master_addr = isc.net.parse.addr_parse(master_addr_str)
  1008. except ValueError:
  1009. logger.error(XFRIN_BAD_MASTER_ADDR_FORMAT, master_addr_str)
  1010. errmsg = "bad format for zone's master: " + master_addr_str
  1011. raise XfrinZoneInfoException(errmsg)
  1012. def set_master_port(self, master_port_str):
  1013. """Set the master port given a port number string. If
  1014. master_port_str is None, the default from the specification
  1015. for this module will be used. Raises XfrinZoneInfoException if
  1016. the string contains an invalid port number"""
  1017. if master_port_str is None:
  1018. self.master_port = self._module_cc.get_default_value("zones/master_port")
  1019. else:
  1020. try:
  1021. self.master_port = isc.net.parse.port_parse(master_port_str)
  1022. except ValueError:
  1023. logger.error(XFRIN_BAD_MASTER_PORT_FORMAT, master_port_str)
  1024. errmsg = "bad format for zone's master port: " + master_port_str
  1025. raise XfrinZoneInfoException(errmsg)
  1026. def set_zone_class(self, zone_class_str):
  1027. """Set the zone class given an RR class str (e.g. "IN"). If
  1028. zone_class_str is None, it will default to what is specified
  1029. in the specification file for this module. Raises
  1030. XfrinZoneInfoException if the string cannot be parsed."""
  1031. # TODO: remove _str
  1032. self.class_str = zone_class_str or self._module_cc.get_default_value("zones/class")
  1033. if zone_class_str == None:
  1034. #TODO rrclass->zone_class
  1035. self.rrclass = RRClass(self._module_cc.get_default_value("zones/class"))
  1036. else:
  1037. try:
  1038. self.rrclass = RRClass(zone_class_str)
  1039. except InvalidRRClass:
  1040. logger.error(XFRIN_BAD_ZONE_CLASS, zone_class_str)
  1041. errmsg = "invalid zone class: " + zone_class_str
  1042. raise XfrinZoneInfoException(errmsg)
  1043. def set_tsig_key(self, tsig_key_str):
  1044. """Set the tsig_key for this zone, given a TSIG key string
  1045. representation. If tsig_key_str is None, no TSIG key will
  1046. be set. Raises XfrinZoneInfoException if tsig_key_str cannot
  1047. be parsed."""
  1048. if tsig_key_str is None:
  1049. self.tsig_key = None
  1050. else:
  1051. try:
  1052. self.tsig_key = TSIGKey(tsig_key_str)
  1053. except InvalidParameter as ipe:
  1054. logger.error(XFRIN_BAD_TSIG_KEY_STRING, tsig_key_str)
  1055. errmsg = "bad TSIG key string: " + tsig_key_str
  1056. raise XfrinZoneInfoException(errmsg)
  1057. def set_use_ixfr(self, use_ixfr):
  1058. """Set use_ixfr. If set to True, it will use
  1059. IXFR for incoming transfers. If set to False, it will use AXFR.
  1060. At this moment there is no automatic fallback"""
  1061. # TODO: http://bind10.isc.org/ticket/1279
  1062. if use_ixfr is None:
  1063. self.use_ixfr = \
  1064. self._module_cc.get_default_value("zones/use_ixfr")
  1065. else:
  1066. self.use_ixfr = use_ixfr
  1067. def get_master_addr_info(self):
  1068. return (self.master_addr.family, socket.SOCK_STREAM,
  1069. (str(self.master_addr), self.master_port))
  1070. class Xfrin:
  1071. def __init__(self):
  1072. self._max_transfers_in = 10
  1073. self._zones = {}
  1074. # This is a set of (zone/class) tuples (both as strings),
  1075. # representing the in-memory zones maintaned by Xfrin. It
  1076. # is used to trigger Auth/in-memory so that it reloads
  1077. # zones when they have been transfered in
  1078. self._memory_zones = set()
  1079. self._cc_setup()
  1080. self.recorder = XfrinRecorder()
  1081. self._shutdown_event = threading.Event()
  1082. def _cc_setup(self):
  1083. '''This method is used only as part of initialization, but is
  1084. implemented separately for convenience of unit tests; by letting
  1085. the test code override this method we can test most of this class
  1086. without requiring a command channel.'''
  1087. # Create one session for sending command to other modules, because the
  1088. # listening session will block the send operation.
  1089. self._send_cc_session = isc.cc.Session()
  1090. self._module_cc = isc.config.ModuleCCSession(SPECFILE_LOCATION,
  1091. self.config_handler,
  1092. self.command_handler)
  1093. self._module_cc.start()
  1094. config_data = self._module_cc.get_full_config()
  1095. self.config_handler(config_data)
  1096. self._module_cc.add_remote_config(AUTH_SPECFILE_LOCATION,
  1097. self._auth_config_handler)
  1098. def _cc_check_command(self):
  1099. '''This is a straightforward wrapper for cc.check_command,
  1100. but provided as a separate method for the convenience
  1101. of unit tests.'''
  1102. self._module_cc.check_command(False)
  1103. def _get_zone_info(self, name, rrclass):
  1104. """Returns the ZoneInfo object containing the configured data
  1105. for the given zone name. If the zone name did not have any
  1106. data, returns None"""
  1107. return self._zones.get((name.to_text(), rrclass.to_text()))
  1108. def _add_zone_info(self, zone_info):
  1109. """Add the zone info. Raises a XfrinZoneInfoException if a zone
  1110. with the same name and class is already configured"""
  1111. key = (zone_info.name.to_text(), zone_info.class_str)
  1112. if key in self._zones:
  1113. raise XfrinZoneInfoException("zone " + str(key) +
  1114. " configured multiple times")
  1115. self._zones[key] = zone_info
  1116. def _clear_zone_info(self):
  1117. self._zones = {}
  1118. def config_handler(self, new_config):
  1119. # backup all config data (should there be a problem in the new
  1120. # data)
  1121. old_max_transfers_in = self._max_transfers_in
  1122. old_zones = self._zones
  1123. self._max_transfers_in = new_config.get("transfers_in") or self._max_transfers_in
  1124. if 'zones' in new_config:
  1125. self._clear_zone_info()
  1126. for zone_config in new_config.get('zones'):
  1127. try:
  1128. zone_info = ZoneInfo(zone_config, self._module_cc)
  1129. self._add_zone_info(zone_info)
  1130. except XfrinZoneInfoException as xce:
  1131. self._zones = old_zones
  1132. self._max_transfers_in = old_max_transfers_in
  1133. return create_answer(1, str(xce))
  1134. return create_answer(0)
  1135. def _auth_config_handler(self, new_config, config_data):
  1136. # Config handler for changes in Auth configuration
  1137. self._set_db_file()
  1138. self._set_memory_zones(new_config, config_data)
  1139. def _clear_memory_zones(self):
  1140. """Clears the memory_zones set; called before processing the
  1141. changed list of memory datasource zones that have file type
  1142. sqlite3"""
  1143. self._memory_zones.clear()
  1144. def _is_memory_zone(self, zone_name_str, zone_class_str):
  1145. """Returns true if the given zone/class combination is configured
  1146. in the in-memory datasource of the Auth process with file type
  1147. 'sqlite3'.
  1148. Note: this method is not thread-safe. We are considering
  1149. changing the threaded model here, but if we do not, take
  1150. care in accessing and updating the memory zone set (or add
  1151. locks)
  1152. """
  1153. # Normalize them first, if either conversion fails, return false
  1154. # (they won't be in the set anyway)
  1155. try:
  1156. zone_name_str = Name(zone_name_str).to_text().lower()
  1157. zone_class_str = RRClass(zone_class_str).to_text()
  1158. except Exception:
  1159. return False
  1160. return (zone_name_str, zone_class_str) in self._memory_zones
  1161. def _set_memory_zones(self, new_config, config_data):
  1162. """Part of the _auth_config_handler function, keeps an internal set
  1163. of zones in the datasources config subset that have 'sqlite3' as
  1164. their file type.
  1165. Note: this method is not thread-safe. We are considering
  1166. changing the threaded model here, but if we do not, take
  1167. care in accessing and updating the memory zone set (or add
  1168. locks)
  1169. """
  1170. # walk through the data and collect the memory zones
  1171. # If this causes any exception, assume we were passed bad data
  1172. # and keep the original set
  1173. new_memory_zones = set()
  1174. try:
  1175. if "datasources" in new_config:
  1176. for datasource in new_config["datasources"]:
  1177. if "class" in datasource:
  1178. ds_class = RRClass(datasource["class"])
  1179. else:
  1180. # Get the default
  1181. ds_class = RRClass(config_data.get_default_value(
  1182. "datasources/class"))
  1183. if datasource["type"] == "memory":
  1184. for zone in datasource["zones"]:
  1185. if "filetype" in zone and \
  1186. zone["filetype"] == "sqlite3":
  1187. zone_name = Name(zone["origin"])
  1188. zone_name_str = zone_name.to_text().lower()
  1189. new_memory_zones.add((zone_name_str,
  1190. ds_class.to_text()))
  1191. # Ok, we can use the data, update our list
  1192. self._memory_zones = new_memory_zones
  1193. except Exception:
  1194. # Something is wrong with the data. If this data even reached us,
  1195. # we cannot do more than assume the real module has logged and
  1196. # reported an error. Keep the old set.
  1197. return
  1198. def shutdown(self):
  1199. ''' shutdown the xfrin process. the thread which is doing xfrin should be
  1200. terminated.
  1201. '''
  1202. self._module_cc.remove_remote_config(AUTH_SPECFILE_LOCATION)
  1203. self._module_cc.send_stopping()
  1204. self._shutdown_event.set()
  1205. main_thread = threading.currentThread()
  1206. for th in threading.enumerate():
  1207. if th is main_thread:
  1208. continue
  1209. th.join()
  1210. def command_handler(self, command, args):
  1211. answer = create_answer(0)
  1212. try:
  1213. if command == 'shutdown':
  1214. self._shutdown_event.set()
  1215. elif command == 'notify' or command == REFRESH_FROM_ZONEMGR:
  1216. # Xfrin receives the refresh/notify command from zone manager.
  1217. # notify command maybe has the parameters which
  1218. # specify the notifyfrom address and port, according the RFC1996, zone
  1219. # transfer should starts first from the notifyfrom, but now, let 'TODO' it.
  1220. # (using the value now, while we can only set one master address, would be
  1221. # a security hole. Once we add the ability to have multiple master addresses,
  1222. # we should check if it matches one of them, and then use it.)
  1223. (zone_name, rrclass) = self._parse_zone_name_and_class(args)
  1224. zone_str = format_zone_str(zone_name, rrclass)
  1225. zone_info = self._get_zone_info(zone_name, rrclass)
  1226. notify_addr = self._parse_master_and_port(args, zone_name,
  1227. rrclass)
  1228. if zone_info is None:
  1229. # TODO what to do? no info known about zone. defaults?
  1230. errmsg = "Got notification to retransfer unknown zone " + zone_str
  1231. logger.info(XFRIN_RETRANSFER_UNKNOWN_ZONE, zone_str)
  1232. answer = create_answer(1, errmsg)
  1233. else:
  1234. request_type = RRType.AXFR()
  1235. if zone_info.use_ixfr:
  1236. request_type = RRType.IXFR()
  1237. master_addr = zone_info.get_master_addr_info()
  1238. if notify_addr[0] == master_addr[0] and\
  1239. notify_addr[2] == master_addr[2]:
  1240. ret = self.xfrin_start(zone_name,
  1241. rrclass,
  1242. self._get_db_file(),
  1243. master_addr,
  1244. zone_info.tsig_key, request_type,
  1245. True)
  1246. answer = create_answer(ret[0], ret[1])
  1247. else:
  1248. notify_addr_str = format_addrinfo(notify_addr)
  1249. master_addr_str = format_addrinfo(master_addr)
  1250. errmsg = "Got notification for " + zone_str\
  1251. + "from unknown address: " + notify_addr_str;
  1252. logger.info(XFRIN_NOTIFY_UNKNOWN_MASTER, zone_str,
  1253. notify_addr_str, master_addr_str)
  1254. answer = create_answer(1, errmsg)
  1255. elif command == 'retransfer' or command == 'refresh':
  1256. # Xfrin receives the retransfer/refresh from cmdctl(sent by bindctl).
  1257. # If the command has specified master address, do transfer from the
  1258. # master address, or else do transfer from the configured masters.
  1259. (zone_name, rrclass) = self._parse_zone_name_and_class(args)
  1260. master_addr = self._parse_master_and_port(args, zone_name,
  1261. rrclass)
  1262. zone_info = self._get_zone_info(zone_name, rrclass)
  1263. tsig_key = None
  1264. request_type = RRType.AXFR()
  1265. if zone_info:
  1266. tsig_key = zone_info.tsig_key
  1267. if zone_info.use_ixfr:
  1268. request_type = RRType.IXFR()
  1269. db_file = args.get('db_file') or self._get_db_file()
  1270. ret = self.xfrin_start(zone_name,
  1271. rrclass,
  1272. db_file,
  1273. master_addr,
  1274. tsig_key, request_type,
  1275. (False if command == 'retransfer' else True))
  1276. answer = create_answer(ret[0], ret[1])
  1277. else:
  1278. answer = create_answer(1, 'unknown command: ' + command)
  1279. except XfrinException as err:
  1280. logger.error(XFRIN_COMMAND_ERROR, command, str(err))
  1281. answer = create_answer(1, str(err))
  1282. return answer
  1283. def _parse_zone_name_and_class(self, args):
  1284. zone_name_str = args.get('zone_name')
  1285. if zone_name_str is None:
  1286. raise XfrinException('zone name should be provided')
  1287. return (_check_zone_name(zone_name_str), _check_zone_class(args.get('zone_class')))
  1288. def _parse_master_and_port(self, args, zone_name, zone_class):
  1289. """
  1290. Return tuple (family, socktype, sockaddr) for address and port in given
  1291. args dict.
  1292. IPv4 and IPv6 are the only supported addresses now, so sockaddr will be
  1293. (address, port). The socktype is socket.SOCK_STREAM for now.
  1294. """
  1295. # check if we have configured info about this zone, in case
  1296. # port or master are not specified
  1297. zone_info = self._get_zone_info(zone_name, zone_class)
  1298. addr_str = args.get('master')
  1299. if addr_str is None:
  1300. if zone_info is not None:
  1301. addr = zone_info.master_addr
  1302. else:
  1303. raise XfrinException("Master address not given or "
  1304. "configured for " + zone_name.to_text())
  1305. else:
  1306. try:
  1307. addr = isc.net.parse.addr_parse(addr_str)
  1308. except ValueError as err:
  1309. raise XfrinException("failed to resolve master address %s: %s" %
  1310. (addr_str, str(err)))
  1311. port_str = args.get('port')
  1312. if port_str is None:
  1313. if zone_info is not None:
  1314. port = zone_info.master_port
  1315. else:
  1316. port = DEFAULT_MASTER_PORT
  1317. else:
  1318. try:
  1319. port = isc.net.parse.port_parse(port_str)
  1320. except ValueError as err:
  1321. raise XfrinException("failed to parse port=%s: %s" %
  1322. (port_str, str(err)))
  1323. return (addr.family, socket.SOCK_STREAM, (str(addr), port))
  1324. def _get_db_file(self):
  1325. return self._db_file
  1326. def _set_db_file(self):
  1327. db_file, is_default =\
  1328. self._module_cc.get_remote_config_value("Auth", "database_file")
  1329. if is_default and "B10_FROM_BUILD" in os.environ:
  1330. # override the local database setting if it is default and we
  1331. # are running from the source tree
  1332. # This should be hidden inside the data source library and/or
  1333. # done as a configuration, and this special case should be gone).
  1334. db_file = os.environ["B10_FROM_BUILD"] + os.sep +\
  1335. "bind10_zones.sqlite3"
  1336. self._db_file = db_file
  1337. def publish_xfrin_news(self, zone_name, zone_class, xfr_result):
  1338. '''Send command to xfrout/zone manager module.
  1339. If xfrin has finished successfully for one zone, tell the good
  1340. news(command: zone_new_data_ready) to zone manager and xfrout.
  1341. if xfrin failed, just tell the bad news to zone manager, so that
  1342. it can reset the refresh timer for that zone. '''
  1343. param = {'zone_name': zone_name.to_text(),
  1344. 'zone_class': zone_class.to_text()}
  1345. if xfr_result == XFRIN_OK:
  1346. msg = create_command(notify_out.ZONE_NEW_DATA_READY_CMD, param)
  1347. # catch the exception, in case msgq has been killed.
  1348. try:
  1349. seq = self._send_cc_session.group_sendmsg(msg,
  1350. XFROUT_MODULE_NAME)
  1351. try:
  1352. answer, env = self._send_cc_session.group_recvmsg(False,
  1353. seq)
  1354. except isc.cc.session.SessionTimeout:
  1355. pass # for now we just ignore the failure
  1356. seq = self._send_cc_session.group_sendmsg(msg, ZONE_MANAGER_MODULE_NAME)
  1357. try:
  1358. answer, env = self._send_cc_session.group_recvmsg(False,
  1359. seq)
  1360. except isc.cc.session.SessionTimeout:
  1361. pass # for now we just ignore the failure
  1362. except socket.error as err:
  1363. logger.error(XFRIN_MSGQ_SEND_ERROR, XFROUT_MODULE_NAME, ZONE_MANAGER_MODULE_NAME)
  1364. else:
  1365. msg = create_command(ZONE_XFRIN_FAILED, param)
  1366. # catch the exception, in case msgq has been killed.
  1367. try:
  1368. seq = self._send_cc_session.group_sendmsg(msg, ZONE_MANAGER_MODULE_NAME)
  1369. try:
  1370. answer, env = self._send_cc_session.group_recvmsg(False,
  1371. seq)
  1372. except isc.cc.session.SessionTimeout:
  1373. pass # for now we just ignore the failure
  1374. except socket.error as err:
  1375. logger.error(XFRIN_MSGQ_SEND_ERROR_ZONE_MANAGER, ZONE_MANAGER_MODULE_NAME)
  1376. def startup(self):
  1377. logger.debug(DBG_PROCESS, XFRIN_STARTED)
  1378. while not self._shutdown_event.is_set():
  1379. self._cc_check_command()
  1380. def xfrin_start(self, zone_name, rrclass, db_file, master_addrinfo,
  1381. tsig_key, request_type, check_soa=True):
  1382. if "pydnspp" not in sys.modules:
  1383. return (1, "xfrin failed, can't load dns message python library: 'pydnspp'")
  1384. # check max_transfer_in, else return quota error
  1385. if self.recorder.count() >= self._max_transfers_in:
  1386. return (1, 'xfrin quota error')
  1387. if self.recorder.xfrin_in_progress(zone_name):
  1388. return (1, 'zone xfrin is in progress')
  1389. xfrin_thread = threading.Thread(target = process_xfrin,
  1390. args = (self,
  1391. self.recorder,
  1392. zone_name,
  1393. rrclass,
  1394. db_file,
  1395. self._shutdown_event,
  1396. master_addrinfo, check_soa,
  1397. tsig_key, request_type))
  1398. xfrin_thread.start()
  1399. return (0, 'zone xfrin is started')
  1400. xfrind = None
  1401. def signal_handler(signal, frame):
  1402. if xfrind:
  1403. xfrind.shutdown()
  1404. sys.exit(0)
  1405. def set_signal_handler():
  1406. signal.signal(signal.SIGTERM, signal_handler)
  1407. signal.signal(signal.SIGINT, signal_handler)
  1408. def set_cmd_options(parser):
  1409. parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
  1410. help="This option is obsolete and has no effect.")
  1411. def main(xfrin_class, use_signal=True):
  1412. """The main loop of the Xfrin daemon.
  1413. @param xfrin_class: A class of the Xfrin object. This is normally Xfrin,
  1414. but can be a subclass of it for customization.
  1415. @param use_signal: True if this process should catch signals. This is
  1416. normally True, but may be disabled when this function is called in a
  1417. testing context."""
  1418. global xfrind
  1419. try:
  1420. parser = OptionParser(version = __version__)
  1421. set_cmd_options(parser)
  1422. (options, args) = parser.parse_args()
  1423. if use_signal:
  1424. set_signal_handler()
  1425. xfrind = xfrin_class()
  1426. xfrind.startup()
  1427. except KeyboardInterrupt:
  1428. logger.info(XFRIN_STOPPED_BY_KEYBOARD)
  1429. except isc.cc.session.SessionError as e:
  1430. logger.error(XFRIN_CC_SESSION_ERROR, str(e))
  1431. except Exception as e:
  1432. logger.error(XFRIN_UNKNOWN_ERROR, str(e))
  1433. if xfrind:
  1434. xfrind.shutdown()
  1435. if __name__ == '__main__':
  1436. main(Xfrin)