You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

683 lines
23 KiB

4 years ago
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2003-2017 Nominum, Inc.
  3. #
  4. # Permission to use, copy, modify, and distribute this software and its
  5. # documentation for any purpose with or without fee is hereby granted,
  6. # provided that the above copyright notice and this permission notice
  7. # appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
  10. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
  12. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """Talk to a DNS server."""
  17. from __future__ import generators
  18. import errno
  19. import select
  20. import socket
  21. import struct
  22. import sys
  23. import time
  24. import dns.exception
  25. import dns.inet
  26. import dns.name
  27. import dns.message
  28. import dns.rcode
  29. import dns.rdataclass
  30. import dns.rdatatype
  31. from ._compat import long, string_types, PY3
  32. if PY3:
  33. select_error = OSError
  34. else:
  35. select_error = select.error
  36. # Function used to create a socket. Can be overridden if needed in special
  37. # situations.
  38. socket_factory = socket.socket
  39. class UnexpectedSource(dns.exception.DNSException):
  40. """A DNS query response came from an unexpected address or port."""
  41. class BadResponse(dns.exception.FormError):
  42. """A DNS query response does not respond to the question asked."""
  43. class TransferError(dns.exception.DNSException):
  44. """A zone transfer response got a non-zero rcode."""
  45. def __init__(self, rcode):
  46. message = 'Zone transfer error: %s' % dns.rcode.to_text(rcode)
  47. super(TransferError, self).__init__(message)
  48. self.rcode = rcode
  49. def _compute_expiration(timeout):
  50. if timeout is None:
  51. return None
  52. else:
  53. return time.time() + timeout
  54. # This module can use either poll() or select() as the "polling backend".
  55. #
  56. # A backend function takes an fd, bools for readability, writablity, and
  57. # error detection, and a timeout.
  58. def _poll_for(fd, readable, writable, error, timeout):
  59. """Poll polling backend."""
  60. event_mask = 0
  61. if readable:
  62. event_mask |= select.POLLIN
  63. if writable:
  64. event_mask |= select.POLLOUT
  65. if error:
  66. event_mask |= select.POLLERR
  67. pollable = select.poll()
  68. pollable.register(fd, event_mask)
  69. if timeout:
  70. event_list = pollable.poll(long(timeout * 1000))
  71. else:
  72. event_list = pollable.poll()
  73. return bool(event_list)
  74. def _select_for(fd, readable, writable, error, timeout):
  75. """Select polling backend."""
  76. rset, wset, xset = [], [], []
  77. if readable:
  78. rset = [fd]
  79. if writable:
  80. wset = [fd]
  81. if error:
  82. xset = [fd]
  83. if timeout is None:
  84. (rcount, wcount, xcount) = select.select(rset, wset, xset)
  85. else:
  86. (rcount, wcount, xcount) = select.select(rset, wset, xset, timeout)
  87. return bool((rcount or wcount or xcount))
  88. def _wait_for(fd, readable, writable, error, expiration):
  89. # Use the selected polling backend to wait for any of the specified
  90. # events. An "expiration" absolute time is converted into a relative
  91. # timeout.
  92. done = False
  93. while not done:
  94. if expiration is None:
  95. timeout = None
  96. else:
  97. timeout = expiration - time.time()
  98. if timeout <= 0.0:
  99. raise dns.exception.Timeout
  100. try:
  101. if not _polling_backend(fd, readable, writable, error, timeout):
  102. raise dns.exception.Timeout
  103. except select_error as e:
  104. if e.args[0] != errno.EINTR:
  105. raise e
  106. done = True
  107. def _set_polling_backend(fn):
  108. # Internal API. Do not use.
  109. global _polling_backend
  110. _polling_backend = fn
  111. if hasattr(select, 'poll'):
  112. # Prefer poll() on platforms that support it because it has no
  113. # limits on the maximum value of a file descriptor (plus it will
  114. # be more efficient for high values).
  115. _polling_backend = _poll_for
  116. else:
  117. _polling_backend = _select_for
  118. def _wait_for_readable(s, expiration):
  119. _wait_for(s, True, False, True, expiration)
  120. def _wait_for_writable(s, expiration):
  121. _wait_for(s, False, True, True, expiration)
  122. def _addresses_equal(af, a1, a2):
  123. # Convert the first value of the tuple, which is a textual format
  124. # address into binary form, so that we are not confused by different
  125. # textual representations of the same address
  126. try:
  127. n1 = dns.inet.inet_pton(af, a1[0])
  128. n2 = dns.inet.inet_pton(af, a2[0])
  129. except dns.exception.SyntaxError:
  130. return False
  131. return n1 == n2 and a1[1:] == a2[1:]
  132. def _destination_and_source(af, where, port, source, source_port):
  133. # Apply defaults and compute destination and source tuples
  134. # suitable for use in connect(), sendto(), or bind().
  135. if af is None:
  136. try:
  137. af = dns.inet.af_for_address(where)
  138. except Exception:
  139. af = dns.inet.AF_INET
  140. if af == dns.inet.AF_INET:
  141. destination = (where, port)
  142. if source is not None or source_port != 0:
  143. if source is None:
  144. source = '0.0.0.0'
  145. source = (source, source_port)
  146. elif af == dns.inet.AF_INET6:
  147. destination = (where, port, 0, 0)
  148. if source is not None or source_port != 0:
  149. if source is None:
  150. source = '::'
  151. source = (source, source_port, 0, 0)
  152. return (af, destination, source)
  153. def send_udp(sock, what, destination, expiration=None):
  154. """Send a DNS message to the specified UDP socket.
  155. *sock*, a ``socket``.
  156. *what*, a ``binary`` or ``dns.message.Message``, the message to send.
  157. *destination*, a destination tuple appropriate for the address family
  158. of the socket, specifying where to send the query.
  159. *expiration*, a ``float`` or ``None``, the absolute time at which
  160. a timeout exception should be raised. If ``None``, no timeout will
  161. occur.
  162. Returns an ``(int, float)`` tuple of bytes sent and the sent time.
  163. """
  164. if isinstance(what, dns.message.Message):
  165. what = what.to_wire()
  166. _wait_for_writable(sock, expiration)
  167. sent_time = time.time()
  168. n = sock.sendto(what, destination)
  169. return (n, sent_time)
  170. def receive_udp(sock, destination, expiration=None,
  171. ignore_unexpected=False, one_rr_per_rrset=False,
  172. keyring=None, request_mac=b'', ignore_trailing=False):
  173. """Read a DNS message from a UDP socket.
  174. *sock*, a ``socket``.
  175. *destination*, a destination tuple appropriate for the address family
  176. of the socket, specifying where the associated query was sent.
  177. *expiration*, a ``float`` or ``None``, the absolute time at which
  178. a timeout exception should be raised. If ``None``, no timeout will
  179. occur.
  180. *ignore_unexpected*, a ``bool``. If ``True``, ignore responses from
  181. unexpected sources.
  182. *one_rr_per_rrset*, a ``bool``. If ``True``, put each RR into its own
  183. RRset.
  184. *keyring*, a ``dict``, the keyring to use for TSIG.
  185. *request_mac*, a ``binary``, the MAC of the request (for TSIG).
  186. *ignore_trailing*, a ``bool``. If ``True``, ignore trailing
  187. junk at end of the received message.
  188. Raises if the message is malformed, if network errors occur, of if
  189. there is a timeout.
  190. Returns a ``dns.message.Message`` object.
  191. """
  192. wire = b''
  193. while 1:
  194. _wait_for_readable(sock, expiration)
  195. (wire, from_address) = sock.recvfrom(65535)
  196. if _addresses_equal(sock.family, from_address, destination) or \
  197. (dns.inet.is_multicast(destination[0]) and
  198. from_address[1:] == destination[1:]):
  199. break
  200. if not ignore_unexpected:
  201. raise UnexpectedSource('got a response from '
  202. '%s instead of %s' % (from_address,
  203. destination))
  204. received_time = time.time()
  205. r = dns.message.from_wire(wire, keyring=keyring, request_mac=request_mac,
  206. one_rr_per_rrset=one_rr_per_rrset,
  207. ignore_trailing=ignore_trailing)
  208. return (r, received_time)
  209. def udp(q, where, timeout=None, port=53, af=None, source=None, source_port=0,
  210. ignore_unexpected=False, one_rr_per_rrset=False, ignore_trailing=False):
  211. """Return the response obtained after sending a query via UDP.
  212. *q*, a ``dns.message.Message``, the query to send
  213. *where*, a ``text`` containing an IPv4 or IPv6 address, where
  214. to send the message.
  215. *timeout*, a ``float`` or ``None``, the number of seconds to wait before the
  216. query times out. If ``None``, the default, wait forever.
  217. *port*, an ``int``, the port send the message to. The default is 53.
  218. *af*, an ``int``, the address family to use. The default is ``None``,
  219. which causes the address family to use to be inferred from the form of
  220. *where*. If the inference attempt fails, AF_INET is used. This
  221. parameter is historical; you need never set it.
  222. *source*, a ``text`` containing an IPv4 or IPv6 address, specifying
  223. the source address. The default is the wildcard address.
  224. *source_port*, an ``int``, the port from which to send the message.
  225. The default is 0.
  226. *ignore_unexpected*, a ``bool``. If ``True``, ignore responses from
  227. unexpected sources.
  228. *one_rr_per_rrset*, a ``bool``. If ``True``, put each RR into its own
  229. RRset.
  230. *ignore_trailing*, a ``bool``. If ``True``, ignore trailing
  231. junk at end of the received message.
  232. Returns a ``dns.message.Message``.
  233. """
  234. wire = q.to_wire()
  235. (af, destination, source) = _destination_and_source(af, where, port,
  236. source, source_port)
  237. s = socket_factory(af, socket.SOCK_DGRAM, 0)
  238. received_time = None
  239. sent_time = None
  240. try:
  241. expiration = _compute_expiration(timeout)
  242. s.setblocking(0)
  243. if source is not None:
  244. s.bind(source)
  245. (_, sent_time) = send_udp(s, wire, destination, expiration)
  246. (r, received_time) = receive_udp(s, destination, expiration,
  247. ignore_unexpected, one_rr_per_rrset,
  248. q.keyring, q.mac, ignore_trailing)
  249. finally:
  250. if sent_time is None or received_time is None:
  251. response_time = 0
  252. else:
  253. response_time = received_time - sent_time
  254. s.close()
  255. r.time = response_time
  256. if not q.is_response(r):
  257. raise BadResponse
  258. return r
  259. def _net_read(sock, count, expiration):
  260. """Read the specified number of bytes from sock. Keep trying until we
  261. either get the desired amount, or we hit EOF.
  262. A Timeout exception will be raised if the operation is not completed
  263. by the expiration time.
  264. """
  265. s = b''
  266. while count > 0:
  267. _wait_for_readable(sock, expiration)
  268. n = sock.recv(count)
  269. if n == b'':
  270. raise EOFError
  271. count = count - len(n)
  272. s = s + n
  273. return s
  274. def _net_write(sock, data, expiration):
  275. """Write the specified data to the socket.
  276. A Timeout exception will be raised if the operation is not completed
  277. by the expiration time.
  278. """
  279. current = 0
  280. l = len(data)
  281. while current < l:
  282. _wait_for_writable(sock, expiration)
  283. current += sock.send(data[current:])
  284. def send_tcp(sock, what, expiration=None):
  285. """Send a DNS message to the specified TCP socket.
  286. *sock*, a ``socket``.
  287. *what*, a ``binary`` or ``dns.message.Message``, the message to send.
  288. *expiration*, a ``float`` or ``None``, the absolute time at which
  289. a timeout exception should be raised. If ``None``, no timeout will
  290. occur.
  291. Returns an ``(int, float)`` tuple of bytes sent and the sent time.
  292. """
  293. if isinstance(what, dns.message.Message):
  294. what = what.to_wire()
  295. l = len(what)
  296. # copying the wire into tcpmsg is inefficient, but lets us
  297. # avoid writev() or doing a short write that would get pushed
  298. # onto the net
  299. tcpmsg = struct.pack("!H", l) + what
  300. _wait_for_writable(sock, expiration)
  301. sent_time = time.time()
  302. _net_write(sock, tcpmsg, expiration)
  303. return (len(tcpmsg), sent_time)
  304. def receive_tcp(sock, expiration=None, one_rr_per_rrset=False,
  305. keyring=None, request_mac=b'', ignore_trailing=False):
  306. """Read a DNS message from a TCP socket.
  307. *sock*, a ``socket``.
  308. *expiration*, a ``float`` or ``None``, the absolute time at which
  309. a timeout exception should be raised. If ``None``, no timeout will
  310. occur.
  311. *one_rr_per_rrset*, a ``bool``. If ``True``, put each RR into its own
  312. RRset.
  313. *keyring*, a ``dict``, the keyring to use for TSIG.
  314. *request_mac*, a ``binary``, the MAC of the request (for TSIG).
  315. *ignore_trailing*, a ``bool``. If ``True``, ignore trailing
  316. junk at end of the received message.
  317. Raises if the message is malformed, if network errors occur, of if
  318. there is a timeout.
  319. Returns a ``dns.message.Message`` object.
  320. """
  321. ldata = _net_read(sock, 2, expiration)
  322. (l,) = struct.unpack("!H", ldata)
  323. wire = _net_read(sock, l, expiration)
  324. received_time = time.time()
  325. r = dns.message.from_wire(wire, keyring=keyring, request_mac=request_mac,
  326. one_rr_per_rrset=one_rr_per_rrset,
  327. ignore_trailing=ignore_trailing)
  328. return (r, received_time)
  329. def _connect(s, address):
  330. try:
  331. s.connect(address)
  332. except socket.error:
  333. (ty, v) = sys.exc_info()[:2]
  334. if hasattr(v, 'errno'):
  335. v_err = v.errno
  336. else:
  337. v_err = v[0]
  338. if v_err not in [errno.EINPROGRESS, errno.EWOULDBLOCK, errno.EALREADY]:
  339. raise v
  340. def tcp(q, where, timeout=None, port=53, af=None, source=None, source_port=0,
  341. one_rr_per_rrset=False, ignore_trailing=False):
  342. """Return the response obtained after sending a query via TCP.
  343. *q*, a ``dns.message.Message``, the query to send
  344. *where*, a ``text`` containing an IPv4 or IPv6 address, where
  345. to send the message.
  346. *timeout*, a ``float`` or ``None``, the number of seconds to wait before the
  347. query times out. If ``None``, the default, wait forever.
  348. *port*, an ``int``, the port send the message to. The default is 53.
  349. *af*, an ``int``, the address family to use. The default is ``None``,
  350. which causes the address family to use to be inferred from the form of
  351. *where*. If the inference attempt fails, AF_INET is used. This
  352. parameter is historical; you need never set it.
  353. *source*, a ``text`` containing an IPv4 or IPv6 address, specifying
  354. the source address. The default is the wildcard address.
  355. *source_port*, an ``int``, the port from which to send the message.
  356. The default is 0.
  357. *one_rr_per_rrset*, a ``bool``. If ``True``, put each RR into its own
  358. RRset.
  359. *ignore_trailing*, a ``bool``. If ``True``, ignore trailing
  360. junk at end of the received message.
  361. Returns a ``dns.message.Message``.
  362. """
  363. wire = q.to_wire()
  364. (af, destination, source) = _destination_and_source(af, where, port,
  365. source, source_port)
  366. s = socket_factory(af, socket.SOCK_STREAM, 0)
  367. begin_time = None
  368. received_time = None
  369. try:
  370. expiration = _compute_expiration(timeout)
  371. s.setblocking(0)
  372. begin_time = time.time()
  373. if source is not None:
  374. s.bind(source)
  375. _connect(s, destination)
  376. send_tcp(s, wire, expiration)
  377. (r, received_time) = receive_tcp(s, expiration, one_rr_per_rrset,
  378. q.keyring, q.mac, ignore_trailing)
  379. finally:
  380. if begin_time is None or received_time is None:
  381. response_time = 0
  382. else:
  383. response_time = received_time - begin_time
  384. s.close()
  385. r.time = response_time
  386. if not q.is_response(r):
  387. raise BadResponse
  388. return r
  389. def xfr(where, zone, rdtype=dns.rdatatype.AXFR, rdclass=dns.rdataclass.IN,
  390. timeout=None, port=53, keyring=None, keyname=None, relativize=True,
  391. af=None, lifetime=None, source=None, source_port=0, serial=0,
  392. use_udp=False, keyalgorithm=dns.tsig.default_algorithm):
  393. """Return a generator for the responses to a zone transfer.
  394. *where*. If the inference attempt fails, AF_INET is used. This
  395. parameter is historical; you need never set it.
  396. *zone*, a ``dns.name.Name`` or ``text``, the name of the zone to transfer.
  397. *rdtype*, an ``int`` or ``text``, the type of zone transfer. The
  398. default is ``dns.rdatatype.AXFR``. ``dns.rdatatype.IXFR`` can be
  399. used to do an incremental transfer instead.
  400. *rdclass*, an ``int`` or ``text``, the class of the zone transfer.
  401. The default is ``dns.rdataclass.IN``.
  402. *timeout*, a ``float``, the number of seconds to wait for each
  403. response message. If None, the default, wait forever.
  404. *port*, an ``int``, the port send the message to. The default is 53.
  405. *keyring*, a ``dict``, the keyring to use for TSIG.
  406. *keyname*, a ``dns.name.Name`` or ``text``, the name of the TSIG
  407. key to use.
  408. *relativize*, a ``bool``. If ``True``, all names in the zone will be
  409. relativized to the zone origin. It is essential that the
  410. relativize setting matches the one specified to
  411. ``dns.zone.from_xfr()`` if using this generator to make a zone.
  412. *af*, an ``int``, the address family to use. The default is ``None``,
  413. which causes the address family to use to be inferred from the form of
  414. *where*. If the inference attempt fails, AF_INET is used. This
  415. parameter is historical; you need never set it.
  416. *lifetime*, a ``float``, the total number of seconds to spend
  417. doing the transfer. If ``None``, the default, then there is no
  418. limit on the time the transfer may take.
  419. *source*, a ``text`` containing an IPv4 or IPv6 address, specifying
  420. the source address. The default is the wildcard address.
  421. *source_port*, an ``int``, the port from which to send the message.
  422. The default is 0.
  423. *serial*, an ``int``, the SOA serial number to use as the base for
  424. an IXFR diff sequence (only meaningful if *rdtype* is
  425. ``dns.rdatatype.IXFR``).
  426. *use_udp*, a ``bool``. If ``True``, use UDP (only meaningful for IXFR).
  427. *keyalgorithm*, a ``dns.name.Name`` or ``text``, the TSIG algorithm to use.
  428. Raises on errors, and so does the generator.
  429. Returns a generator of ``dns.message.Message`` objects.
  430. """
  431. if isinstance(zone, string_types):
  432. zone = dns.name.from_text(zone)
  433. if isinstance(rdtype, string_types):
  434. rdtype = dns.rdatatype.from_text(rdtype)
  435. q = dns.message.make_query(zone, rdtype, rdclass)
  436. if rdtype == dns.rdatatype.IXFR:
  437. rrset = dns.rrset.from_text(zone, 0, 'IN', 'SOA',
  438. '. . %u 0 0 0 0' % serial)
  439. q.authority.append(rrset)
  440. if keyring is not None:
  441. q.use_tsig(keyring, keyname, algorithm=keyalgorithm)
  442. wire = q.to_wire()
  443. (af, destination, source) = _destination_and_source(af, where, port,
  444. source, source_port)
  445. if use_udp:
  446. if rdtype != dns.rdatatype.IXFR:
  447. raise ValueError('cannot do a UDP AXFR')
  448. s = socket_factory(af, socket.SOCK_DGRAM, 0)
  449. else:
  450. s = socket_factory(af, socket.SOCK_STREAM, 0)
  451. s.setblocking(0)
  452. if source is not None:
  453. s.bind(source)
  454. expiration = _compute_expiration(lifetime)
  455. _connect(s, destination)
  456. l = len(wire)
  457. if use_udp:
  458. _wait_for_writable(s, expiration)
  459. s.send(wire)
  460. else:
  461. tcpmsg = struct.pack("!H", l) + wire
  462. _net_write(s, tcpmsg, expiration)
  463. done = False
  464. delete_mode = True
  465. expecting_SOA = False
  466. soa_rrset = None
  467. if relativize:
  468. origin = zone
  469. oname = dns.name.empty
  470. else:
  471. origin = None
  472. oname = zone
  473. tsig_ctx = None
  474. first = True
  475. while not done:
  476. mexpiration = _compute_expiration(timeout)
  477. if mexpiration is None or mexpiration > expiration:
  478. mexpiration = expiration
  479. if use_udp:
  480. _wait_for_readable(s, expiration)
  481. (wire, from_address) = s.recvfrom(65535)
  482. else:
  483. ldata = _net_read(s, 2, mexpiration)
  484. (l,) = struct.unpack("!H", ldata)
  485. wire = _net_read(s, l, mexpiration)
  486. is_ixfr = (rdtype == dns.rdatatype.IXFR)
  487. r = dns.message.from_wire(wire, keyring=q.keyring, request_mac=q.mac,
  488. xfr=True, origin=origin, tsig_ctx=tsig_ctx,
  489. multi=True, first=first,
  490. one_rr_per_rrset=is_ixfr)
  491. rcode = r.rcode()
  492. if rcode != dns.rcode.NOERROR:
  493. raise TransferError(rcode)
  494. tsig_ctx = r.tsig_ctx
  495. first = False
  496. answer_index = 0
  497. if soa_rrset is None:
  498. if not r.answer or r.answer[0].name != oname:
  499. raise dns.exception.FormError(
  500. "No answer or RRset not for qname")
  501. rrset = r.answer[0]
  502. if rrset.rdtype != dns.rdatatype.SOA:
  503. raise dns.exception.FormError("first RRset is not an SOA")
  504. answer_index = 1
  505. soa_rrset = rrset.copy()
  506. if rdtype == dns.rdatatype.IXFR:
  507. if soa_rrset[0].serial <= serial:
  508. #
  509. # We're already up-to-date.
  510. #
  511. done = True
  512. else:
  513. expecting_SOA = True
  514. #
  515. # Process SOAs in the answer section (other than the initial
  516. # SOA in the first message).
  517. #
  518. for rrset in r.answer[answer_index:]:
  519. if done:
  520. raise dns.exception.FormError("answers after final SOA")
  521. if rrset.rdtype == dns.rdatatype.SOA and rrset.name == oname:
  522. if expecting_SOA:
  523. if rrset[0].serial != serial:
  524. raise dns.exception.FormError(
  525. "IXFR base serial mismatch")
  526. expecting_SOA = False
  527. elif rdtype == dns.rdatatype.IXFR:
  528. delete_mode = not delete_mode
  529. #
  530. # If this SOA RRset is equal to the first we saw then we're
  531. # finished. If this is an IXFR we also check that we're seeing
  532. # the record in the expected part of the response.
  533. #
  534. if rrset == soa_rrset and \
  535. (rdtype == dns.rdatatype.AXFR or
  536. (rdtype == dns.rdatatype.IXFR and delete_mode)):
  537. done = True
  538. elif expecting_SOA:
  539. #
  540. # We made an IXFR request and are expecting another
  541. # SOA RR, but saw something else, so this must be an
  542. # AXFR response.
  543. #
  544. rdtype = dns.rdatatype.AXFR
  545. expecting_SOA = False
  546. if done and q.keyring and not r.had_tsig:
  547. raise dns.exception.FormError("missing TSIG")
  548. yield r
  549. s.close()