session.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. # Copyright (C) 2012 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. from isc.dns import *
  16. import isc.ddns.zone_config
  17. from isc.log import *
  18. from isc.ddns.logger import logger, ClientFormatter, ZoneFormatter,\
  19. RRsetFormatter
  20. from isc.log_messages.libddns_messages import *
  21. from isc.datasrc import ZoneFinder
  22. import isc.xfrin.diff
  23. from isc.acl.acl import ACCEPT, REJECT, DROP
  24. import copy
  25. # Result codes for UpdateSession.handle()
  26. UPDATE_SUCCESS = 0
  27. UPDATE_ERROR = 1
  28. UPDATE_DROP = 2
  29. # Convenient aliases of update-specific section names
  30. SECTION_ZONE = Message.SECTION_QUESTION
  31. SECTION_PREREQUISITE = Message.SECTION_ANSWER
  32. SECTION_UPDATE = Message.SECTION_AUTHORITY
  33. # Shortcut
  34. DBGLVL_TRACE_BASIC = logger.DBGLVL_TRACE_BASIC
  35. class UpdateError(Exception):
  36. '''Exception for general error in update request handling.
  37. This exception is intended to be used internally within this module.
  38. When UpdateSession.handle() encounters an error in handling an update
  39. request it can raise this exception to terminate the handling.
  40. This class is constructed with some information that may be useful for
  41. subsequent possible logging:
  42. - msg (string) A string explaining the error.
  43. - zname (isc.dns.Name) The zone name. Can be None when not identified.
  44. - zclass (isc.dns.RRClass) The zone class. Like zname, can be None.
  45. - rcode (isc.dns.RCode or None) The RCODE to be set in the response
  46. message; this can be None if the response is not expected to be sent.
  47. - nolog (bool) If True, it indicates there's no more need for logging.
  48. '''
  49. def __init__(self, msg, zname, zclass, rcode, nolog=False):
  50. Exception.__init__(self, msg)
  51. self.zname = zname
  52. self.zclass = zclass
  53. self.rcode = rcode
  54. self.nolog = nolog
  55. def foreach_rr(rrset):
  56. '''
  57. Generator that creates a new RRset with one RR from
  58. the given RRset upon each iteration, usable in calls that
  59. need to loop over an RRset and perform an action with each
  60. of the individual RRs in it.
  61. Example:
  62. for rr in foreach_rr(rrset):
  63. print(str(rr))
  64. '''
  65. for rdata in rrset.get_rdata():
  66. rr = isc.dns.RRset(rrset.get_name(),
  67. rrset.get_class(),
  68. rrset.get_type(),
  69. rrset.get_ttl())
  70. rr.add_rdata(rdata)
  71. yield rr
  72. def convert_rrset_class(rrset, rrclass):
  73. '''Returns a (new) rrset with the data from the given rrset,
  74. but of the given class. Useful to convert from NONE and ANY to
  75. a real class.
  76. Note that the caller should be careful what to convert;
  77. and DNS error that could happen during wire-format reading
  78. could technically occur here, and is not caught by this helper.
  79. '''
  80. new_rrset = isc.dns.RRset(rrset.get_name(), rrclass,
  81. rrset.get_type(), rrset.get_ttl())
  82. for rdata in rrset.get_rdata():
  83. # Rdata class is nof modifiable, and must match rrset's
  84. # class, so we need to to some ugly conversion here.
  85. # And we cannot use to_text() (since the class may be unknown)
  86. wire = rdata.to_wire(bytes())
  87. new_rrset.add_rdata(isc.dns.Rdata(rrset.get_type(), rrclass, wire))
  88. return new_rrset
  89. def collect_rrsets(collection, rrset):
  90. '''
  91. Helper function to collect similar rrsets.
  92. Collect all rrsets with the same name, class, and type
  93. collection is the currently collected list of RRsets,
  94. rrset is the RRset to add;
  95. if an RRset with the same name, class and type as the
  96. given rrset exists in the collection, its rdata fields
  97. are added to that RRset. Otherwise, the rrset is added
  98. to the given collection.
  99. TTL is ignored.
  100. This method does not check rdata contents for duplicate
  101. values.
  102. The collection and its rrsets are modified in-place,
  103. this method does not return anything.
  104. '''
  105. found = False
  106. for existing_rrset in collection:
  107. if existing_rrset.get_name() == rrset.get_name() and\
  108. existing_rrset.get_class() == rrset.get_class() and\
  109. existing_rrset.get_type() == rrset.get_type():
  110. for rdata in rrset.get_rdata():
  111. existing_rrset.add_rdata(rdata)
  112. found = True
  113. if not found:
  114. collection.append(rrset)
  115. class DDNS_SOA:
  116. '''Class to handle the SOA in the DNS update '''
  117. def __get_serial_internal(self, origin_soa):
  118. '''Get serial number from soa'''
  119. return Serial(int(origin_soa.get_rdata()[0].to_text().split()[2]))
  120. def __write_soa_internal(self, origin_soa, soa_num):
  121. '''Write back serial number to soa'''
  122. new_soa = RRset(origin_soa.get_name(), origin_soa.get_class(),
  123. RRType.SOA(), origin_soa.get_ttl())
  124. soa_rdata_parts = origin_soa.get_rdata()[0].to_text().split()
  125. soa_rdata_parts[2] = str(soa_num.get_value())
  126. new_soa.add_rdata(Rdata(origin_soa.get_type(), origin_soa.get_class(),
  127. " ".join(soa_rdata_parts)))
  128. return new_soa
  129. def soa_update_check(self, origin_soa, new_soa):
  130. '''Check whether the new soa is valid. If the serial number is bigger
  131. than the old one, it is valid, then return True, otherwise, return
  132. False. Make sure the origin_soa and new_soa parameters are not none
  133. before invoke soa_update_check.
  134. Parameters:
  135. origin_soa, old SOA resource record.
  136. new_soa, new SOA resource record.
  137. Output:
  138. if the serial number of new soa is bigger than the old one, return
  139. True, otherwise return False.
  140. '''
  141. old_serial = self.__get_serial_internal(origin_soa)
  142. new_serial = self.__get_serial_internal(new_soa)
  143. if(new_serial > old_serial):
  144. return True
  145. else:
  146. return False
  147. def update_soa(self, origin_soa, inc_number = 1):
  148. ''' Update the soa number incrementally as RFC 2136. Please make sure
  149. that the origin_soa exists and not none before invoke this function.
  150. Parameters:
  151. origin_soa, the soa resource record which will be updated.
  152. inc_number, the number which will be added into the serial number of
  153. origin_soa, the default value is one.
  154. Output:
  155. The new origin soa whoes serial number has been updated.
  156. '''
  157. soa_num = self.__get_serial_internal(origin_soa)
  158. soa_num = soa_num + inc_number
  159. if soa_num.get_value() == 0:
  160. soa_num = soa_num + 1
  161. return self.__write_soa_internal(origin_soa, soa_num)
  162. class UpdateSession:
  163. '''Protocol handling for a single dynamic update request.
  164. This class is instantiated with a request message and some other
  165. information that will be used for handling the request. Its main
  166. method, handle(), will process the request, and normally build
  167. a response message according to the result. The application of this
  168. class can use the message to send a response to the client.
  169. '''
  170. def __init__(self, req_message, client_addr, zone_config):
  171. '''Constructor.
  172. Parameters:
  173. - req_message (isc.dns.Message) The request message. This must be
  174. in the PARSE mode, its Opcode must be UPDATE, and must have been
  175. TSIG validatd if it's TSIG signed.
  176. - client_addr (socket address) The address/port of the update client
  177. in the form of Python socket address object. This is mainly for
  178. logging and access control.
  179. - zone_config (ZoneConfig) A tentative container that encapsulates
  180. the server's zone configuration. See zone_config.py.
  181. - req_data (binary) Wire format data of the request message.
  182. It will be used for TSIG verification if necessary.
  183. '''
  184. self.__message = req_message
  185. self.__tsig = req_message.get_tsig_record()
  186. self.__client_addr = client_addr
  187. self.__zone_config = zone_config
  188. self.__added_soa = None
  189. def get_message(self):
  190. '''Return the update message.
  191. After handle() is called, it's generally transformed to the response
  192. to be returned to the client. If the request has been dropped,
  193. this method returns None. If this method is called before handle()
  194. the return value would be identical to the request message passed on
  195. construction, although it's of no practical use.
  196. '''
  197. return self.__message
  198. def handle(self):
  199. '''Handle the update request according to RFC2136.
  200. This method returns a tuple of the following three elements that
  201. indicate the result of the request.
  202. - Result code of the request processing, which are:
  203. UPDATE_SUCCESS Update request granted and succeeded.
  204. UPDATE_ERROR Some error happened to be reported in the response.
  205. UPDATE_DROP Error happened and no response should be sent.
  206. Except the case of UPDATE_DROP, the UpdateSession object will have
  207. created a response that is to be returned to the request client,
  208. which can be retrieved by get_message(). If it's UPDATE_DROP,
  209. subsequent call to get_message() returns None.
  210. - The name of the updated zone (isc.dns.Name object) in case of
  211. UPDATE_SUCCESS; otherwise None.
  212. - The RR class of the updated zone (isc.dns.RRClass object) in case
  213. of UPDATE_SUCCESS; otherwise None.
  214. '''
  215. try:
  216. self._get_update_zone()
  217. # Contrary to what RFC2136 specifies, we do ACL checks before
  218. # prerequisites. It's now generally considered to be a bad
  219. # idea, and actually does harm such as information
  220. # leak. It should make more sense to prevent any security issues
  221. # by performing ACL check as early as possible.
  222. self.__check_update_acl(self.__zname, self.__zclass)
  223. self._create_diff()
  224. prereq_result = self.__check_prerequisites()
  225. if prereq_result != Rcode.NOERROR():
  226. self.__make_response(prereq_result)
  227. return UPDATE_ERROR, self.__zname, self.__zclass
  228. update_result = self.__do_update()
  229. if update_result != Rcode.NOERROR():
  230. self.__make_response(update_result)
  231. return UPDATE_ERROR, self.__zname, self.__zclass
  232. self.__make_response(Rcode.NOERROR())
  233. return UPDATE_SUCCESS, self.__zname, self.__zclass
  234. except UpdateError as e:
  235. if not e.nolog:
  236. logger.debug(logger.DBGLVL_TRACE_BASIC, LIBDDNS_UPDATE_ERROR,
  237. ClientFormatter(self.__client_addr, self.__tsig),
  238. ZoneFormatter(e.zname, e.zclass), e)
  239. # If RCODE is specified, create a corresponding resonse and return
  240. # ERROR; otherwise clear the message and return DROP.
  241. if e.rcode is not None:
  242. self.__make_response(e.rcode)
  243. return UPDATE_ERROR, None, None
  244. self.__message = None
  245. return UPDATE_DROP, None, None
  246. except isc.datasrc.Error as e:
  247. logger.error(LIBDDNS_DATASRC_ERROR,
  248. ClientFormatter(self.__client_addr, self.__tsig), e)
  249. self.__make_response(Rcode.SERVFAIL())
  250. return UPDATE_ERROR, None, None
  251. def _get_update_zone(self):
  252. '''Parse the zone section and find the zone to be updated.
  253. If the zone section is valid and the specified zone is found in
  254. the configuration, sets private member variables for this session:
  255. __datasrc_client: A matching data source that contains the specified
  256. zone
  257. __zname: The zone name as a Name object
  258. __zclass: The zone class as an RRClass object
  259. If this method raises an exception, these members are not set.
  260. Note: This method is protected for ease of use in tests, where
  261. methods are tested that need the setup done here without calling
  262. the full handle() method.
  263. '''
  264. # Validation: the zone section must contain exactly one question,
  265. # and it must be of type SOA.
  266. n_zones = self.__message.get_rr_count(SECTION_ZONE)
  267. if n_zones != 1:
  268. raise UpdateError('Invalid number of records in zone section: ' +
  269. str(n_zones), None, None, Rcode.FORMERR())
  270. zrecord = self.__message.get_question()[0]
  271. if zrecord.get_type() != RRType.SOA():
  272. raise UpdateError('update zone section contains non-SOA',
  273. None, None, Rcode.FORMERR())
  274. # See if we're serving a primary zone specified in the zone section.
  275. zname = zrecord.get_name()
  276. zclass = zrecord.get_class()
  277. zone_type, datasrc_client = self.__zone_config.find_zone(zname, zclass)
  278. if zone_type == isc.ddns.zone_config.ZONE_PRIMARY:
  279. self.__datasrc_client = datasrc_client
  280. self.__zname = zname
  281. self.__zclass = zclass
  282. return
  283. elif zone_type == isc.ddns.zone_config.ZONE_SECONDARY:
  284. # We are a secondary server; since we don't yet support update
  285. # forwarding, we return 'not implemented'.
  286. logger.debug(DBGLVL_TRACE_BASIC, LIBDDNS_UPDATE_FORWARD_FAIL,
  287. ClientFormatter(self.__client_addr, self.__tsig),
  288. ZoneFormatter(zname, zclass))
  289. raise UpdateError('forward', zname, zclass, Rcode.NOTIMP(), True)
  290. # zone wasn't found
  291. logger.debug(DBGLVL_TRACE_BASIC, LIBDDNS_UPDATE_NOTAUTH,
  292. ClientFormatter(self.__client_addr, self.__tsig),
  293. ZoneFormatter(zname, zclass))
  294. raise UpdateError('notauth', zname, zclass, Rcode.NOTAUTH(), True)
  295. def _create_diff(self):
  296. '''
  297. Initializes the internal data structure used for searching current
  298. data and for adding and deleting data. This is supposed to be called
  299. after ACL checks but before prerequisite checks (since the latter
  300. needs the find calls provided by the Diff class).
  301. Adds the private member:
  302. __diff: A buffer of changes made against the zone by this update
  303. This object also contains find() calls, see documentation
  304. of the Diff class.
  305. Note: This method is protected for ease of use in tests, where
  306. methods are tested that need the setup done here without calling
  307. the full handle() method.
  308. '''
  309. self.__diff = isc.xfrin.diff.Diff(self.__datasrc_client,
  310. self.__zname,
  311. journaling=True,
  312. single_update_mode=True)
  313. def __check_update_acl(self, zname, zclass):
  314. '''Apply update ACL for the zone to be updated.'''
  315. acl = self.__zone_config.get_update_acl(zname, zclass)
  316. action = acl.execute(isc.acl.dns.RequestContext(
  317. (self.__client_addr[0], self.__client_addr[1]), self.__tsig))
  318. if action == REJECT:
  319. logger.info(LIBDDNS_UPDATE_DENIED,
  320. ClientFormatter(self.__client_addr, self.__tsig),
  321. ZoneFormatter(zname, zclass))
  322. raise UpdateError('rejected', zname, zclass, Rcode.REFUSED(), True)
  323. if action == DROP:
  324. logger.info(LIBDDNS_UPDATE_DROPPED,
  325. ClientFormatter(self.__client_addr, self.__tsig),
  326. ZoneFormatter(zname, zclass))
  327. raise UpdateError('dropped', zname, zclass, None, True)
  328. logger.debug(logger.DBGLVL_TRACE_BASIC, LIBDDNS_UPDATE_APPROVED,
  329. ClientFormatter(self.__client_addr, self.__tsig),
  330. ZoneFormatter(zname, zclass))
  331. def __make_response(self, rcode):
  332. '''Transform the internal message to the update response.
  333. According RFC2136 Section 3.8, the zone section will be cleared
  334. as well as other sections. The response Rcode will be set to the
  335. given value.
  336. '''
  337. self.__message.make_response()
  338. self.__message.clear_section(SECTION_ZONE)
  339. self.__message.set_rcode(rcode)
  340. def __prereq_rrset_exists(self, rrset):
  341. '''Check whether an rrset with the given name and type exists. Class,
  342. TTL, and Rdata (if any) of the given RRset are ignored.
  343. RFC2136 Section 2.4.1.
  344. Returns True if the prerequisite is satisfied, False otherwise.
  345. Note: the only thing used in the call to find() here is the
  346. result status. The actual data is immediately dropped. As
  347. a future optimization, we may want to add a find() option to
  348. only return what the result code would be (and not read/copy
  349. any actual data).
  350. '''
  351. result, _, _ = self.__diff.find(rrset.get_name(), rrset.get_type())
  352. return result == ZoneFinder.SUCCESS
  353. def __prereq_rrset_exists_value(self, rrset):
  354. '''Check whether an rrset that matches name, type, and rdata(s) of the
  355. given rrset exists.
  356. RFC2136 Section 2.4.2
  357. Returns True if the prerequisite is satisfied, False otherwise.
  358. '''
  359. result, found_rrset, _ = self.__diff.find(rrset.get_name(),
  360. rrset.get_type())
  361. if result == ZoneFinder.SUCCESS and\
  362. rrset.get_name() == found_rrset.get_name() and\
  363. rrset.get_type() == found_rrset.get_type():
  364. # We need to match all actual RRs, unfortunately there is no
  365. # direct order-independent comparison for rrsets, so this
  366. # a slightly inefficient way to handle that.
  367. # shallow copy of the rdata list, so we are sure that this
  368. # loop does not mess with actual data.
  369. found_rdata = copy.copy(found_rrset.get_rdata())
  370. for rdata in rrset.get_rdata():
  371. if rdata in found_rdata:
  372. found_rdata.remove(rdata)
  373. else:
  374. return False
  375. return len(found_rdata) == 0
  376. return False
  377. def __prereq_rrset_does_not_exist(self, rrset):
  378. '''Check whether no rrsets with the same name and type as the given
  379. rrset exist.
  380. RFC2136 Section 2.4.3.
  381. Returns True if the prerequisite is satisfied, False otherwise.
  382. '''
  383. return not self.__prereq_rrset_exists(rrset)
  384. def __prereq_name_in_use(self, rrset):
  385. '''Check whether the name of the given RRset is in use (i.e. has
  386. 1 or more RRs).
  387. RFC2136 Section 2.4.4
  388. Returns True if the prerequisite is satisfied, False otherwise.
  389. Note: the only thing used in the call to find_all() here is
  390. the result status. The actual data is immediately dropped. As
  391. a future optimization, we may want to add a find_all() option
  392. to only return what the result code would be (and not read/copy
  393. any actual data).
  394. '''
  395. result, rrsets, flags = self.__diff.find_all(rrset.get_name())
  396. if result == ZoneFinder.SUCCESS and\
  397. (flags & ZoneFinder.RESULT_WILDCARD == 0):
  398. return True
  399. return False
  400. def __prereq_name_not_in_use(self, rrset):
  401. '''Check whether the name of the given RRset is not in use (i.e. does
  402. not exist at all, or is an empty nonterminal.
  403. RFC2136 Section 2.4.5.
  404. Returns True if the prerequisite is satisfied, False otherwise.
  405. '''
  406. return not self.__prereq_name_in_use(rrset)
  407. def __check_in_zone(self, rrset):
  408. '''Returns true if the name of the given rrset is equal to
  409. or a subdomain of the zname from the Zone Section.'''
  410. relation = rrset.get_name().compare(self.__zname).get_relation()
  411. return relation == NameComparisonResult.SUBDOMAIN or\
  412. relation == NameComparisonResult.EQUAL
  413. def __check_prerequisites(self):
  414. '''Check the prerequisites section of the UPDATE Message.
  415. RFC2136 Section 2.4.
  416. Returns a dns Rcode signaling either no error (Rcode.NOERROR())
  417. or that one of the prerequisites failed (any other Rcode).
  418. '''
  419. # Temporary array to store exact-match RRsets
  420. exact_match_rrsets = []
  421. for rrset in self.__message.get_section(SECTION_PREREQUISITE):
  422. # First check if the name is in the zone
  423. if not self.__check_in_zone(rrset):
  424. logger.info(LIBDDNS_PREREQ_NOTZONE,
  425. ClientFormatter(self.__client_addr),
  426. ZoneFormatter(self.__zname, self.__zclass),
  427. RRsetFormatter(rrset))
  428. return Rcode.NOTZONE()
  429. # Algorithm taken from RFC2136 Section 3.2
  430. if rrset.get_class() == RRClass.ANY():
  431. if rrset.get_ttl().get_value() != 0 or\
  432. rrset.get_rdata_count() != 0:
  433. logger.info(LIBDDNS_PREREQ_FORMERR_ANY,
  434. ClientFormatter(self.__client_addr),
  435. ZoneFormatter(self.__zname, self.__zclass),
  436. RRsetFormatter(rrset))
  437. return Rcode.FORMERR()
  438. elif rrset.get_type() == RRType.ANY():
  439. if not self.__prereq_name_in_use(rrset):
  440. rcode = Rcode.NXDOMAIN()
  441. logger.info(LIBDDNS_PREREQ_NAME_IN_USE_FAILED,
  442. ClientFormatter(self.__client_addr),
  443. ZoneFormatter(self.__zname, self.__zclass),
  444. RRsetFormatter(rrset), rcode)
  445. return rcode
  446. else:
  447. if not self.__prereq_rrset_exists(rrset):
  448. rcode = Rcode.NXRRSET()
  449. logger.info(LIBDDNS_PREREQ_RRSET_EXISTS_FAILED,
  450. ClientFormatter(self.__client_addr),
  451. ZoneFormatter(self.__zname, self.__zclass),
  452. RRsetFormatter(rrset), rcode)
  453. return rcode
  454. elif rrset.get_class() == RRClass.NONE():
  455. if rrset.get_ttl().get_value() != 0 or\
  456. rrset.get_rdata_count() != 0:
  457. logger.info(LIBDDNS_PREREQ_FORMERR_NONE,
  458. ClientFormatter(self.__client_addr),
  459. ZoneFormatter(self.__zname, self.__zclass),
  460. RRsetFormatter(rrset))
  461. return Rcode.FORMERR()
  462. elif rrset.get_type() == RRType.ANY():
  463. if not self.__prereq_name_not_in_use(rrset):
  464. rcode = Rcode.YXDOMAIN()
  465. logger.info(LIBDDNS_PREREQ_NAME_NOT_IN_USE_FAILED,
  466. ClientFormatter(self.__client_addr),
  467. ZoneFormatter(self.__zname, self.__zclass),
  468. RRsetFormatter(rrset), rcode)
  469. return rcode
  470. else:
  471. if not self.__prereq_rrset_does_not_exist(rrset):
  472. rcode = Rcode.YXRRSET()
  473. logger.info(LIBDDNS_PREREQ_RRSET_DOES_NOT_EXIST_FAILED,
  474. ClientFormatter(self.__client_addr),
  475. ZoneFormatter(self.__zname, self.__zclass),
  476. RRsetFormatter(rrset), rcode)
  477. return rcode
  478. elif rrset.get_class() == self.__zclass:
  479. if rrset.get_ttl().get_value() != 0:
  480. logger.info(LIBDDNS_PREREQ_FORMERR,
  481. ClientFormatter(self.__client_addr),
  482. ZoneFormatter(self.__zname, self.__zclass),
  483. RRsetFormatter(rrset))
  484. return Rcode.FORMERR()
  485. else:
  486. collect_rrsets(exact_match_rrsets, rrset)
  487. else:
  488. logger.info(LIBDDNS_PREREQ_FORMERR_CLASS,
  489. ClientFormatter(self.__client_addr),
  490. ZoneFormatter(self.__zname, self.__zclass),
  491. RRsetFormatter(rrset))
  492. return Rcode.FORMERR()
  493. for collected_rrset in exact_match_rrsets:
  494. if not self.__prereq_rrset_exists_value(collected_rrset):
  495. rcode = Rcode.NXRRSET()
  496. logger.info(LIBDDNS_PREREQ_RRSET_EXISTS_VAL_FAILED,
  497. ClientFormatter(self.__client_addr),
  498. ZoneFormatter(self.__zname, self.__zclass),
  499. RRsetFormatter(collected_rrset), rcode)
  500. return rcode
  501. # All prerequisites are satisfied
  502. return Rcode.NOERROR()
  503. def __set_soa_rrset(self, rrset):
  504. '''Sets the given rrset to the member __added_soa (which
  505. is used by __do_update for updating the SOA record'''
  506. self.__added_soa = rrset
  507. def __do_prescan(self):
  508. '''Perform the prescan as defined in RFC2136 section 3.4.1.
  509. This method has a side-effect; it sets self._new_soa if
  510. it encounters the addition of a SOA record in the update
  511. list (so serial can be checked by update later, etc.).
  512. It puts the added SOA in self.__added_soa.
  513. '''
  514. for rrset in self.__message.get_section(SECTION_UPDATE):
  515. if not self.__check_in_zone(rrset):
  516. logger.info(LIBDDNS_UPDATE_NOTZONE,
  517. ClientFormatter(self.__client_addr),
  518. ZoneFormatter(self.__zname, self.__zclass),
  519. RRsetFormatter(rrset))
  520. return Rcode.NOTZONE()
  521. if rrset.get_class() == self.__zclass:
  522. # In fact, all metatypes are in a specific range,
  523. # so one check can test TKEY to ANY
  524. # (some value check is needed anyway, since we do
  525. # not have defined RRtypes for MAILA and MAILB)
  526. if rrset.get_type().get_code() >= 249:
  527. logger.info(LIBDDNS_UPDATE_ADD_BAD_TYPE,
  528. ClientFormatter(self.__client_addr),
  529. ZoneFormatter(self.__zname, self.__zclass),
  530. RRsetFormatter(rrset))
  531. return Rcode.FORMERR()
  532. if rrset.get_type() == RRType.SOA():
  533. # In case there's multiple soa records in the update
  534. # somehow, just take the last
  535. for rr in foreach_rr(rrset):
  536. self.__set_soa_rrset(rr)
  537. elif rrset.get_class() == RRClass.ANY():
  538. if rrset.get_ttl().get_value() != 0:
  539. logger.info(LIBDDNS_UPDATE_DELETE_NONZERO_TTL,
  540. ClientFormatter(self.__client_addr),
  541. ZoneFormatter(self.__zname, self.__zclass),
  542. RRsetFormatter(rrset))
  543. return Rcode.FORMERR()
  544. if rrset.get_rdata_count() > 0:
  545. logger.info(LIBDDNS_UPDATE_DELETE_RRSET_NOT_EMPTY,
  546. ClientFormatter(self.__client_addr),
  547. ZoneFormatter(self.__zname, self.__zclass),
  548. RRsetFormatter(rrset))
  549. return Rcode.FORMERR()
  550. if rrset.get_type().get_code() >= 249 and\
  551. rrset.get_type().get_code() <= 254:
  552. logger.info(LIBDDNS_UPDATE_DELETE_BAD_TYPE,
  553. ClientFormatter(self.__client_addr),
  554. ZoneFormatter(self.__zname, self.__zclass),
  555. RRsetFormatter(rrset))
  556. return Rcode.FORMERR()
  557. elif rrset.get_class() == RRClass.NONE():
  558. if rrset.get_ttl().get_value() != 0:
  559. logger.info(LIBDDNS_UPDATE_DELETE_RR_NONZERO_TTL,
  560. ClientFormatter(self.__client_addr),
  561. ZoneFormatter(self.__zname, self.__zclass),
  562. RRsetFormatter(rrset))
  563. return Rcode.FORMERR()
  564. if rrset.get_type().get_code() >= 249:
  565. logger.info(LIBDDNS_UPDATE_DELETE_RR_BAD_TYPE,
  566. ClientFormatter(self.__client_addr),
  567. ZoneFormatter(self.__zname, self.__zclass),
  568. RRsetFormatter(rrset))
  569. return Rcode.FORMERR()
  570. else:
  571. logger.info(LIBDDNS_UPDATE_BAD_CLASS,
  572. ClientFormatter(self.__client_addr),
  573. ZoneFormatter(self.__zname, self.__zclass),
  574. RRsetFormatter(rrset))
  575. return Rcode.FORMERR()
  576. return Rcode.NOERROR()
  577. def __do_update_add_single_rr(self, rr, existing_rrset):
  578. '''Helper for __do_update_add_rrs_to_rrset: only add the
  579. rr if it is not present yet
  580. (note that rr here should already be a single-rr rrset)
  581. '''
  582. if existing_rrset is None:
  583. self.__diff.add_data(rr)
  584. else:
  585. rr_rdata = rr.get_rdata()[0]
  586. if not rr_rdata in existing_rrset.get_rdata():
  587. self.__diff.add_data(rr)
  588. def __do_update_add_rrs_to_rrset(self, rrset):
  589. '''Add the rrs from the given rrset to the internal diff.
  590. There is handling for a number of special cases mentioned
  591. in RFC2136;
  592. - If the addition is a CNAME, but existing data at its
  593. name is not, the addition is ignored, and vice versa.
  594. - If it is a CNAME, and existing data is too, it is
  595. replaced (existing data is deleted)
  596. An additional restriction is that SOA data is ignored as
  597. well (it is handled separately by the __do_update method).
  598. Note that in the (near) future, this method may have
  599. addition special-cases processing.
  600. '''
  601. # For a number of cases, we may need to remove data in the zone
  602. # (note; SOA is handled separately by __do_update, so that one
  603. # is explicitely ignored here)
  604. if rrset.get_type() == RRType.SOA():
  605. return
  606. result, orig_rrset, _ = self.__diff.find(rrset.get_name(),
  607. rrset.get_type())
  608. if result == ZoneFinder.CNAME:
  609. # Ignore non-cname rrs that try to update CNAME records
  610. # (if rrset itself is a CNAME, the finder result would be
  611. # SUCCESS, see next case)
  612. return
  613. elif result == ZoneFinder.SUCCESS:
  614. # if update is cname, and zone rr is not, ignore
  615. if rrset.get_type() == RRType.CNAME():
  616. # Remove original CNAME record (the new one
  617. # is added below)
  618. self.__diff.delete_data(orig_rrset)
  619. # We do not have WKS support at this time, but if there
  620. # are special Update equality rules such as for WKS, and
  621. # we do have support for the type, this is where the check
  622. # (and potential delete) would go.
  623. elif result == ZoneFinder.NXRRSET:
  624. # There is data present, but not for this type.
  625. # If this type is CNAME, ignore the update
  626. if rrset.get_type() == RRType.CNAME():
  627. return
  628. for rr in foreach_rr(rrset):
  629. self.__do_update_add_single_rr(rr, orig_rrset)
  630. def __do_update_delete_rrset(self, rrset):
  631. '''Deletes the rrset with the name and type of the given
  632. rrset from the zone data (by putting all existing data
  633. in the internal diff as delete statements).
  634. Special cases: if the delete statement is for the
  635. zone's apex, and the type is either SOA or NS, it
  636. is ignored.'''
  637. # find the rrset with local updates
  638. result, to_delete, _ = self.__diff.find_updated(rrset.get_name(),
  639. rrset.get_type())
  640. if result == ZoneFinder.SUCCESS:
  641. if to_delete.get_name() == self.__zname and\
  642. (to_delete.get_type() == RRType.SOA() or\
  643. to_delete.get_type() == RRType.NS()):
  644. # ignore
  645. return
  646. for rr in foreach_rr(to_delete):
  647. self.__diff.delete_data(rr)
  648. def __ns_deleter_helper(self, rrset):
  649. '''Special case helper for deleting NS resource records
  650. at the zone apex. In that scenario, the last NS record
  651. may never be removed (and any action that would do so
  652. should be ignored).
  653. '''
  654. # Find the current NS rrset, including local additions and deletions
  655. result, orig_rrset, _ = self.__diff.find_updated(rrset.get_name(),
  656. rrset.get_type())
  657. # Even a real rrset comparison wouldn't help here...
  658. # The goal is to make sure that after deletion of the
  659. # given rrset, at least 1 NS record is left (at the apex).
  660. # So we make a (shallow) copy of the existing rrset,
  661. # and for each rdata in the to_delete set, we check if it wouldn't
  662. # delete the last one. If it would, that specific one is ignored.
  663. # If it would not, the rdata is removed from the temporary list
  664. orig_rrset_rdata = copy.copy(orig_rrset.get_rdata())
  665. for rdata in rrset.get_rdata():
  666. if len(orig_rrset_rdata) == 1 and rdata == orig_rrset_rdata[0]:
  667. # ignore
  668. continue
  669. else:
  670. # create an individual RRset for deletion
  671. to_delete = isc.dns.RRset(rrset.get_name(),
  672. rrset.get_class(),
  673. rrset.get_type(),
  674. rrset.get_ttl())
  675. to_delete.add_rdata(rdata)
  676. orig_rrset_rdata.remove(rdata)
  677. self.__diff.delete_data(to_delete)
  678. def __do_update_delete_name(self, rrset):
  679. '''Delete all data at the name of the given rrset,
  680. by adding all data found by find_all as delete statements
  681. to the internal diff.
  682. Special case: if the name is the zone's apex, SOA and
  683. NS records are kept.
  684. '''
  685. # Find everything with the name, including local additions
  686. result, rrsets, flags = self.__diff.find_all_updated(rrset.get_name())
  687. if result == ZoneFinder.SUCCESS and\
  688. (flags & ZoneFinder.RESULT_WILDCARD == 0):
  689. for to_delete in rrsets:
  690. # if name == self.__zname and type is soa or ns, don't delete!
  691. if to_delete.get_name() == self.__zname and\
  692. (to_delete.get_type() == RRType.SOA() or
  693. to_delete.get_type() == RRType.NS()):
  694. continue
  695. else:
  696. for rr in foreach_rr(to_delete):
  697. self.__diff.delete_data(rr)
  698. def __do_update_delete_rrs_from_rrset(self, rrset):
  699. '''Deletes all resource records in the given rrset from the
  700. zone. Resource records that do not exist are ignored.
  701. If the rrset if of type SOA, it is ignored.
  702. Uses the __ns_deleter_helper if the rrset's name is the
  703. zone's apex, and the type is NS.
  704. '''
  705. # Delete all rrs in the rrset, except if name=self.__zname and type=soa, or
  706. # type = ns and there is only one left (...)
  707. # The delete does not want class NONE, we would not have gotten here
  708. # if it wasn't, but now is a good time to change it to the zclass.
  709. to_delete = convert_rrset_class(rrset, self.__zclass)
  710. if rrset.get_name() == self.__zname:
  711. if rrset.get_type() == RRType.SOA():
  712. # ignore
  713. return
  714. elif rrset.get_type() == RRType.NS():
  715. # hmm. okay. annoying. There must be at least one left,
  716. # delegate to helper method
  717. self.__ns_deleter_helper(to_delete)
  718. return
  719. for rr in foreach_rr(to_delete):
  720. self.__diff.delete_data(rr)
  721. def __update_soa(self):
  722. '''Checks the member value __added_soa, and depending on
  723. whether it has been set and what its value is, creates
  724. a new SOA if necessary.
  725. Then removes the original SOA and adds the new one,
  726. by adding the needed operations to the internal diff.'''
  727. # Get the existing SOA
  728. # if a new soa was specified, add that one, otherwise, do the
  729. # serial magic and add the newly created one
  730. # get it from DS and to increment and stuff
  731. result, old_soa, _ = self.__diff.find(self.__zname, RRType.SOA(),
  732. ZoneFinder.NO_WILDCARD |
  733. ZoneFinder.FIND_GLUE_OK)
  734. # We may implement recovering from missing SOA data at some point, but
  735. # for now servfail on such a broken state
  736. if result != ZoneFinder.SUCCESS:
  737. raise UpdateError("Error finding SOA record in datasource.",
  738. self.__zname, self.__zclass, Rcode.SERVFAIL())
  739. serial_operation = DDNS_SOA()
  740. if self.__added_soa is not None and\
  741. serial_operation.soa_update_check(old_soa, self.__added_soa):
  742. new_soa = self.__added_soa
  743. else:
  744. # increment goes here
  745. new_soa = serial_operation.update_soa(old_soa)
  746. self.__diff.delete_data(old_soa)
  747. self.__diff.add_data(new_soa)
  748. def __do_update(self):
  749. '''Scan, check, and execute the Update section in the
  750. DDNS Update message.
  751. Returns an Rcode to signal the result (NOERROR upon success,
  752. any error result otherwise).
  753. '''
  754. # prescan
  755. prescan_result = self.__do_prescan()
  756. if prescan_result != Rcode.NOERROR():
  757. return prescan_result
  758. # update
  759. try:
  760. # Do special handling for SOA first
  761. self.__update_soa()
  762. # Algorithm from RFC2136 Section 3.4
  763. # Note that this works on full rrsets, not individual RRs.
  764. # Some checks might be easier with individual RRs, but only if we
  765. # would use the ZoneUpdater directly (so we can query the
  766. # 'zone-as-it-would-be-so-far'. However, due to the current use
  767. # of the Diff class, this is not the case, and therefore it
  768. # is easier to work with full rrsets for the most parts
  769. # (less lookups needed; conversion to individual rrs is
  770. # the same effort whether it is done here or in the several
  771. # do_update statements)
  772. for rrset in self.__message.get_section(SECTION_UPDATE):
  773. if rrset.get_class() == self.__zclass:
  774. self.__do_update_add_rrs_to_rrset(rrset)
  775. elif rrset.get_class() == RRClass.ANY():
  776. if rrset.get_type() == RRType.ANY():
  777. self.__do_update_delete_name(rrset)
  778. else:
  779. self.__do_update_delete_rrset(rrset)
  780. elif rrset.get_class() == RRClass.NONE():
  781. self.__do_update_delete_rrs_from_rrset(rrset)
  782. self.__diff.commit()
  783. return Rcode.NOERROR()
  784. except isc.datasrc.Error as dse:
  785. logger.info(LIBDDNS_UPDATE_DATASRC_ERROR, dse)
  786. return Rcode.SERVFAIL()
  787. except Exception as uce:
  788. logger.error(LIBDDNS_UPDATE_UNCAUGHT_EXCEPTION,
  789. ClientFormatter(self.__client_addr),
  790. ZoneFormatter(self.__zname, self.__zclass),
  791. uce)
  792. return Rcode.SERVFAIL()