From 854591cd142e1d31ece785bd540a2905b2792722 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 22 Apr 2024 15:17:42 +0200 Subject: [PATCH] Fix parsing of relative names --- TODO | 1 - nsconfig/core.py | 4 +++- nsconfig/util.py | 8 ++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/TODO b/TODO index b4b14c5..783852a 100644 --- a/TODO +++ b/TODO @@ -3,4 +3,3 @@ - Blackhole zones - DNSSEC - Logging -- fix parse_name to know @ diff --git a/nsconfig/core.py b/nsconfig/core.py index 885a460..88baf66 100644 --- a/nsconfig/core.py +++ b/nsconfig/core.py @@ -64,7 +64,7 @@ class NscNode: else: self._add(dns.rdtypes.IN.AAAA.AAAA(RdataClass.IN, RdataType.AAAA, str(a))) if reverse: - self.nsc_zone.nsc._add_reverse_mapping(a, parse_name(self.name + '.' + self.nsc_zone.name) if self.name != "" else parse_name(self.nsc_zone.name + '.')) + self.nsc_zone.nsc._add_reverse_mapping(a, parse_name(self.name, origin=self.nsc_zone.dns_name)) return self def MX(self, pri: int, name: str) -> Self: @@ -199,6 +199,7 @@ class ZoneType(Enum): class NscZone: nsc: 'Nsc' name: str + dns_name: Name safe_name: str # For use in file names zone_type: ZoneType reverse_for: Optional[IPNetwork] @@ -210,6 +211,7 @@ class NscZone: **kwargs) -> None: self.nsc = nsc self.name = name + self.dns_name = dns.name.from_text(name) self.safe_name = name.replace('/', '@') self.config = NscZoneConfig(**kwargs).finalize() self.reverse_for = reverse_for diff --git a/nsconfig/util.py b/nsconfig/util.py index 6acc1d8..17c67ee 100644 --- a/nsconfig/util.py +++ b/nsconfig/util.py @@ -2,7 +2,7 @@ import dns.name from dns.name import Name from ipaddress import ip_address, IPv4Address, IPv6Address, ip_network, IPv4Network, IPv6Network from datetime import timedelta -from typing import Any, List +from typing import Any, List, Optional IPAddress = IPv4Address | IPv6Address @@ -41,12 +41,12 @@ def parse_network(addr: IPNetwork | str) -> IPNetwork: raise ValueError('Cannot parse IP network') -def parse_name(name: str, relative: bool = False) -> Name: +def parse_name(name: str, relative: bool = False, origin: Optional[Name] = None) -> Name: # FIXME: Names with escaped dots - if '.' in name and not relative: + if '.' in name and not relative and origin is None: return dns.name.from_text(name) else: - return dns.name.from_text(name, origin=None) + return dns.name.from_text(name, origin=origin) def parse_duration(delta: timedelta | int) -> int: -- 2.39.5