Exemplo n.º 1
0
    def build_largest_reverse_zone(self, cfg_zones: ConfigDict,
                                   records: List[Union['PTRRecord',
                                                       'NSRecord']]):
        """
        Create the ConfigDict object representing a new reverse zone whose
        prefix is the largest one that includes all the PTR records.
        Then it adds it to the cfg_zones dict.

        :param cfg_zones: The dict of ConfigDict representing existing zones
        :param records: The list of PTR records to place a new reverse zone
        """
        if len(records) == 0:
            return

        # Find common prefix between all records
        common = records[0].domain_name.split(".")
        for i in range(1, len(records)):
            prefix = records[i].domain_name.split(".")
            for j in range(1, len(common)):
                if prefix[len(prefix) - j] != common[len(common) - j]:
                    common = common[len(prefix) + 1 - j:]
                    break
        domain_name = ".".join(common)

        # Retrieve the NS Record for the new zone
        ns_record = None
        for zone in cfg_zones.values():
            if "arpa" in zone.name:
                continue
            for record in zone.soa_record.records:
                if record.rtype == "NS" \
                        and self._node.name in record.name_server:
                    ns_record = NSRecord(record.domain_name, self._node.name)
                    ns_record.domain_name = domain_name
        if ns_record is None:
            lg.warning("Cannot forge a DNS reverse zone because there is no"
                       " NS Record for this node in regular zones.\n")
            return
        records.append(ns_record)

        # Build the reverse zone
        soa_record = SOARecord(domain_name=domain_name, records=records)

        reverse_zone = ConfigDict(name=soa_record.domain_name,
                                  soa_record=soa_record,
                                  records=soa_record.records,
                                  master=True,
                                  master_ips=[])
        self._node.params.setdefault('dns_zones', []).append(reverse_zone)
        cfg_zones[self.zone_filename(reverse_zone.name)] = reverse_zone
Exemplo n.º 2
0
def _addresses_of(devname, node=None):
    """Return the addresses of a named interface"""
    cmdline = ['ip', 'address', 'show', 'dev', devname]
    try:
        addrstr = node.cmd(*cmdline)
    except AttributeError:
        addrstr = subprocess.check_output(cmdline).decode("utf-8")
    except (OSError, subprocess.CalledProcessError):
        addrstr = None
    if not addrstr:
        log.warning('Failed to run ip address!')
        return None, (), ()
    mac, v4, v6 = _parse_addresses(addrstr)
    return (mac, sorted(v4, key=OrderedAddress, reverse=True),
            sorted(v6, key=OrderedAddress, reverse=True))
Exemplo n.º 3
0
def _addresses_of(devname, node=None):
    """Return the addresses of a named interface"""
    cmdline = ['ip', 'address', 'show', 'dev', devname]
    try:
        addrstr = node.cmd(*cmdline)
    except AttributeError:
        addrstr = subprocess.check_output(cmdline)
    except (OSError, subprocess.CalledProcessError):
        addrstr = None
    if not addrstr:
        log.warning('Failed to run ip address!')
        return None, (), ()
    mac, v4, v6 = _parse_addresses(addrstr)
    return (mac,
            sorted(v4, cmp=address_comparator, reverse=True),
            sorted(v6, cmp=address_comparator, reverse=True))
Exemplo n.º 4
0
    def __init__(self, db=None, net=None, *args, **kwargs):
        """Either extract properties from a network or load a save file

        :param db: a path towards a saved version of this class which will
                        be loaded
        :param net: an IPNet instance which will be parsed in order to extract
                    useful properties
        """
        super().__init__(*args, **kwargs)
        # dict keyed by node name ->
        #     dict keyed by - properties -> val
        #                   - neighbor   -> interface properties"""
        self._network = {}
        if db:
            self.load(db)
        if net:
            self.parse_net(net)
        if not db and not net:
            lg.warning('TopologyDB instantiated without any data')