name.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2001-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. """DNS Names.
  17. """
  18. import copy
  19. import encodings.idna # type: ignore
  20. import functools
  21. import struct
  22. from typing import Any, Callable, Dict, Iterable, Optional, Tuple, Union
  23. import dns._features
  24. import dns.enum
  25. import dns.exception
  26. import dns.immutable
  27. import dns.wire
  28. if dns._features.have("idna"):
  29. import idna # type: ignore
  30. have_idna_2008 = True
  31. else: # pragma: no cover
  32. have_idna_2008 = False
  33. CompressType = Dict["Name", int]
  34. class NameRelation(dns.enum.IntEnum):
  35. """Name relation result from fullcompare()."""
  36. # This is an IntEnum for backwards compatibility in case anyone
  37. # has hardwired the constants.
  38. #: The compared names have no relationship to each other.
  39. NONE = 0
  40. #: the first name is a superdomain of the second.
  41. SUPERDOMAIN = 1
  42. #: The first name is a subdomain of the second.
  43. SUBDOMAIN = 2
  44. #: The compared names are equal.
  45. EQUAL = 3
  46. #: The compared names have a common ancestor.
  47. COMMONANCESTOR = 4
  48. @classmethod
  49. def _maximum(cls):
  50. return cls.COMMONANCESTOR # pragma: no cover
  51. @classmethod
  52. def _short_name(cls):
  53. return cls.__name__ # pragma: no cover
  54. # Backwards compatibility
  55. NAMERELN_NONE = NameRelation.NONE
  56. NAMERELN_SUPERDOMAIN = NameRelation.SUPERDOMAIN
  57. NAMERELN_SUBDOMAIN = NameRelation.SUBDOMAIN
  58. NAMERELN_EQUAL = NameRelation.EQUAL
  59. NAMERELN_COMMONANCESTOR = NameRelation.COMMONANCESTOR
  60. class EmptyLabel(dns.exception.SyntaxError):
  61. """A DNS label is empty."""
  62. class BadEscape(dns.exception.SyntaxError):
  63. """An escaped code in a text format of DNS name is invalid."""
  64. class BadPointer(dns.exception.FormError):
  65. """A DNS compression pointer points forward instead of backward."""
  66. class BadLabelType(dns.exception.FormError):
  67. """The label type in DNS name wire format is unknown."""
  68. class NeedAbsoluteNameOrOrigin(dns.exception.DNSException):
  69. """An attempt was made to convert a non-absolute name to
  70. wire when there was also a non-absolute (or missing) origin."""
  71. class NameTooLong(dns.exception.FormError):
  72. """A DNS name is > 255 octets long."""
  73. class LabelTooLong(dns.exception.SyntaxError):
  74. """A DNS label is > 63 octets long."""
  75. class AbsoluteConcatenation(dns.exception.DNSException):
  76. """An attempt was made to append anything other than the
  77. empty name to an absolute DNS name."""
  78. class NoParent(dns.exception.DNSException):
  79. """An attempt was made to get the parent of the root name
  80. or the empty name."""
  81. class NoIDNA2008(dns.exception.DNSException):
  82. """IDNA 2008 processing was requested but the idna module is not
  83. available."""
  84. class IDNAException(dns.exception.DNSException):
  85. """IDNA processing raised an exception."""
  86. supp_kwargs = {"idna_exception"}
  87. fmt = "IDNA processing exception: {idna_exception}"
  88. # We do this as otherwise mypy complains about unexpected keyword argument
  89. # idna_exception
  90. def __init__(self, *args, **kwargs):
  91. super().__init__(*args, **kwargs)
  92. class NeedSubdomainOfOrigin(dns.exception.DNSException):
  93. """An absolute name was provided that is not a subdomain of the specified origin."""
  94. _escaped = b'"().;\\@$'
  95. _escaped_text = '"().;\\@$'
  96. def _escapify(label: Union[bytes, str]) -> str:
  97. """Escape the characters in label which need it.
  98. @returns: the escaped string
  99. @rtype: string"""
  100. if isinstance(label, bytes):
  101. # Ordinary DNS label mode. Escape special characters and values
  102. # < 0x20 or > 0x7f.
  103. text = ""
  104. for c in label:
  105. if c in _escaped:
  106. text += "\\" + chr(c)
  107. elif c > 0x20 and c < 0x7F:
  108. text += chr(c)
  109. else:
  110. text += "\\%03d" % c
  111. return text
  112. # Unicode label mode. Escape only special characters and values < 0x20
  113. text = ""
  114. for uc in label:
  115. if uc in _escaped_text:
  116. text += "\\" + uc
  117. elif uc <= "\x20":
  118. text += "\\%03d" % ord(uc)
  119. else:
  120. text += uc
  121. return text
  122. class IDNACodec:
  123. """Abstract base class for IDNA encoder/decoders."""
  124. def __init__(self):
  125. pass
  126. def is_idna(self, label: bytes) -> bool:
  127. return label.lower().startswith(b"xn--")
  128. def encode(self, label: str) -> bytes:
  129. raise NotImplementedError # pragma: no cover
  130. def decode(self, label: bytes) -> str:
  131. # We do not apply any IDNA policy on decode.
  132. if self.is_idna(label):
  133. try:
  134. slabel = label[4:].decode("punycode")
  135. return _escapify(slabel)
  136. except Exception as e:
  137. raise IDNAException(idna_exception=e)
  138. else:
  139. return _escapify(label)
  140. class IDNA2003Codec(IDNACodec):
  141. """IDNA 2003 encoder/decoder."""
  142. def __init__(self, strict_decode: bool = False):
  143. """Initialize the IDNA 2003 encoder/decoder.
  144. *strict_decode* is a ``bool``. If `True`, then IDNA2003 checking
  145. is done when decoding. This can cause failures if the name
  146. was encoded with IDNA2008. The default is `False`.
  147. """
  148. super().__init__()
  149. self.strict_decode = strict_decode
  150. def encode(self, label: str) -> bytes:
  151. """Encode *label*."""
  152. if label == "":
  153. return b""
  154. try:
  155. return encodings.idna.ToASCII(label)
  156. except UnicodeError:
  157. raise LabelTooLong
  158. def decode(self, label: bytes) -> str:
  159. """Decode *label*."""
  160. if not self.strict_decode:
  161. return super().decode(label)
  162. if label == b"":
  163. return ""
  164. try:
  165. return _escapify(encodings.idna.ToUnicode(label))
  166. except Exception as e:
  167. raise IDNAException(idna_exception=e)
  168. class IDNA2008Codec(IDNACodec):
  169. """IDNA 2008 encoder/decoder."""
  170. def __init__(
  171. self,
  172. uts_46: bool = False,
  173. transitional: bool = False,
  174. allow_pure_ascii: bool = False,
  175. strict_decode: bool = False,
  176. ):
  177. """Initialize the IDNA 2008 encoder/decoder.
  178. *uts_46* is a ``bool``. If True, apply Unicode IDNA
  179. compatibility processing as described in Unicode Technical
  180. Standard #46 (https://unicode.org/reports/tr46/).
  181. If False, do not apply the mapping. The default is False.
  182. *transitional* is a ``bool``: If True, use the
  183. "transitional" mode described in Unicode Technical Standard
  184. #46. The default is False.
  185. *allow_pure_ascii* is a ``bool``. If True, then a label which
  186. consists of only ASCII characters is allowed. This is less
  187. strict than regular IDNA 2008, but is also necessary for mixed
  188. names, e.g. a name with starting with "_sip._tcp." and ending
  189. in an IDN suffix which would otherwise be disallowed. The
  190. default is False.
  191. *strict_decode* is a ``bool``: If True, then IDNA2008 checking
  192. is done when decoding. This can cause failures if the name
  193. was encoded with IDNA2003. The default is False.
  194. """
  195. super().__init__()
  196. self.uts_46 = uts_46
  197. self.transitional = transitional
  198. self.allow_pure_ascii = allow_pure_ascii
  199. self.strict_decode = strict_decode
  200. def encode(self, label: str) -> bytes:
  201. if label == "":
  202. return b""
  203. if self.allow_pure_ascii and is_all_ascii(label):
  204. encoded = label.encode("ascii")
  205. if len(encoded) > 63:
  206. raise LabelTooLong
  207. return encoded
  208. if not have_idna_2008:
  209. raise NoIDNA2008
  210. try:
  211. if self.uts_46:
  212. # pylint: disable=possibly-used-before-assignment
  213. label = idna.uts46_remap(label, False, self.transitional)
  214. return idna.alabel(label)
  215. except idna.IDNAError as e:
  216. if e.args[0] == "Label too long":
  217. raise LabelTooLong
  218. else:
  219. raise IDNAException(idna_exception=e)
  220. def decode(self, label: bytes) -> str:
  221. if not self.strict_decode:
  222. return super().decode(label)
  223. if label == b"":
  224. return ""
  225. if not have_idna_2008:
  226. raise NoIDNA2008
  227. try:
  228. ulabel = idna.ulabel(label)
  229. if self.uts_46:
  230. ulabel = idna.uts46_remap(ulabel, False, self.transitional)
  231. return _escapify(ulabel)
  232. except (idna.IDNAError, UnicodeError) as e:
  233. raise IDNAException(idna_exception=e)
  234. IDNA_2003_Practical = IDNA2003Codec(False)
  235. IDNA_2003_Strict = IDNA2003Codec(True)
  236. IDNA_2003 = IDNA_2003_Practical
  237. IDNA_2008_Practical = IDNA2008Codec(True, False, True, False)
  238. IDNA_2008_UTS_46 = IDNA2008Codec(True, False, False, False)
  239. IDNA_2008_Strict = IDNA2008Codec(False, False, False, True)
  240. IDNA_2008_Transitional = IDNA2008Codec(True, True, False, False)
  241. IDNA_2008 = IDNA_2008_Practical
  242. def _validate_labels(labels: Tuple[bytes, ...]) -> None:
  243. """Check for empty labels in the middle of a label sequence,
  244. labels that are too long, and for too many labels.
  245. Raises ``dns.name.NameTooLong`` if the name as a whole is too long.
  246. Raises ``dns.name.EmptyLabel`` if a label is empty (i.e. the root
  247. label) and appears in a position other than the end of the label
  248. sequence
  249. """
  250. l = len(labels)
  251. total = 0
  252. i = -1
  253. j = 0
  254. for label in labels:
  255. ll = len(label)
  256. total += ll + 1
  257. if ll > 63:
  258. raise LabelTooLong
  259. if i < 0 and label == b"":
  260. i = j
  261. j += 1
  262. if total > 255:
  263. raise NameTooLong
  264. if i >= 0 and i != l - 1:
  265. raise EmptyLabel
  266. def _maybe_convert_to_binary(label: Union[bytes, str]) -> bytes:
  267. """If label is ``str``, convert it to ``bytes``. If it is already
  268. ``bytes`` just return it.
  269. """
  270. if isinstance(label, bytes):
  271. return label
  272. if isinstance(label, str):
  273. return label.encode()
  274. raise ValueError # pragma: no cover
  275. @dns.immutable.immutable
  276. class Name:
  277. """A DNS name.
  278. The dns.name.Name class represents a DNS name as a tuple of
  279. labels. Each label is a ``bytes`` in DNS wire format. Instances
  280. of the class are immutable.
  281. """
  282. __slots__ = ["labels"]
  283. def __init__(self, labels: Iterable[Union[bytes, str]]):
  284. """*labels* is any iterable whose values are ``str`` or ``bytes``."""
  285. blabels = [_maybe_convert_to_binary(x) for x in labels]
  286. self.labels = tuple(blabels)
  287. _validate_labels(self.labels)
  288. def __copy__(self):
  289. return Name(self.labels)
  290. def __deepcopy__(self, memo):
  291. return Name(copy.deepcopy(self.labels, memo))
  292. def __getstate__(self):
  293. # Names can be pickled
  294. return {"labels": self.labels}
  295. def __setstate__(self, state):
  296. super().__setattr__("labels", state["labels"])
  297. _validate_labels(self.labels)
  298. def is_absolute(self) -> bool:
  299. """Is the most significant label of this name the root label?
  300. Returns a ``bool``.
  301. """
  302. return len(self.labels) > 0 and self.labels[-1] == b""
  303. def is_wild(self) -> bool:
  304. """Is this name wild? (I.e. Is the least significant label '*'?)
  305. Returns a ``bool``.
  306. """
  307. return len(self.labels) > 0 and self.labels[0] == b"*"
  308. def __hash__(self) -> int:
  309. """Return a case-insensitive hash of the name.
  310. Returns an ``int``.
  311. """
  312. h = 0
  313. for label in self.labels:
  314. for c in label.lower():
  315. h += (h << 3) + c
  316. return h
  317. def fullcompare(self, other: "Name") -> Tuple[NameRelation, int, int]:
  318. """Compare two names, returning a 3-tuple
  319. ``(relation, order, nlabels)``.
  320. *relation* describes the relation ship between the names,
  321. and is one of: ``dns.name.NameRelation.NONE``,
  322. ``dns.name.NameRelation.SUPERDOMAIN``, ``dns.name.NameRelation.SUBDOMAIN``,
  323. ``dns.name.NameRelation.EQUAL``, or ``dns.name.NameRelation.COMMONANCESTOR``.
  324. *order* is < 0 if *self* < *other*, > 0 if *self* > *other*, and ==
  325. 0 if *self* == *other*. A relative name is always less than an
  326. absolute name. If both names have the same relativity, then
  327. the DNSSEC order relation is used to order them.
  328. *nlabels* is the number of significant labels that the two names
  329. have in common.
  330. Here are some examples. Names ending in "." are absolute names,
  331. those not ending in "." are relative names.
  332. ============= ============= =========== ===== =======
  333. self other relation order nlabels
  334. ============= ============= =========== ===== =======
  335. www.example. www.example. equal 0 3
  336. www.example. example. subdomain > 0 2
  337. example. www.example. superdomain < 0 2
  338. example1.com. example2.com. common anc. < 0 2
  339. example1 example2. none < 0 0
  340. example1. example2 none > 0 0
  341. ============= ============= =========== ===== =======
  342. """
  343. sabs = self.is_absolute()
  344. oabs = other.is_absolute()
  345. if sabs != oabs:
  346. if sabs:
  347. return (NameRelation.NONE, 1, 0)
  348. else:
  349. return (NameRelation.NONE, -1, 0)
  350. l1 = len(self.labels)
  351. l2 = len(other.labels)
  352. ldiff = l1 - l2
  353. if ldiff < 0:
  354. l = l1
  355. else:
  356. l = l2
  357. order = 0
  358. nlabels = 0
  359. namereln = NameRelation.NONE
  360. while l > 0:
  361. l -= 1
  362. l1 -= 1
  363. l2 -= 1
  364. label1 = self.labels[l1].lower()
  365. label2 = other.labels[l2].lower()
  366. if label1 < label2:
  367. order = -1
  368. if nlabels > 0:
  369. namereln = NameRelation.COMMONANCESTOR
  370. return (namereln, order, nlabels)
  371. elif label1 > label2:
  372. order = 1
  373. if nlabels > 0:
  374. namereln = NameRelation.COMMONANCESTOR
  375. return (namereln, order, nlabels)
  376. nlabels += 1
  377. order = ldiff
  378. if ldiff < 0:
  379. namereln = NameRelation.SUPERDOMAIN
  380. elif ldiff > 0:
  381. namereln = NameRelation.SUBDOMAIN
  382. else:
  383. namereln = NameRelation.EQUAL
  384. return (namereln, order, nlabels)
  385. def is_subdomain(self, other: "Name") -> bool:
  386. """Is self a subdomain of other?
  387. Note that the notion of subdomain includes equality, e.g.
  388. "dnspython.org" is a subdomain of itself.
  389. Returns a ``bool``.
  390. """
  391. (nr, _, _) = self.fullcompare(other)
  392. if nr == NameRelation.SUBDOMAIN or nr == NameRelation.EQUAL:
  393. return True
  394. return False
  395. def is_superdomain(self, other: "Name") -> bool:
  396. """Is self a superdomain of other?
  397. Note that the notion of superdomain includes equality, e.g.
  398. "dnspython.org" is a superdomain of itself.
  399. Returns a ``bool``.
  400. """
  401. (nr, _, _) = self.fullcompare(other)
  402. if nr == NameRelation.SUPERDOMAIN or nr == NameRelation.EQUAL:
  403. return True
  404. return False
  405. def canonicalize(self) -> "Name":
  406. """Return a name which is equal to the current name, but is in
  407. DNSSEC canonical form.
  408. """
  409. return Name([x.lower() for x in self.labels])
  410. def __eq__(self, other):
  411. if isinstance(other, Name):
  412. return self.fullcompare(other)[1] == 0
  413. else:
  414. return False
  415. def __ne__(self, other):
  416. if isinstance(other, Name):
  417. return self.fullcompare(other)[1] != 0
  418. else:
  419. return True
  420. def __lt__(self, other):
  421. if isinstance(other, Name):
  422. return self.fullcompare(other)[1] < 0
  423. else:
  424. return NotImplemented
  425. def __le__(self, other):
  426. if isinstance(other, Name):
  427. return self.fullcompare(other)[1] <= 0
  428. else:
  429. return NotImplemented
  430. def __ge__(self, other):
  431. if isinstance(other, Name):
  432. return self.fullcompare(other)[1] >= 0
  433. else:
  434. return NotImplemented
  435. def __gt__(self, other):
  436. if isinstance(other, Name):
  437. return self.fullcompare(other)[1] > 0
  438. else:
  439. return NotImplemented
  440. def __repr__(self):
  441. return "<DNS name " + self.__str__() + ">"
  442. def __str__(self):
  443. return self.to_text(False)
  444. def to_text(self, omit_final_dot: bool = False) -> str:
  445. """Convert name to DNS text format.
  446. *omit_final_dot* is a ``bool``. If True, don't emit the final
  447. dot (denoting the root label) for absolute names. The default
  448. is False.
  449. Returns a ``str``.
  450. """
  451. if len(self.labels) == 0:
  452. return "@"
  453. if len(self.labels) == 1 and self.labels[0] == b"":
  454. return "."
  455. if omit_final_dot and self.is_absolute():
  456. l = self.labels[:-1]
  457. else:
  458. l = self.labels
  459. s = ".".join(map(_escapify, l))
  460. return s
  461. def to_unicode(
  462. self, omit_final_dot: bool = False, idna_codec: Optional[IDNACodec] = None
  463. ) -> str:
  464. """Convert name to Unicode text format.
  465. IDN ACE labels are converted to Unicode.
  466. *omit_final_dot* is a ``bool``. If True, don't emit the final
  467. dot (denoting the root label) for absolute names. The default
  468. is False.
  469. *idna_codec* specifies the IDNA encoder/decoder. If None, the
  470. dns.name.IDNA_2003_Practical encoder/decoder is used.
  471. The IDNA_2003_Practical decoder does
  472. not impose any policy, it just decodes punycode, so if you
  473. don't want checking for compliance, you can use this decoder
  474. for IDNA2008 as well.
  475. Returns a ``str``.
  476. """
  477. if len(self.labels) == 0:
  478. return "@"
  479. if len(self.labels) == 1 and self.labels[0] == b"":
  480. return "."
  481. if omit_final_dot and self.is_absolute():
  482. l = self.labels[:-1]
  483. else:
  484. l = self.labels
  485. if idna_codec is None:
  486. idna_codec = IDNA_2003_Practical
  487. return ".".join([idna_codec.decode(x) for x in l])
  488. def to_digestable(self, origin: Optional["Name"] = None) -> bytes:
  489. """Convert name to a format suitable for digesting in hashes.
  490. The name is canonicalized and converted to uncompressed wire
  491. format. All names in wire format are absolute. If the name
  492. is a relative name, then an origin must be supplied.
  493. *origin* is a ``dns.name.Name`` or ``None``. If the name is
  494. relative and origin is not ``None``, then origin will be appended
  495. to the name.
  496. Raises ``dns.name.NeedAbsoluteNameOrOrigin`` if the name is
  497. relative and no origin was provided.
  498. Returns a ``bytes``.
  499. """
  500. digest = self.to_wire(origin=origin, canonicalize=True)
  501. assert digest is not None
  502. return digest
  503. def to_wire(
  504. self,
  505. file: Optional[Any] = None,
  506. compress: Optional[CompressType] = None,
  507. origin: Optional["Name"] = None,
  508. canonicalize: bool = False,
  509. ) -> Optional[bytes]:
  510. """Convert name to wire format, possibly compressing it.
  511. *file* is the file where the name is emitted (typically an
  512. io.BytesIO file). If ``None`` (the default), a ``bytes``
  513. containing the wire name will be returned.
  514. *compress*, a ``dict``, is the compression table to use. If
  515. ``None`` (the default), names will not be compressed. Note that
  516. the compression code assumes that compression offset 0 is the
  517. start of *file*, and thus compression will not be correct
  518. if this is not the case.
  519. *origin* is a ``dns.name.Name`` or ``None``. If the name is
  520. relative and origin is not ``None``, then *origin* will be appended
  521. to it.
  522. *canonicalize*, a ``bool``, indicates whether the name should
  523. be canonicalized; that is, converted to a format suitable for
  524. digesting in hashes.
  525. Raises ``dns.name.NeedAbsoluteNameOrOrigin`` if the name is
  526. relative and no origin was provided.
  527. Returns a ``bytes`` or ``None``.
  528. """
  529. if file is None:
  530. out = bytearray()
  531. for label in self.labels:
  532. out.append(len(label))
  533. if canonicalize:
  534. out += label.lower()
  535. else:
  536. out += label
  537. if not self.is_absolute():
  538. if origin is None or not origin.is_absolute():
  539. raise NeedAbsoluteNameOrOrigin
  540. for label in origin.labels:
  541. out.append(len(label))
  542. if canonicalize:
  543. out += label.lower()
  544. else:
  545. out += label
  546. return bytes(out)
  547. labels: Iterable[bytes]
  548. if not self.is_absolute():
  549. if origin is None or not origin.is_absolute():
  550. raise NeedAbsoluteNameOrOrigin
  551. labels = list(self.labels)
  552. labels.extend(list(origin.labels))
  553. else:
  554. labels = self.labels
  555. i = 0
  556. for label in labels:
  557. n = Name(labels[i:])
  558. i += 1
  559. if compress is not None:
  560. pos = compress.get(n)
  561. else:
  562. pos = None
  563. if pos is not None:
  564. value = 0xC000 + pos
  565. s = struct.pack("!H", value)
  566. file.write(s)
  567. break
  568. else:
  569. if compress is not None and len(n) > 1:
  570. pos = file.tell()
  571. if pos <= 0x3FFF:
  572. compress[n] = pos
  573. l = len(label)
  574. file.write(struct.pack("!B", l))
  575. if l > 0:
  576. if canonicalize:
  577. file.write(label.lower())
  578. else:
  579. file.write(label)
  580. return None
  581. def __len__(self) -> int:
  582. """The length of the name (in labels).
  583. Returns an ``int``.
  584. """
  585. return len(self.labels)
  586. def __getitem__(self, index):
  587. return self.labels[index]
  588. def __add__(self, other):
  589. return self.concatenate(other)
  590. def __sub__(self, other):
  591. return self.relativize(other)
  592. def split(self, depth: int) -> Tuple["Name", "Name"]:
  593. """Split a name into a prefix and suffix names at the specified depth.
  594. *depth* is an ``int`` specifying the number of labels in the suffix
  595. Raises ``ValueError`` if *depth* was not >= 0 and <= the length of the
  596. name.
  597. Returns the tuple ``(prefix, suffix)``.
  598. """
  599. l = len(self.labels)
  600. if depth == 0:
  601. return (self, dns.name.empty)
  602. elif depth == l:
  603. return (dns.name.empty, self)
  604. elif depth < 0 or depth > l:
  605. raise ValueError("depth must be >= 0 and <= the length of the name")
  606. return (Name(self[:-depth]), Name(self[-depth:]))
  607. def concatenate(self, other: "Name") -> "Name":
  608. """Return a new name which is the concatenation of self and other.
  609. Raises ``dns.name.AbsoluteConcatenation`` if the name is
  610. absolute and *other* is not the empty name.
  611. Returns a ``dns.name.Name``.
  612. """
  613. if self.is_absolute() and len(other) > 0:
  614. raise AbsoluteConcatenation
  615. labels = list(self.labels)
  616. labels.extend(list(other.labels))
  617. return Name(labels)
  618. def relativize(self, origin: "Name") -> "Name":
  619. """If the name is a subdomain of *origin*, return a new name which is
  620. the name relative to origin. Otherwise return the name.
  621. For example, relativizing ``www.dnspython.org.`` to origin
  622. ``dnspython.org.`` returns the name ``www``. Relativizing ``example.``
  623. to origin ``dnspython.org.`` returns ``example.``.
  624. Returns a ``dns.name.Name``.
  625. """
  626. if origin is not None and self.is_subdomain(origin):
  627. return Name(self[: -len(origin)])
  628. else:
  629. return self
  630. def derelativize(self, origin: "Name") -> "Name":
  631. """If the name is a relative name, return a new name which is the
  632. concatenation of the name and origin. Otherwise return the name.
  633. For example, derelativizing ``www`` to origin ``dnspython.org.``
  634. returns the name ``www.dnspython.org.``. Derelativizing ``example.``
  635. to origin ``dnspython.org.`` returns ``example.``.
  636. Returns a ``dns.name.Name``.
  637. """
  638. if not self.is_absolute():
  639. return self.concatenate(origin)
  640. else:
  641. return self
  642. def choose_relativity(
  643. self, origin: Optional["Name"] = None, relativize: bool = True
  644. ) -> "Name":
  645. """Return a name with the relativity desired by the caller.
  646. If *origin* is ``None``, then the name is returned.
  647. Otherwise, if *relativize* is ``True`` the name is
  648. relativized, and if *relativize* is ``False`` the name is
  649. derelativized.
  650. Returns a ``dns.name.Name``.
  651. """
  652. if origin:
  653. if relativize:
  654. return self.relativize(origin)
  655. else:
  656. return self.derelativize(origin)
  657. else:
  658. return self
  659. def parent(self) -> "Name":
  660. """Return the parent of the name.
  661. For example, the parent of ``www.dnspython.org.`` is ``dnspython.org``.
  662. Raises ``dns.name.NoParent`` if the name is either the root name or the
  663. empty name, and thus has no parent.
  664. Returns a ``dns.name.Name``.
  665. """
  666. if self == root or self == empty:
  667. raise NoParent
  668. return Name(self.labels[1:])
  669. def predecessor(self, origin: "Name", prefix_ok: bool = True) -> "Name":
  670. """Return the maximal predecessor of *name* in the DNSSEC ordering in the zone
  671. whose origin is *origin*, or return the longest name under *origin* if the
  672. name is origin (i.e. wrap around to the longest name, which may still be
  673. *origin* due to length considerations.
  674. The relativity of the name is preserved, so if this name is relative
  675. then the method will return a relative name, and likewise if this name
  676. is absolute then the predecessor will be absolute.
  677. *prefix_ok* indicates if prefixing labels is allowed, and
  678. defaults to ``True``. Normally it is good to allow this, but if computing
  679. a maximal predecessor at a zone cut point then ``False`` must be specified.
  680. """
  681. return _handle_relativity_and_call(
  682. _absolute_predecessor, self, origin, prefix_ok
  683. )
  684. def successor(self, origin: "Name", prefix_ok: bool = True) -> "Name":
  685. """Return the minimal successor of *name* in the DNSSEC ordering in the zone
  686. whose origin is *origin*, or return *origin* if the successor cannot be
  687. computed due to name length limitations.
  688. Note that *origin* is returned in the "too long" cases because wrapping
  689. around to the origin is how NSEC records express "end of the zone".
  690. The relativity of the name is preserved, so if this name is relative
  691. then the method will return a relative name, and likewise if this name
  692. is absolute then the successor will be absolute.
  693. *prefix_ok* indicates if prefixing a new minimal label is allowed, and
  694. defaults to ``True``. Normally it is good to allow this, but if computing
  695. a minimal successor at a zone cut point then ``False`` must be specified.
  696. """
  697. return _handle_relativity_and_call(_absolute_successor, self, origin, prefix_ok)
  698. #: The root name, '.'
  699. root = Name([b""])
  700. #: The empty name.
  701. empty = Name([])
  702. def from_unicode(
  703. text: str, origin: Optional[Name] = root, idna_codec: Optional[IDNACodec] = None
  704. ) -> Name:
  705. """Convert unicode text into a Name object.
  706. Labels are encoded in IDN ACE form according to rules specified by
  707. the IDNA codec.
  708. *text*, a ``str``, is the text to convert into a name.
  709. *origin*, a ``dns.name.Name``, specifies the origin to
  710. append to non-absolute names. The default is the root name.
  711. *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
  712. encoder/decoder. If ``None``, the default IDNA 2003 encoder/decoder
  713. is used.
  714. Returns a ``dns.name.Name``.
  715. """
  716. if not isinstance(text, str):
  717. raise ValueError("input to from_unicode() must be a unicode string")
  718. if not (origin is None or isinstance(origin, Name)):
  719. raise ValueError("origin must be a Name or None")
  720. labels = []
  721. label = ""
  722. escaping = False
  723. edigits = 0
  724. total = 0
  725. if idna_codec is None:
  726. idna_codec = IDNA_2003
  727. if text == "@":
  728. text = ""
  729. if text:
  730. if text in [".", "\u3002", "\uff0e", "\uff61"]:
  731. return Name([b""]) # no Unicode "u" on this constant!
  732. for c in text:
  733. if escaping:
  734. if edigits == 0:
  735. if c.isdigit():
  736. total = int(c)
  737. edigits += 1
  738. else:
  739. label += c
  740. escaping = False
  741. else:
  742. if not c.isdigit():
  743. raise BadEscape
  744. total *= 10
  745. total += int(c)
  746. edigits += 1
  747. if edigits == 3:
  748. escaping = False
  749. label += chr(total)
  750. elif c in [".", "\u3002", "\uff0e", "\uff61"]:
  751. if len(label) == 0:
  752. raise EmptyLabel
  753. labels.append(idna_codec.encode(label))
  754. label = ""
  755. elif c == "\\":
  756. escaping = True
  757. edigits = 0
  758. total = 0
  759. else:
  760. label += c
  761. if escaping:
  762. raise BadEscape
  763. if len(label) > 0:
  764. labels.append(idna_codec.encode(label))
  765. else:
  766. labels.append(b"")
  767. if (len(labels) == 0 or labels[-1] != b"") and origin is not None:
  768. labels.extend(list(origin.labels))
  769. return Name(labels)
  770. def is_all_ascii(text: str) -> bool:
  771. for c in text:
  772. if ord(c) > 0x7F:
  773. return False
  774. return True
  775. def from_text(
  776. text: Union[bytes, str],
  777. origin: Optional[Name] = root,
  778. idna_codec: Optional[IDNACodec] = None,
  779. ) -> Name:
  780. """Convert text into a Name object.
  781. *text*, a ``bytes`` or ``str``, is the text to convert into a name.
  782. *origin*, a ``dns.name.Name``, specifies the origin to
  783. append to non-absolute names. The default is the root name.
  784. *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
  785. encoder/decoder. If ``None``, the default IDNA 2003 encoder/decoder
  786. is used.
  787. Returns a ``dns.name.Name``.
  788. """
  789. if isinstance(text, str):
  790. if not is_all_ascii(text):
  791. # Some codepoint in the input text is > 127, so IDNA applies.
  792. return from_unicode(text, origin, idna_codec)
  793. # The input is all ASCII, so treat this like an ordinary non-IDNA
  794. # domain name. Note that "all ASCII" is about the input text,
  795. # not the codepoints in the domain name. E.g. if text has value
  796. #
  797. # r'\150\151\152\153\154\155\156\157\158\159'
  798. #
  799. # then it's still "all ASCII" even though the domain name has
  800. # codepoints > 127.
  801. text = text.encode("ascii")
  802. if not isinstance(text, bytes):
  803. raise ValueError("input to from_text() must be a string")
  804. if not (origin is None or isinstance(origin, Name)):
  805. raise ValueError("origin must be a Name or None")
  806. labels = []
  807. label = b""
  808. escaping = False
  809. edigits = 0
  810. total = 0
  811. if text == b"@":
  812. text = b""
  813. if text:
  814. if text == b".":
  815. return Name([b""])
  816. for c in text:
  817. byte_ = struct.pack("!B", c)
  818. if escaping:
  819. if edigits == 0:
  820. if byte_.isdigit():
  821. total = int(byte_)
  822. edigits += 1
  823. else:
  824. label += byte_
  825. escaping = False
  826. else:
  827. if not byte_.isdigit():
  828. raise BadEscape
  829. total *= 10
  830. total += int(byte_)
  831. edigits += 1
  832. if edigits == 3:
  833. escaping = False
  834. label += struct.pack("!B", total)
  835. elif byte_ == b".":
  836. if len(label) == 0:
  837. raise EmptyLabel
  838. labels.append(label)
  839. label = b""
  840. elif byte_ == b"\\":
  841. escaping = True
  842. edigits = 0
  843. total = 0
  844. else:
  845. label += byte_
  846. if escaping:
  847. raise BadEscape
  848. if len(label) > 0:
  849. labels.append(label)
  850. else:
  851. labels.append(b"")
  852. if (len(labels) == 0 or labels[-1] != b"") and origin is not None:
  853. labels.extend(list(origin.labels))
  854. return Name(labels)
  855. # we need 'dns.wire.Parser' quoted as dns.name and dns.wire depend on each other.
  856. def from_wire_parser(parser: "dns.wire.Parser") -> Name:
  857. """Convert possibly compressed wire format into a Name.
  858. *parser* is a dns.wire.Parser.
  859. Raises ``dns.name.BadPointer`` if a compression pointer did not
  860. point backwards in the message.
  861. Raises ``dns.name.BadLabelType`` if an invalid label type was encountered.
  862. Returns a ``dns.name.Name``
  863. """
  864. labels = []
  865. biggest_pointer = parser.current
  866. with parser.restore_furthest():
  867. count = parser.get_uint8()
  868. while count != 0:
  869. if count < 64:
  870. labels.append(parser.get_bytes(count))
  871. elif count >= 192:
  872. current = (count & 0x3F) * 256 + parser.get_uint8()
  873. if current >= biggest_pointer:
  874. raise BadPointer
  875. biggest_pointer = current
  876. parser.seek(current)
  877. else:
  878. raise BadLabelType
  879. count = parser.get_uint8()
  880. labels.append(b"")
  881. return Name(labels)
  882. def from_wire(message: bytes, current: int) -> Tuple[Name, int]:
  883. """Convert possibly compressed wire format into a Name.
  884. *message* is a ``bytes`` containing an entire DNS message in DNS
  885. wire form.
  886. *current*, an ``int``, is the offset of the beginning of the name
  887. from the start of the message
  888. Raises ``dns.name.BadPointer`` if a compression pointer did not
  889. point backwards in the message.
  890. Raises ``dns.name.BadLabelType`` if an invalid label type was encountered.
  891. Returns a ``(dns.name.Name, int)`` tuple consisting of the name
  892. that was read and the number of bytes of the wire format message
  893. which were consumed reading it.
  894. """
  895. if not isinstance(message, bytes):
  896. raise ValueError("input to from_wire() must be a byte string")
  897. parser = dns.wire.Parser(message, current)
  898. name = from_wire_parser(parser)
  899. return (name, parser.current - current)
  900. # RFC 4471 Support
  901. _MINIMAL_OCTET = b"\x00"
  902. _MINIMAL_OCTET_VALUE = ord(_MINIMAL_OCTET)
  903. _SUCCESSOR_PREFIX = Name([_MINIMAL_OCTET])
  904. _MAXIMAL_OCTET = b"\xff"
  905. _MAXIMAL_OCTET_VALUE = ord(_MAXIMAL_OCTET)
  906. _AT_SIGN_VALUE = ord("@")
  907. _LEFT_SQUARE_BRACKET_VALUE = ord("[")
  908. def _wire_length(labels):
  909. return functools.reduce(lambda v, x: v + len(x) + 1, labels, 0)
  910. def _pad_to_max_name(name):
  911. needed = 255 - _wire_length(name.labels)
  912. new_labels = []
  913. while needed > 64:
  914. new_labels.append(_MAXIMAL_OCTET * 63)
  915. needed -= 64
  916. if needed >= 2:
  917. new_labels.append(_MAXIMAL_OCTET * (needed - 1))
  918. # Note we're already maximal in the needed == 1 case as while we'd like
  919. # to add one more byte as a new label, we can't, as adding a new non-empty
  920. # label requires at least 2 bytes.
  921. new_labels = list(reversed(new_labels))
  922. new_labels.extend(name.labels)
  923. return Name(new_labels)
  924. def _pad_to_max_label(label, suffix_labels):
  925. length = len(label)
  926. # We have to subtract one here to account for the length byte of label.
  927. remaining = 255 - _wire_length(suffix_labels) - length - 1
  928. if remaining <= 0:
  929. # Shouldn't happen!
  930. return label
  931. needed = min(63 - length, remaining)
  932. return label + _MAXIMAL_OCTET * needed
  933. def _absolute_predecessor(name: Name, origin: Name, prefix_ok: bool) -> Name:
  934. # This is the RFC 4471 predecessor algorithm using the "absolute method" of section
  935. # 3.1.1.
  936. #
  937. # Our caller must ensure that the name and origin are absolute, and that name is a
  938. # subdomain of origin.
  939. if name == origin:
  940. return _pad_to_max_name(name)
  941. least_significant_label = name[0]
  942. if least_significant_label == _MINIMAL_OCTET:
  943. return name.parent()
  944. least_octet = least_significant_label[-1]
  945. suffix_labels = name.labels[1:]
  946. if least_octet == _MINIMAL_OCTET_VALUE:
  947. new_labels = [least_significant_label[:-1]]
  948. else:
  949. octets = bytearray(least_significant_label)
  950. octet = octets[-1]
  951. if octet == _LEFT_SQUARE_BRACKET_VALUE:
  952. octet = _AT_SIGN_VALUE
  953. else:
  954. octet -= 1
  955. octets[-1] = octet
  956. least_significant_label = bytes(octets)
  957. new_labels = [_pad_to_max_label(least_significant_label, suffix_labels)]
  958. new_labels.extend(suffix_labels)
  959. name = Name(new_labels)
  960. if prefix_ok:
  961. return _pad_to_max_name(name)
  962. else:
  963. return name
  964. def _absolute_successor(name: Name, origin: Name, prefix_ok: bool) -> Name:
  965. # This is the RFC 4471 successor algorithm using the "absolute method" of section
  966. # 3.1.2.
  967. #
  968. # Our caller must ensure that the name and origin are absolute, and that name is a
  969. # subdomain of origin.
  970. if prefix_ok:
  971. # Try prefixing \000 as new label
  972. try:
  973. return _SUCCESSOR_PREFIX.concatenate(name)
  974. except NameTooLong:
  975. pass
  976. while name != origin:
  977. # Try extending the least significant label.
  978. least_significant_label = name[0]
  979. if len(least_significant_label) < 63:
  980. # We may be able to extend the least label with a minimal additional byte.
  981. # This is only "may" because we could have a maximal length name even though
  982. # the least significant label isn't maximally long.
  983. new_labels = [least_significant_label + _MINIMAL_OCTET]
  984. new_labels.extend(name.labels[1:])
  985. try:
  986. return dns.name.Name(new_labels)
  987. except dns.name.NameTooLong:
  988. pass
  989. # We can't extend the label either, so we'll try to increment the least
  990. # signficant non-maximal byte in it.
  991. octets = bytearray(least_significant_label)
  992. # We do this reversed iteration with an explicit indexing variable because
  993. # if we find something to increment, we're going to want to truncate everything
  994. # to the right of it.
  995. for i in range(len(octets) - 1, -1, -1):
  996. octet = octets[i]
  997. if octet == _MAXIMAL_OCTET_VALUE:
  998. # We can't increment this, so keep looking.
  999. continue
  1000. # Finally, something we can increment. We have to apply a special rule for
  1001. # incrementing "@", sending it to "[", because RFC 4034 6.1 says that when
  1002. # comparing names, uppercase letters compare as if they were their
  1003. # lower-case equivalents. If we increment "@" to "A", then it would compare
  1004. # as "a", which is after "[", "\", "]", "^", "_", and "`", so we would have
  1005. # skipped the most minimal successor, namely "[".
  1006. if octet == _AT_SIGN_VALUE:
  1007. octet = _LEFT_SQUARE_BRACKET_VALUE
  1008. else:
  1009. octet += 1
  1010. octets[i] = octet
  1011. # We can now truncate all of the maximal values we skipped (if any)
  1012. new_labels = [bytes(octets[: i + 1])]
  1013. new_labels.extend(name.labels[1:])
  1014. # We haven't changed the length of the name, so the Name constructor will
  1015. # always work.
  1016. return Name(new_labels)
  1017. # We couldn't increment, so chop off the least significant label and try
  1018. # again.
  1019. name = name.parent()
  1020. # We couldn't increment at all, so return the origin, as wrapping around is the
  1021. # DNSSEC way.
  1022. return origin
  1023. def _handle_relativity_and_call(
  1024. function: Callable[[Name, Name, bool], Name],
  1025. name: Name,
  1026. origin: Name,
  1027. prefix_ok: bool,
  1028. ) -> Name:
  1029. # Make "name" absolute if needed, ensure that the origin is absolute,
  1030. # call function(), and then relativize the result if needed.
  1031. if not origin.is_absolute():
  1032. raise NeedAbsoluteNameOrOrigin
  1033. relative = not name.is_absolute()
  1034. if relative:
  1035. name = name.derelativize(origin)
  1036. elif not name.is_subdomain(origin):
  1037. raise NeedSubdomainOfOrigin
  1038. result_name = function(name, origin, prefix_ok)
  1039. if relative:
  1040. result_name = result_name.relativize(origin)
  1041. return result_name