diff --git a/nsupdate/main/forms.py b/nsupdate/main/forms.py index 629dc9f..4d0e5be 100644 --- a/nsupdate/main/forms.py +++ b/nsupdate/main/forms.py @@ -26,3 +26,9 @@ class CreateDomainForm(forms.ModelForm): widgets = { 'domain': forms.widgets.TextInput(attrs=dict(autofocus=None)), } + + +class EditDomainForm(forms.ModelForm): + class Meta(object): + model = Domain + fields = ['nameserver_ip', 'public', 'available', 'nameserver_update_algorithm', 'nameserver_update_key'] diff --git a/nsupdate/main/models.py b/nsupdate/main/models.py index 64c2641..241a636 100644 --- a/nsupdate/main/models.py +++ b/nsupdate/main/models.py @@ -1,5 +1,5 @@ import re -from django.utils.timezone import now +import base64 import dns.resolver @@ -10,6 +10,7 @@ from django.core.validators import RegexValidator from django.conf import settings from django.db.models.signals import pre_delete from django.contrib.auth.hashers import make_password +from django.utils.timezone import now from . import dnstools @@ -74,6 +75,23 @@ class Domain(models.Model): def __unicode__(self): return u"%s" % (self.domain, ) + def generate_ns_secret(self): + secret = User.objects.make_random_password(length=64) # 512 bits + self.nameserver_update_key = key = base64.b64encode(secret) + self.nameserver_update_algorithm = 'HMAC_SHA512' + self.save() + return key + + def get_bind9_algorithm(self): + mapping = { + 'HMAC_SHA512': 'hmac-sha512', + 'HMAC_SHA384': 'hmac-sha384', + 'HMAC_SHA256': 'hmac-sha256', + 'HMAC_SHA224': 'hmac-sha224', + 'HMAC_SHA1': 'hmac-sha1', + 'HMAC_MD5': 'hmac-md5', + } + return mapping.get(self.nameserver_update_algorithm) class Host(models.Model): subdomain = models.CharField(max_length=256, validators=[ diff --git a/nsupdate/main/templates/main/domain_overview.html b/nsupdate/main/templates/main/domain_overview.html index 07f2eed..222b2ae 100644 --- a/nsupdate/main/templates/main/domain_overview.html +++ b/nsupdate/main/templates/main/domain_overview.html @@ -13,19 +13,15 @@ Available? Nameserver IP Algorithm - Action {% for domain in domains %} - {{ domain.domain }} + {{ domain.domain }} {{ domain.public|yesno }} {{ domain.available|yesno }} {{ domain.nameserver_ip }} {{ domain.get_nameserver_update_algorithm_display }} - - delete - Key: {{ domain.nameserver_update_key }} diff --git a/nsupdate/main/urls.py b/nsupdate/main/urls.py index 898adae..387de84 100644 --- a/nsupdate/main/urls.py +++ b/nsupdate/main/urls.py @@ -2,8 +2,8 @@ from django.conf.urls import patterns, url from django.views.generic import TemplateView from .views import ( - HomeView, OverviewView, HostView, DeleteHostView, AboutView, HelpView, GenerateSecretView, - RobotsTxtView, DomainOverwievView, DeleteDomainView, ScreenshotsView) + HomeView, OverviewView, HostView, DeleteHostView, AboutView, HelpView, GenerateSecretView, GenerateNSSecretView, + RobotsTxtView, DomainOverwievView, DomainView, DeleteDomainView, ScreenshotsView) from ..api.views import ( MyIpView, DetectIpView, AjaxGetIps, NicUpdateView, AuthorizedNicUpdateView) @@ -18,7 +18,9 @@ urlpatterns = patterns( url(r'^screenshots/$', ScreenshotsView.as_view(), name="screenshots"), url(r'^overview/$', OverviewView.as_view(), name='overview'), url(r'^host/(?P\d+)/$', HostView.as_view(), name='host_view'), + url(r'^domain/(?P\d+)/$', DomainView.as_view(), name='domain_view'), url(r'^generate_secret/(?P\d+)/$', GenerateSecretView.as_view(), name='generate_secret_view'), + url(r'^generate_ns_secret/(?P\d+)/$', GenerateNSSecretView.as_view(), name='generate_ns_secret_view'), url(r'^host/(?P\d+)/delete/$', DeleteHostView.as_view(), name='delete_host'), url(r'^domain_overview/$', diff --git a/nsupdate/main/views.py b/nsupdate/main/views.py index 0776c27..1d990fd 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -12,7 +12,7 @@ from django.core.exceptions import PermissionDenied import dnstools -from .forms import CreateHostForm, EditHostForm, CreateDomainForm +from .forms import CreateHostForm, EditHostForm, CreateDomainForm, EditDomainForm from .models import Host, Domain @@ -40,6 +40,29 @@ class GenerateSecretView(UpdateView): return context +class GenerateNSSecretView(UpdateView): + model = Domain + template_name = "main/generate_ns_secret.html" + + @method_decorator(login_required) + def dispatch(self, *args, **kwargs): + return super(GenerateNSSecretView, self).dispatch(*args, **kwargs) + + def get_object(self, *args, **kwargs): + obj = super(GenerateNSSecretView, self).get_object(*args, **kwargs) + if obj.created_by != self.request.user: + raise PermissionDenied() # or Http404 + return obj + + def get_context_data(self, *args, **kwargs): + context = super(GenerateNSSecretView, self).get_context_data(*args, **kwargs) + context['nav_overview'] = True + context['shared_secret'] = self.object.generate_ns_secret() + context['domains'] = Domain.objects.filter(created_by=self.request.user) + messages.add_message(self.request, messages.SUCCESS, 'Nameserver shared secret created.') + return context + + class AboutView(TemplateView): template_name = "main/about.html" @@ -210,6 +233,37 @@ class DomainOverwievView(CreateView): return context +class DomainView(UpdateView): + model = Domain + template_name = "main/domain.html" + form_class = EditDomainForm + + @method_decorator(login_required) + def dispatch(self, *args, **kwargs): + return super(DomainView, self).dispatch(*args, **kwargs) + + def get_success_url(self): + return reverse('domain_overview') + + def form_valid(self, form): + self.object = form.save(commit=False) + self.object.save() + messages.add_message(self.request, messages.SUCCESS, 'Domain updated.') + return HttpResponseRedirect(self.get_success_url()) + + def get_object(self, *args, **kwargs): + obj = super(DomainView, self).get_object(*args, **kwargs) + if obj.created_by != self.request.user: + raise PermissionDenied() # or Http404 + return obj + + def get_context_data(self, *args, **kwargs): + context = super(DomainView, self).get_context_data(*args, **kwargs) + context['nav_overview'] = True + context['domains'] = Domain.objects.filter(created_by=self.request.user) + return context + + class DeleteDomainView(DeleteView): model = Domain template_name = "main/delete_object.html"