Exemplo n.º 1
0
    def test_cleanup_cname(self):
        # Make sure CNAME record block
        c = Domain(name='foo1')
        c.save()
        self.assertFalse(c.purgeable)
        s, _ = SOA.objects.get_or_create(primary="foo",
                                         contact="Foo",
                                         comment="dddfoo")
        f_c = Domain(name='foo.foo1')
        f_c.soa = s
        f_c.save()

        self.assertFalse(Domain.objects.filter(name="x.y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="z.foo.foo1"))
        self.assertTrue(Domain.objects.filter(name="foo.foo1"))

        self.assertFalse(f_c.purgeable)
        fqdn = "cname.x.y.z.foo.foo1"
        label, the_domain = ensure_label_domain(fqdn)

        cname = CNAME(label=label, domain=the_domain, target="foo")
        cname.save()
        self.assertFalse(prune_tree(the_domain))
        cname.delete()

        self.assertFalse(Domain.objects.filter(name="x.y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="z.foo.foo1"))
        fqdn = "bar.x.y.z.foo.poo"
        self.assertTrue(Domain.objects.filter(name="foo.foo1"))
Exemplo n.º 2
0
    def test_cleanup_cname(self):
        # Make sure CNAME record block
        c = Domain(name='foo1')
        c.save()
        self.assertFalse(c.purgeable)
        f_c = create_fake_zone('foo.foo1', suffix="")
        self.assertEqual(f_c.name, 'foo.foo1')

        self.assertFalse(Domain.objects.filter(name="x.y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="z.foo.foo1"))
        self.assertTrue(Domain.objects.filter(name="foo.foo1"))

        self.assertFalse(f_c.purgeable)
        fqdn = "cname.x.y.z.foo.foo1"
        label, the_domain = ensure_label_domain(fqdn)

        cname = CNAME(label=label, domain=the_domain, target="foo")
        cname.save()
        self.assertFalse(prune_tree(the_domain))
        cname.delete()

        self.assertFalse(Domain.objects.filter(name="x.y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="z.foo.foo1"))
        fqdn = "bar.x.y.z.foo.poo"
        self.assertTrue(Domain.objects.filter(name="foo.foo1"))
Exemplo n.º 3
0
    def test_cleanup_cname(self):
        # Make sure CNAME record block
        c = Domain(name='foo1')
        c.save()
        self.assertFalse(c.purgeable)
        f_c = create_fake_zone('foo.foo1', suffix="")
        self.assertEqual(f_c.name, 'foo.foo1')

        self.assertFalse(Domain.objects.filter(name="x.y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="z.foo.foo1"))
        self.assertTrue(Domain.objects.filter(name="foo.foo1"))

        self.assertFalse(f_c.purgeable)
        fqdn = "cname.x.y.z.foo.foo1"
        label, the_domain = ensure_label_domain(fqdn)

        cname = CNAME(label=label, domain=the_domain, target="foo")
        cname.save()
        self.assertFalse(prune_tree(the_domain))
        cname.delete()

        self.assertFalse(Domain.objects.filter(name="x.y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="z.foo.foo1"))
        fqdn = "bar.x.y.z.foo.poo"
        self.assertTrue(Domain.objects.filter(name="foo.foo1"))
Exemplo n.º 4
0
    def test_cleanup_cname(self):
        # Make sure CNAME record block
        c = Domain(name='foo1')
        c.save()
        self.assertFalse(c.purgeable)
        s, _ = SOA.objects.get_or_create(primary="foo", contact="Foo",
                                         comment="dddfoo")
        f_c = Domain(name='foo.foo1')
        f_c.soa = s
        f_c.save()

        self.assertFalse(Domain.objects.filter(name="x.y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="z.foo.foo1"))
        self.assertTrue(Domain.objects.filter(name="foo.foo1"))

        self.assertFalse(f_c.purgeable)
        fqdn = "cname.x.y.z.foo.foo1"
        label, the_domain = ensure_label_domain(fqdn)

        cname = CNAME(label=label, domain=the_domain, target="foo")
        cname.save()
        self.assertFalse(prune_tree(the_domain))
        cname.delete()

        self.assertFalse(Domain.objects.filter(name="x.y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="y.z.foo.foo1"))
        self.assertFalse(Domain.objects.filter(name="z.foo.foo1"))
        fqdn = "bar.x.y.z.foo.poo"
        self.assertTrue(Domain.objects.filter(name="foo.foo1"))
Exemplo n.º 5
0
 def test_dirty_cname(self):
     self.soa.dirty = False
     self.dom.dirty = False
     c = CNAME(label="asfd", domain=self.dom, data="nerp")
     c.full_clean()
     c.save()
     self.assertTrue(self.dom.dirty)
     self.assertFalse(self.soa.dirty)
Exemplo n.º 6
0
    def do_add(self, label, domain, data):
        cn = CNAME(label=label, domain=domain, target=data)
        cn.full_clean()
        cn.save()
        cn.save()
        self.assertTrue(cn.details())

        cs = CNAME.objects.filter(label=label, domain=domain, target=data)
        self.assertEqual(len(cs), 1)
        return cn
Exemplo n.º 7
0
    def do_add(self, label, domain, data):
        cn = CNAME(label=label, domain=domain, target=data)
        cn.full_clean()
        cn.save()
        cn.save()
        self.assertTrue(cn.details())

        cs = CNAME.objects.filter(
            label=label, domain=domain, target=data)
        self.assertEqual(len(cs), 1)
        return cn
Exemplo n.º 8
0
    def test_add_with_cname(self):
        label = "cnamederp"
        domain = self.o_e
        data = "foo.com"
        cn = CNAME( label = label, domain = domain, data = data )
        cn.full_clean()
        cn.save()

        data = { 'label':'' ,'domain':self.o_e ,'server':'cnamederp.oregonstate.org' ,'priority':2 ,'ttl':2222 }
        mx = MX( **data )
        self.assertRaises( ValidationError, mx.save )
Exemplo n.º 9
0
 def test_delete_with_cname_pointing_to_a(self):
     label = 'foo100'
     a = AddressRecord(label=label, domain=self.o_e, ip_str=
                       '128.193.1.10', ip_type='4')
     a.clean()
     a.save()
     cn = CNAME(label="foomom", domain=self.o_e, target=label + "." +
                self.o_e.name)
     cn.clean()
     cn.save()
     self.assertRaises(ValidationError, a.delete)
     a.delete(check_cname=False)
Exemplo n.º 10
0
    def test_basic_add_remove6(self):
        # Make sure CNAME record block
        f_c = create_fake_zone("foo.foo1", suffix="")
        f_c.save()
        self.assertFalse(f_c.purgeable)
        fqdn = "cname.x.y.z.foo.foo1"
        label, the_domain = ensure_label_domain(fqdn)

        cname = CNAME(label=label, domain=the_domain, target="foo")
        cname.save()
        self.assertFalse(prune_tree(the_domain))
        cname.delete()
Exemplo n.º 11
0
    def do_add(self, label, domain, data):
        cn = CNAME(label = label, domain = domain, data = data)
        cn.full_clean()
        cn.save()
        cn.save()
        self.assertTrue(cn.get_absolute_url())
        self.assertTrue(cn.get_edit_url())
        self.assertTrue(cn.get_delete_url())
        self.assertTrue(cn.details())

        cs = CNAME.objects.filter(label = label, domain = domain, data = data)
        self.assertEqual(len(cs), 1)
        return cn
Exemplo n.º 12
0
    def test_basic_add_remove7(self):
        # try a star record
        f_c = create_fake_zone("foo.foo2", suffix="")
        f_c.save()
        self.assertFalse(f_c.purgeable)
        fqdn = "*.x.y.z.foo.foo2"
        label, the_domain = ensure_label_domain(fqdn)
        self.assertEqual('*', label)

        cname = CNAME(label=label, domain=the_domain, target="foo")
        cname.save()
        self.assertFalse(prune_tree(the_domain))
        cname.delete()
Exemplo n.º 13
0
def gen_CNAME():
    """Migrates CNAME objects.

    .. note::
        Run this only after migrating other DNS objects for every zone.

    .. note::
        Because MAINTAIN is totally messed up, some hostnames in the CNAME
        table have ``.``'s in them, so the fully qualified domain name is
        created first, then the label is stripped off of the front of that.

    .. note::
        If the fully qualified domain name of the label + domain name already
        exists as a domain object, that object becomes the alias and the label
        prefix is set to the empty string. Otherwise, the alias is the
        label + domain name.

    :uniqueness: label, domain, target
    """
    print "Creating CNAMEs."
    cursor.execute("SELECT * FROM zone_cname")

    for _, server, name, domain_id, ttl, zone, enabled in cursor.fetchall():
        server, name = server.lower(), name.lower()
        cursor.execute("SELECT name FROM domain WHERE id = '%s'" % domain_id)
        dname, = cursor.fetchone()
        if not dname:
            continue
        dname = dname.lower()

        fqdn = ".".join([name, dname])
        name, dname = fqdn.split(".", 1)

        if Domain.objects.filter(name=fqdn).exists():
            domain = Domain.objects.get(name=fqdn)
            name = ""
        elif Domain.objects.filter(name=dname).exists():
            domain = Domain.objects.get(name=dname)
        else:
            continue

        if server == ".".join([name, domain.name]):
            # In maintain, at least one CNAME is a loop: biosys.bioe.orst.edu
            continue

        cn = CNAME(label=name, domain=domain, target=server)
        # CNAMEs need to be cleaned independently of saving (no get_or_create)
        cn.full_clean()
        cn.save()
        if enabled:
            cn.views.add(public)
Exemplo n.º 14
0
def gen_CNAME():
    """Migrates CNAME objects.

    .. note::
        Run this only after migrating other DNS objects for every zone.

    .. note::
        Because MAINTAIN is totally messed up, some hostnames in the CNAME
        table have ``.``'s in them, so the fully qualified domain name is
        created first, then the label is stripped off of the front of that.

    .. note::
        If the fully qualified domain name of the label + domain name already
        exists as a domain object, that object becomes the alias and the label
        prefix is set to the empty string. Otherwise, the alias is the
        label + domain name.

    :uniqueness: label, domain, target
    """
    print "Creating CNAMEs."
    cursor.execute("SELECT * FROM zone_cname")

    for _, server, name, domain_id, ttl, zone, enabled in cursor.fetchall():
        server, name = server.lower(), name.lower()
        cursor.execute("SELECT name FROM domain WHERE id = '%s'" % domain_id)
        dname, = cursor.fetchone()
        if not dname:
            continue
        dname = dname.lower()

        fqdn = ".".join([name, dname])
        name, dname = fqdn.split(".", 1)

        if Domain.objects.filter(name=fqdn).exists():
            domain = Domain.objects.get(name=fqdn)
            name = ""
        elif Domain.objects.filter(name=dname).exists():
            domain = Domain.objects.get(name=dname)
        else:
            continue

        if server == ".".join([name, domain.name]):
            # In maintain, at least one CNAME is a loop: biosys.bioe.orst.edu
            continue

        cn = CNAME(label=name, domain=domain, target=server)
        # CNAMEs need to be cleaned independently of saving (no get_or_create)
        cn.full_clean()
        cn.save()
        if enabled:
            cn.views.add(public)
Exemplo n.º 15
0
    def test_existing_cname_new_domain(self):
        name = "bo"
        b_dom,_ = Domain.objects.get_or_create( name = name, delegated=False )

        name = "to.bo"
        t_dom,_ = Domain.objects.get_or_create( name = name, delegated=False )

        cn = CNAME(domain=t_dom, label="no", data="asdf")
        cn.full_clean()
        cn.save()

        name = "no.to.bo"
        n_dom = Domain( name = name, delegated=False )
        self.assertRaises(ValidationError, n_dom.save)
Exemplo n.º 16
0
    def test_existing_cname_new_domain(self):
        name = "bo"
        b_dom, _ = Domain.objects.get_or_create(name=name, delegated=False)

        name = "to.bo"
        t_dom, _ = Domain.objects.get_or_create(name=name, delegated=False)

        cn = CNAME(domain=t_dom, label="no", target="asdf")
        cn.full_clean()
        cn.save()

        name = "no.to.bo"
        n_dom = Domain(name=name, delegated=False)
        self.assertRaises(ValidationError, n_dom.save)
Exemplo n.º 17
0
 def test_delete_with_cname_pointing_to_a(self):
     label = 'foo100'
     a = AddressRecord(label=label,
                       domain=self.o_e,
                       ip_str='128.193.1.10',
                       ip_type='4')
     a.clean()
     a.save()
     cn = CNAME(label="foomom",
                domain=self.o_e,
                target=label + "." + self.o_e.name)
     cn.clean()
     cn.save()
     self.assertRaises(ValidationError, a.delete)
     a.delete(check_cname=False)
Exemplo n.º 18
0
    def test_add_with_cname(self):
        label = "cnamederp"
        domain = self.o_e
        data = "foo.com"
        cn = CNAME(label=label, domain=domain, target=data)
        cn.full_clean()
        cn.save()

        data = {
            'label': '',
            'domain': self.o_e,
            'server': 'cnamederp.oregonstate.org',
            'priority': 2,
            'ttl': 2222
        }
        mx = MX(**data)
        self.assertRaises(ValidationError, mx.save)
Exemplo n.º 19
0
    def test_basic_add_remove6(self):
        # Make sure CNAME record block
        c = Domain(name='foo1')
        c.save()
        self.assertFalse(c.purgeable)
        f_c = Domain(name='foo.foo1')
        s, _ = SOA.objects.get_or_create(primary="foo", contact="foo",
                                         comment="foo.foo1")
        f_c.soa = s
        f_c.save()
        self.assertFalse(f_c.purgeable)
        fqdn = "cname.x.y.z.foo.foo1"
        label, the_domain = ensure_label_domain(fqdn)

        cname = CNAME(label=label, domain=the_domain, target="foo")
        cname.save()
        self.assertFalse(prune_tree(the_domain))
        cname.delete()
Exemplo n.º 20
0
    def test_basic_add_remove6(self):
        # Make sure CNAME record block
        c = Domain(name='foo1')
        c.save()
        self.assertFalse(c.purgeable)
        f_c = Domain(name='foo.foo1')
        s, _ = SOA.objects.get_or_create(primary="foo",
                                         contact="foo",
                                         comment="foo.foo1")
        f_c.soa = s
        f_c.save()
        self.assertFalse(f_c.purgeable)
        fqdn = "cname.x.y.z.foo.foo1"
        label, the_domain = ensure_label_domain(fqdn)

        cname = CNAME(label=label, domain=the_domain, target="foo")
        cname.save()
        self.assertFalse(prune_tree(the_domain))
        cname.delete()
Exemplo n.º 21
0
    def test_integration3_zone(self):
        root_domain = create_fake_zone("wee3.wee.mozilla.com", "")
        res, error = self.search("zone=:wee3.wee.mozilla.com")
        self.assertFalse(error)
        self.assertEqual(len(res['SOA']), 1)
        self.assertEqual(len(res['NS']), 1)
        cn = CNAME(label="host1", domain=root_domain, target="whop.whop")
        cn.save()
        res, error = self.search("zone=:wee3.wee.mozilla.com host1")
        self.assertFalse(error)
        self.assertEqual(len(res['SOA']), 0)
        self.assertEqual(len(res['NS']), 0)
        self.assertEqual(len(res['CNAME']), 1)

        res, error = self.search("zone=:wee3.wee.mozilla.com " "type=:CNAME")
        self.assertFalse(error)
        self.assertEqual(len(res['SOA']), 0)
        self.assertEqual(len(res['NS']), 0)
        self.assertEqual(len(res['CNAME']), 1)
Exemplo n.º 22
0
    def test_integration3_zone(self):
        root_domain = create_fake_zone("wee3.wee.mozilla.com", "")
        res, error = self.search("zone=:wee3.wee.mozilla.com")
        self.assertFalse(error)
        self.assertEqual(len(res['SOA']), 1)
        self.assertEqual(len(res['NS']), 1)
        cn = CNAME(label="host1", domain=root_domain, target="whop.whop")
        cn.save()
        res, error = self.search("zone=:wee3.wee.mozilla.com host1")
        self.assertFalse(error)
        self.assertEqual(len(res['SOA']), 0)
        self.assertEqual(len(res['NS']), 0)
        self.assertEqual(len(res['CNAME']), 1)

        res, error = self.search("zone=:wee3.wee.mozilla.com "
                                       "type=:CNAME")
        self.assertFalse(error)
        self.assertEqual(len(res['SOA']), 0)
        self.assertEqual(len(res['NS']), 0)
        self.assertEqual(len(res['CNAME']), 1)
Exemplo n.º 23
0
 def normal_CNAMES(self):
     self.cur.execute("SELECT id, server, name, domain, ttl, zone FROM `zone_cname` WHERE `name` NOT LIKE '%.%';")
     cnames = self.cur.fetchall()
     for cname in cnames:
         id_ = cname[0]
         server = cname[1]
         label = cname[2]
         domain_id = cname[3]
         ttl = cname[4]
         zone = cname[5]
         # Get it's domain
         self.cur.execute("SELECT name FROM domain where id='%s'" %
                 (domain_id))
         dname = self.cur.fetchone()
         if not dname:
             print "ERROR: CNAME with id ({0}) doens't have a valid domain".format(id_)
             continue
         dname = dname[0]
         domain = Domain.objects.filter(name=dname)
         domain = domain[0]
         possible = CNAME.objects.filter(label=label, domain=domain, data=server)
         if possible:
             continue
         cn = CNAME(label=label, domain=domain, data=server)
         print "server:{0} label:{1}".format(server, label)
         # server:www.orst.edu label:dev
         try:
             cn.full_clean()
         except ValidationError, e:
             fqdn = label+"."+dname
             dom = Domain.objects.filter(name=fqdn)
             if dom:
                 dom = dom[0]
                 cn, _ = CNAME.objects.get_or_create(label='', domain=dom, data=server)
                 cn.full_clean()
                 cn.save()
                 print "Re-Added CNAME ({0})".format(id_)
                 continue
             else:
                 print "Couldn't fix {0}".format(e)
         cn.save()
Exemplo n.º 24
0
    def test_basic_add_remove8(self):
        # Make sure a record's label is changed to '' when a domain with the
        # same name as it's fqdn is created.
        f_c = create_fake_zone("foo.foo3", suffix="")
        f_c.save()
        self.assertFalse(f_c.purgeable)
        fqdn = "www.x.y.z.foo.foo3"
        label, the_domain = ensure_label_domain(fqdn)
        self.assertEqual('www', label)
        self.assertEqual('x.y.z.foo.foo3', the_domain.name)
        self.assertTrue(the_domain.pk)

        cname = CNAME(label=label, domain=the_domain, target="foo")
        cname.save()
        fqdn = "*.www.x.y.z.foo.foo3"
        label2, the_domain2 = ensure_label_domain(fqdn)
        cname = CNAME.objects.get(fqdn=cname.fqdn)
        self.assertEqual('', cname.label)
        self.assertEqual('www.x.y.z.foo.foo3', cname.domain.name)
        self.assertEqual('*', label2)
        self.assertEqual('www.x.y.z.foo.foo3', the_domain2.name)
Exemplo n.º 25
0
def migrate_CNAME(zone, root_domain, soa, views):
    for (name, ttl, rdata) in zone.iterate_rdatas('CNAME'):
        name = name.to_text().strip('.')
        print str(name) + " CNAME " + str(rdata)
        exists_domain = Domain.objects.filter(name=name)
        if exists_domain:
            label = ''
            domain = exists_domain[0]
        else:
            label = name.split('.')[0]
            domain_name = name.split('.')[1:]
            domain = ensure_domain('.'.join(domain_name), force=True)
        data = rdata.target.to_text().strip('.')

        if not CNAME.objects.filter(label=label, domain=domain,
                                    target=data).exists():
            cn = CNAME(label=label, domain=domain, target=data)
            cn.full_clean()
            cn.save()
            for view in views:
                cn.views.add(view)
                cn.save()
Exemplo n.º 26
0
def migrate_CNAME(zone, root_domain, soa, views):
    for (name, ttl, rdata) in zone.iterate_rdatas('CNAME'):
        name = name.to_text().strip('.')
        print str(name) + " CNAME " + str(rdata)
        exists_domain = Domain.objects.filter(name=name)
        if exists_domain:
            label = ''
            domain = exists_domain[0]
        else:
            label = name.split('.')[0]
            domain_name = name.split('.')[1:]
            domain = ensure_domain('.'.join(domain_name), force=True)
        data = rdata.target.to_text().strip('.')

        if not CNAME.objects.filter(label=label, domain=domain,
                                    target=data).exists():
            cn = CNAME(label=label, domain=domain, target=data)
            cn.full_clean()
            cn.save()
            for view in views:
                cn.views.add(view)
                cn.save()
Exemplo n.º 27
0
def gen_CNAME():
    """Migrates CNAME objects.

    .. note::
        Run this only after migrating other DNS objects for every zone.

    .. note::
        Because MAINTAIN is totally messed up, some hostnames in the CNAME
        table have ``.``'s in them, so the fully qualified domain name is
        created first, then the label is stripped off of the front of that.

    .. note::
        If the fully qualified domain name of the label + domain name already
        exists as a domain object, that object becomes the alias and the label
        prefix is set to the empty string. Otherwise, the alias is the
        label + domain name.

    :uniqueness: label, domain, target
    """
    print "Creating CNAMEs."
    sql = ("SELECT zone_cname.id, zone_cname.server, zone_cname.name, "
           "zone_cname.enabled, zone.name, domain.name FROM zone_cname "
           "JOIN zone ON zone_cname.zone = zone.id "
           "JOIN domain ON zone_cname.domain = domain.id")
    cursor.execute(sql)

    for pk, server, name, enabled, zone, dname in cursor.fetchall():
        server, name = server.lower(), name.lower()
        dname = dname.lower()

        fqdn = ".".join([name, dname])
        name, dname = fqdn.split(".", 1)

        if Domain.objects.filter(name=fqdn).exists():
            domain = Domain.objects.get(name=fqdn)
            name = ""
        elif Domain.objects.filter(name=dname).exists():
            domain = Domain.objects.get(name=dname)
        else:
            _, domain = get_label_domain_workaround(fqdn)

        if server == ".".join([name, domain.name]):
            # In maintain, at least one CNAME is a loop: biosys.bioe.orst.edu
            print "Ignoring CNAME %s: Is a loop." % server
            continue

        if CNAME.objects.filter(label=name, domain=domain).exists():
            c = CNAME.objects.get(label=name, domain=domain)
            if c.target != server:
                print("ALERT: Conflicting CNAME with fqdn %s already exists." %
                      fqdn)
            continue

        ctnr = Zone.ctnr_from_zone_name(zone, 'CNAME')
        if ctnr is None:
            continue

        if ctnr not in domain.ctnr_set.all():
            print "CNAME %s has mismatching container for its domain." % pk
            continue

        cn = CNAME(label=name, domain=domain, target=server, ctnr=ctnr)
        cn.set_fqdn()
        dup_ptrs = PTR.objects.filter(fqdn=cn.fqdn)
        if dup_ptrs:
            print "Removing duplicate PTR for %s" % cn.fqdn
            dup_ptrs.delete(update_range_usage=False)

        # CNAMEs need to be cleaned independently of saving (no get_or_create)
        try:
            cn.full_clean()
            cn.save()
            if enabled:
                cn.views.add(public)
                cn.views.add(private)
        except ValidationError, e:
            print "Error:", e
Exemplo n.º 28
0
        exists_domain = Domain.objects.filter(name=name)
        if exists_domain:
            label = ''
            domain = exists_domain[0]
        else:
            label = name.split('.')[0]
            domain_name = name.split('.')[1:]
            domain = ensure_domain('.'.join(domain_name))
        data = rdata.target.to_text().strip('.')

        if not CNAME.objects.filter(label=label, domain=domain,
                                    data=data).exists():
            cn = CNAME(label=label, domain=domain,
                       data=data)
            cn.full_clean()
            cn.save()
            if views:
                for view in views:
                    cn.views.add(view)
                    cn.save()
    # TODO, records not done yet. TXT, SSHFP, AAAA
    # Create list
    for (name, ttl, rdata) in zone.iterate_rdatas('TXT'):
        name = name.to_text().strip('.')
        print str(name) + " TXT " + str(rdata)
        exists_domain = Domain.objects.filter(name=name)
        if exists_domain:
            label = ''
            domain = exists_domain[0]
        else:
            label = name.split('.')[0]
Exemplo n.º 29
0
def gen_CNAME():
    """Migrates CNAME objects.

    .. note::
        Run this only after migrating other DNS objects for every zone.

    .. note::
        Because MAINTAIN is totally messed up, some hostnames in the CNAME
        table have ``.``'s in them, so the fully qualified domain name is
        created first, then the label is stripped off of the front of that.

    .. note::
        If the fully qualified domain name of the label + domain name already
        exists as a domain object, that object becomes the alias and the label
        prefix is set to the empty string. Otherwise, the alias is the
        label + domain name.

    :uniqueness: label, domain, target
    """
    print "Creating CNAMEs."
    sql = ("SELECT zone_cname.id, zone_cname.server, zone_cname.name, "
           "zone_cname.enabled, zone.name, domain.name FROM zone_cname "
           "JOIN zone ON zone_cname.zone = zone.id "
           "JOIN domain ON zone_cname.domain = domain.id")
    cursor.execute(sql)

    for pk, server, name, enabled, zone, dname in cursor.fetchall():
        server, name = server.lower(), name.lower()
        dname = dname.lower()

        fqdn = ".".join([name, dname])
        name, dname = fqdn.split(".", 1)

        if Domain.objects.filter(name=fqdn).exists():
            domain = Domain.objects.get(name=fqdn)
            name = ""
        elif Domain.objects.filter(name=dname).exists():
            domain = Domain.objects.get(name=dname)
        else:
            _, domain = get_label_domain_workaround(fqdn)

        if server == ".".join([name, domain.name]):
            # In maintain, at least one CNAME is a loop: biosys.bioe.orst.edu
            print "Ignoring CNAME %s: Is a loop." % server
            continue

        if CNAME.objects.filter(label=name, domain=domain).exists():
            c = CNAME.objects.get(label=name, domain=domain)
            if c.target != server:
                print ("ALERT: Conflicting CNAME with fqdn %s already exists."
                       % fqdn)
            continue

        ctnr = Zone.ctnr_from_zone_name(zone, 'CNAME')
        if ctnr is None:
            continue

        if ctnr not in domain.ctnr_set.all():
            print "CNAME %s has mismatching container for its domain." % pk
            continue

        cn = CNAME(label=name, domain=domain, target=server, ctnr=ctnr)
        cn.set_fqdn()
        dup_ptrs = PTR.objects.filter(fqdn=cn.fqdn)
        if dup_ptrs:
            print "Removing duplicate PTR for %s" % cn.fqdn
            dup_ptrs.delete(update_range_usage=False)

        # CNAMEs need to be cleaned independently of saving (no get_or_create)
        try:
            cn.full_clean()
            cn.save()
            if enabled:
                cn.views.add(public)
                cn.views.add(private)
        except ValidationError, e:
            print "Error:", e
Exemplo n.º 30
0
        print str(name) + " CNAME " + str(rdata)
        exists_domain = Domain.objects.filter(name=name)
        if exists_domain:
            label = ''
            domain = exists_domain[0]
        else:
            label = name.split('.')[0]
            domain_name = name.split('.')[1:]
            domain = ensure_domain('.'.join(domain_name))
        data = rdata.target.to_text().strip('.')

        if not CNAME.objects.filter(label=label, domain=domain,
                                    data=data).exists():
            cn = CNAME(label=label, domain=domain, data=data)
            cn.full_clean()
            cn.save()
            if views:
                for view in views:
                    cn.views.add(view)
                    cn.save()
    # TODO, records not done yet. TXT, SSHFP, AAAA
    # Create list
    for (name, ttl, rdata) in zone.iterate_rdatas('TXT'):
        name = name.to_text().strip('.')
        print str(name) + " TXT " + str(rdata)
        exists_domain = Domain.objects.filter(name=name)
        if exists_domain:
            label = ''
            domain = exists_domain[0]
        else:
            label = name.split('.')[0]
Exemplo n.º 31
0
def populate_forward_dns(svn_zones):
    for site, data in svn_zones.iteritems():
        zone, records = data
        print "-" * 15 + " " + site

        for (name, ttl, rdata) in zone.iterate_rdatas('SOA'):
            print str(name) + " SOA " + str(rdata)
            exists = SOA.objects.filter(
                minimum=rdata.minimum,
                contact=rdata.rname.to_text().strip('.'),
                primary=rdata.mname.to_text().strip('.'),
                comment="SOA for"
                " {0}.mozilla.com".format(site))
            if exists:
                soa = exists[0]
            else:
                soa = SOA(serial=rdata.serial,
                          minimum=rdata.minimum,
                          contact=rdata.rname.to_text().strip('.'),
                          primary=rdata.mname.to_text().strip('.'),
                          comment="SOA for"
                          " {0}.mozilla.com".format(site))
                soa.clean()
                soa.save()
            domain_split = list(reversed(name.to_text().strip('.').split('.')))
            for i in range(len(domain_split)):
                domain_name = domain_split[:i + 1]
                base_domain, created = Domain.objects.get_or_create(
                    name='.'.join(list(reversed(domain_name))))
            base_domain.soa = soa
            base_domain.save()
        """
            Algo for creating names and domains.
            Get all names.
            Sort by number of labels, longest first.
            For each name:
                if exists_domain(name):
                    label = ''
                    domain = name
                else:
                    label = name.split('.')[0]
                    domain_name = name.split('.')[1:]
                    if domain_name exists:
                        domain = domain_name
                    else:
                        domain = create(domain_name)
        """
        # Create list
        names = []
        for (name, ttl, rdata) in zone.iterate_rdatas('A'):
            names.append((name.to_text().strip('.'), rdata))
        sorted_names = list(
            sorted(names,
                   cmp=lambda n1, n2: -1
                   if len(n1[0].split('.')) > len(n2[0].split('.')) else 1))

        for name, rdata in sorted_names:
            print str(name) + " A " + str(rdata)
            exists_domain = Domain.objects.filter(name=name)
            if exists_domain:
                label = ''
                domain = exists_domain[0]
            else:
                label = name.split('.')[0]
                if label.find('unused') != -1:
                    continue
                parts = list(reversed(name.split('.')[1:]))
                domain_name = ''
                for i in range(len(parts)):
                    domain_name = parts[i] + '.' + domain_name
                    domain_name = domain_name.strip('.')
                    domain, created = Domain.objects.get_or_create(
                        name=domain_name)
                    if domain.master_domain and domain.master_domain.soa:
                        domain.soa = domain.master_domain.soa
            a, _ = AddressRecord.objects.get_or_create(label=label,
                                                       domain=domain,
                                                       ip_str=rdata.to_text(),
                                                       ip_type='4')

        for (name, ttl, rdata) in zone.iterate_rdatas('NS'):
            name = name.to_text().strip('.')
            print str(name) + " NS " + str(rdata)
            domain = ensure_domain(name)
            ns, _ = Nameserver.objects.get_or_create(
                domain=domain, server=rdata.target.to_text().strip('.'))
        for (name, ttl, rdata) in zone.iterate_rdatas('MX'):
            name = name.to_text().strip('.')
            print str(name) + " MX " + str(rdata)
            exists_domain = Domain.objects.filter(name=name)
            if exists_domain:
                label = ''
                domain = exists_domain[0]
            else:
                label = name.split('.')[0]
                domain_name = name.split('.')[1:]
                domain = ensure_domain(domain_name)
            priority = rdata.preference
            server = rdata.exchange.to_text().strip('.')
            mx, _ = MX.objects.get_or_create(label=label,
                                             domain=domain,
                                             server=server,
                                             priority=priority,
                                             ttl="3600")
        for (name, ttl, rdata) in zone.iterate_rdatas('CNAME'):
            name = name.to_text().strip('.')
            print str(name) + " CNAME " + str(rdata)
            exists_domain = Domain.objects.filter(name=name)
            if exists_domain:
                label = ''
                domain = exists_domain[0]
            else:
                label = name.split('.')[0]
                domain_name = name.split('.')[1:]
                domain = ensure_domain('.'.join(domain_name))
            data = rdata.target.to_text().strip('.')

            if not CNAME.objects.filter(label=label, domain=domain,
                                        data=data).exists():
                cn = CNAME(label=label, domain=domain, data=data)
                cn.full_clean()
                cn.save()
Exemplo n.º 32
0
def populate_forward_dns(svn_zones):
    for site, data in svn_zones.iteritems():
        zone, records = data
        print "-" * 15 + " " + site

        for (name, ttl, rdata) in zone.iterate_rdatas('SOA'):
            print str(name) + " SOA " + str(rdata)
            exists = SOA.objects.filter(minimum=rdata.minimum,
                                        contact=rdata.rname.to_text(
                                        ).strip('.'),
                                        primary=rdata.mname.to_text().strip('.'), comment="SOA for"
                                        " {0}.mozilla.com".format(site))
            if exists:
                soa = exists[0]
            else:
                soa = SOA(serial=rdata.serial, minimum=rdata.minimum,
                          contact=rdata.rname.to_text().strip('.'),
                          primary=rdata.mname.to_text().strip('.'), comment="SOA for"
                          " {0}.mozilla.com".format(site))
                soa.clean()
                soa.save()
            domain_split = list(reversed(name.to_text().strip('.').split('.')))
            for i in range(len(domain_split)):
                domain_name = domain_split[:i + 1]
                base_domain, created = Domain.objects.get_or_create(name=
                                                                    '.'.join(list(reversed(domain_name))))
            base_domain.soa = soa
            base_domain.save()

        """
            Algo for creating names and domains.
            Get all names.
            Sort by number of labels, longest first.
            For each name:
                if exists_domain(name):
                    label = ''
                    domain = name
                else:
                    label = name.split('.')[0]
                    domain_name = name.split('.')[1:]
                    if domain_name exists:
                        domain = domain_name
                    else:
                        domain = create(domain_name)
        """
        # Create list
        names = []
        for (name, ttl, rdata) in zone.iterate_rdatas('A'):
            names.append((name.to_text().strip('.'), rdata))
        sorted_names = list(sorted(names, cmp=lambda n1, n2: -1 if
                                   len(n1[0].split('.')) > len(n2[0].split('.')) else 1))

        for name, rdata in sorted_names:
            print str(name) + " A " + str(rdata)
            exists_domain = Domain.objects.filter(name=name)
            if exists_domain:
                label = ''
                domain = exists_domain[0]
            else:
                label = name.split('.')[0]
                if label.find('unused') != -1:
                    continue
                parts = list(reversed(name.split('.')[1:]))
                domain_name = ''
                for i in range(len(parts)):
                    domain_name = parts[i] + '.' + domain_name
                    domain_name = domain_name.strip('.')
                    domain, created = Domain.objects.get_or_create(name=
                                                                   domain_name)
                    if domain.master_domain and domain.master_domain.soa:
                        domain.soa = domain.master_domain.soa
            a, _ = AddressRecord.objects.get_or_create(label=label,
                                                       domain=domain, ip_str=rdata.to_text(), ip_type='4')

        for (name, ttl, rdata) in zone.iterate_rdatas('NS'):
            name = name.to_text().strip('.')
            print str(name) + " NS " + str(rdata)
            domain = ensure_domain(name)
            ns, _ = Nameserver.objects.get_or_create(domain=domain,
                                                     server=rdata.target.to_text().strip('.'))
        for (name, ttl, rdata) in zone.iterate_rdatas('MX'):
            name = name.to_text().strip('.')
            print str(name) + " MX " + str(rdata)
            exists_domain = Domain.objects.filter(name=name)
            if exists_domain:
                label = ''
                domain = exists_domain[0]
            else:
                label = name.split('.')[0]
                domain_name = name.split('.')[1:]
                domain = ensure_domain(domain_name)
            priority = rdata.preference
            server = rdata.exchange.to_text().strip('.')
            mx, _ = MX.objects.get_or_create(label=label, domain=domain,
                                             server=server, priority=priority, ttl="3600")
        for (name, ttl, rdata) in zone.iterate_rdatas('CNAME'):
            name = name.to_text().strip('.')
            print str(name) + " CNAME " + str(rdata)
            exists_domain = Domain.objects.filter(name=name)
            if exists_domain:
                label = ''
                domain = exists_domain[0]
            else:
                label = name.split('.')[0]
                domain_name = name.split('.')[1:]
                domain = ensure_domain('.'.join(domain_name))
            data = rdata.target.to_text().strip('.')

            if not CNAME.objects.filter(label=label, domain=domain,
                                        data=data).exists():
                cn = CNAME(label=label, domain=domain,
                           data=data)
                cn.full_clean()
                cn.save()