Exemple #1
0
    def find_dns_owners(self, dns_owner_id, only_type=True):
        """Return information about entries using this dns_owner.  If
        only_type=True, returns a list of owner_type.  Otherwise
        returns a list of (owner_type, owner_id) tuples"""

        ret = []
        arecord = ARecord.ARecord(self._db)
        for row in arecord.list_ext(dns_owner_id=dns_owner_id):
            ret.append((dns.A_RECORD, row['a_record_id']))
        aaaarecord = AAAARecord.AAAARecord(self._db)
        for row in aaaarecord.list_ext(dns_owner_id=dns_owner_id):
            ret.append((dns.AAAA_RECORD, row['aaaa_record_id']))
        hi = HostInfo.HostInfo(self._db)
        try:
            hi.find_by_dns_owner_id(dns_owner_id)
            ret.append((dns.HOST_INFO, hi.entity_id))
        except Errors.NotFoundError:
            pass
        dns_owner = DnsOwner.DnsOwner(self._db)
        for row in dns_owner.list_srv_records(owner_id=dns_owner_id):
            ret.append((dns.SRV_OWNER, row['service_owner_id']))
        for row in dns_owner.list_general_dns_records(
                dns_owner_id=dns_owner_id):
            ret.append((dns.GENERAL_DNS_RECORD, row['dns_owner_id']))
        cn = CNameRecord.CNameRecord(self._db)
        for row in cn.list_ext(cname_owner=dns_owner_id):
            ret.append((dns.CNAME_OWNER, row['cname_id']))
        if only_type:
            return [x[0] for x in ret]
        return ret
Exemple #2
0
    def _get_zone_data(self, zone):
        # ARecord has key=a_record_id
        # HostInfo, SrvRecord has key=dns_owner_id
        # CnameRecords key=target_owner_id
        # entity2txt, entity2note has_key=entity_id
        for row in ARecord.ARecord(db).list_ext(zone=zone):
            id = int(row['dns_owner_id'])
            if self.a_records_by_dns_owner.has_key(id):
                self.a_records_by_dns_owner[id] += [row]
            else:
                self.a_records_by_dns_owner[id] = [row]

            # Following dict is populated to support HostFile
            self.a_records[int(row['a_record_id'])] = row
        logger.debug("... arecords")

        for row in AAAARecord.AAAARecord(db).list_ext(zone=zone):
            id = int(row['dns_owner_id'])
            if self.aaaa_records_by_dns_owner.has_key(id):
                self.aaaa_records_by_dns_owner[id] += [row]
            else:
                self.aaaa_records_by_dns_owner[id] = [row]
            # Following dict is populated to support HostFile
            self.aaaa_records[int(row['aaaa_record_id'])] = row
        logger.debug("... aaaarecords")

        for row in HostInfo.HostInfo(db).list(zone=zone):
            # Unique constraint on dns_owner_id
            self.hosts[int(row['dns_owner_id'])] = row

        logger.debug("... hosts")
        for row in CNameRecord.CNameRecord(db).list_ext(zone=zone):
            # TBD:  skal vi ha unique constraint på dns_owner?
            self.cnames.setdefault(int(row['target_owner_id']), []).append(row)
        logger.debug("... cnames")

        # From mix-in classes
        for row in DnsOwner.MXSet(db).list_mx_sets():
            self.mx_sets.setdefault(int(row['mx_set_id']), []).append(row)

        logger.debug("... mx_sets")
        for row in DnsOwner.DnsOwner(db).list(zone=zone):
            self.owner_id2mx_set[int(row['dns_owner_id'])] = int(
                row['mx_set_id'] or 0)

        logger.debug("... mx_set owners")

        for row in DnsOwner.DnsOwner(db).list_general_dns_records(
                field_type=co.field_type_txt, zone=zone):
            self.dnsowner2txt_record[int(row['dns_owner_id'])] = row
        logger.debug("... txt reocrds")

        for row in DnsOwner.DnsOwner(db).list_srv_records(zone=zone):
            # We want them listed in the same place
            # TODO: while doing that, we want it below the first target_owner_id
            self.srv_records.setdefault(int(row['service_owner_id']),
                                        []).append(row)
        logger.debug("... srv records")
Exemple #3
0
 def remove_host_info(self, dns_owner_id, try_dns_remove=False):
     hi = HostInfo.HostInfo(self._db)
     try:
         hi.find_by_dns_owner_id(dns_owner_id)
     except Errors.NotFoundError:
         return  # No deletion needed
     hi._delete()
     if try_dns_remove:
         self.remove_dns_owner(dns.entity_id)
Exemple #4
0
 def __init__(self, db, default_zone):
     self._db = db
     self._arecord = ARecord.ARecord(self._db)
     self._ip_number = IPNumber.IPNumber(self._db)
     self._dns_owner = DnsOwner.DnsOwner(self._db)
     self._mx_set = DnsOwner.MXSet(self._db)
     self._host = HostInfo.HostInfo(self._db)
     self._cname = CNameRecord.CNameRecord(self._db)
     self._default_zone = default_zone
Exemple #5
0
    def __init__(self, *args, **kwargs):
        """Instantiate dns specific functionality."""
        super(HostSync, self).__init__(*args, **kwargs)
        self.host = HostInfo.HostInfo(self.db)

        self.subnet = Subnet.Subnet(self.db)
        self.subnet6 = IPv6Subnet.IPv6Subnet(self.db)

        self.ar = ARecord.ARecord(self.db)
        self.aaaar = AAAARecord.AAAARecord(self.db)
Exemple #6
0
 def __init__(self, db, default_zone):
     self._db = db
     self._ip_number = IPNumber.IPNumber(db)
     self._ipv6_number = IPv6Number.IPv6Number(db)
     self._arecord = ARecord.ARecord(db)
     self._aaaarecord = AAAARecord.AAAARecord(db)
     self._dns_owner = DnsOwner.DnsOwner(db)
     self._mx_set = DnsOwner.MXSet(db)
     self._host = HostInfo.HostInfo(db)
     self._cname = CNameRecord.CNameRecord(db)
     self._dns_parser = DnsParser(db, default_zone)
Exemple #7
0
    def set_ttl(self, owner_id, ttl):
        """Set TTL entries for this dns_owner"""

        # TODO: Currently we do this by updating the TTL in all
        # tables.  It has been decided to move ttl-information into
        # dns_owner.  However, we will not do this until after we have
        # gone into production to avoid a huge diff when comparing
        # autogenerated zone files to the original ones.

        dns_owner = DnsOwner.DnsOwner(self.db)
        dns_owner.find(owner_id)

        arecord = ARecord.ARecord(self.db)
        for row in arecord.list_ext(dns_owner_id=owner_id):
            arecord.clear()
            arecord.find(row['a_record_id'])
            arecord.ttl = ttl
            arecord.write_db()

        aaaarecord = AAAARecord.AAAARecord(self.db)
        for row in aaaarecord.list_ext(dns_owner_id=owner_id):
            aaaarecord.clear()
            aaaarecord.find(row['aaaa_record_id'])
            aaaarecord.ttl = ttl
            aaaarecord.write_db()

        host = HostInfo.HostInfo(self.db)
        try:
            host.find_by_dns_owner_id(owner_id)
        except Errors.NotFoundError:
            pass
        else:
            host.ttl = ttl
            host.write_db()

        for row in dns_owner.list_general_dns_records(dns_owner_id=owner_id):
            dns_owner.update_general_dns_record(owner_id, row['field_type'],
                                                ttl, row['data'])

        mx_set = DnsOwner.MXSet(self.db)
        for row in mx_set.list_mx_sets(target_id=owner_id):
            mx_set.clear()
            mx_set.find(row['mx_set_id'])
            mx_set.update_mx_set_member(ttl, row['pri'], row['target_id'])
        cname = CNameRecord.CNameRecord(self.db)
        for row in cname.list_ext(cname_owner=owner_id):
            cname.clear()
            cname.find(row['cname_id'])
            cname.ttl = ttl
            cname.write_db()

        for row in dns_owner.list_srv_records(owner_id=owner_id):
            dns_owner.update_srv_record_ttl(owner_id, ttl)
Exemple #8
0
def import_hinfo(fname):
    """Import the hinfo file.  It has the format::
      hostname, hinfo, TODO = line.split()

    Update HINFO for all hosts in the file unless they are already
    registered as DHCP.
    """
    host = HostInfo.HostInfo(db)
    dns_id2name = {}
    for row in DnsOwner.DnsOwner(db).list():
        dns_id2name[int(row['dns_owner_id'])] = row['name']
    name2info = {}
    for row in host.list():
        name = dns_id2name[int(row['dns_owner_id'])]
        name = name[:-1]  # FQDN without trailing dot
        name2info[name] = (row['hinfo'], int(row['dns_owner_id']))

    ok_line = re.compile(r'^\S*?\.uio\.no')  # Temporary work-around for
    # data & info messages in same file
    n_lines, n_changed, n_unknown, n_dhcp, n_nf = 0, 0, 0, 0, 0
    for line in open(fname, "r"):
        n_lines += 1
        line = line.strip()
        if not ok_line.match(line):
            logger.info("Ignoring '%s'" % line)
            continue
        hostname, new_hinfo1, new_hinfo2 = line.split(None, 2)
        new_hinfo = "%s\t%s" % (new_hinfo1, new_hinfo2)
        if not name2info.has_key(hostname):
            logger.info("unknown host '%s'" % hostname)
            n_unknown += 1
            continue
        old_hinfo, dns_owner_id = name2info.get(hostname)
        if old_hinfo.startswith('DHCP') or new_hinfo == old_hinfo:
            n_dhcp += 1
            continue
        try:
            host.clear()
            host.find_by_dns_owner_id(dns_owner_id)
        except Errors.NotFoundError:
            n_nf += 1
            continue
        logger.debug("Setting new hinfo '%s' for %s/%s (old=%s)" %
                     (new_hinfo, dns_owner_id, hostname, host.hinfo))
        host.hinfo = new_hinfo
        host.write_db()
        db.commit()
        n_changed += 1
    logger.info("Read %i lines, changed %i, %i unknown, %i dhcp, %i nf" %
                (n_lines, n_changed, n_unknown, n_dhcp, n_nf))
Exemple #9
0
 def __init__(self, db, logger, default_zone):
     self.logger = logger
     self.db = db
     self.const = Factory.get('Constants')(self.db)
     # TBD: This pre-allocating may interfere with multi-threaded bofhd
     self._arecord = ARecord.ARecord(self.db)
     self._aaaarecord = AAAARecord.AAAARecord(self.db)
     self._host = HostInfo.HostInfo(self.db)
     self._dns_owner = DnsOwner.DnsOwner(self.db)
     self._ip_number = IPNumber.IPNumber(self.db)
     self._ipv6_number = IPv6Number.IPv6Number(self.db)
     self._cname = CNameRecord.CNameRecord(self.db)
     self._validator = IntegrityHelper.Validator(self.db, default_zone)
     self._update_helper = IntegrityHelper.Updater(self.db)
     self._mx_set = DnsOwner.MXSet(self.db)
     self.default_zone = default_zone
     self._find = Utils.Find(self.db, default_zone)
     self._parser = Utils.DnsParser(self.db, default_zone)
Exemple #10
0
    def find_referers(self, ip_number_id=None, dns_owner_id=None,
                      only_type=True, ip_type=dns.IP_NUMBER):
        """Return information about registrations that point to this
        ip-number/dns-owner. If only_type=True, returns a list of
        owner_type.  Otherwise returns a list of (owner_type,
        owner_id) tuples"""

        # We choose classes and record type depending on the ip_type
        # parameter. This is a bit dirty, but reduces the amount of
        # functions required.
        ip_class = IPNumber.IPNumber if (
            ip_type == dns.IP_NUMBER
        ) else IPv6Number.IPv6Number
        record_class = ARecord.ARecord if (
            ip_type == dns.IP_NUMBER
        ) else AAAARecord.AAAARecord
        record_type = dns.A_RECORD if (
            ip_type == dns.IP_NUMBER
        ) else dns.AAAA_RECORD

        ip_key = 'ip_number_id' if (
            ip_type == dns.IP_NUMBER
        ) else 'ipv6_number_id'
        record_key = 'a_record_id' if (
            ip_type == dns.IP_NUMBER
        ) else 'aaaa_record_id'

        # Not including entity-note
        assert not (ip_number_id and dns_owner_id)
        ret = []

        if ip_number_id and ip_type == dns.REV_IP_NUMBER:
            for ipn, key in [
                    (IPNumber.IPNumber(self._db), 'ip_number_id'),
                    (IPv6Number.IPv6Number(self._db), 'ipv6_number_id')]:
                for row in ipn.list_override(ip_number_id=ip_number_id):
                    ret.append((dns.REV_IP_NUMBER, row[key]))

            if only_type:
                return [x[0] for x in ret]
            return ret

        if ip_number_id:
            ipnumber = ip_class(self._db)
            for row in ipnumber.list_override(ip_number_id=ip_number_id):
                ret.append((dns.REV_IP_NUMBER, row[ip_key]))
            arecord = record_class(self._db)
            for row in arecord.list_ext(ip_number_id=ip_number_id):
                ret.append((record_type, row[record_key]))
            if only_type:
                return [x[0] for x in ret]
            return ret
        mx = DnsOwner.MXSet(self._db)
        for row in mx.list_mx_sets(target_id=dns_owner_id):
            ret.append((dns.MX_SET, row['mx_set_id']))
        dns_owner = DnsOwner.DnsOwner(self._db)
        for row in dns_owner.list_srv_records(target_owner_id=dns_owner_id):
            ret.append((dns.SRV_TARGET, row['service_owner_id']))
        cn = CNameRecord.CNameRecord(self._db)
        for row in cn.list_ext(target_owner=dns_owner_id):
            ret.append((dns.CNAME_TARGET, row['cname_id']))
        arecord = record_class(self._db)
        for row in arecord.list_ext(dns_owner_id=dns_owner_id):
            ret.append((record_type, row[record_key]))
        hi = HostInfo.HostInfo(self._db)
        for row in hi.list_ext(dns_owner_id=dns_owner_id):
            ret.append((dns.HOST_INFO, row['host_id'],))
        if only_type:
            return [x[0] for x in ret]
        return ret
Exemple #11
0
    def _setup_project_hosts(self, creator_id):
        """Setup the hosts initially needed for the given project."""
        projectid = self.get_project_id()
        host = HostInfo.HostInfo(self._db)
        dns_owner = DnsOwner.DnsOwner(self._db)
        vm_trait = self.get_trait(self.const.trait_project_vm_type)

        if vm_trait:
            vm_type = vm_trait['strval']
        else:  # Set win as default if trait is not set.
            vm_type = 'win_vm'

        if vm_type in ('win_vm', 'win_and_linux_vm'):
            # Create a Windows host for the whole project if it doesn't exist
            hostname = '%s-win01.tsd.usit.no.' % projectid
            hinfo = 'IBM-PC\tWINDOWS'
            host_dns_owner = None
            try:
                host.find_by_name(hostname)
            except Errors.NotFoundError:
                host_dns_owner = self._populate_dnsowner(hostname)
                try:
                    host.find_by_dns_owner_id(host_dns_owner.entity_id)
                except Errors.NotFoundError:
                    host.populate(host_dns_owner.entity_id, hinfo)

            if host_dns_owner is None:
                dns_owner.find_by_name(hostname)
                host_dns_owner = dns_owner

            host.hinfo = hinfo
            host.write_db()

            for comp in getattr(cereconf, 'TSD_HOSTPOLICIES_WIN', ()):
                TSDUtils.add_host_to_policy_component(self._db,
                                                      host_dns_owner.entity_id,
                                                      comp)

        if vm_type in ('linux_vm', 'win_and_linux_vm'):
            host.clear()
            # Create a Linux host for the whole project if it doesn' exist
            hostname = '%s-tl01-l.tsd.usit.no.' % projectid
            hinfo = 'IBM-PC\tLINUX'
            host_dns_owner = None
            try:
                host.find_by_name(hostname)
            except Errors.NotFoundError:
                host_dns_owner = self._populate_dnsowner(hostname)
                try:
                    host.find_by_dns_owner_id(host_dns_owner.entity_id)
                except Errors.NotFoundError:
                    host.populate(host_dns_owner.entity_id, hinfo)

            if host_dns_owner is None:
                dns_owner.clear()
                dns_owner.find_by_name(hostname)
                host_dns_owner = dns_owner

            host.hinfo = hinfo
            host.write_db()

            for comp in getattr(cereconf, 'TSD_HOSTPOLICIES_LINUX', ()):
                TSDUtils.add_host_to_policy_component(self._db,
                                                      host_dns_owner.entity_id,
                                                      comp)

            # Add CNAME-record for connecting via thinlinc-proxy
            cname_record_name = '%s-tl01-l.tl.tsd.usit.no.' % projectid
            TSDUtils.add_cname_record(self._db,
                                      cname_record_name,
                                      cereconf.TSD_THINLINC_PROXY,
                                      fail_on_exists=False)
Exemple #12
0
from Cerebrum.modules.dns import CNameRecord
from Cerebrum.modules.dns import DnsOwner
from Cerebrum.modules.dns import HostInfo
from Cerebrum.modules.dns import IPNumber
from Cerebrum.modules.dns import Utils

sys.argv.extend(["--logger-level", "DEBUG"])
logger = Factory.get_logger("cronjob")
db = Factory.get('Database')()
db.cl_init(change_program='import_dns')
co = Factory.get('Constants')(db)
ipnumber = IPNumber.IPNumber(db)
arecord = ARecord.ARecord(db)
cname = CNameRecord.CNameRecord(db)
dnsowner = DnsOwner.DnsOwner(db)
host = HostInfo.HostInfo(db)
mx_set = DnsOwner.MXSet(db)

# logger.setLevel(logger.debug)
header_splitter = r'^; AUTOGENERATED: do not edit below this line'

class Netgroups(object):
    class MergeRestart(Exception): pass

    def __init__(self, fname, default_zone):
        self._fname = fname
        self._default_zone = default_zone
        self._import_netgroups()
        
    def _parse_netgroups(self):
        import pprint