def do_generic_add(self, ip_str, fqdn, ip_type, domain=None): ret = PTR(name=fqdn, ip_str=ip_str, ip_type=ip_type) ret.full_clean() ret.save() self.assertTrue(ret.details()) self.assertTrue(ret.get_detail_url()) self.assertTrue(ret.get_update_url()) self.assertTrue(ret.get_delete_url()) ip = Ip(ip_str=ip_str, ip_type=ip_type) ip.clean_ip() ptr = PTR.objects.filter(name=fqdn, ip_upper=ip.ip_upper, ip_lower=ip.ip_lower) ptr.__repr__() self.assertTrue(ptr) ip_str = ip_str.lower() self.assertEqual(ptr[0].ip_str, ip_str) if domain: if ptr[0].name == "": self.assertEqual(fqdn, domain.name) else: self.assertEqual(fqdn, ptr[0].name + "." + domain.name) else: self.assertEqual(fqdn, ptr[0].name) return ret
def gen_domain( self, domain, dname, records, rdomain ): if domain == 0 or dname == "root": print "Root domain, skipping" return else: print "Generating %s" % (dname) #if not re.search( "^10" ,self.ip_from_domainname(dname) ): # return self.gen_NS( domain, dname, rdomain ) records_to_remove = [] search_string = "^"+self.ip_from_domainname(dname).replace('.','\.')+"\." for record in records: longip, name = record ip = long2ip(longip) # TODO compile this if re.search( search_string, ip ): #self.printer.print_PTR( ip, name ) possible = PTR.objects.filter( ip_str = ip, name=name, ip_type='4' ) if possible: ptr = possible[0] else: ptr = PTR( ip_str = ip, name=name, ip_type='4' ) ptr.full_clean() ptr.save() records_to_remove.append( record ) for record in records_to_remove: records.remove(record) self.gen_ORIGIN( domain, dname, 999 )
def test_look_for_cnames_ptrs(self): name = "sucks1" a_dom,_ = Domain.objects.get_or_create( name = name, delegated=False ) a_dom.save() name = "adsfme1" c_dom,_ = Domain.objects.get_or_create( name = name, delegated=False ) c_dom.save() cn, _ = CNAME.objects.get_or_create(domain=c_dom, label="nddo", data="really.teebow.sucks1") cn.full_clean() cn.save() ptr = PTR(ip_str="128.193.2.1", name="seriously.teebow.sucks1", ip_type='4') ptr.full_clean() ptr.save() name = "teebow.sucks1" b_dom,_ = Domain.objects.get_or_create( name = name, delegated=False ) b_dom.save() cn = CNAME.objects.get(pk=cn.pk) ptr = PTR.objects.get(pk=ptr.pk) self.assertTrue( cn.data_domain == b_dom ) self.assertTrue( ptr.data_domain == b_dom )
def test_dirty_ptr(self): self.rsoa.dirty = False self.rdom.dirty = False c = PTR(name="asfd", ip_str="123.123.123.123", ip_type="4") c.full_clean() c.save() self.rdom = ReverseDomain.objects.get(pk=self.rdom.pk) self.assertTrue(self.rdom.dirty) self.assertFalse(self.rsoa.dirty)
def gen_AR(self): """ Generates the Address Record and PTR objects related to this zone's domain. .. note:: Some AddressRecords may need to be added to the pointer table in MAINTAIN for successful migration, for example, cob-dc81 and cob-dc82.bus.oregonstate.edu .. note:: AddressRecords/PTRs with the same ip as a StaticInterface can't coexist, so if a StaticInterface with the same ip exists, it has priority. :AddressRecord uniqueness: label, domain, ip_str, ip_type :PTR uniqueness: name, ip_str, ip_type """ name = self.domain.name cursor.execute("SELECT ip, hostname, type, enabled " "FROM pointer " "WHERE hostname LIKE '%%.%s';" % name) for ip, hostname, ptr_type, enabled, in cursor.fetchall(): hostname = hostname.lower() label, dname = hostname.split('.', 1) if dname != name: continue if StaticInterface.objects.filter(ip_str=long2ip(ip)).exists(): continue if ptr_type == 'forward': arec, _ = AddressRecord.objects.get_or_create( label=label, domain=self.domain, ip_str=long2ip(ip), ip_type='4') if enabled: arec.views.add(public) elif ptr_type == 'reverse': if not PTR.objects.filter(name=name, ip_str=long2ip(ip)).exists(): ptr = PTR(name=name, ip_str=long2ip(ip), ip_type='4') # PTRs need to be cleaned independently of saving # (no get_or_create) ptr.full_clean() ptr.save() if enabled: ptr.views.add(public)
def do_generic_remove(self, ip, fqdn, ip_type): ptr = PTR(ip_str=ip, name=fqdn, ip_type=ip_type) ptr.full_clean() ptr.save() ptr.delete() ip = Ip(ip_str=ip, ip_type=ip_type) ip.clean_ip() ptr = PTR.objects.filter(name=fqdn, ip_upper=ip.ip_upper, ip_lower=ip.ip_lower) self.assertFalse(ptr)
def test_ptr_exists(self): label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec = PTR(ip_str="10.193.1.1", ip_type='4', name='testyfoo.what.cd') rec.full_clean() rec.save() cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean)
def gen_AR(self): """ Generates the Address Record and PTR objects related to this zone's domain. .. note:: Some AddressRecords may need to be added to the pointer table in MAINTAIN for successful migration, for example, cob-dc81 and cob-dc82.bus.oregonstate.edu .. note:: AddressRecords/PTRs with the same ip as a StaticInterface can't coexist, so if a StaticInterface with the same ip exists, it has priority. :AddressRecord uniqueness: label, domain, ip_str, ip_type :PTR uniqueness: name, ip_str, ip_type """ name = self.domain.name cursor.execute("SELECT ip, hostname, type, enabled " "FROM pointer " "WHERE hostname LIKE '%%.%s';" % name) for ip, hostname, ptr_type, enabled in cursor.fetchall(): hostname = hostname.lower() label, dname = hostname.split(".", 1) if dname != name: continue if StaticInterface.objects.filter(ip_str=long2ip(ip)).exists(): continue if ptr_type == "forward": arec, _ = AddressRecord.objects.get_or_create( label=label, domain=self.domain, ip_str=long2ip(ip), ip_type="4" ) if enabled: arec.views.add(public) elif ptr_type == "reverse": if not PTR.objects.filter(name=name, ip_str=long2ip(ip)).exists(): ptr = PTR(name=name, ip_str=long2ip(ip), ip_type="4") # PTRs need to be cleaned independently of saving # (no get_or_create) ptr.full_clean() ptr.save() if enabled: ptr.views.add(public)
def test_remove_domain_with_child_objects(self): """Removing a domain should remove CNAMES and PTR records that have data in that domain.""" name = "sucks" a_dom, _ = Domain.objects.get_or_create(name=name, delegated=False) a_dom.save() name = "teebow.sucks" b_dom, _ = Domain.objects.get_or_create(name=name, delegated=False) b_dom.save() name = "adsfme" c_dom, _ = Domain.objects.get_or_create(name=name, delegated=False) c_dom.save() cn, _ = CNAME.objects.get_or_create(domain=c_dom, label="nddo", target="really.teebow.sucks") cn.full_clean() cn.save() ptr = PTR( ip_str="128.193.2.1", name="seriously.teebow.sucks", ip_type='4') ptr.full_clean() ptr.save() self.assertTrue(cn.target_domain == b_dom) self.assertTrue(ptr.data_domain == b_dom) b_dom, _ = Domain.objects.get_or_create( name="teebow.sucks", delegated=False) b_dom.delete() try: cn = CNAME.objects.get(pk=cn.pk) except: self.fail("CNAME was deleted.") self.assertTrue(cn.target_domain == a_dom) try: ptr = PTR.objects.get(pk=ptr.pk) except: self.fail("PTR was deleted") self.assertTrue(ptr.data_domain == a_dom)
def migrate_PTR(zone, root_domain, soa, views): for (name, ttl, rdata) in zone.iterate_rdatas('PTR'): fqdn = rdata.target.to_text().strip('.') if fqdn.find('unused') != -1: print "Skipping " + name.to_text() + " " + fqdn continue # 4.3.2.1.IN-ADDR.ARPA. --> 1.2.3.4 name = name.to_text().lower().strip('.') if name.endswith('.in-addr.arpa'): ip_type = '4' ip_str = name.replace('.in-addr.arpa', '') elif name.endswith('.ipv6.arpa'): ip_type = '6' ip_str = name.replace('.ipv6.arpa', '') raise NotImplemented("Ipv6 migration isn't done yet.") else: print "We so f****d. Lol" pdb.set_trace() continue ip_str = '.'.join(list(reversed(ip_str.split('.')))) if ip_str == '10.2.171.IN': print "Skipping " + ip_str + " " + fqdn 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: 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)
def gen_AR(self): """ Generates the Address Record and PTR objects related to this zone's domain. .. note:: Some AddressRecords may need to be added to the pointer table in MAINTAIN for successful migration, for example, cob-dc81 and cob-dc82.bus.oregonstate.edu .. note:: AddressRecords/PTRs with the same ip as a StaticInterface can't coexist, so if a StaticInterface with the same ip exists, it has priority. :AddressRecord uniqueness: label, domain, ip_str, ip_type :PTR uniqueness: name, ip_str, ip_type """ name = self.domain.name cursor.execute("SELECT ip, hostname, type, zone.name, enabled " "FROM pointer JOIN zone ON pointer.zone = zone.id " "WHERE hostname LIKE '%%.%s';" % name) for ip, hostname, ptr_type, zone, enabled, in cursor.fetchall(): hostname = hostname.lower() label, dname = hostname.split('.', 1) if dname != name: continue dup_stats = StaticInterface.objects.filter(ip_str=long2ip(ip)) if dup_stats.exists(): if ptr_type == 'reverse': print "Ignoring PTR %s; Static intr exists." % long2ip(ip) continue elif dup_stats.filter(fqdn=hostname).exists(): print "Ignoring AR %s; Static intr exists." % hostname continue else: pass ctnr = self.ctnr_from_zone_name(zone, 'AR/PTR') if ctnr is None: continue if ptr_type == 'forward': if AddressRecord.objects.filter( fqdn=hostname, ip_str=long2ip(ip)).exists(): continue try: arec, _ = range_usage_get_create( AddressRecord, label=label, domain=self.domain, ip_str=long2ip(ip), ip_type='4', ctnr=ctnr) except ValidationError, e: print "Could not migrate AR %s: %s" % (hostname, e) continue if enabled: arec.views.add(public) arec.views.add(private) elif ptr_type == 'reverse': if not PTR.objects.filter(ip_str=long2ip(ip)).exists(): ptr = PTR(fqdn=hostname, ip_str=long2ip(ip), ip_type='4', ctnr=ctnr) # PTRs need to be cleaned independently of saving # (no get_or_create) try: ptr.full_clean() except ValidationError, e: print "Could not migrate PTR %s: %s" % (ptr.ip_str, e) continue ptr.save(update_range_usage=False) if enabled: ptr.views.add(public) ptr.views.add(private)
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)
r, _ = ReverseDomain.objects.get_or_create(name='128.193') r.soa = s r.save() # ReverseNameserver rns, _ = ReverseNameserver.objects.get_or_create( reverse_domain= r, server = random_label()+"."+random_label()+"."+random_label() ) r1, _ = ReverseDomain.objects.get_or_create(name='128.193.15') r1.soa = s1 r1.save() # ReverseNameserver rns, _ = ReverseNameserver.objects.get_or_create( reverse_domain= r1, server = random_label()+"."+random_label()+"."+random_label() ) for i in range(0, 100): try: ptr= PTR( name = random_label(), ip_str= random_ipv4('128.193'), ip_type = '4') except: continue ptr.full_clean() ptr.save() for i in range(0, 100): try: ptr = PTR( name = random_label(), ip_str= random_ipv4('128.193.15'), ip_type = '4') except: continue ptr.full_clean() ptr.save()
def add_ptr_ipv6(self, ip): ptr = PTR(name=random_label(), ip_str=ip, ip_type='6') ptr.full_clean() ptr.save() return ptr
def add_ptr_ipv6(self, ip): ptr = PTR(name = random_label(), ip_str = ip, ip_type='6') ptr.full_clean() ptr.save() return ptr
def gen_AR(self): """ Generates the Address Record and PTR objects related to this zone's domain. .. note:: Some AddressRecords may need to be added to the pointer table in MAINTAIN for successful migration, for example, cob-dc81 and cob-dc82.bus.oregonstate.edu .. note:: AddressRecords/PTRs with the same ip as a StaticInterface can't coexist, so if a StaticInterface with the same ip exists, it has priority. :AddressRecord uniqueness: label, domain, ip_str, ip_type :PTR uniqueness: name, ip_str, ip_type """ name = self.domain.name cursor.execute("SELECT ip, hostname, type, zone.name, enabled " "FROM pointer JOIN zone ON pointer.zone = zone.id " "WHERE hostname LIKE '%%.%s';" % name) for ip, hostname, ptr_type, zone, enabled, in cursor.fetchall(): hostname = hostname.lower() label, dname = hostname.split('.', 1) if dname != name: continue dup_stats = StaticInterface.objects.filter(ip_str=long2ip(ip)) if dup_stats.exists(): if ptr_type == 'reverse': print "Ignoring PTR %s; Static intr exists." % long2ip(ip) continue elif dup_stats.filter(fqdn=hostname).exists(): print "Ignoring AR %s; Static intr exists." % hostname continue else: pass ctnr = self.ctnr_from_zone_name(zone, 'AR/PTR') if ctnr is None: continue if ptr_type == 'forward': if AddressRecord.objects.filter(fqdn=hostname, ip_str=long2ip(ip)).exists(): continue try: arec, _ = range_usage_get_create(AddressRecord, label=label, domain=self.domain, ip_str=long2ip(ip), ip_type='4', ctnr=ctnr) except ValidationError, e: print "Could not migrate AR %s: %s" % (hostname, e) continue if enabled: arec.views.add(public) arec.views.add(private) elif ptr_type == 'reverse': if not PTR.objects.filter(ip_str=long2ip(ip)).exists(): ptr = PTR(fqdn=hostname, ip_str=long2ip(ip), ip_type='4', ctnr=ctnr) # PTRs need to be cleaned independently of saving # (no get_or_create) try: ptr.full_clean() except ValidationError, e: print "Could not migrate PTR %s: %s" % (ptr.ip_str, e) continue ptr.save(update_range_usage=False) if enabled: ptr.views.add(public) ptr.views.add(private)
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() """