2017-01-15 23:01:18 +00:00
|
|
|
# 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.
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
from django.db import models
|
2016-07-25 21:54:40 +00:00
|
|
|
from django.db.models.signals import post_save, post_delete
|
|
|
|
from django.dispatch import receiver
|
2016-07-06 20:49:16 +00:00
|
|
|
from django.forms import ValidationError
|
2016-07-03 14:49:29 +00:00
|
|
|
from macaddress.fields import MACAddressField
|
2016-11-20 16:57:10 +00:00
|
|
|
from netaddr import mac_bare, EUI
|
2016-11-15 14:25:27 +00:00
|
|
|
from django.core.validators import MinValueValidator,MaxValueValidator
|
2016-07-02 22:27:22 +00:00
|
|
|
|
2016-10-08 08:46:55 +00:00
|
|
|
from re2o.settings import MAIN_EXTENSION
|
2016-07-25 21:54:40 +00:00
|
|
|
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
class Machine(models.Model):
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Machine"
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
user = models.ForeignKey('users.User', on_delete=models.PROTECT)
|
2016-07-04 01:08:53 +00:00
|
|
|
name = models.CharField(max_length=255, help_text="Optionnel", blank=True, null=True)
|
2016-07-05 10:21:43 +00:00
|
|
|
active = models.BooleanField(default=True)
|
2016-07-02 22:27:22 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-07-04 00:48:24 +00:00
|
|
|
return str(self.user) + ' - ' + str(self.id) + ' - ' + str(self.name)
|
2016-07-02 22:27:22 +00:00
|
|
|
|
|
|
|
class MachineType(models.Model):
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Type de machine"
|
|
|
|
|
2016-10-22 22:55:58 +00:00
|
|
|
type = models.CharField(max_length=255)
|
|
|
|
ip_type = models.ForeignKey('IpType', on_delete=models.PROTECT, blank=True, null=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.type
|
|
|
|
|
|
|
|
class IpType(models.Model):
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Type d'ip"
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
type = models.CharField(max_length=255)
|
2016-07-08 15:54:06 +00:00
|
|
|
extension = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
2016-10-13 00:11:23 +00:00
|
|
|
need_infra = models.BooleanField(default=False)
|
2016-11-19 16:44:43 +00:00
|
|
|
domaine_ip = models.GenericIPAddressField(protocol='IPv4')
|
|
|
|
domaine_range = models.IntegerField(validators=[MinValueValidator(8), MaxValueValidator(32)])
|
2016-07-02 22:27:22 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.type
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2016-07-08 15:54:06 +00:00
|
|
|
class Extension(models.Model):
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Extensions dns"
|
|
|
|
|
2016-11-19 17:52:59 +00:00
|
|
|
name = models.CharField(max_length=255, unique=True)
|
2016-11-19 22:19:44 +00:00
|
|
|
need_infra = models.BooleanField(default=False)
|
2016-11-19 17:52:59 +00:00
|
|
|
origin = models.OneToOneField('IpList', on_delete=models.PROTECT, blank=True, null=True)
|
2016-07-08 15:54:06 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2016-11-19 16:44:43 +00:00
|
|
|
class Mx(models.Model):
|
|
|
|
PRETTY_NAME = "Enregistrements MX"
|
|
|
|
|
|
|
|
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
|
|
|
priority = models.IntegerField(unique=True)
|
2016-12-24 17:07:09 +00:00
|
|
|
name = models.OneToOneField('Domain', on_delete=models.PROTECT)
|
2016-11-19 16:44:43 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.zone) + ' ' + str(self.priority) + ' ' + str(self.name)
|
|
|
|
|
|
|
|
class Ns(models.Model):
|
|
|
|
PRETTY_NAME = "Enregistrements NS"
|
|
|
|
|
|
|
|
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
2016-12-26 16:43:41 +00:00
|
|
|
ns = models.OneToOneField('Domain', on_delete=models.PROTECT)
|
2016-11-19 16:44:43 +00:00
|
|
|
|
2016-11-19 17:21:05 +00:00
|
|
|
def __str__(self):
|
2016-12-26 16:43:41 +00:00
|
|
|
return str(self.zone) + ' ' + str(self.ns)
|
2016-11-19 17:21:05 +00:00
|
|
|
|
2016-07-03 14:49:29 +00:00
|
|
|
class Interface(models.Model):
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Interface"
|
|
|
|
|
2016-07-03 14:49:29 +00:00
|
|
|
ipv4 = models.OneToOneField('IpList', on_delete=models.PROTECT, blank=True, null=True)
|
2016-07-04 00:48:24 +00:00
|
|
|
#ipv6 = models.GenericIPAddressField(protocol='IPv6', null=True)
|
|
|
|
mac_address = MACAddressField(integer=False, unique=True)
|
2016-07-18 03:30:35 +00:00
|
|
|
machine = models.ForeignKey('Machine', on_delete=models.CASCADE)
|
2016-07-18 17:14:48 +00:00
|
|
|
type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
|
2016-07-04 00:48:24 +00:00
|
|
|
details = models.CharField(max_length=255, blank=True)
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2016-07-18 21:22:59 +00:00
|
|
|
def is_active(self):
|
|
|
|
""" Renvoie si une interface doit avoir accès ou non """
|
|
|
|
machine = self.machine
|
|
|
|
user = self.machine.user
|
|
|
|
return machine.active and user.has_access()
|
|
|
|
|
2016-11-20 16:57:10 +00:00
|
|
|
def mac_bare(self):
|
|
|
|
return str(EUI(self.mac_address, dialect=mac_bare)).lower()
|
|
|
|
|
2016-11-12 08:09:28 +00:00
|
|
|
def clean(self, *args, **kwargs):
|
|
|
|
self.mac_address = str(EUI(self.mac_address)) or None
|
|
|
|
|
2016-07-03 14:49:29 +00:00
|
|
|
def __str__(self):
|
2016-12-24 19:04:53 +00:00
|
|
|
try:
|
|
|
|
domain = self.domain
|
|
|
|
except:
|
|
|
|
domain = None
|
|
|
|
return str(domain)
|
2016-07-03 14:49:29 +00:00
|
|
|
|
2016-12-24 17:07:09 +00:00
|
|
|
class Domain(models.Model):
|
|
|
|
PRETTY_NAME = "Domaine dns"
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2016-12-24 17:07:09 +00:00
|
|
|
interface_parent = models.OneToOneField('Interface', on_delete=models.CASCADE, blank=True, null=True)
|
|
|
|
name = models.CharField(help_text="Obligatoire et unique, ne doit pas comporter de points", max_length=255)
|
2016-11-18 16:36:30 +00:00
|
|
|
extension = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
2016-12-24 17:07:09 +00:00
|
|
|
cname = models.ForeignKey('self', null=True, blank=True, related_name='related_domain')
|
2016-11-18 16:36:30 +00:00
|
|
|
|
|
|
|
class Meta:
|
2016-12-24 17:07:09 +00:00
|
|
|
unique_together = ("name", "extension")
|
2016-11-18 16:36:30 +00:00
|
|
|
|
2016-12-24 17:07:09 +00:00
|
|
|
def clean(self):
|
|
|
|
if self.interface_parent and self.cname:
|
|
|
|
raise ValidationError("On ne peut créer à la fois A et CNAME")
|
2016-12-24 19:04:53 +00:00
|
|
|
if self.cname==self:
|
2016-12-24 17:07:09 +00:00
|
|
|
raise ValidationError("On ne peut créer un cname sur lui même")
|
2016-11-01 01:14:06 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-12-24 17:07:09 +00:00
|
|
|
return str(self.name) + str(self.extension)
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2016-07-03 14:49:29 +00:00
|
|
|
class IpList(models.Model):
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Addresses ipv4"
|
|
|
|
|
2016-07-04 00:48:24 +00:00
|
|
|
ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True)
|
2016-10-22 22:55:58 +00:00
|
|
|
ip_type = models.ForeignKey('IpType', on_delete=models.PROTECT)
|
2016-12-01 02:00:55 +00:00
|
|
|
need_infra = models.BooleanField(default=False)
|
2016-07-03 14:49:29 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-07-04 00:48:24 +00:00
|
|
|
return self.ipv4
|
|
|
|
|
2016-07-25 21:54:40 +00:00
|
|
|
|
|
|
|
@receiver(post_save, sender=Interface)
|
|
|
|
def interface_post_save(sender, **kwargs):
|
|
|
|
user = kwargs['instance'].machine.user
|
2016-11-20 15:53:59 +00:00
|
|
|
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
|
2016-07-25 21:54:40 +00:00
|
|
|
|
|
|
|
@receiver(post_delete, sender=Interface)
|
|
|
|
def interface_post_delete(sender, **kwargs):
|
2016-12-14 17:09:24 +00:00
|
|
|
interface = kwargs['instance']
|
|
|
|
user = interface.machine.user
|
2016-11-20 15:53:59 +00:00
|
|
|
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
|
2016-12-14 17:09:24 +00:00
|
|
|
|