More validation, started search window

This commit is contained in:
Sebastian Lohff 2017-03-27 12:18:02 +02:00
parent eb722f950b
commit 33de8402b7
6 changed files with 68 additions and 2 deletions

View File

@ -8,7 +8,17 @@
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading">Welcome!</div> <div class="panel-heading">Welcome!</div>
<div class="panel-body"> <div class="panel-body">
<p>
Here you can manage your darknet resources! Here you can manage your darknet resources!
</p>
<p>
<form class="form navbar-form" method="post" action="{% url "whoisdb:search" %}">
{% csrf_token %}
<input class="textinput form-control" type="text" name="q" placeholder="Search in DB">
<button class="btn btn-primary">Search</button>
</form>
</p>
</div> </div>
</div> </div>
</div> </div>

View File

@ -179,7 +179,6 @@ class InetNumForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
prefix = cleaned_data['prefix'] prefix = cleaned_data['prefix']
parent = cleaned_data['parent_range'] parent = cleaned_data['parent_range']
if parent: if parent:
parentNet = parent.getNetwork() parentNet = parent.getNetwork()
if cleaned_data['protocol'] != parent.protocol: if cleaned_data['protocol'] != parent.protocol:
@ -189,6 +188,7 @@ class InetNumForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
if prefix.network_address not in parentNet or prefix.prefixlen < parentNet.prefixlen: if prefix.network_address not in parentNet or prefix.prefixlen < parentNet.prefixlen:
raise forms.ValidationError("Prefix must be inside parent network range") raise forms.ValidationError("Prefix must be inside parent network range")
# check if parent block has net that overlaps with us # check if parent block has net that overlaps with us
for otherNet in parent.inetnum_set.all(): for otherNet in parent.inetnum_set.all():
if self.instance and self.instance.pk == otherNet.pk: if self.instance and self.instance.pk == otherNet.pk:
@ -197,6 +197,12 @@ class InetNumForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
if otherNet.getNetwork().overlaps(prefix): if otherNet.getNetwork().overlaps(prefix):
raise forms.ValidationError("The given prefix overlaps with network %s" % otherNet.handle) raise forms.ValidationError("The given prefix overlaps with network %s" % otherNet.handle)
# check if subnets to this subnet are (still) in current network
if not self._create:
for subnet in self.instance.inetnum_set.all():
if subnet.getNetwork().network_address not in self.instance.getNetwork():
raise forms.ValidationError("Subnet %s with %s is not in block anymore" % (subnet, subnet.getNetwork()))
self.instance.address = str(prefix.network_address) self.instance.address = str(prefix.network_address)
self.instance.netmask = prefix.prefixlen self.instance.netmask = prefix.prefixlen
@ -272,6 +278,18 @@ class ASBlockForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
asBegin <= block.asBegin <= asEnd or asBegin <= block.asEnd <= asEnd: asBegin <= block.asBegin <= asEnd or asBegin <= block.asEnd <= asEnd:
raise forms.ValidationError("Block overlaps with block %s" % block.handle) raise forms.ValidationError("Block overlaps with block %s" % block.handle)
if not self._create:
# check if subblocks are still in range
for subblock in self.instance.asblock_set.all():
if not (asBegin <= subblock.asBegin <= asEnd and asBegin <= subblock.asEnd <= asEnd):
raise forms.ValidationError("Subblock %s (%s - %s) is not contained in this block anymore" % (subblock, subblock.asBegin, subblock.asEnd))
# check if asnumbers are still in range
for asnumber in self.instance.asnumber_set.all():
if not (asBegin <= asnumber.number <= asEnd):
raise forms.ValidationError("AS %s (%s) is not contained in this block anymore" % (asnumber, asnumber.number))
return cleaned_data return cleaned_data

View File

@ -1,6 +1,7 @@
import whoisdb.models import whoisdb.models
import domains.models import domains.models
import ipaddress
def _addFields(fields, obj, fieldNames): def _addFields(fields, obj, fieldNames):
for fieldName in fieldNames: for fieldName in fieldNames:
@ -25,7 +26,7 @@ def getWhoisObjectFields(obj, owner):
fields.append(("AS Range", "%s - %s" % (obj.asBegin, obj.asEnd))) fields.append(("AS Range", "%s - %s" % (obj.asBegin, obj.asEnd)))
_addFields(fields, obj, ["description", "parent_block", "mnt_by", "mnt_lower", "admin_c"]) _addFields(fields, obj, ["description", "parent_block", "mnt_by", "mnt_lower", "admin_c"])
elif c == whoisdb.models.ASNumber: elif c == whoisdb.models.ASNumber:
_addFields(fields, obj, ["name", "number", "description", "volatile", "mnt_by", "mnt_lower", "admin_c"]) _addFields(fields, obj, ["name", "number", "description", "asblock", "volatile", "mnt_by", "mnt_lower", "admin_c"])
elif c == whoisdb.models.InetNum: elif c == whoisdb.models.InetNum:
_addFields(fields, obj, ["name"]) _addFields(fields, obj, ["name"])
fields.append(("Address CIDR", obj.prefix())) fields.append(("Address CIDR", obj.prefix()))
@ -45,3 +46,23 @@ def getWhoisObjectFields(obj, owner):
def guessWhoisObject(self, handle): def guessWhoisObject(self, handle):
# is it a normal handle? # is it a normal handle?
pass pass
def findInDatabase(rawValue):
# is this an ip address?
rawValue = rawValue.strip()
value = None
isIp = False
try:
value = ipaddress.ip_network(rawValue, strict=False)
isIp = true
except ValueError:
try:
value = ipaddress.ip_address(rawValue)
except ValueError:
pass
# try:
# - ipnetwork
# - ipaddress
# - asnumber (either asXXXXXX or just number XXXXXX)
# - handle name

View File

@ -179,6 +179,9 @@ class ASBlock(MntdObject):
mnt_lower = models.ManyToManyField(Maintainer, related_name='lower_asblock_set', blank=True) mnt_lower = models.ManyToManyField(Maintainer, related_name='lower_asblock_set', blank=True)
def contains(self, block):
return self.asBegin <= block.asBegin <= self.asEnd and self.asBegin <= block.asEnd <= self.asEnd
def getResource(self): def getResource(self):
return "%s - %s" % (self.asBegin, self.asEnd) return "%s - %s" % (self.asBegin, self.asEnd)

View File

@ -4,6 +4,10 @@ from . import views as whoisdb_views
urlpatterns = [ urlpatterns = [
url(r'^$', whoisdb_views.dbDashboard, name='dashboard'), url(r'^$', whoisdb_views.dbDashboard, name='dashboard'),
url(r'^search/$', whoisdb_views.searchObject, name='search'),
url(r'^create/$', whoisdb_views.createObjectOverview, name='createObjectOverview'), url(r'^create/$', whoisdb_views.createObjectOverview, name='createObjectOverview'),
url(r'^handle/(?P<handle>[A-Z0-9-]+)/$', whoisdb_views.showHandle, name='showhandle'), url(r'^handle/(?P<handle>[A-Z0-9-]+)/$', whoisdb_views.showHandle, name='showhandle'),
url(r'^handle/(?P<handle>[A-Z0-9-]+)/$', whoisdb_views.showHandle, name='handle-detail'), url(r'^handle/(?P<handle>[A-Z0-9-]+)/$', whoisdb_views.showHandle, name='handle-detail'),

View File

@ -11,6 +11,7 @@ from formtools.wizard.views import SessionWizardView
from .models import Maintainer, Contact, InetNum, ASBlock, ASNumber from .models import Maintainer, Contact, InetNum, ASBlock, ASNumber
from .forms import MntForm, MntInitialForm, ContactForm, ContactInitialForm, InetNumForm, ASBlockForm, ASNumberForm from .forms import MntForm, MntInitialForm, ContactForm, ContactInitialForm, InetNumForm, ASBlockForm, ASNumberForm
from .generic import DeleteCheckView, MntGenericMixin from .generic import DeleteCheckView, MntGenericMixin
from .helpers import findInDatabase
@login_required @login_required
@ -80,6 +81,15 @@ def showHandle(request, handle):
return render(request, "whoisdb/handle_show.html", {"object": obj}) return render(request, "whoisdb/handle_show.html", {"object": obj})
def searchObject(request):
if request.method == "POST":
name = request.POST.get("q", None)
if name:
#print(name)
findInDatabase(name)
return render(request, "whoisdb/search.html", {})
class MaintainerCreate(LoginRequiredMixin, CreateView): class MaintainerCreate(LoginRequiredMixin, CreateView):
template_name = "whoisdb/obj_create.html" template_name = "whoisdb/obj_create.html"
form_class = MntForm form_class = MntForm