Ejemplo n.º 1
0
 def test2_ipv6_to_longs(self):
     ip_upper_str = "aFFF:FaFF:FFaF:FFFa"
     ip_lower_str = "0000:0000:0000:0000"
     ip_upper, ip_lower = ipv6_to_longs(ip_upper_str + ":" + ip_lower_str)
     self.assertEqual(hex(ip_upper).lower(), "0x%sl" % (ip_upper_str.lower()
         .replace(':', '')))
     self.assertEqual(hex(ip_lower).lower(), "0x0l")
Ejemplo n.º 2
0
    def _do_generic_update_test(self, record, new_name, new_ip, ip_type):
        if new_ip:
            if ip_type == '4':
                ip_upper, ip_lower = 0, ipaddr.IPv4Address(new_ip).__int__()
            else:
                ip_upper, ip_lower = ipv6_to_longs(new_ip)
        else:
            ip_upper, ip_lower = record.ip_upper, record.ip_lower

        if new_name is not None and new_ip is not None:
            aret = AddressRecord.objects.filter(
                label=new_name, ip_upper=ip_upper,
                ip_lower=ip_lower, ip_type=ip_type)[0]
        elif new_name is not None:
            # Just new_name
            aret = AddressRecord.objects.filter(
                label=new_name, ip_upper=ip_upper,
                ip_lower=ip_lower, ip_type=ip_type)[0]
        else:
            # Just new_ip
            aret = AddressRecord.objects.filter(
                label=new_name, ip_upper=ip_upper,
                ip_lower=ip_lower, ip_type=ip_type)[0]
        if new_name:
            self.assertEqual(aret.label, new_name)
        if new_ip:
            if ip_type == '4':
                self.assertEqual(
                    aret.ip_str, ipaddr.IPv4Address(new_ip).__str__())
            else:
                self.assertEqual(
                    aret.ip_str, ipaddr.IPv6Address(new_ip).__str__())
Ejemplo n.º 3
0
def redirect_to_range_from_ip(request):
    ip_str = request.GET.get('ip_str')
    ip_type = request.GET.get('ip_type')
    if not (ip_str and ip_type):
        return HttpResponse(json.dumps({'failure': "Slob"}))

    if ip_type == '4':
        try:
            ip_upper, ip_lower = 0, int(ipaddr.IPv4Address(ip_str))
        except ipaddr.AddressValueError:
            return HttpResponse(json.dumps(
                {'success': False,
                 'message': "Failure to recognize {0} as an IPv4 "
                            "Address.".format(ip_str)}))
    else:
        try:
            ip_upper, ip_lower = ipv6_to_longs(ip_str)
        except ValidationError:
            return HttpResponse(json.dumps({'success': False,
                                            'message': 'Invalid IP'}))

    range_ = Range.objects.filter(
        start_upper__lte=ip_upper, start_lower__lte=ip_lower,
        end_upper__gte=ip_upper, end_lower__gte=ip_lower)
    if not len(range_) == 1:
        return HttpResponse(json.dumps({'failure': "Failture to find range"}))
    else:
        return HttpResponse(json.dumps(
            {'success': True,
             'redirect_url': range_[0].get_detail_url()}))
Ejemplo n.º 4
0
def migrate_AAAA(zone, root_domain, soa, views):
    for (name, ttl, rdata) in zone.iterate_rdatas('AAAA'):
        name = name.to_text().strip('.')
        print str(name) + " AAAA " + str(rdata)
        exists_domain = Domain.objects.filter(name=name)
        if exists_domain:
            label = ''
            domain = exists_domain[0]
        else:
            label = name.split('.')[0]
            if label.startswith('unused'):
                continue
            domain_name = '.'.join(name.split('.')[1:])
            domain = ensure_domain(domain_name, force=True)

        ip_upper, ip_lower = ipv6_to_longs(rdata.to_text())
        if AddressRecord.objects.filter(label=label,
                                        domain=domain, ip_upper=ip_upper, ip_lower=ip_lower,
                                        ip_type='6').exists():
            a = AddressRecord.objects.get(label=label,
                                          domain=domain, ip_type='6', ip_upper=ip_upper,
                                          ip_lower=ip_lower)
        else:
            a = AddressRecord(label=label, domain=domain,
                              ip_str=rdata.to_text(), ip_type='6')
            a.clean()
            a.save()
        for view in views:
            a.views.add(view)
            a.save()
Ejemplo n.º 5
0
    def _do_generic_update_test(self, record, new_name, new_ip, ip_type):
        if new_ip:
            if ip_type == '4':
                ip_upper, ip_lower = 0, ipaddr.IPv4Address(new_ip).__int__()
            else:
                ip_upper, ip_lower = ipv6_to_longs(new_ip)
        else:
            ip_upper, ip_lower = record.ip_upper, record.ip_lower

        if new_name is not None and new_ip is not None:
            aret = AddressRecord.objects.filter(label=new_name,
                                                ip_upper=ip_upper,
                                                ip_lower=ip_lower,
                                                ip_type=ip_type)[0]
        elif new_name is not None:
            # Just new_name
            aret = AddressRecord.objects.filter(label=new_name,
                                                ip_upper=ip_upper,
                                                ip_lower=ip_lower,
                                                ip_type=ip_type)[0]
        else:
            # Just new_ip
            aret = AddressRecord.objects.filter(label=new_name,
                                                ip_upper=ip_upper,
                                                ip_lower=ip_lower,
                                                ip_type=ip_type)[0]
        if new_name:
            self.assertEqual(aret.label, new_name)
        if new_ip:
            if ip_type == '4':
                self.assertEqual(aret.ip_str,
                                 ipaddr.IPv4Address(new_ip).__str__())
            else:
                self.assertEqual(aret.ip_str,
                                 ipaddr.IPv6Address(new_ip).__str__())
Ejemplo n.º 6
0
    def test_add_remove_reverse_ipv6_domains(self):
        osu_block = "2620:105:F000"
        rd0 = boot_strap_ipv6_reverse_domain("2.6.2.0.0.1.0.5.f.0.0.0")

        ip1 = self.add_ptr_ipv6(osu_block + ":8000::1")
        self.assertEqual(ip1.reverse_domain, rd0)
        ip2 = self.add_ptr_ipv6(osu_block + ":8000::2")
        self.assertEqual(ip2.reverse_domain, rd0)
        ip3 = self.add_ptr_ipv6(osu_block + ":8000::3")
        self.assertEqual(ip3.reverse_domain, rd0)
        ip4 = self.add_ptr_ipv6(osu_block + ":8000::4")

        rd1 = self.create_domain(name="2.6.2.0.0.1.0.5.f.0.0.0.8", ip_type='6')
        rd1.save()
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::1")
        ptr1 = PTR.objects.filter(ip_upper=ip_upper,
                                  ip_lower=ip_lower,
                                  ip_type='6')[0]
        self.assertEqual(ptr1.reverse_domain, rd1)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::2")
        ptr2 = PTR.objects.filter(ip_upper=ip_upper,
                                  ip_lower=ip_lower,
                                  ip_type='6')[0]
        self.assertEqual(ptr2.reverse_domain, rd1)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::3")
        ptr3 = PTR.objects.filter(ip_upper=ip_upper,
                                  ip_lower=ip_lower,
                                  ip_type='6')[0]
        self.assertEqual(ptr3.reverse_domain, rd1)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::4")
        ptr4 = PTR.objects.filter(ip_upper=ip_upper,
                                  ip_lower=ip_lower,
                                  ip_type='6')[0]
        self.assertEqual(ptr4.reverse_domain, rd1)

        rd1.delete()

        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::1")
        ptr1 = PTR.objects.filter(ip_upper=ip_upper,
                                  ip_lower=ip_lower,
                                  ip_type='6')[0]
        self.assertEqual(ptr1.reverse_domain, rd0)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::2")
        ptr2 = PTR.objects.filter(ip_upper=ip_upper,
                                  ip_lower=ip_lower,
                                  ip_type='6')[0]
        self.assertEqual(ptr2.reverse_domain, rd0)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::3")
        ptr3 = PTR.objects.filter(ip_upper=ip_upper,
                                  ip_lower=ip_lower,
                                  ip_type='6')[0]
        self.assertEqual(ptr3.reverse_domain, rd0)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::4")
        ptr4 = PTR.objects.filter(ip_upper=ip_upper,
                                  ip_lower=ip_lower,
                                  ip_type='6')[0]
        self.assertEqual(ptr4.reverse_domain, rd0)
Ejemplo n.º 7
0
 def test3_ipv6_to_longs(self):
     ip_upper_str = "aFFF:FaFF:FFaF:FFFa"
     ip_lower_str = "0000:0000:0000:0000"
     ip_upper, ip_lower = ipv6_to_longs(ip_upper_str + ":" + ip_lower_str)
     self.assertEqual(
         hex(ip_upper).lower(),
         "0x%sl" % (ip_upper_str.lower().replace(':', '')))
     self.assertEqual(hex(ip_lower).lower(), "0x0l")
Ejemplo n.º 8
0
    def test_ipv6_str(self):
        rd = boot_strap_ipv6_reverse_domain('1.2.3.4')

        ip_str = '1234:1234:1243:1243:1243::'
        new_ip = Ip(ip_str=ip_str, ip_type='6')
        new_ip.clean_ip()

        ip_upper, ip_lower = ipv6_to_longs(ip_str)
        self.assertEqual(new_ip.ip_upper, ip_upper)
        self.assertEqual(new_ip.ip_lower, ip_lower)

        ip_str = '1234:432:3:0:3414:22::'
        new_ip = Ip(ip_str=ip_str, ip_type='6')
        new_ip.clean_ip()

        ip_upper, ip_lower = ipv6_to_longs(ip_str)
        self.assertEqual(new_ip.ip_upper, ip_upper)
        self.assertEqual(new_ip.ip_lower, ip_lower)
Ejemplo n.º 9
0
    def test_ipv6_str(self):
        boot_strap_ipv6_reverse_domain('1.2.3.4')

        ip_str = '1234:1234:1243:1243:1243::'
        new_ip = Ip(ip_str=ip_str, ip_type='6')
        new_ip.clean_ip()

        ip_upper, ip_lower = ipv6_to_longs(ip_str)
        self.assertEqual(new_ip.ip_upper, ip_upper)
        self.assertEqual(new_ip.ip_lower, ip_lower)

        ip_str = '1234:432:3:0:3414:22::'
        new_ip = Ip(ip_str=ip_str, ip_type='6')
        new_ip.clean_ip()

        ip_upper, ip_lower = ipv6_to_longs(ip_str)
        self.assertEqual(new_ip.ip_upper, ip_upper)
        self.assertEqual(new_ip.ip_lower, ip_lower)
Ejemplo n.º 10
0
 def test2_create_ipv6(self):
     s = Network.objects.create(
         network_str='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/24',
         ip_type='6')
     str(s)
     s.__repr__()
     ip_upper, ip_lower = ipv6_to_longs(
         'ffff:ff00:0000:0000:0000:0000:0000:0000')
     # Network address was canonicalized.
     self.assertEqual(s.ip_upper, ip_upper)
     self.assertEqual(s.ip_lower, ip_lower)
Ejemplo n.º 11
0
 def test2_create_ipv6(self):
     s = Network.objects.create(
         network_str='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/24',
         ip_type='6')
     str(s)
     s.__repr__()
     ip_upper, ip_lower = ipv6_to_longs(
         'ffff:ff00:0000:0000:0000:0000:0000:0000')
     # Network address was canonicalized.
     self.assertEqual(s.ip_upper, ip_upper)
     self.assertEqual(s.ip_lower, ip_lower)
Ejemplo n.º 12
0
 def test2_create_ipv6(self):
     network = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
     prefixlen = "24"
     kwargs = {'network': network, 'prefixlen': prefixlen, 'ip_type': '6'}
     s = self.do_basic_add(**kwargs)
     str(s)
     s.__repr__()
     self.assertTrue(s)
     ip_upper, ip_lower = ipv6_to_longs(network)
     self.assertEqual(s.ip_upper, ip_upper)
     self.assertEqual(s.ip_lower, ip_lower)
Ejemplo n.º 13
0
 def test2_create_ipv6(self):
     network = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
     prefixlen = "24"
     kwargs = {'network': network, 'prefixlen': prefixlen, 'ip_type': '6'}
     s = self.do_basic_add(**kwargs)
     str(s)
     s.__repr__()
     self.assertTrue(s)
     ip_upper, ip_lower = ipv6_to_longs(network)
     self.assertEqual(s.ip_upper, ip_upper)
     self.assertEqual(s.ip_lower, ip_lower)
Ejemplo n.º 14
0
 def test_large_ipv6(self):
     rd = create_reverse_domain('f', ip_type='6')
     ip_str = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
     ip = ipaddr.IPv6Address(ip_str)
     ip_upper, ip_lower = ipv6_to_longs(ip_str)
     self.assertEqual(ip.__int__(), (2 ** 64) * ip_upper + ip_lower)
     new_ip = Ip(ip_str=ip_str, ip_upper=ip_upper,
                 ip_lower=ip_lower, ip_type='6')
     new_ip.clean_ip()
     self.assertEqual(ip_str, new_ip.ip_str)
     self.assertEqual(ip.__int__(), new_ip.__int__())
Ejemplo n.º 15
0
 def test_large_ipv6(self):
     rd = create_reverse_domain('f', ip_type='6')
     ip_str = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
     ip = ipaddr.IPv6Address(ip_str)
     ip_upper, ip_lower = ipv6_to_longs(ip_str)
     self.assertEqual(ip.__int__(), (2**64) * ip_upper + ip_lower)
     new_ip = Ip(ip_str=ip_str,
                 ip_upper=ip_upper,
                 ip_lower=ip_lower,
                 ip_type='6')
     new_ip.clean_ip()
     self.assertEqual(ip_str, new_ip.ip_str)
     self.assertEqual(ip.__int__(), new_ip.__int__())
Ejemplo n.º 16
0
    def test_add_remove_reverse_ipv6_domains(self):
        osu_block = "2620:105:F000"
        rd0 = boot_strap_ipv6_reverse_domain("2.6.2.0.0.1.0.5.f.0.0.0")

        ip1 = self.add_ptr_ipv6(osu_block + ":8000::1")
        self.assertEqual(ip1.reverse_domain, rd0)
        ip2 = self.add_ptr_ipv6(osu_block + ":8000::2")
        self.assertEqual(ip2.reverse_domain, rd0)
        ip3 = self.add_ptr_ipv6(osu_block + ":8000::3")
        self.assertEqual(ip3.reverse_domain, rd0)
        ip4 = self.add_ptr_ipv6(osu_block + ":8000::4")

        rd1 = self.create_domain(name="2.6.2.0.0.1.0.5.f.0.0.0.8", ip_type='6')
        rd1.save()
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::1")
        ptr1 = PTR.objects.filter(
            ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]
        self.assertEqual(ptr1.reverse_domain, rd1)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::2")
        ptr2 = PTR.objects.filter(
            ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]
        self.assertEqual(ptr2.reverse_domain, rd1)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::3")
        ptr3 = PTR.objects.filter(
            ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]
        self.assertEqual(ptr3.reverse_domain, rd1)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::4")
        ptr4 = PTR.objects.filter(
            ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]
        self.assertEqual(ptr4.reverse_domain, rd1)

        rd1.delete()

        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::1")
        ptr1 = PTR.objects.filter(
            ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]
        self.assertEqual(ptr1.reverse_domain, rd0)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::2")
        ptr2 = PTR.objects.filter(
            ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]
        self.assertEqual(ptr2.reverse_domain, rd0)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::3")
        ptr3 = PTR.objects.filter(
            ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]
        self.assertEqual(ptr3.reverse_domain, rd0)
        ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::4")
        ptr4 = PTR.objects.filter(
            ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]
        self.assertEqual(ptr4.reverse_domain, rd0)
Ejemplo n.º 17
0
 def test_large_ipv6(self):
     try:
         rd = boot_strap_ipv6_reverse_domain('f')
         rd.save()
     except ValidationError:
         pass
     ip_str = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
     ip = ipaddr.IPv6Address(ip_str)
     ip_upper, ip_lower = ipv6_to_longs(ip_str)
     self.assertEqual(ip.__int__(), (2 ** 64) * ip_upper + ip_lower)
     new_ip = Ip(ip_str=ip_str, ip_upper=ip_upper,
                 ip_lower=ip_lower, ip_type='6')
     new_ip.clean_ip()
     self.assertEqual(ip_str, new_ip.ip_str)
     self.assertEqual(ip.__int__(), new_ip.__int__())
Ejemplo n.º 18
0
    def check_valid_range(self):
        # Look at all ranges that claim to be in this subnet, are they actually
        # in the subnet?
        self.update_network()
        fail = False
        for range_ in self.range_set.all():
            # TODO
            """
                I was writing checks to make sure that subnets wouldn't orphan
                ranges. IPv6 needs support.
            """
            # Check the start addresses.
            if range_.start_upper < self.ip_upper:
                fail = True
                break
            elif (range_.start_upper > self.ip_upper and range_.start_lower <
                  self.ip_lower):
                fail = True
                break
            elif (range_.start_upper == self.ip_upper and range_.start_lower <
                    self.ip_lower):
                fail = True
                break

            if self.ip_type == IP_TYPE_4:
                brdcst_upper, brdcst_lower = 0, int(self.network.broadcast)
            else:
                brdcst_upper, brdcst_lower = ipv6_to_longs(str(
                    self.network.broadcast))

            # Check the end addresses.
            if range_.end_upper > brdcst_upper:
                fail = True
                break
            elif (range_.end_upper < brdcst_upper and range_.end_lower >
                    brdcst_lower):
                fail = True
                break
            elif (range_.end_upper == brdcst_upper and range_.end_lower
                    > brdcst_lower):
                fail = True

            if fail:
                raise ValidationError("Resizing this subnet to the requested "
                                      "network prefix would orphan existing "
                                      "ranges.")
Ejemplo n.º 19
0
    def check_valid_range(self):
        # Look at all ranges that claim to be in this subnet, are they actually
        # in the subnet?
        self.update_network()
        fail = False
        for range_ in self.range_set.all():
            # TODO
            """
                I was writing checks to make sure that subnets wouldn't orphan
                ranges. IPv6 needs support.
            """
            # Check the start addresses.
            if range_.start_upper < self.ip_upper:
                fail = True
                break
            elif (range_.start_upper > self.ip_upper and range_.start_lower <
                  self.ip_lower):
                fail = True
                break
            elif (range_.start_upper == self.ip_upper and range_.start_lower <
                    self.ip_lower):
                fail = True
                break

            if self.ip_type == IP_TYPE_4:
                brdcst_upper, brdcst_lower = 0, int(self.network.broadcast)
            else:
                brdcst_upper, brdcst_lower = ipv6_to_longs(str(
                    self.network.broadcast))

            # Check the end addresses.
            if range_.end_upper > brdcst_upper:
                fail = True
                break
            elif (range_.end_upper < brdcst_upper and range_.end_lower >
                    brdcst_lower):
                fail = True
                break
            elif (range_.end_upper == brdcst_upper and range_.end_lower
                    > brdcst_lower):
                fail = True

            if fail:
                raise ValidationError("Resizing this subnet to the requested "
                                      "network prefix would orphan existing "
                                      "ranges.")
Ejemplo n.º 20
0
 def test_large_ipv6(self):
     try:
         rd = boot_strap_ipv6_reverse_domain('f')
         rd.save()
     except ValidationError:
         pass
     ip_str = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
     ip = ipaddr.IPv6Address(ip_str)
     ip_upper, ip_lower = ipv6_to_longs(ip_str)
     self.assertEqual(ip.__int__(), (2**64) * ip_upper + ip_lower)
     new_ip = Ip(ip_str=ip_str,
                 ip_upper=ip_upper,
                 ip_lower=ip_lower,
                 ip_type='6')
     new_ip.clean_ip()
     self.assertEqual(ip_str, new_ip.ip_str)
     self.assertEqual(ip.__int__(), new_ip.__int__())
Ejemplo n.º 21
0
def network_wizard1(request):
    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == "network":
            # return the allocate network block page.
            form = NetworkForm_network(request.POST)
            try:
                ip_type = form.data.get('ip_type')
                if ip_type not in ('4', '6'):
                    raise ValidationError("IP type must be either IPv4 or "
                                          "IPv6.")
                network_str = form.data.get('network', '')
                try:
                    if ip_type == '4':
                        network = ipaddr.IPv4Network(network_str)
                    elif ip_type == '6':
                        network = ipaddr.IPv6Network(network_str)
                except ipaddr.AddressValueError, e:
                    raise ValidationError("Bad Ip address {0}".format(e))
                except ipaddr.NetmaskValueError, e:
                    raise ValidationError("Bad Netmask {0}".format(e))

                # Make sure this network doesn't exist.
                if ip_type == '4':
                    ip_upper, ip_lower = 0, int(network.network)
                else:
                    ip_upper, ip_lower = ipv6_to_longs(network.network)
                if (Network.objects.filter(ip_upper=ip_upper,
                                           ip_lower=ip_lower).exists()):
                    raise ValidationError("This network has already been "
                                          "allocated.")
            except ValidationError, e:
                form = NetworkForm_network(request.POST)
                if form._errors is None:
                    form._errors = ErrorDict()
                form._errors['__all__'] = ErrorList(e.messages)
                return render(request, 'network/wizard_form.html', {
                    'form': form,
                    'action': 'network'
                })
Ejemplo n.º 22
0
def network_wizard1(request):
    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == "network":
            # return the allocate network block page.
            form = NetworkForm_network(request.POST)
            try:
                ip_type = form.data.get('ip_type')
                if ip_type not in ('4', '6'):
                    raise ValidationError("IP type must be either IPv4 or "
                                          "IPv6.")
                network_str = form.data.get('network', '')
                try:
                    if ip_type == '4':
                        network = ipaddr.IPv4Network(network_str)
                    elif ip_type == '6':
                        network = ipaddr.IPv6Network(network_str)
                except ipaddr.AddressValueError, e:
                    raise ValidationError("Bad Ip address {0}".format(e))
                except ipaddr.NetmaskValueError, e:
                    raise ValidationError("Bad Netmask {0}".format(e))

                # Make sure this network doesn't exist.
                if ip_type == '4':
                    ip_upper, ip_lower = 0, int(network.network)
                else:
                    ip_upper, ip_lower = ipv6_to_longs(network.network)
                if (Network.objects.filter(ip_upper=ip_upper,
                                           ip_lower=ip_lower).exists()):
                    raise ValidationError("This network has already been "
                                          "allocated.")
            except ValidationError, e:
                form = NetworkForm_network(request.POST)
                if form._errors is None:
                    form._errors = ErrorDict()
                form._errors['__all__'] = ErrorList(e.messages)
                return render(request, 'network/wizard_form.html', {
                    'form': form,
                    'action': 'network'
                })
Ejemplo n.º 23
0
    def check_valid_range(self):
        # Look at all ranges that claim to be in this subnet, are they actually
        # in the subnet?
        self.update_network()
        fail = False
        for range_ in self.range_set.all():
            # Check the start addresses.
            if range_.start_upper < self.ip_upper:
                break
            elif (range_.start_upper > self.ip_upper
                  and range_.start_lower < self.ip_lower):
                break
            elif (range_.start_upper == self.ip_upper
                  and range_.start_lower < self.ip_lower):
                break

            if self.ip_type == IP_TYPE_4:
                brdcst_upper, brdcst_lower = 0, int(self.network.broadcast)
            else:
                brdcst_upper, brdcst_lower = ipv6_to_longs(
                    str(self.network.broadcast))

            # Check the end addresses.
            if range_.end_upper > brdcst_upper:
                break
            elif (range_.end_upper < brdcst_upper
                  and range_.end_lower > brdcst_lower):
                break
            elif (range_.end_upper == brdcst_upper
                  and range_.end_lower > brdcst_lower):
                break
        else:  # All ranges are valid.
            return

        raise ValidationError(
            "Resizing this subnet to the requested network prefix would "
            "orphan existing ranges.")
Ejemplo n.º 24
0
    def check_valid_range(self):
        # Look at all ranges that claim to be in this subnet, are they actually
        # in the subnet?
        self.update_network()
        fail = False
        for range_ in self.range_set.all():
            # Check the start addresses.
            if range_.start_upper < self.ip_upper:
                break
            elif (range_.start_upper > self.ip_upper and range_.start_lower <
                  self.ip_lower):
                break
            elif (range_.start_upper == self.ip_upper and range_.start_lower <
                    self.ip_lower):
                break

            if self.ip_type == IP_TYPE_4:
                brdcst_upper, brdcst_lower = 0, int(self.network.broadcast)
            else:
                brdcst_upper, brdcst_lower = ipv6_to_longs(str(
                    self.network.broadcast))

            # Check the end addresses.
            if range_.end_upper > brdcst_upper:
                break
            elif (range_.end_upper < brdcst_upper and range_.end_lower >
                    brdcst_lower):
                break
            elif (range_.end_upper == brdcst_upper and range_.end_lower
                    > brdcst_lower):
                break
        else:  # All ranges are valid.
            return

        raise ValidationError(
            "Resizing this subnet to the requested network prefix would "
            "orphan existing ranges.")
Ejemplo n.º 25
0
def migrate_AAAA(zone, root_domain, soa, views):
    for (name, ttl, rdata) in zone.iterate_rdatas('AAAA'):
        name = name.to_text().strip('.')
        print str(name) + " AAAA " + str(rdata)
        exists_domain = Domain.objects.filter(name=name)
        if exists_domain:
            label = ''
            domain = exists_domain[0]
        else:
            label = name.split('.')[0]
            if label.startswith('unused'):
                continue
            domain_name = '.'.join(name.split('.')[1:])
            domain = ensure_domain(domain_name, force=True)

        ip_upper, ip_lower = ipv6_to_longs(rdata.to_text())
        if AddressRecord.objects.filter(label=label,
                                        domain=domain,
                                        ip_upper=ip_upper,
                                        ip_lower=ip_lower,
                                        ip_type='6').exists():
            a = AddressRecord.objects.get(label=label,
                                          domain=domain,
                                          ip_type='6',
                                          ip_upper=ip_upper,
                                          ip_lower=ip_lower)
        else:
            a = AddressRecord(label=label,
                              domain=domain,
                              ip_str=rdata.to_text(),
                              ip_type='6')
            a.clean()
            a.save()
        for view in views:
            a.views.add(view)
            a.save()
Ejemplo n.º 26
0
def create_ipv4_interface(label,
                          vlan_str,
                          site_str,
                          system,
                          mac,
                          domain_suffix,
                          network_str=None):
    """This is an api for creating an interface.

    :param label: The label of the interface.
    :type lavel: str
    :param vlan_str: The name of the vlan the interface should be put into.
    :type vlan_str: str
    :param site_str: The Datacenter (and possibly the Buisness Unit)
                     the vlan is in.
    :type site_str: str
    :param system: The system the interface belongs to.
    :type system: :class:`System`
    :param mac: The mac address of the new interface.
    :type mac: str
    :param domain_suffix: The suffix of the domain. This is usually
        'mozilla.com'.
    :type domain_suffix: str
    :param network_str: This is an optional paramter. If you get a
        :class:`MultipleObjectsReturned` error when trying to find a network,
        you can specify which network to use by passing the network's name
        using this kwarg. E.g. ``network="10.22.9.0/24"``
    :type network_str: str

    This function returns two values. The first value is the
    :class:`StaticInterface` instance. If the interface was succesfully created
    the second return value is meaningless. If there were errors while creating
    the interface the first value is None and the second return value is an
    ErrorDict containing errors that should be displayed to the user. Always
    check the first return value for being a NoneType.

    Using this function requires that certain objects exist in the database.
    *Understanding these objects before using this function is a good thing*.
    ::

                    <label> (.<BU>) .<DC> . <domain_suffix>

    1. This function starts by looking for a site in the site table that has a
        site path (i.e. `<BU>.<DC>` like 'relenge.scl3' or just 'scl3') equal
        to the 'site_str' paramter.

        If you get errors from this step (i.e. There is a 'site' key in the
        errors dictionary), create the site you are trying to use in the Web
        UI.


    2.  The function then looks in the site found in Step 1 for a :class:`Vlan`
        instance with a name equal to the 'vlan_str' parameter.

        If you get errors from this step (i.e. There is a 'vlan' key in the
        errors dictionary), create the vlan you are trying to use in the Web
        UI.


    3.  Using the :class:`Site` and :class:`Vlan` instance found in Step 1 & 2
        and the `domain_suffix` paramter, the function constructs the following
        string.::

            <vlan>.<site>.<domain_suffix>

        As an example, imaging we were using::

            site = relenge.scl3
            vlan = db
            domain_suffix = mozilla.com

        The constructed string would be::

            db.relenge.scl3.mozilla.com

        The function will now use this constructed string as the domain name
        for creating the interface's A/PTR records. For this reason *a domain
        with the constructed name _must_ be in the database*.

        If you get errors from this step (i.e. There is a 'domain' key in the
        errors dictionary), create the domain you are trying to use in the Web
        UI.


    4.  The function then looks at the networks associated with that vlan found
        in Step 2 and chooses the networks that are associated to the site
        found in Step 1.

        If you get errors from this step (i.e. There is a 'network' key in the
        errors dictionary), create the network you are trying to use in the Web
        UI and associate it with the vlan *and* site you are trying to use.


    5.  The function then looks for ranges within the networks found in Step 4.
        If the function finds more than one range it does not make a choice for
        you and returns an error. If the function finds only one range it looks
        for a free IP in that range while returning an error if no free IP is
        found.

    6.  Using the 'label', 'system', 'mac', and IP address found in Step 4, a
        new StaticInterface is created. If there are errors while creating the
        Interface those errors are returned. If there are no errors while
        creating the Interface the Interface is returned.
    """
    errors = ErrorDict()
    if not label:
        errors['label'] = ErrorList(["Please supply a label."])
        return None, errors

    if not system:
        errors['system'] = ErrorList(
            "Please supply a system.".format(site_str))
        return None, errors

    site = None
    for s in Site.objects.all():
        if '.'.join(s.get_site_path().split('.')) == site_str:
            site = s
            break
    if not site:
        errors['site'] = ErrorList(
            ["Site {0} does not exist".format(site_str)])
        return None, errors

    try:
        vlan = Vlan.objects.get(name=vlan_str)
    except ObjectDoesNotExist:
        errors['vlan'] = ErrorList(
            ["Vlan {0} does not exist.".format(vlan_str)])
        return None, errors

    tmp_site_str = '.'.join(s.get_site_path().split('.'))
    domain_name = vlan.name + "." + tmp_site_str + "." + domain_suffix
    try:
        domain = Domain.objects.get(name=domain_name)
    except ObjectDoesNotExist:
        errors['domain'] = ErrorList(
            ["Could not find domain "
             "{0}".format(domain_name)])
        return None, errors

    if not network_str:
        try:
            network = vlan.network_set.get(site=site)
        except MultipleObjectsReturned:
            networks = vlan.network_set.filter(site=site)
            errors['network'] = ErrorList([
                "There were too many networks "
                "associated with vlan {0} in {1}. "
                "Manually specify which network to "
                "use. Your choces are {2}".format(
                    vlan, site, ", ".join([n.network_str for n in networks]))
            ])
            return None, errors
        except ObjectDoesNotExist:
            errors['network'] = "No network for vlan {0} in {1}.".format(
                vlan, site)
            return None, errors
    else:
        try:
            # Guess which type of network it is.
            try:
                if network_str.find(':') > -1:
                    ip_type = '6'
                    tmp_network = ipaddr.IPv6Network(network_str)
                    ip_upper, ip_lower = ipv6_to_longs(tmp_network)
                else:
                    ip_type = '4'
                    tmp_network = ipaddr.IPv4Network(network_str)
                    ip_upper, ip_lower = 0, int(tmp_network)
            except ipaddr.AddressValueError:
                errors['network'] = ErrorList([
                    "The network {0} is not a "
                    "valid IPv{1} network.".format(network_str, ip_type)
                ])
                return None, errors
            # Now try to find a network that matches the query. Error out if we
            # can't find one and recommend the user create it.
            network = Network.objects.get(ip_type=ip_type,
                                          ip_upper=ip_upper,
                                          ip_lower=ip_lower,
                                          prefixlen=tmp_network.prefixlen)
        except ObjectDoesNotExist:
            errors['network'] = ErrorList([
                "The network {0} was not found. "
                "Consider creating it in the web "
                "UI.".format(network_str)
            ])
            return None, errors

    if not network.range_set.all().exists():
        errors['range'] = ErrorList([
            "No range for network {0} in vlan {1} in "
            "site {0}. Create it via the web UI too "
            "many networks associated with vlan "
            "{0} in {1}".format(network, vlan, site)
        ])
        return None, errors

    if network.range_set.all().count() > 1:
        errors['ip'] = ErrorList([
            "Too many ranges. In the face of ambiguity, "
            "*this script* has refused the temptation "
            "to guess which range you want to put the "
            "interface in."
        ])
        return None, errors

    range_ = network.range_set.all()[0]
    return _create_ipv4_intr_from_range(label, domain.name, system, mac,
                                        range_.start_lower, range_.end_lower)
Ejemplo n.º 27
0
def redirect_to_range_from_ip(request):
    ip_str = request.GET.get('ip_str')
    ip_type = request.GET.get('ip_type')
    if not (ip_str and ip_type):
        return HttpResonse(json.dumps({'failure': "Slob"}))

    if ip_type == '4':
        try:
            ip_upper, ip_lower = 0, int(ipaddr.IPv4Address(ip_str))
        except ipaddr.AddressValueError, e:
            return HttpResonse(json.dumps({'success': False,
                                           'message': "Failure to recognize {0} as an IPv4 "
                                           "Address.".format(ip_str)}))
    else:
        try:
            ip_upper, ip_lower = ipv6_to_longs(ip_str)
        except ValidationError, e:
            return HttpResponse(json.dumps({'success': False,
                                            'message': 'Invalid IP'}))

    range_ = Range.objects.filter(start_upper__lte=ip_upper,
                                  start_lower__lte=ip_lower, end_upper__gte=ip_upper,
                                  end_lower__gte=ip_lower)
    if not len(range_) == 1:
        return HttpResponse(json.dumps({'failure': "Failture to find range"}))
    else:
        return HttpResponse(json.dumps(
            {'success': True,
             'redirect_url': range_[0].get_detail_url()}))

Ejemplo n.º 28
0
 def test_ipv6_to_longs(self):
     ip = ipaddr.IPv6Address('2001:0db8:85a3:0000:0000:8a2e:0370:733')
     ret = ipv6_to_longs(ip.__str__())
     self.assertEqual(ret, (2306139570357600256, 151930230802227))
Ejemplo n.º 29
0
                                                               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)
        ip_upper, ip_lower = ipv6_to_longs(rdata.to_text())
        if AddressRecord.objects.filter(label=label,
                                        domain=domain, ip_upper=ip_upper, ip_lower=ip_lower,
                                        ip_type='6').exists():
            a = AddressRecord.objects.get(label=label,
                                          domain=domain, ip_type='6', ip_upper=ip_upper,
                                          ip_lower=ip_lower)
        else:
            a, _ = AddressRecord.objects.get_or_create(label=label,
                                                       domain=domain, ip_str=rdata.to_text(), ip_type='6')
        if views:
            for view in views:
                a.views.add(view)
                try:
                    a.save()
                except Exception, e:
Ejemplo n.º 30
0
class SimpleTest(TestCase):
    def setUp(self):
        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.i6_arpa = self.create_domain(name='ipv6.arpa')
        self.i6_arpa.save()

        rd = self.create_domain(name='66', ip_type='4')
        rd.save()

    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 test_ipv4_str(self):
        rd = self.create_domain(name='192', ip_type='4')
        rd.save()
        ip_str = '192.168.1.1'
        ip = ipaddr.IPv4Address(ip_str)
        Ip(ip_str=ip_str, ip_type='4').clean_ip()

        rd = self.create_domain(name='128', ip_type='4')
        rd.save()
        ip_str = '128.168.1.1'
        ip = ipaddr.IPv4Address(ip_str)
        Ip(ip_str=ip_str, ip_type='4').clean_ip()

    def test_update_ipv4_str(self):
        ip_str = '66.168.1.1'
        v_ip = ipaddr.IPv4Address(ip_str)
        ip = Ip(ip_str=ip_str, ip_type='4')
        ip.clean_ip()
        self.assertEqual(ip.ip_upper, 0)
        self.assertEqual(ip.ip_lower, int(v_ip))

        # Make sure ip_lower is update.
        ip_str = '66.213.1.9'
        v_ip = ipaddr.IPv4Address(ip_str)
        ip.ip_str = ip_str
        ip.clean_ip()
        self.assertEqual(ip.ip_upper, 0)
        self.assertEqual(ip.ip_lower, int(v_ip))

    def test_ipv6_str(self):
        rd = boot_strap_ipv6_reverse_domain('1.2.3.4')

        ip_str = '1234:1234:1243:1243:1243::'
        new_ip = Ip(ip_str=ip_str, ip_type='6')
        new_ip.clean_ip()

        ip_upper, ip_lower = ipv6_to_longs(ip_str)
        self.assertEqual(new_ip.ip_upper, ip_upper)
        self.assertEqual(new_ip.ip_lower, ip_lower)

        ip_str = '1234:432:3:0:3414:22::'
        new_ip = Ip(ip_str=ip_str, ip_type='6')
        new_ip.clean_ip()

        ip_upper, ip_lower = ipv6_to_longs(ip_str)
        self.assertEqual(new_ip.ip_upper, ip_upper)
        self.assertEqual(new_ip.ip_lower, ip_lower)

    def test_large_ipv6(self):
        try:
            rd = boot_strap_ipv6_reverse_domain('f')
            rd.save()
        except ValidationError, e:
            pass
        ip_str = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
        ip = ipaddr.IPv6Address(ip_str)
        ip_upper, ip_lower = ipv6_to_longs(ip_str)
        self.assertEqual(ip.__int__(), (2**64) * ip_upper + ip_lower)
        new_ip = Ip(ip_str=ip_str,
                    ip_upper=ip_upper,
                    ip_lower=ip_lower,
                    ip_type='6')
        new_ip.clean_ip()
        self.assertEqual(ip_str, new_ip.ip_str)
        self.assertEqual(ip.__int__(), new_ip.__int__())
Ejemplo n.º 31
0
def search_ip(request):
    if request.method == "POST":
        form = IpSearchForm(request.POST)
        try:
            if form.is_valid():
                ip_type = form.cleaned_data['ip_type']
                search_ip = form.cleaned_data['search_ip']
                try:
                    if ip_type == '4':
                        network = ipaddr.IPv4Network(search_ip)
                    if ip_type == '6':
                        network = ipaddr.IPv6Network(search_ip)
                except ipaddr.AddressValueError, e:
                    form._errors['__all__'] = ErrorList(
                        ["Bad IPv{0} Address {1}".format(ip_type, search_ip)])
                    return render(request, 'cydhcp/cydhcp_form.html', {
                        'form': form
                    })
                try:
                    network = Network.objects.get(network_str=search_ip)
                    search_ip = network
                    found_exact = True
                except ObjectDoesNotExist:
                    found_exact = False
                    network = Network(ip_type, network_str=search_ip,
                                      ip_type=ip_type)
                parent = calc_parent(network)
                eldars, sub_networks = calc_networks(network)
                if ip_type == '6':
                    sip_upper, sip_lower = ipv6_to_longs(network.network.ip)
                    eip_upper, eip_lower = ipv6_to_longs(
                        network.network.broadcast)
                else:
                    sip_upper, sip_lower = 0, int(network.network.ip)
                    eip_upper, eip_lower = 0, int(network.network.broadcast)

                addrs_count = AddressRecord.objects.filter(
                    ip_upper__gte=sip_upper,
                    ip_lower__gte=sip_lower,
                    ip_upper__lte=eip_upper,
                    ip_lower__lte=eip_lower).count()

                if addrs_count > 100:
                    addrs = None  # This is too much
                else:
                    addrs = AddressRecord.objects.filter(
                        ip_upper__gte=sip_upper,
                        ip_lower__gte=sip_lower,
                        ip_upper__lte=eip_upper,
                        ip_lower__lte=eip_lower)

                ptrs_count = PTR.objects.filter(
                    ip_upper__gte=sip_upper,
                    ip_lower__gte=sip_lower,
                    ip_upper__lte=eip_upper,
                    ip_lower__lte=eip_lower).count()

                if ptrs_count > 100:
                    ptrs = None  # This is too much
                else:
                    ptrs = PTR.objects.filter(
                        ip_upper__gte=sip_upper,
                        ip_lower__gte=sip_lower,
                        ip_upper__lte=eip_upper,
                        ip_lower__lte=eip_lower)

            return render(request, 'cydhcp/cydhcp_results.html', {
                'search_ip': search_ip,
                'found_exact': found_exact,
                'addrs': addrs,
                'addrs_count': addrs_count,
                'ptrs_count': ptrs_count,
                'ptrs': ptrs,
                'parent': parent,
                'eldars': eldars,
                'sub_networks': sub_networks,
            })
        except ValidationError, e:
            form._errors['__all__'] = ErrorList(e.messages)
            return render(request, 'cydhcp/cydhcp_form.html', {
                'form': form
            })
Ejemplo n.º 32
0
 def test_ipv6_to_longs(self):
     ip = ipaddr.IPv6Address('2001:0db8:85a3:0000:0000:8a2e:0370:733')
     ret = ipv6_to_longs(str(ip))
     self.assertEqual(ret, (2306139570357600256, 151930230802227))
Ejemplo n.º 33
0
def create_ipv4_interface(label, vlan_str, site_str, system,
                          mac, domain_suffix, network_str=None):
    """This is an api for creating an interface.

    :param label: The label of the interface.
    :type lavel: str
    :param vlan_str: The name of the vlan the interface should be put into.
    :type vlan_str: str
    :param site_str: The Datacenter (and possibly the Buisness Unit)
                     the vlan is in.
    :type site_str: str
    :param system: The system the interface belongs to.
    :type system: :class:`System`
    :param mac: The mac address of the new interface.
    :type mac: str
    :param domain_suffix: The suffix of the domain. This is usually
        'mozilla.com'.
    :type domain_suffix: str
    :param network_str: This is an optional paramter. If you get a
        :class:`MultipleObjectsReturned` error when trying to find a network,
        you can specify which network to use by passing the network's name
        using this kwarg. E.g. ``network="10.22.9.0/24"``
    :type network_str: str

    This function returns two values. The first value is the
    :class:`StaticInterface` instance. If the interface was succesfully created
    the second return value is meaningless. If there were errors while creating
    the interface the first value is None and the second return value is an
    ErrorDict containing errors that should be displayed to the user. Always
    check the first return value for being a NoneType.

    Using this function requires that certain objects exist in the database.
    *Understanding these objects before using this function is a good thing*.
    ::

                    <label> (.<BU>) .<DC> . <domain_suffix>

    1. This function starts by looking for a site in the site table that has a
        site path (i.e. `<BU>.<DC>` like 'relenge.scl3' or just 'scl3') equal
        to the 'site_str' paramter.

        If you get errors from this step (i.e. There is a 'site' key in the
        errors dictionary), create the site you are trying to use in the Web
        UI.


    2.  The function then looks in the site found in Step 1 for a :class:`Vlan`
        instance with a name equal to the 'vlan_str' parameter.

        If you get errors from this step (i.e. There is a 'vlan' key in the
        errors dictionary), create the vlan you are trying to use in the Web
        UI.


    3.  Using the :class:`Site` and :class:`Vlan` instance found in Step 1 & 2
        and the `domain_suffix` paramter, the function constructs the following
        string.::

            <vlan>.<site>.<domain_suffix>

        As an example, imaging we were using::

            site = relenge.scl3
            vlan = db
            domain_suffix = mozilla.com

        The constructed string would be::

            db.relenge.scl3.mozilla.com

        The function will now use this constructed string as the domain name
        for creating the interface's A/PTR records. For this reason *a domain
        with the constructed name _must_ be in the database*.

        If you get errors from this step (i.e. There is a 'domain' key in the
        errors dictionary), create the domain you are trying to use in the Web
        UI.


    4.  The function then looks at the networks associated with that vlan found
        in Step 2 and chooses the networks that are associated to the site
        found in Step 1.

        If you get errors from this step (i.e. There is a 'network' key in the
        errors dictionary), create the network you are trying to use in the Web
        UI and associate it with the vlan *and* site you are trying to use.


    5.  The function then looks for ranges within the networks found in Step 4.
        If the function finds more than one range it does not make a choice for
        you and returns an error. If the function finds only one range it looks
        for a free IP in that range while returning an error if no free IP is
        found.

    6.  Using the 'label', 'system', 'mac', and IP address found in Step 4, a
        new StaticInterface is created. If there are errors while creating the
        Interface those errors are returned. If there are no errors while
        creating the Interface the Interface is returned.
    """
    errors = ErrorDict()
    if not label:
        errors['label'] = ErrorList(["Please supply a label."])
        return None, errors

    if not system:
        errors['system'] = ErrorList(
            "Please supply a system.".format(site_str))
        return None, errors

    site = None
    for s in Site.objects.all():
        if '.'.join(s.get_site_path().split('.')) == site_str:
            site = s
            break
    if not site:
        errors['site'] = ErrorList(
            ["Site {0} does not exist".format(site_str)])
        return None, errors

    try:
        vlan = Vlan.objects.get(name=vlan_str)
    except ObjectDoesNotExist:
        errors['vlan'] = ErrorList(["Vlan {0} does not exist.".format(
            vlan_str)])
        return None, errors

    tmp_site_str = '.'.join(s.get_site_path().split('.'))
    domain_name = vlan.name + "." + tmp_site_str + "." + domain_suffix
    try:
        domain = Domain.objects.get(name=domain_name)
    except ObjectDoesNotExist:
        errors['domain'] = ErrorList(["Could not find domain "
                                      "{0}".format(domain_name)])
        return None, errors

    if not network_str:
        try:
            network = vlan.network_set.get(site=site)
        except MultipleObjectsReturned:
            networks = vlan.network_set.filter(site=site)
            errors['network'] = ErrorList(["There were too many networks "
                                           "associated with vlan {0} in {1}. "
                                           "Manually specify which network to "
                                           "use. Your choces are {2}".format(
                                           vlan, site,
                                           ", ".join([n.network_str
                                           for n in networks]))])
            return None, errors
        except ObjectDoesNotExist:
            errors['network'] = "No network for vlan {0} in {1}.".format(
                vlan, site)
            return None, errors
    else:
        try:
            # Guess which type of network it is.
            try:
                if network_str.find(':') > -1:
                    ip_type = '6'
                    tmp_network = ipaddr.IPv6Network(network_str)
                    ip_upper, ip_lower = ipv6_to_longs(tmp_network)
                else:
                    ip_type = '4'
                    tmp_network = ipaddr.IPv4Network(network_str)
                    ip_upper, ip_lower = 0, int(tmp_network)
            except ipaddr.AddressValueError:
                errors['network'] = ErrorList(["The network {0} is not a "
                                               "valid IPv{1} network.".format(
                                               network_str, ip_type)])
                return None, errors
            # Now try to find a network that matches the query. Error out if we
            # can't find one and recommend the user create it.
            network = Network.objects.get(ip_type=ip_type, ip_upper=ip_upper,
                                          ip_lower=ip_lower,
                                          prefixlen=tmp_network.prefixlen)
        except ObjectDoesNotExist:
            errors['network'] = ErrorList(["The network {0} was not found. "
                                           "Consider creating it in the web "
                                           "UI.".format(network_str)])
            return None, errors

    if not network.range_set.all().exists():
        errors['range'] = ErrorList(["No range for network {0} in vlan {1} in "
                                     "site {0}. Create it via the web UI too "
                                     "many networks associated with vlan "
                                     "{0} in {1}".format(network, vlan, site)])
        return None, errors

    if network.range_set.all().count() > 1:
        errors['ip'] = ErrorList(["Too many ranges. In the face of ambiguity, "
                                  "*this script* has refused the temptation "
                                  "to guess which range you want to put the "
                                  "interface in."])
        return None, errors

    range_ = network.range_set.all()[0]
    return _create_ipv4_intr_from_range(label,
                                        domain.name,
                                        system,
                                        mac,
                                        range_.start_lower,
                                        range_.end_lower)
Ejemplo n.º 34
0
def search_ip(request):
    if request.method == "POST":
        form = IpSearchForm(request.POST)
        try:
            if form.is_valid():
                ip_type = form.cleaned_data['ip_type']
                search_ip = form.cleaned_data['search_ip']
                try:
                    if ip_type == '4':
                        network = ipaddr.IPv4Network(search_ip)
                    if ip_type == '6':
                        network = ipaddr.IPv6Network(search_ip)
                except ipaddr.AddressValueError, e:
                    form._errors['__all__'] = ErrorList(
                        ["Bad IPv{0} Address {1}".format(ip_type, search_ip)])
                    return render(request, 'cydhcp/cydhcp_form.html',
                                  {'form': form})
                try:
                    network = Network.objects.get(network_str=search_ip)
                    search_ip = network
                    found_exact = True
                except ObjectDoesNotExist:
                    found_exact = False
                    network = Network(ip_type,
                                      network_str=search_ip,
                                      ip_type=ip_type)
                parent = calc_parent(network)
                eldars, sub_networks = calc_networks(network)
                if ip_type == '6':
                    sip_upper, sip_lower = ipv6_to_longs(network.network.ip)
                    eip_upper, eip_lower = ipv6_to_longs(
                        network.network.broadcast)
                else:
                    sip_upper, sip_lower = 0, int(network.network.ip)
                    eip_upper, eip_lower = 0, int(network.network.broadcast)

                addrs_count = AddressRecord.objects.filter(
                    ip_upper__gte=sip_upper,
                    ip_lower__gte=sip_lower,
                    ip_upper__lte=eip_upper,
                    ip_lower__lte=eip_lower).count()

                if addrs_count > 100:
                    addrs = None  # This is too much
                else:
                    addrs = AddressRecord.objects.filter(
                        ip_upper__gte=sip_upper,
                        ip_lower__gte=sip_lower,
                        ip_upper__lte=eip_upper,
                        ip_lower__lte=eip_lower)

                ptrs_count = PTR.objects.filter(
                    ip_upper__gte=sip_upper,
                    ip_lower__gte=sip_lower,
                    ip_upper__lte=eip_upper,
                    ip_lower__lte=eip_lower).count()

                if ptrs_count > 100:
                    ptrs = None  # This is too much
                else:
                    ptrs = PTR.objects.filter(ip_upper__gte=sip_upper,
                                              ip_lower__gte=sip_lower,
                                              ip_upper__lte=eip_upper,
                                              ip_lower__lte=eip_lower)

            return render(
                request, 'cydhcp/cydhcp_results.html', {
                    'search_ip': search_ip,
                    'found_exact': found_exact,
                    'addrs': addrs,
                    'addrs_count': addrs_count,
                    'ptrs_count': ptrs_count,
                    'ptrs': ptrs,
                    'parent': parent,
                    'eldars': eldars,
                    'sub_networks': sub_networks,
                })
        except ValidationError, e:
            form._errors['__all__'] = ErrorList(e.messages)
            return render(request, 'cydhcp/cydhcp_form.html', {'form': form})
Ejemplo n.º 35
0
                    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)
        ip_upper, ip_lower = ipv6_to_longs(rdata.to_text())
        if AddressRecord.objects.filter(label=label,
                                        domain=domain,
                                        ip_upper=ip_upper,
                                        ip_lower=ip_lower,
                                        ip_type='6').exists():
            a = AddressRecord.objects.get(label=label,
                                          domain=domain,
                                          ip_type='6',
                                          ip_upper=ip_upper,
                                          ip_lower=ip_lower)
        else:
            a, _ = AddressRecord.objects.get_or_create(label=label,
                                                       domain=domain,
                                                       ip_str=rdata.to_text(),
                                                       ip_type='6')
Ejemplo n.º 36
0
 def test1_ipv6_to_longs(self):
     ip_str = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
     ip_upper, ip_lower = ipv6_to_longs(ip_str)
     self.assertEqual(hex(ip_upper).lower(), "0xffffffffffffffffl")
     self.assertEqual(hex(ip_lower).lower(), "0xffffffffffffffffl")
Ejemplo n.º 37
0
 def test1_ipv6_to_longs(self):
     ip_str = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
     ip_upper, ip_lower = ipv6_to_longs(ip_str)
     self.assertEqual(hex(ip_upper).lower(), "0xffffffffffffffffl")
     self.assertEqual(hex(ip_lower).lower(), "0xffffffffffffffffl")