def test_bad_nameserver_soa_state_case_3_2(self): # This is Case 3 ... with ptrs root_domain = create_fake_zone("32.in-addr.arpa", suffix="") for ns in root_domain.nameserver_set.all(): ns.delete() soa = ns.domain.soa ns.domain.soa = None root_domain.soa = None # Shit's getting cached ns.domain.save() soa.delete() # At his point we should have a domain pointed at no SOA record with no # records attached to it. It also has no child domains. # Add a record to the domain. ptr = PTR(name="asdf", ip_str="32.1.1.1", ip_type="4") ptr.save() s = SOA(primary="asdf.asdf", contact="asdf.asdf", description="asdf") s.save() root_domain.soa = s self.assertRaises(ValidationError, root_domain.save)
def test_bad_nameserver_soa_state_case_3_0(self): # This is Case 3 root_domain = create_fake_zone("asdf30") for ns in root_domain.nameserver_set.all(): ns.delete() soa = ns.domain.soa ns.domain.soa = None root_domain.soa = None # Shit's getting cached ns.domain.save() soa.delete() # At his point we should have a domain pointed at no SOA record with no # records attached to it. It also has no child domains. # Add a record to the domain. a = AddressRecord(label='', domain=root_domain, ip_type="6", ip_str="1::") a.save() s = SOA(primary="asdf.asdf", contact="asdf.asdf", description="asdf") s.save() root_domain.soa = s self.assertRaises(ValidationError, root_domain.save)
def test_soa_perms(self): """ Test SOA perms """ self.setup_request() perm_table = { 'cyder_admin': ['all'], 'admin': [cy.ACTION_VIEW], 'user': [cy.ACTION_VIEW], 'guest': [cy.ACTION_VIEW], } # initialize obj into ctnrs obj = SOA() obj.primary = '192.168.1.1' obj.contact = '192.168.1.1' obj.save() domain = Domain(id=None, name='foo') domain.soa = obj domain.save() self.ctnr_admin.domains.add(domain) self.ctnr_user.domains.add(domain) self.ctnr_guest.domains.add(domain) self.save_all_ctnrs() self.check_perms_each_user(obj, perm_table)
def test_chain_soa_domain_add(self): data = {'primary': "ns1.foo.com", 'contact': "email.foo.com"} soa = SOA(**data) soa.save() d0 = Domain(name='com') d0.save() d1 = Domain(name='foo.com', soa=soa) d1.save() self.assertTrue(soa == d1.soa) d2 = Domain(name='bar.foo.com', soa=soa) d2.save() self.assertTrue(soa == d2.soa) d3 = Domain(name='new.foo.com', soa=soa) d3.save() self.assertTrue(soa == d3.soa) d4 = Domain(name='far.bar.foo.com', soa=soa) d4.save() self.assertTrue(soa == d4.soa) d5 = Domain(name='tee.new.foo.com', soa=soa) d5.save() self.assertTrue(soa == d5.soa) d5.delete() d4.delete() self.assertTrue(soa == d1.soa) self.assertTrue(soa == d2.soa) self.assertTrue(soa == d3.soa)
def test_bad_nameserver_soa_state_case_3_0(self): # This is Case 3 root_domain = create_fake_zone("asdf30") for ns in root_domain.nameserver_set.all(): ns.delete() soa = ns.domain.soa ns.domain.soa = None root_domain.soa = None # Shit's getting cached ns.domain.save() soa.delete() # At his point we should have a domain pointed at no SOA record with no # records attached to it. It also has no child domains. # Add a record to the domain. a = AddressRecord( label='', domain=root_domain, ip_type="6", ip_str="1::") a.save() s = SOA(primary="asdf.asdf", contact="asdf.asdf", description="asdf") s.save() root_domain.soa = s self.assertRaises(ValidationError, root_domain.save)
def test_soa_perms(self): """ Test SOA perms """ self.setup_request() perm_table = { 'cyder_admin': ['all'], 'admin': ['view'], 'user': ['view'], 'guest': ['view'], } # initialize obj into ctnrs obj = SOA() obj.primary = '192.168.1.1' obj.contact = '192.168.1.1' obj.save() domain = Domain(id=None, name='foo') domain.soa = obj domain.save() self.ctnr_admin.domains.add(domain) self.ctnr_user.domains.add(domain) self.ctnr_guest.domains.add(domain) self.save_all_ctnrs() self.check_perms_each_user(obj, perm_table)
def do_generic_add(self, primary, contact, retry, refresh, description): soa = SOA(primary=primary, contact=contact, retry=retry, refresh=refresh, description=description) soa.save() soa.save() rsoa = SOA.objects.filter(primary=primary, contact=contact, retry=retry, refresh=refresh) self.assertTrue(len(rsoa) == 1) return soa
def build_sample_domain(): domain_name = '' for i in range(2): domain_name = random_label() domain = Domain(name=domain_name) soa = SOA(primary=random_label(), contact="asf", comment=random_label()) soa.save() domain.soa = soa domain.save() return domain
class SOAViewTests(cyder.base.tests.TestCase): def setUp(self): self.test_obj = SOA(primary=random_label(), contact=random_label(), comment=random_label()) self.test_obj.save() def test_base_cydns_app_soa(self): resp = self.client.get(reverse('soa-list'), follow=True) self.assertEqual(resp.status_code, 200) def test_get_create_soa(self): resp = self.client.get(reverse('soa-create'), follow=True) self.assertEqual(resp.status_code, 200) def test_post_create_soa(self): resp = self.client.post(reverse('soa-create'), self.post_data(), follow=True) self.assertTrue(resp.status_code in (302, 200)) def test_get_object_update_soa(self): resp = self.client.get(reverse('soa-update', args=[self.test_obj.pk]), follow=True) self.assertEqual(resp.status_code, 200) def test_post_object_update_soa(self): resp = self.client.post(reverse('soa-update', args=[self.test_obj.pk]), self.post_data(), follow=True) self.assertTrue(resp.status_code in (302, 200)) def test_get_object_details_soa(self): resp = self.client.get(reverse('soa-detail', args=[self.test_obj.pk]), follow=True) self.assertEqual(resp.status_code, 200) def test_get_object_delete_soa(self): resp = self.client.get(reverse('soa-delete', args=[self.test_obj.pk]), follow=True) self.assertEqual(resp.status_code, 200) def post_data(self): return { 'primary': random_label(), 'contact': random_label(), 'retry': '123', 'refresh': '123', 'comment': random_label() }
def migrate_soa(zone, root_domain_name): 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}".format(root_domain_name)) 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}".format(root_domain_name)) soa.clean() soa.save() return soa
class SOAViewTests(TestCase): def setUp(self): self.url_slug = "soa" self.test_obj = SOA(primary=random_label(), contact=random_label(), comment=random_label()) self.test_obj.save() def test_base_app_soa(self): resp = self.client.get(CYDNS_BASE_URL + "/%s/" % (self.url_slug)) self.assertEqual(resp.status_code, 200) # url(r'^cyder/cydns/nameserver/create$', NSCreateView.as_view()), def test_get_create_soa(self): resp = self.client.get(CYDNS_BASE_URL + "/%s/create/" % (self.url_slug)) self.assertEqual(resp.status_code, 200) def test_post_create_soa(self): resp = self.client.post(CYDNS_BASE_URL + "/%s/create/" % (self.url_slug), self.post_data()) self.assertTrue(resp.status_code in (302, 200)) # url(r'^cyder/cydns/nameserver/(?P<pk>[\w-]+)/update$', NSUpdateView.as_view() ), def test_get_object_update_soa(self): resp = self.client.get(CYDNS_BASE_URL + "/%s/%s/update/" % (self.url_slug, self.test_obj.pk)) self.assertEqual(resp.status_code, 200) def test_post_object_update_soa(self): resp = self.client.post(CYDNS_BASE_URL + "/%s/%s/update/" % (self.url_slug, self.test_obj.pk), self.post_data()) self.assertTrue(resp.status_code in (302, 200)) def test_get_object_details_soa(self): resp = self.client.get(CYDNS_BASE_URL + "/%s/%s/" % (self.url_slug, self.test_obj.pk)) self.assertEqual(resp.status_code, 200) def test_get_object_delete_soa(self): resp = self.client.get(CYDNS_BASE_URL + "/%s/%s/delete/" % (self.url_slug, self.test_obj.pk)) self.assertEqual(resp.status_code, 200) def post_data(self): return { "primary": random_label(), "contact": random_label(), "retry": "123", "refresh": "123", "comment": random_label(), }
def test_ctnr_soa(self): """ Test being in ctnr /w soa record gives appropriate perms """ request = HttpRequest() request.user = self.test_user request.session = SessionStore() request.session['ctnr'] = self.ctnr # create domain with soa, add domain to ctnr soa = SOA() soa.primary = '192.168.1.1' soa.contact = '192.168.1.1' soa.save() domain = Domain(id=None, name='foo') domain.soa = soa domain.save() self.ctnr.domains.add(domain) self.ctnr.save() self.ctnr_admin.domains.add(domain) self.ctnr_admin.save() # checks where user is not admin has_perm = self.test_user.get_profile().has_perm(request, soa, write=False) self.assertTrue(has_perm, 'user should have read access') has_perm = self.test_user.get_profile().has_perm(request, soa, write=True) self.assertFalse(has_perm, 'user should not have write access') # checks where user is admin request.session = {'ctnr': self.ctnr_admin} has_perm = self.test_user.get_profile().has_perm(request, soa, write=False) self.assertTrue(has_perm, 'user should have read access') has_perm = self.test_user.get_profile().has_perm(request, soa, write=True) self.assertTrue(has_perm, 'user should have write access') # checks where obj not in ctnr request.session = {'ctnr': self.ctnr_empty} has_perm = self.test_user.get_profile().has_perm(request, soa, write=False) self.assertFalse(has_perm, 'user should not have read access') has_perm = self.test_user.get_profile().has_perm(request, soa, write=True) self.assertFalse(has_perm, 'user should not have write access')
def gen_SOA(self): """Generates an SOA record object if the SOA record exists. :uniqueness: primary, contact, refresh, retry, expire, minimum, comment """ if self.domain_id is None: return None cursor.execute("SELECT primary_master, hostmaster, refresh, " "retry, expire, ttl " "FROM soa " "WHERE domain = %s" % self.domain_id) record = cursor.fetchone() if record: primary, contact, refresh, retry, expire, minimum = record primary, contact = primary.lower(), contact.lower() try: soa = SOA.objects.get(root_domain=self.domain) except SOA.DoesNotExist: soa = SOA() soa.primary = primary soa.contact = contact soa.refresh = refresh soa.retry = retry soa.expire = expire soa.minimum = minimum soa.root_domain = self.domain soa.description = '' soa.save() return soa else: master_domain = self.domain.master_domain if master_domain and master_domain.soa: soa = master_domain.soa else: print "WARNING: No SOA exists for %s." % self.domain.name return None return soa
def test_soa_validators(self): m = self.create_domain(name='8') m.save() f_m = self.create_domain(name='8.2') f_m.save() n_f_m = self.create_domain(name='8.2.3') n_f_m.save() b_m = self.create_domain(name='8.3') b_m.save() s = SOA(primary="ns1.foo.com", contact="asdf", comment="test") s.save() f_m.soa = s f_m.save() b_m.soa = s self.assertRaises(ValidationError, b_m.save) n_f_m = Domain.objects.get(pk=n_f_m.pk) # Refresh object n_f_m.soa = s n_f_m.save() m.soa = s m.save() b_m = Domain.objects.get(pk=b_m.pk) # Refresh object b_m.soa = s b_m.save() m.soa = None self.assertRaises(ValidationError, m.save) s2 = SOA(primary="ns1.foo.com", contact="asdf", comment="test2") s2.save() m.soa = s2 self.assertRaises(ValidationError, m.save)
def test_soa_validators(self): m = ReverseDomain( name = '8') m.save() f_m = ReverseDomain( name = '8.2') f_m.save() n_f_m = ReverseDomain( name = '8.2.3') n_f_m.save() b_m = ReverseDomain( name = '8.3') b_m.save() s = SOA( primary="ns1.foo.com", contact="asdf", comment="test") s.save() f_m.soa = s f_m.save() b_m.soa = s self.assertRaises(ValidationError, b_m.save) n_f_m = ReverseDomain.objects.get(pk=n_f_m.pk) # Refresh object n_f_m.soa = s n_f_m.save() m.soa = s m.save() b_m = ReverseDomain.objects.get(pk=b_m.pk) # Refresh object b_m.soa = s b_m.save() m.soa = None self.assertRaises(ValidationError, m.save) s2 = SOA( primary="ns1.foo.com", contact="asdf", comment="test2") s2.save() m.soa = s2 self.assertRaises(ValidationError, m.save)
def test_soa_validators(self): m = Domain(name='moo') m.save() f_m = Domain(name='foo.moo') f_m.save() n_f_m = Domain(name='noo.foo.moo') n_f_m.save() b_m = Domain(name='baz.moo') b_m.save() s = SOA(primary="ns1.foo.com", contact="asdf", description="test") s.save() f_m.soa = s f_m.save() b_m.soa = s self.assertRaises(ValidationError, b_m.save) n_f_m = Domain.objects.get(pk=n_f_m.pk) # Refresh object n_f_m.soa = s n_f_m.save() m.soa = s m.save() b_m.soa = s b_m.save() m.soa = None self.assertRaises(ValidationError, m.save) s2 = SOA(primary="ns1.foo.com", contact="asdf", description="test2") s2.save() m.soa = s2 self.assertRaises(ValidationError, m.save)
def test_soa_perms(self): """Test SOA perms""" self.setup_request() perm_table = { 'cyder_admin': ['all'], 'admin': [ACTION_VIEW], 'user': [ACTION_VIEW], 'guest': [ACTION_VIEW], } # initialize obj into ctnrs domain = Domain(id=None, name='foo') domain.save() obj = SOA(root_domain=domain) obj.primary = 'foo.bar' obj.contact = 'foo.gaz' obj.save() self.ctnr_admin.domains.add(domain) self.ctnr_user.domains.add(domain) self.ctnr_guest.domains.add(domain) self.save_all_ctnrs() self.check_perms_each_user(obj, perm_table)
def test_add_soa(self): primary = "ns1.oregonstate.edu" contact = "admin.oregonstate.edu" retry = 1234 refresh = 1234123 description = "1" self.do_generic_add(primary, contact, retry, refresh, description=description) soa = SOA.objects.filter(primary=primary, contact=contact, retry=retry, refresh=refresh) soa[0].save() self.assertTrue(soa) soa[0].__repr__() soa = soa[0] self.assertTrue(soa.details()) primary = "do.com" contact = "admf.asdf" retry = 432152 refresh = 1235146134 description = "2" self.do_generic_add(primary, contact, retry, refresh, description=description) soa = SOA.objects.filter(primary=primary, contact=contact, retry=retry, refresh=refresh) self.assertTrue(soa) soa = soa[0] self.assertTrue(soa.details()) primary = "ns1.derp.com" contact = "admf.asdf" soa = SOA(primary=primary, contact=contact) soa.save() self.assertTrue( soa.serial and soa.expire and soa.retry and soa.refresh) self.assertTrue(soa.details())
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()
def create_zone_ajax(request): """ This view tries to create a new zone and returns an JSON with either 'success' = True or 'success' = False and some errors. By default all records are created and added to the public view. Throughout this function note that objects that are created are recorded, and if an error is caught, the previously created objects are deleted. This backing-out *may* be better handling by a transaction. Django has this sort of thing (middleware and decorators), but I'm in a time crunch so this manual deletetion will have to do. """ qd = request.POST.copy() # See if the domain exists. # Fail if it already exists or if it's under a delegated domain. root_domain = qd.get('root_domain', None) if not root_domain: error = "Please specify a root_domain" return HttpResponse(json.dumps({'success': False, 'error': error})) if Domain.objects.filter(name=root_domain).exists(): error = gt("<b>{0}</b> is already a domain. To make it a new zone, " "assign it a newly created SOA.".format(root_domain)) return HttpResponse(json.dumps({'success': False, 'error': error})) primary = qd.get('soa_primary', None) if not primary: error = "Please specify a primary nameserver for the SOA record." return HttpResponse(json.dumps({'success': False, 'error': error})) contact = qd.get('soa_contact', None) if not contact: error = "Please specify a contact address for the SOA record." return HttpResponse(json.dumps({'success': False, 'error': error})) contact.replace('@', '.') # Find all the NS entries nss = [] for k, v in request.POST.iteritems(): if k.startswith('nameserver_'): ttl = qd.get('ttl_{0}'.format(k[-1:]), 3600) server = v nss.append(Nameserver(server=server, ttl=ttl)) if not nss: # They must create at least one nameserver error = gt("You must choose an authoritative nameserver to serve this " "zone") return HttpResponse(json.dumps({'success': False, 'error': error})) # We want all domains created up to this point to inherit their # master_domain's soa. We will override the return domain's SOA. # Everything under this domain can be purgeable becase we will set this # domain to non-purgeable. This will also allow us to call prune tree. domain = ensure_domain(root_domain, purgeable=True, inherit_soa=False, force=True) soa = SOA(primary=primary, contact=contact, serial=int(time.time()), description="SOA for {0}".format(root_domain)) try: soa.save() except ValidationError, e: _clean_domain_tree(domain) return HttpResponse( json.dumps({ 'success': False, 'error': e.messages[0] }))
def populate_forward_dns(zone, root_domain, views=None): 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}".format(root_domain)) 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}".format(root_domain)) 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)))) null_all_soas(base_domain) base_domain.soa = soa base_domain.save() set_all_soas(base_domain, soa) 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('.') # We need to check for A records who have a name with this # domain. addrs = AddressRecord.objects.filter(fqdn=domain_name, ip_type='4') clober_objects = [] if addrs: for exists_a in addrs: # It got here. It exists need_to_recreate_a = True ip_str = exists_a.ip_str exists_a.delete(check_cname=False) a = AddressRecord(label='', ip_str=ip_str, ip_type='4') clober_objects.append(a) domain, created = Domain.objects.get_or_create(name= domain_name) for a in clober_objects: a.domain = domain a.clean() try: a.save() except Exception, e: pdb.set_trace() pass if created and domain.master_domain and domain.master_domain.soa: #domain.soa = domain.master_domain.soa #domain.save() null_all_soas(domain) set_all_soas(domain, domain.master_domain.soa) a, _ = AddressRecord.objects.get_or_create(label=label, domain=domain, ip_str=rdata.to_text(), ip_type='4') if views: for view in views: a.views.add(view) try: a.save() except Exception, e: pdb.set_trace() pass
def populate_reverse_dns(rev_svn_zones, views=None): arpa = create_domain(name='arpa') i_arpa = create_domain(name='in-addr.arpa') i6_arpa = create_domain(name='ipv6.arpa') for site, data in rev_svn_zones.iteritems(): site_path, more_data = data zone, records = more_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}.in-addr.arpa".format( '.'.join(reversed(site.split('.'))))) 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}.in-addr.arpa".format( '.'.join(reversed(site.split('.'))))) soa.clean() soa.save() name = name.to_text().replace('.IN-ADDR.ARPA.', '') domain_split = list(reversed(name.split('.'))) for i in range(len(domain_split)): domain_name = domain_split[:i + 1] rev_name = ip_to_domain_name( '.'.join(domain_name), ip_type='4') base_domain, created = Domain.objects.get_or_create( name=rev_name) #null_all_soas(base_domain) base_domain.soa = soa base_domain.save() #set_all_soas(base_domain, soa) for (name, ttl, rdata) in zone.iterate_rdatas('NS'): name = name.to_text().strip('.') name = name.replace('.IN-ADDR.ARPA', '') name = name.replace('.in-addr.arpa', '') print str(name) + " NS " + str(rdata) domain_name = '.'.join(list(reversed(name.split('.')))) domain = ensure_rev_domain(domain_name) ns, _ = Nameserver.objects.get_or_create(domain=domain, server=rdata.target.to_text().strip('.')) if views: for view in views: ns.views.add(view) ns.save() for (name, ttl, rdata) in zone.iterate_rdatas('PTR'): ip_str = name.to_text().strip('.') ip_str = ip_str.replace('.IN-ADDR.ARPA', '') ip_str = ip_str.replace('.in-addr.arpa', '') ip_str = '.'.join(list(reversed(ip_str.split('.')))) fqdn = rdata.target.to_text().strip('.') if fqdn.startswith('unused'): print "Skipping " + ip_str + " " + fqdn continue if ip_str == '10.2.171.IN': log("Skipping 10.2.171.IN", WARNING) continue print str(name) + " PTR " + str(fqdn) ptr = PTR.objects.filter(name=fqdn, ip_str=ip_str, ip_type='4') if ptr: ptr = ptr[0] else: try: ptr = PTR(name=fqdn, ip_str=ip_str, ip_type='4') ptr.full_clean() ptr.save() if views: for view in views: ptr.views.add(view) ptr.save() except Exception, e: pdb.set_trace() if views: for view in views: ptr.views.add(view)
class CNAMETests(TestCase): def setUp(self): primary = "ns5.oregonstate.edu" contact = "admin.oregonstate.edu" retry = 1234 refresh = 1234123 self.soa = SOA(primary = primary, contact = contact, retry = retry, refresh = refresh) self.soa.save() self.g = Domain(name = "gz") self.g.save() self.c_g = Domain(name = "coo.gz") self.c_g.soa = self.soa self.c_g.save() self.d = Domain(name = "dz") self.d.save() 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 def test_add(self): label = "foo" domain = self.g data = "foo.com" x = self.do_add(label, domain, data) label = "boo" domain = self.c_g data = "foo.foo.com" self.do_add(label, domain, data) label = "fo1" domain = self.g data = "foo.com" self.do_add(label, domain, data) self.assertRaises(ValidationError, self.do_add, *(label, domain, data)) label = "" domain = self.g data = "foo.com" self.do_add(label, domain, data) def test_soa_condition(self): label = "" domain = self.c_g data = "foo.com" self.assertRaises(ValidationError, self.do_add, *(label, domain, data)) def test_data_domain(self): label = "fo1" domain = self.g data = "foo.dz" cn = self.do_add(label, domain, data) self.assertTrue(self.d == cn.data_domain) def test_add_bad(self): label = "" domain = self.g data = "..foo.com" self.assertRaises(ValidationError, self.do_add, *(label, domain, data)) def test_add_mx_with_cname(self): label = "cnamederp1" domain = self.c_g data = "foo.com" fqdn = label+'.'+domain.name data = { 'label':'' ,'domain':self.c_g ,'server':fqdn ,'priority':2 ,'ttl':2222 } mx = MX(**data) mx.save() cn = CNAME(label = label, domain = domain, data = data) self.assertRaises(ValidationError, cn.full_clean) def test_address_record_exists(self): label = "testyfoo" data = "wat" dom,_ = Domain.objects.get_or_create(name="cd") dom,_ = Domain.objects.get_or_create(name="what.cd") rec, _ = AddressRecord.objects.get_or_create(label=label, domain=dom, ip_type='4', ip_str="128.193.1.1") cn = CNAME(label = label, domain = dom, data = data) self.assertRaises(ValidationError, cn.full_clean) def test_address_record_cname_exists(self): label = "testyfoo" data = "wat" dom,_ = Domain.objects.get_or_create(name="cd") dom,_ = Domain.objects.get_or_create(name="what.cd") cn = CNAME.objects.get_or_create(label = label, domain = dom, data = data) rec = AddressRecord(label=label, domain=dom, ip_str="128.193.1.1") self.assertRaises(ValidationError, rec.save) def test_srv_exists(self): label = "_testyfoo" data = "wat" dom,_ = Domain.objects.get_or_create(name="cd") dom,_ = Domain.objects.get_or_create(name="what.cd") rec, _ = SRV.objects.get_or_create(label=label, domain=dom, target="asdf", \ port=2, priority=2, weight=4) cn = CNAME(label = label, domain = dom, data = data) self.assertRaises(ValidationError, cn.full_clean) def test_srv_cname_exists(self): label = "testyfoo" data = "wat" dom,_ = Domain.objects.get_or_create(name="cd") dom,_ = Domain.objects.get_or_create(name="what.cd") cn = CNAME.objects.get_or_create(label = label, domain = dom, data = data) rec = SRV(label=label, domain=dom, target="asdf", \ port=2, priority=2, weight=4) self.assertRaises(ValidationError, rec.save) def test_txt_exists(self): label = "testyfoo" data = "wat" dom,_ = Domain.objects.get_or_create(name="cd") dom,_ = Domain.objects.get_or_create(name="what.cd") rec, _ = TXT.objects.get_or_create(label=label, domain=dom, txt_data="asdf") cn = CNAME(label = label, domain = dom, data = data) self.assertRaises(ValidationError, cn.full_clean) def test_txt_cname_exists(self): label = "testyfoo" data = "wat" dom,_ = Domain.objects.get_or_create(name="cd") dom,_ = Domain.objects.get_or_create(name="what.cd") cn,_ = CNAME.objects.get_or_create(label = label, domain = dom, data = data) cn.full_clean() cn.save() rec = TXT(label=label, domain=dom, txt_data="asdf1") self.assertRaises(ValidationError, rec.save) def test_mx_exists(self): label = "testyfoo" data = "wat" dom,_ = Domain.objects.get_or_create(name="cd") dom,_ = Domain.objects.get_or_create(name="what.cd") rec, _ = MX.objects.get_or_create(label=label, domain=dom, server="asdf",\ priority=123, ttl=123) cn = CNAME(label = label, domain = dom, data = data) self.assertRaises(ValidationError, cn.full_clean) def test_mx_cname_exists(self): # Duplicate test? label = "testyfoo" data = "wat" dom,_ = Domain.objects.get_or_create(name="cd") dom,_ = Domain.objects.get_or_create(name="what.cd") cn,_ = CNAME.objects.get_or_create(label = label, domain = dom, data = data) cn.full_clean() cn.save() rec = MX(label=label, domain=dom, server="asdf1",\ priority=123, ttl=123) self.assertRaises(ValidationError, rec.save)
class CNAMETests(cyder.base.tests.TestCase): def create_domain(self, name, ip_type=None, delegated=False): if ip_type is None: ip_type = '4' if name in ('arpa', 'in-addr.arpa', 'ipv6.arpa'): pass else: name = ip_to_domain_name(name, ip_type=ip_type) d = Domain(name=name, delegated=delegated) d.clean() self.assertTrue(d.is_reverse) return d def setUp(self): primary = "ns5.oregonstate.edu" contact = "admin.oregonstate.edu" retry = 1234 refresh = 1234123 self.soa = SOA(primary=primary, contact=contact, retry=retry, refresh=refresh) self.soa.save() self.g = Domain(name="gz") self.g.save() self.c_g = Domain(name="coo.gz") self.c_g.soa = self.soa self.c_g.save() self.d = Domain(name="dz") self.d.save() self.arpa = self.create_domain(name='arpa') self.arpa.save() self.i_arpa = self.create_domain(name='in-addr.arpa') self.i_arpa.save() self.r1 = self.create_domain(name="10") self.r1.save() self.s = System() self.s.save() 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.get_detail_url()) self.assertTrue(cn.get_update_url()) self.assertTrue(cn.get_delete_url()) self.assertTrue(cn.details()) cs = CNAME.objects.filter( label=label, domain=domain, target=data) self.assertEqual(len(cs), 1) return cn def test_add(self): label = "foo" domain = self.g data = "foo.com" x = self.do_add(label, domain, data) label = "boo" domain = self.c_g data = "foo.foo.com" self.do_add(label, domain, data) label = "fo1" domain = self.g data = "foo.com" self.do_add(label, domain, data) self.assertRaises(ValidationError, self.do_add, *(label, domain, data)) label = "" domain = self.g data = "foo.com" self.do_add(label, domain, data) def test1_add_glob(self): label = "*foo" domain = self.g data = "foo.com" x = self.do_add(label, domain, data) label = "*" domain = self.c_g data = "foo.foo.com" self.do_add(label, domain, data) label = "*.fo1" domain = self.g data = "foo.com" self.assertRaises(ValidationError, self.do_add, *(label, domain, data)) label = "*sadfasfd-asdf" domain = self.g data = "foo.com" self.do_add(label, domain, data) def test2_add_glob(self): label = "*coo" domain = self.g data = "foo.com" x = self.do_add(label, domain, data) label = "*" domain = self.c_g data = "foo.com" x = self.do_add(label, domain, data) def test_soa_condition(self): label = "" domain = self.c_g data = "foo.com" self.assertRaises(ValidationError, self.do_add, *(label, domain, data)) def test_data_domain(self): label = "fo1" domain = self.g data = "foo.dz" cn = self.do_add(label, domain, data) self.assertTrue(self.d == cn.target_domain) def test_add_bad(self): label = "" domain = self.g data = "..foo.com" self.assertRaises(ValidationError, self.do_add, *(label, domain, data)) def test_add_mx_with_cname(self): label = "cnamederp1" domain = self.c_g data = "foo.com" fqdn = label + '.' + domain.name mx_data = {'label': '', 'domain': self.c_g, 'server': fqdn, 'priority': 2, 'ttl': 2222} mx = MX(**mx_data) mx.save() cn = CNAME(label=label, domain=domain, target=data) self.assertRaises(ValidationError, cn.full_clean) def test_address_record_exists(self): label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec, _ = AddressRecord.objects.get_or_create( label=label, domain=dom, ip_type='4', ip_str="128.193.1.1") cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean) def test_address_record_cname_exists(self): label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") cn = CNAME.objects.get_or_create( label=label, domain=dom, target=data) rec = AddressRecord(label=label, domain=dom, ip_str="128.193.1.1") self.assertRaises(ValidationError, rec.save) def test_srv_exists(self): label = "_testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec, _ = SRV.objects.get_or_create( label=label, domain=dom, target="asdf", port=2, priority=2, weight=4) cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean) def test_srv_cname_exists(self): label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") cn = CNAME.objects.get_or_create( label=label, domain=dom, target=data) rec = SRV(label=label, domain=dom, target="asdf", port=2, priority=2, weight=4) self.assertRaises(ValidationError, rec.save) def test_txt_exists(self): label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec, _ = TXT.objects.get_or_create( label=label, domain=dom, txt_data="asdf") cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean) def test_txt_cname_exists(self): label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") cn, _ = CNAME.objects.get_or_create( label=label, domain=dom, target=data) cn.full_clean() cn.save() rec = TXT(label=label, domain=dom, txt_data="asdf1") self.assertRaises(ValidationError, rec.save) def test_mx_exists(self): label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec, _ = MX.objects.get_or_create( label=label, domain=dom, server="asdf", priority=123, ttl=123) cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean) def test_mx_cname_exists(self): # Duplicate test? label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") cn, _ = CNAME.objects.get_or_create( label=label, domain=dom, target=data) cn.full_clean() cn.save() rec = MX(label=label, domain=dom, server="asdf1", priority=123, ttl=123) self.assertRaises(ValidationError, rec.save) def test_ns_exists(self): # Duplicate test? label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec = Nameserver(domain=dom, server="asdf1") rec.save() cn = CNAME(label='', domain=dom, target=data) self.assertRaises(ValidationError, cn.clean) def test_ns_cname_exists(self): # Duplicate test? label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") cn, _ = CNAME.objects.get_or_create( label='', domain=dom, target=data) cn.full_clean() cn.save() rec = Nameserver(domain=dom, server="asdf1") self.assertRaises(ValidationError, rec.save) def test_intr_exists(self): label = "tdfestyfoo" data = "waasdft" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") intr = StaticInterface(label=label, domain=dom, ip_str="10.0.0.1", ip_type='4', system=self.s, mac="11:22:33:44:55:66") intr.clean() intr.save() cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean) def test_intr_cname_exists(self): # Duplicate test? label = "tesafstyfoo" data = "wadfakt" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") cn, _ = CNAME.objects.get_or_create( label=label, domain=dom, target=data) cn.full_clean() cn.save() intr = StaticInterface(label=label, domain=dom, ip_str="10.0.0.2", ip_type='4', system=self.s, mac="00:11:22:33:44:55") self.assertRaises(ValidationError, intr.clean) cn.label = "differentlabel" cn.save() intr.clean() intr.save()
def populate_forward_dns(zone, root_domain, views=None): 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}".format(root_domain)) 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}".format(root_domain)) 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)))) null_all_soas(base_domain) base_domain.soa = soa base_domain.save() set_all_soas(base_domain, soa) 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('.') # We need to check for A records who have a name with this # domain. addrs = AddressRecord.objects.filter(fqdn=domain_name, ip_type='4') clober_objects = [] if addrs: for exists_a in addrs: # It got here. It exists need_to_recreate_a = True ip_str = exists_a.ip_str exists_a.delete(check_cname=False) a = AddressRecord(label='', ip_str=ip_str, ip_type='4') clober_objects.append(a) domain, created = Domain.objects.get_or_create( name=domain_name) for a in clober_objects: a.domain = domain a.clean() try: a.save() except Exception, e: pdb.set_trace() pass if created and domain.master_domain and domain.master_domain.soa: #domain.soa = domain.master_domain.soa #domain.save() null_all_soas(domain) set_all_soas(domain, domain.master_domain.soa) a, _ = AddressRecord.objects.get_or_create(label=label, domain=domain, ip_str=rdata.to_text(), ip_type='4') if views: for view in views: a.views.add(view) try: a.save() except Exception, e: pdb.set_trace() pass
def create_zone_ajax(request): """This view tries to create a new zone and returns an JSON with either 'success' = True or 'success' = False and some errors. By default all records are created and added to the public view. Throughout this function note that objects that are created are recorded, and if an error is caught, the previously created objects are deleted. This backing-out *may* be better handling by a transaction. Django has this sort of thing (middleware and decorators), but I'm in a time crunch so this manual deletetion will have to do. """ qd = request.POST.copy() # See if the domain exists. # Fail if it already exists or if it's under a delegated domain. root_domain = qd.get('root_domain', None) if not root_domain: error = "Please specify a root_domain" return HttpResponse(json.dumps({'success': False, 'error': error})) if Domain.objects.filter(name=root_domain).exists(): error = gt("<b>{0}</b> is already a domain. To make it a new zone, " "assign it a newly created SOA.".format(root_domain)) return HttpResponse(json.dumps({'success': False, 'error': error})) primary = qd.get('soa_primary', None) if not primary: error = "Please specify a primary nameserver for the SOA record." return HttpResponse(json.dumps({'success': False, 'error': error})) contact = qd.get('soa_contact', None) if not contact: error = "Please specify a contact address for the SOA record." return HttpResponse(json.dumps({'success': False, 'error': error})) contact.replace('@', '.') # Find all the NS entries nss = [] for k, v in request.POST.iteritems(): if k.startswith('nameserver_'): ttl = qd.get('ttl_{0}'.format(k[-1:]), 3600) server = v nss.append(Nameserver(server=server, ttl=ttl)) if not nss: # They must create at least one nameserver error = gt("You must choose an authoritative nameserver to serve this " "zone") return HttpResponse(json.dumps({'success': False, 'error': error})) # We want all domains created up to this point to inherit their # master_domain's soa. We will override the return domain's SOA. # Everything under this domain can be purgeable becase we will set this # domain to non-purgeable. This will also allow us to call prune tree. domain = ensure_domain(root_domain, purgeable=True, inherit_soa=False, force=True) soa = SOA(primary=primary, contact=contact, serial=int(time.time()), description="SOA for {0}".format(root_domain)) try: soa.save() except ValidationError, e: _clean_domain_tree(domain) return HttpResponse(json.dumps({'success': False, 'error': e.messages[0]}))
def populate_reverse_dns(rev_svn_zones): arpa = create_domain(name='arpa') i_arpa = create_domain(name='in-addr.arpa') i6_arpa = create_domain(name='ipv6.arpa') for site, data in rev_svn_zones.iteritems(): site_path, more_data = data zone, records = more_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}.in-addr.arpa".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}.in-addr.arpa".format(site)) soa.clean() soa.save() name = name.to_text().replace('.IN-ADDR.ARPA.', '') domain_split = list(reversed(name.split('.'))) for i in range(len(domain_split)): domain_name = domain_split[:i + 1] rev_name = ip_to_domain_name('.'.join(domain_name), ip_type='4') base_domain, created = Domain.objects.get_or_create( name=rev_name) base_domain.soa = soa base_domain.save() for (name, ttl, rdata) in zone.iterate_rdatas('NS'): name = name.to_text().strip('.') name = name.replace('.IN-ADDR.ARPA', '') name = name.replace('.in-addr.arpa', '') print str(name) + " NS " + str(rdata) domain_name = '.'.join(list(reversed(name.split('.')))) domain = ensure_rev_domain(domain_name) ns, _ = Nameserver.objects.get_or_create( domain=domain, server=rdata.target.to_text().strip('.')) for (name, ttl, rdata) in zone.iterate_rdatas('PTR'): ip_str = name.to_text().strip('.') ip_str = ip_str.replace('.IN-ADDR.ARPA', '') ip_str = ip_str.replace('.in-addr.arpa', '') ip_str = '.'.join(list(reversed(ip_str.split('.')))) fqdn = rdata.target.to_text().strip('.') if fqdn.startswith('unused'): print "Skipping " + ip_str + " " + fqdn continue if ip_str == '10.2.171.IN': log("Skipping 10.2.171.IN", WARNING) continue print str(name) + " PTR " + str(fqdn) ptr = PTR.objects.filter(name=fqdn, ip_str=ip_str, ip_type='4') if ptr: continue else: ptr = PTR(name=fqdn, ip_str=ip_str, ip_type='4') ptr.full_clean() ptr.save() """
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()