diff --git a/machines/admin.py b/machines/admin.py index 763cff43..e6165672 100644 --- a/machines/admin.py +++ b/machines/admin.py @@ -27,8 +27,19 @@ from django.contrib import admin from reversion.admin import VersionAdmin from .models import IpType, Machine, MachineType, Domain, IpList, Interface -from .models import Extension, SOA, Mx, Ns, Vlan, Txt, Nas, Service -from .models import OuverturePort, OuverturePortList +from .models import ( + Extension, + SOA, + Mx, + Ns, + Vlan, + Txt, + Srv, + Nas, + Service, + OuverturePort, + OuverturePortList +) class MachineAdmin(VersionAdmin): @@ -67,6 +78,10 @@ class TxtAdmin(VersionAdmin): pass +class SrvAdmin(VersionAdmin): + pass + + class NasAdmin(VersionAdmin): pass @@ -103,6 +118,7 @@ admin.site.register(SOA, SOAAdmin) admin.site.register(Mx, MxAdmin) admin.site.register(Ns, NsAdmin) admin.site.register(Txt, TxtAdmin) +admin.site.register(Srv, SrvAdmin) admin.site.register(IpList, IpListAdmin) admin.site.register(Interface, InterfaceAdmin) admin.site.register(Domain, DomainAdmin) diff --git a/machines/forms.py b/machines/forms.py index b043d9f5..d6aa5e3e 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -51,6 +51,7 @@ from .models import ( Ns, Service, Vlan, + Srv, Nas, IpType, OuverturePortList, @@ -380,6 +381,26 @@ class DelTxtForm(Form): widget=forms.CheckboxSelectMultiple ) + +class SrvForm(ModelForm): + """Ajout d'un srv pour une zone""" + class Meta: + model = Srv + fields = '__all__' + + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', self.Meta.model.__name__) + super(SrvForm, self).__init__(*args, prefix=prefix, **kwargs) + + +class DelSrvForm(Form): + """Suppression d'un ou plusieurs Srv""" + srv = forms.ModelMultipleChoiceField( + queryset=Srv.objects.all(), + label="Enregistrements Srv actuels", + widget=forms.CheckboxSelectMultiple + ) + class NasForm(ModelForm): """Ajout d'un type de nas (machine d'authentification, diff --git a/machines/migrations/0066_srv.py b/machines/migrations/0066_srv.py new file mode 100644 index 00000000..94b7d0ce --- /dev/null +++ b/machines/migrations/0066_srv.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-11-16 00:10 +from __future__ import unicode_literals + +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0065_auto_20171115_1514'), + ] + + operations = [ + migrations.CreateModel( + name='Srv', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('service', models.CharField(max_length=31)), + ('protocole', models.CharField(choices=[('TCP', 'TCP'), ('UDP', 'UDP')], default='TCP', max_length=3)), + ('ttl', models.PositiveIntegerField(default=172800, help_text='Time To Live')), + ('priority', models.PositiveIntegerField(validators=[django.core.validators.MaxValueValidator(65535)])), + ('weight', models.PositiveIntegerField(validators=[django.core.validators.MaxValueValidator(65535)])), + ('port', models.PositiveIntegerField(validators=[django.core.validators.MaxValueValidator(65535)])), + ('extension', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='machines.Extension')), + ('target', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='machines.Domain')), + ], + ), + ] diff --git a/machines/migrations/0067_auto_20171116_0152.py b/machines/migrations/0067_auto_20171116_0152.py new file mode 100644 index 00000000..dec7f865 --- /dev/null +++ b/machines/migrations/0067_auto_20171116_0152.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-11-16 00:52 +from __future__ import unicode_literals + +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0066_srv'), + ] + + operations = [ + migrations.AlterField( + model_name='srv', + name='port', + field=models.PositiveIntegerField(help_text='Port (tcp/udp)', validators=[django.core.validators.MaxValueValidator(65535)]), + ), + migrations.AlterField( + model_name='srv', + name='priority', + field=models.PositiveIntegerField(help_text="La priorité du serveur cible (valeur entière non négative, plus elle est faible, plus ce serveur sera utilisé s'il est disponible)", validators=[django.core.validators.MaxValueValidator(65535)]), + ), + migrations.AlterField( + model_name='srv', + name='target', + field=models.ForeignKey(help_text='Serveur cible', on_delete=django.db.models.deletion.PROTECT, to='machines.Domain'), + ), + migrations.AlterField( + model_name='srv', + name='weight', + field=models.PositiveIntegerField(help_text='Poids relatif pour les enregistrements de même priorité (valeur entière de 0 à 65535)', validators=[django.core.validators.MaxValueValidator(65535)]), + ), + ] diff --git a/machines/migrations/0068_auto_20171116_0252.py b/machines/migrations/0068_auto_20171116_0252.py new file mode 100644 index 00000000..781f1038 --- /dev/null +++ b/machines/migrations/0068_auto_20171116_0252.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-11-16 01:52 +from __future__ import unicode_literals + +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0067_auto_20171116_0152'), + ] + + operations = [ + migrations.AlterField( + model_name='extension', + name='name', + field=models.CharField(help_text='Nom de la zone, doit commencer par un point (.example.org)', max_length=255, unique=True), + ), + migrations.AlterField( + model_name='extension', + name='origin', + field=models.OneToOneField(blank=True, help_text='Enregistrement A associé à la zone', null=True, on_delete=django.db.models.deletion.PROTECT, to='machines.IpList'), + ), + migrations.AlterField( + model_name='extension', + name='origin_v6', + field=models.GenericIPAddressField(blank=True, help_text='Enregistremen AAAA associé à la zone', null=True, protocol='IPv6'), + ), + migrations.AlterField( + model_name='srv', + name='priority', + field=models.PositiveIntegerField(default=0, help_text="La priorité du serveur cible (valeur entière non négative, plus elle est faible, plus ce serveur sera utilisé s'il est disponible)", validators=[django.core.validators.MaxValueValidator(65535)]), + ), + migrations.AlterField( + model_name='srv', + name='weight', + field=models.PositiveIntegerField(default=0, help_text='Poids relatif pour les enregistrements de même priorité (valeur entière de 0 à 65535)', validators=[django.core.validators.MaxValueValidator(65535)]), + ), + ] diff --git a/machines/models.py b/machines/models.py index e3104f22..9bfb4b55 100644 --- a/machines/models.py +++ b/machines/models.py @@ -311,18 +311,24 @@ class Extension(models.Model): l'utiliser, associé à un origin (ip d'origine)""" PRETTY_NAME = "Extensions dns" - name = models.CharField(max_length=255, unique=True) + name = models.CharField( + max_length=255, + unique=True, + help_text="Nom de la zone, doit commencer par un point (.example.org)" + ) need_infra = models.BooleanField(default=False) origin = models.OneToOneField( 'IpList', on_delete=models.PROTECT, blank=True, - null=True + null=True, + help_text="Enregistrement A associé à la zone" ) origin_v6 = models.GenericIPAddressField( protocol='IPv6', null=True, - blank=True + blank=True, + help_text="Enregistrement AAAA associé à la zone" ) soa = models.ForeignKey( 'SOA', @@ -345,6 +351,11 @@ class Extension(models.Model): def __str__(self): return self.name + def clean(self): + if self.name and self.name[0] != '.': + raise ValidationError("Une extension doit commencer par un point") + super(Extension, self).clean(*args, **kwargs) + class Mx(models.Model): """ Entrées des MX. Enregistre la zone (extension) associée et la @@ -400,6 +411,63 @@ class Txt(models.Model): return str(self.field1).ljust(15) + " IN TXT " + str(self.field2) +class Srv(models.Model): + PRETTY_NAME = "Enregistrement Srv" + + TCP = 'TCP' + UDP = 'UDP' + + service = models.CharField(max_length=31) + protocole = models.CharField( + max_length=3, + choices=( + (TCP, 'TCP'), + (UDP, 'UDP'), + ), + default=TCP, + ) + extension = models.ForeignKey('Extension', on_delete=models.PROTECT) + ttl = models.PositiveIntegerField( + default=172800, # 2 days + help_text='Time To Live' + ) + priority = models.PositiveIntegerField( + default=0, + validators=[MaxValueValidator(65535)], + help_text="La priorité du serveur cible (valeur entière non négative,\ + plus elle est faible, plus ce serveur sera utilisé s'il est disponible)" + + ) + weight = models.PositiveIntegerField( + default=0, + validators=[MaxValueValidator(65535)], + help_text="Poids relatif pour les enregistrements de même priorité\ + (valeur entière de 0 à 65535)" + ) + port = models.PositiveIntegerField( + validators=[MaxValueValidator(65535)], + help_text="Port (tcp/udp)" + ) + target = models.ForeignKey( + 'Domain', + on_delete=models.PROTECT, + help_text="Serveur cible" + ) + + def __str__(self): + return str(self.service) + ' ' + str(self.protocole) + ' ' +\ + str(self.extension) + ' ' + str(self.priority) +\ + ' ' + str(self.weight) + str(self.port) + str(self.target) + + @cached_property + def dns_entry(self): + """Renvoie l'enregistrement SRV complet pour le fichier de zone""" + return str(self.service) + '._' + str(self.protocole).lower() +\ + str(self.extension) + '. ' + str(self.ttl) + ' IN SRV ' +\ + str(self.priority) + ' ' + str(self.weight) + ' ' +\ + str(self.port) + ' ' + str(self.target) + '.' + + class Interface(models.Model): """ Une interface. Objet clef de l'application machine : - une address mac unique. Possibilité de la rendre unique avec le @@ -930,3 +998,15 @@ def text_post_save(sender, **kwargs): def text_post_delete(sender, **kwargs): """Regeneration dns après modification d'un TX""" regen('dns') + + +@receiver(post_save, sender=Srv) +def srv_post_save(sender, **kwargs): + """Regeneration dns après modification d'un SRV""" + regen('dns') + + +@receiver(post_delete, sender=Srv) +def text_post_delete(sender, **kwargs): + """Regeneration dns après modification d'un SRV""" + regen('dns') diff --git a/machines/serializers.py b/machines/serializers.py index 6d128da4..d1876e06 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -33,6 +33,7 @@ from machines.models import ( Domain, Txt, Mx, + Srv, Service_link, Ns, OuverturePortList, @@ -212,6 +213,32 @@ class TxtSerializer(serializers.ModelSerializer): return str(obj.dns_entry) +class SrvSerializer(serializers.ModelSerializer): + """Serialisation d'un srv : zone cible et l'entrée txt""" + extension = serializers.SerializerMethodField('get_extension_name') + srv_entry = serializers.SerializerMethodField('get_srv_name') + + class Meta: + model = Srv + fields = ( + 'service', + 'protocole', + 'extension', + 'ttl', + 'priority', + 'weight', + 'port', + 'target', + 'srv_entry' + ) + + def get_extension_name(self, obj): + return str(obj.extension.name) + + def get_srv_name(self, obj): + return str(obj.dns_entry) + + class NsSerializer(serializers.ModelSerializer): """Serialisation d'un NS : la zone, l'entrée ns complète et le serveur ns sont évalués à part""" diff --git a/machines/templates/machines/aff_srv.html b/machines/templates/machines/aff_srv.html new file mode 100644 index 00000000..773815d9 --- /dev/null +++ b/machines/templates/machines/aff_srv.html @@ -0,0 +1,60 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au rezometz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2017 Gabriel Détraz +Copyright © 2017 Goulven Kermarec +Copyright © 2017 Augustin Lemesle + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +
Service | +Protocole | +Extension | +TTL | +Priorité | +Poids | +Port | +Cible | ++ | + |
---|---|---|---|---|---|---|---|---|---|
{{ srv.service }} | +{{ srv.protocole }} | +{{ srv.extension }} | +{{ srv.ttl }} | +{{ srv.priority }} | +{{ srv.weight }} | +{{ srv.port }} | +{{ srv.target }} | ++ {% if is_infra %} + {% include 'buttons/edit.html' with href='machines:edit-srv' id=srv.id %} + {% endif %} + {% include 'buttons/history.html' with href='machines:history' name='srv' id=srv.id %} + | +