From ac262bd17c0e786d628a1acc6beb5ad8d1cb8364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Schneider?= Date: Tue, 16 Apr 2019 17:56:53 +0200 Subject: [PATCH] add support for TXT records! This commit change the way to edit a record. This add a unique identifier to all records (a hash of the name, data, ttl, class and type). All the records are now stored on the session (this avoid a too long GET). --- binder/forms.py | 2 +- binder/helpers.py | 2 ++ binder/models.py | 12 ++++++-- binder/settings.py | 5 +++- binder/templates/bcommon/add_record_form.html | 2 +- binder/templates/bcommon/list_zone.html | 2 +- binder/urls.py | 2 +- binder/views.py | 30 +++++++++++++++---- 8 files changed, 44 insertions(+), 13 deletions(-) diff --git a/binder/forms.py b/binder/forms.py index 9860b56..791af47 100644 --- a/binder/forms.py +++ b/binder/forms.py @@ -56,7 +56,7 @@ class FormAddForwardRecord(forms.Form): required=False) record_type = forms.ChoiceField(choices=settings.RECORD_TYPE_CHOICES) zone_name = forms.CharField(max_length=100) - record_data = forms.CharField(max_length=100) + record_data = forms.CharField(max_length=1000, widget=forms.Textarea) ttl = forms.ChoiceField(choices=settings.TTL_CHOICES) create_reverse = forms.BooleanField(required=False) key_name = forms.ModelChoiceField(queryset=Key.objects.all(), required=True) diff --git a/binder/helpers.py b/binder/helpers.py index 18f0439..112ebb0 100644 --- a/binder/helpers.py +++ b/binder/helpers.py @@ -148,6 +148,8 @@ def create_update(dns_server, zone_name, record_name, record_type, record_data, dns_update = dns.update.Update(zone_name, keyring=keyring, keyalgorithm=algorithm) + if record_type == "TXT": + record_data = '"{}"'.format(record_data) dns_update.replace(record_name, ttl, record_type, record_data) output = send_dns_update(dns_update, dns_server, server.dns_port, key_name) diff --git a/binder/models.py b/binder/models.py index bf7317d..e6e4301 100644 --- a/binder/models.py +++ b/binder/models.py @@ -3,6 +3,8 @@ # Standard Imports import base64 import binascii +import hashlib +import shlex import socket # 3rd Party @@ -221,14 +223,20 @@ class BindServer(models.Model): for current_name in names: current_record = zone[current_name].to_text(current_name) for split_record in current_record.split("\n"): - current_record = split_record.split(" ") + current_record = shlex.split(split_record) rr_dict = {} rr_dict["rr_name"] = current_record[0] rr_dict["rr_ttl"] = current_record[1] rr_dict["rr_class"] = current_record[2] rr_dict["rr_type"] = current_record[3] rr_dict["rr_data"] = current_record[4] - + rr_dict["rr_uid"] = hashlib.sha1("{}{}{}{}{}".format( + rr_dict["rr_name"], + rr_dict["rr_ttl"], + rr_dict["rr_class"], + rr_dict["rr_type"], + rr_dict["rr_data"], + ).encode()).hexdigest() record_array.append(rr_dict) return record_array diff --git a/binder/settings.py b/binder/settings.py index 1bdbd29..d9e28a9 100644 --- a/binder/settings.py +++ b/binder/settings.py @@ -162,7 +162,10 @@ TTL_CHOICES = ((300, "5 minutes"), RECORD_TYPE_CHOICES = (("A", "A"), ("AAAA", "AAAA"), - ("CNAME", "CNAME")) + ("CNAME", "CNAME"), + ("TXT", "TXT")) + +SESSION_SAVE_EVERY_REQUEST = True LOGIN_REDIRECT_URL = '/' diff --git a/binder/templates/bcommon/add_record_form.html b/binder/templates/bcommon/add_record_form.html index 4b34868..8ffd348 100644 --- a/binder/templates/bcommon/add_record_form.html +++ b/binder/templates/bcommon/add_record_form.html @@ -75,7 +75,7 @@ record in {{ form.zone_name.value }}
- +
{% if form.record_data.errors %}
diff --git a/binder/templates/bcommon/list_zone.html b/binder/templates/bcommon/list_zone.html index 513985a..6687727 100644 --- a/binder/templates/bcommon/list_zone.html +++ b/binder/templates/bcommon/list_zone.html @@ -33,7 +33,7 @@ Record Actions