2017-09-10 14:53:02 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
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.
|
|
|
|
#
|
2018-07-22 18:43:38 +00:00
|
|
|
# Copyright © 2016-2018 Gabriel Détraz
|
2017-01-15 23:01:18 +00:00
|
|
|
# Copyright © 2017 Goulven Kermarec
|
|
|
|
# Copyright © 2017 Augustin Lemesle
|
2018-07-22 18:43:38 +00:00
|
|
|
# Copyright © 2018 Charlie Jacomme
|
2017-01-15 23:01:18 +00:00
|
|
|
#
|
|
|
|
# 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.
|
2018-04-14 18:19:02 +00:00
|
|
|
"""machines.models
|
|
|
|
The models definitions for the Machines app
|
|
|
|
"""
|
2017-01-15 23:01:18 +00:00
|
|
|
|
2017-09-10 23:29:24 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import re
|
2018-01-30 03:51:15 +00:00
|
|
|
from ipaddress import IPv6Address
|
2018-04-02 01:52:15 +00:00
|
|
|
from itertools import chain
|
2018-04-14 18:19:02 +00:00
|
|
|
from netaddr import mac_bare, EUI, IPSet, IPRange, IPNetwork, IPAddress
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
from django.db import models
|
2017-10-15 20:10:33 +00:00
|
|
|
from django.db.models.signals import post_save, post_delete
|
2016-07-25 21:54:40 +00:00
|
|
|
from django.dispatch import receiver
|
2016-07-06 20:49:16 +00:00
|
|
|
from django.forms import ValidationError
|
2017-07-21 03:06:53 +00:00
|
|
|
from django.utils.functional import cached_property
|
2017-08-08 04:56:19 +00:00
|
|
|
from django.utils import timezone
|
2017-10-15 20:10:33 +00:00
|
|
|
from django.core.validators import MaxValueValidator
|
|
|
|
|
2016-07-03 14:49:29 +00:00
|
|
|
from macaddress.fields import MACAddressField
|
2016-07-02 22:27:22 +00:00
|
|
|
|
2017-12-30 00:48:23 +00:00
|
|
|
from re2o.field_permissions import FieldPermissionModelMixin
|
2018-04-13 22:48:44 +00:00
|
|
|
from re2o.mixins import AclMixin, RevMixin
|
2017-12-30 00:48:23 +00:00
|
|
|
|
2017-11-28 22:33:47 +00:00
|
|
|
import users.models
|
|
|
|
import preferences.models
|
|
|
|
|
2017-08-08 17:40:30 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Machine(RevMixin, FieldPermissionModelMixin, models.Model):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Class définissant une machine, object parent user, objets fils
|
|
|
|
interfaces"""
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Machine"
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
user = models.ForeignKey('users.User', on_delete=models.PROTECT)
|
2017-10-15 20:10:33 +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
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_machine", "Peut voir un objet machine quelquonque"),
|
2018-04-13 22:48:44 +00:00
|
|
|
("change_machine_user",
|
|
|
|
"Peut changer le propriétaire d'une machine"),
|
2017-12-31 18:47:22 +00:00
|
|
|
)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@classmethod
|
|
|
|
def get_instance(cls, machineid, *_args, **_kwargs):
|
2018-03-29 02:35:23 +00:00
|
|
|
"""Get the Machine instance with machineid.
|
|
|
|
:param userid: The id
|
|
|
|
:return: The user
|
|
|
|
"""
|
2018-04-14 18:19:02 +00:00
|
|
|
return cls.objects.get(pk=machineid)
|
2018-03-29 02:35:23 +00:00
|
|
|
|
2018-04-02 01:52:15 +00:00
|
|
|
def linked_objects(self):
|
|
|
|
"""Return linked objects : machine and domain.
|
|
|
|
Usefull in history display"""
|
2018-04-13 22:48:44 +00:00
|
|
|
return chain(
|
|
|
|
self.interface_set.all(),
|
|
|
|
Domain.objects.filter(
|
|
|
|
interface_parent__in=self.interface_set.all()
|
|
|
|
)
|
|
|
|
)
|
2018-04-02 01:52:15 +00:00
|
|
|
|
2017-12-30 00:48:23 +00:00
|
|
|
@staticmethod
|
2018-04-14 18:19:02 +00:00
|
|
|
def can_change_user(user_request, *_args, **_kwargs):
|
2017-12-30 00:48:23 +00:00
|
|
|
"""Checks if an user is allowed to change the user who owns a
|
|
|
|
Machine.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_request: The user requesting to change owner.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A tuple with a boolean stating if edition is allowed and an
|
|
|
|
explanation message.
|
|
|
|
"""
|
2018-04-13 22:48:44 +00:00
|
|
|
return (user_request.has_perm('machines.change_machine_user'),
|
|
|
|
"Vous ne pouvez pas modifier l'utilisateur de la machine.")
|
2017-12-30 00:48:23 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_view_all(user_request, *_args, **_kwargs):
|
2018-03-28 15:15:29 +00:00
|
|
|
"""Vérifie qu'on peut bien afficher l'ensemble des machines,
|
|
|
|
droit particulier correspondant
|
|
|
|
:param user_request: instance user qui fait l'edition
|
|
|
|
:return: True ou False avec la raison de l'échec le cas échéant"""
|
|
|
|
if not user_request.has_perm('machines.view_machine'):
|
2018-04-13 22:48:44 +00:00
|
|
|
return False, (u"Vous ne pouvez pas afficher l'ensemble des "
|
|
|
|
"machines sans permission")
|
2018-03-28 15:15:29 +00:00
|
|
|
return True, None
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_create(user_request, userid, *_args, **_kwargs):
|
2017-12-26 23:18:39 +00:00
|
|
|
"""Vérifie qu'un user qui fait la requète peut bien créer la machine
|
|
|
|
et n'a pas atteint son quota, et crée bien une machine à lui
|
|
|
|
:param user_request: Utilisateur qui fait la requête
|
|
|
|
:param userid: id de l'user dont on va créer une machine
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
2017-11-28 22:33:47 +00:00
|
|
|
try:
|
2017-11-30 20:38:16 +00:00
|
|
|
user = users.models.User.objects.get(pk=userid)
|
2017-11-28 22:33:47 +00:00
|
|
|
except users.models.User.DoesNotExist:
|
|
|
|
return False, u"Utilisateur inexistant"
|
2018-04-13 22:48:44 +00:00
|
|
|
max_lambdauser_interfaces = (preferences.models.OptionalMachine
|
|
|
|
.get_cached_value(
|
|
|
|
'max_lambdauser_interfaces'
|
|
|
|
))
|
2017-12-31 18:47:22 +00:00
|
|
|
if not user_request.has_perm('machines.add_machine'):
|
2018-04-13 22:48:44 +00:00
|
|
|
if not (preferences.models.OptionalMachine
|
|
|
|
.get_cached_value('create_machine')):
|
2018-02-28 15:36:59 +00:00
|
|
|
return False, u"Vous ne pouvez pas ajouter une machine"
|
2017-11-28 22:33:47 +00:00
|
|
|
if user != user_request:
|
2018-04-13 22:48:44 +00:00
|
|
|
return False, (u"Vous ne pouvez pas ajouter une machine à un "
|
|
|
|
"autre user que vous sans droit")
|
2017-11-28 22:33:47 +00:00
|
|
|
if user.user_interfaces().count() >= max_lambdauser_interfaces:
|
2018-04-13 22:48:44 +00:00
|
|
|
return False, (u"Vous avez atteint le maximum d'interfaces "
|
|
|
|
"autorisées que vous pouvez créer vous même "
|
|
|
|
"(%s) " % max_lambdauser_interfaces)
|
2017-11-28 22:33:47 +00:00
|
|
|
return True, None
|
|
|
|
|
2017-11-30 20:48:32 +00:00
|
|
|
def can_edit(self, user_request, *args, **kwargs):
|
2017-12-26 23:18:39 +00:00
|
|
|
"""Vérifie qu'on peut bien éditer cette instance particulière (soit
|
|
|
|
machine de soi, soit droit particulier
|
|
|
|
:param self: instance machine à éditer
|
|
|
|
:param user_request: instance user qui fait l'edition
|
|
|
|
:return: True ou False avec la raison le cas échéant"""
|
2018-01-08 00:21:24 +00:00
|
|
|
if self.user != user_request:
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.change_interface') or
|
|
|
|
not self.user.can_edit(
|
|
|
|
self.user,
|
|
|
|
user_request,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
)[0]):
|
|
|
|
return False, (u"Vous ne pouvez pas éditer une machine "
|
|
|
|
"d'un autre user que vous sans droit")
|
2017-12-11 21:02:32 +00:00
|
|
|
return True, None
|
|
|
|
|
2017-12-03 15:32:43 +00:00
|
|
|
def can_delete(self, user_request, *args, **kwargs):
|
2017-12-26 23:18:39 +00:00
|
|
|
"""Vérifie qu'on peut bien supprimer cette instance particulière (soit
|
|
|
|
machine de soi, soit droit particulier
|
|
|
|
:param self: instance machine à supprimer
|
|
|
|
:param user_request: instance user qui fait l'edition
|
|
|
|
:return: True ou False avec la raison de l'échec le cas échéant"""
|
2018-01-08 00:21:24 +00:00
|
|
|
if self.user != user_request:
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.change_interface') or
|
|
|
|
not self.user.can_edit(
|
|
|
|
self.user,
|
|
|
|
user_request,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
)[0]):
|
|
|
|
return False, (u"Vous ne pouvez pas éditer une machine "
|
|
|
|
"d'un autre user que vous sans droit")
|
2017-12-03 15:32:43 +00:00
|
|
|
return True, None
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
def can_view(self, user_request, *_args, **_kwargs):
|
2017-12-26 23:18:39 +00:00
|
|
|
"""Vérifie qu'on peut bien voir cette instance particulière (soit
|
|
|
|
machine de soi, soit droit particulier
|
|
|
|
:param self: instance machine à éditer
|
|
|
|
:param user_request: instance user qui fait l'edition
|
|
|
|
:return: True ou False avec la raison de l'échec le cas échéant"""
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.view_machine') and
|
|
|
|
self.user != user_request):
|
|
|
|
return False, (u"Vous n'avez pas droit de voir les machines autre "
|
|
|
|
"que les vôtres")
|
2017-12-29 00:59:55 +00:00
|
|
|
return True, None
|
2017-12-03 16:43:07 +00:00
|
|
|
|
2018-01-08 22:57:19 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Machine, self).__init__(*args, **kwargs)
|
|
|
|
self.field_permissions = {
|
2018-04-13 22:48:44 +00:00
|
|
|
'user': self.can_change_user,
|
2018-01-08 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
def __str__(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
return str(self.user) + ' - ' + str(self.id) + ' - ' + str(self.name)
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class MachineType(RevMixin, AclMixin, models.Model):
|
2017-07-23 03:03:56 +00:00
|
|
|
""" Type de machine, relié à un type d'ip, affecté aux interfaces"""
|
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)
|
2017-10-15 20:10:33 +00:00
|
|
|
ip_type = models.ForeignKey(
|
|
|
|
'IpType',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
blank=True,
|
|
|
|
null=True
|
|
|
|
)
|
2016-10-22 22:55:58 +00:00
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_machinetype", "Peut voir un objet machinetype"),
|
2018-04-13 22:48:44 +00:00
|
|
|
("use_all_machinetype",
|
|
|
|
"Peut utiliser n'importe quel type de machine"),
|
2017-12-31 18:47:22 +00:00
|
|
|
)
|
|
|
|
|
2017-07-23 03:03:56 +00:00
|
|
|
def all_interfaces(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Renvoie toutes les interfaces (cartes réseaux) de type
|
|
|
|
machinetype"""
|
2017-07-23 03:03:56 +00:00
|
|
|
return Interface.objects.filter(type=self)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_use_all(user_request, *_args, **_kwargs):
|
2017-12-30 00:48:23 +00:00
|
|
|
"""Check if an user can use every MachineType.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_request: The user requesting edition.
|
|
|
|
Returns:
|
|
|
|
A tuple with a boolean stating if user can acces and an explanation
|
|
|
|
message is acces is not allowed.
|
|
|
|
"""
|
2017-12-31 18:47:22 +00:00
|
|
|
if not user_request.has_perm('machines.use_all_machinetype'):
|
2018-04-13 22:48:44 +00:00
|
|
|
return False, (u"Vous n'avez pas le droit d'utiliser tout types "
|
|
|
|
"de machines")
|
2017-12-30 00:48:23 +00:00
|
|
|
return True, None
|
|
|
|
|
2016-10-22 22:55:58 +00:00
|
|
|
def __str__(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
return self.type
|
|
|
|
|
2016-10-22 22:55:58 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class IpType(RevMixin, AclMixin, models.Model):
|
2017-07-23 03:03:56 +00:00
|
|
|
""" Type d'ip, définissant un range d'ip, affecté aux machine types"""
|
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)
|
2017-08-28 22:35:03 +00:00
|
|
|
domaine_ip_start = models.GenericIPAddressField(protocol='IPv4')
|
|
|
|
domaine_ip_stop = models.GenericIPAddressField(protocol='IPv4')
|
2017-10-15 20:10:33 +00:00
|
|
|
prefix_v6 = models.GenericIPAddressField(
|
|
|
|
protocol='IPv6',
|
|
|
|
null=True,
|
|
|
|
blank=True
|
|
|
|
)
|
|
|
|
vlan = models.ForeignKey(
|
|
|
|
'Vlan',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
blank=True,
|
|
|
|
null=True
|
|
|
|
)
|
|
|
|
ouverture_ports = models.ForeignKey(
|
|
|
|
'OuverturePortList',
|
|
|
|
blank=True,
|
|
|
|
null=True
|
|
|
|
)
|
2016-07-02 22:27:22 +00:00
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_iptype", "Peut voir un objet iptype"),
|
|
|
|
("use_all_iptype", "Peut utiliser tous les iptype"),
|
|
|
|
)
|
|
|
|
|
2017-07-21 03:06:53 +00:00
|
|
|
@cached_property
|
2017-08-28 22:35:03 +00:00
|
|
|
def ip_range(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Renvoie un objet IPRange à partir de l'objet IpType"""
|
|
|
|
return IPRange(self.domaine_ip_start, end=self.domaine_ip_stop)
|
2017-07-21 03:06:53 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def ip_set(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Renvoie une IPSet à partir de l'iptype"""
|
2017-08-28 22:35:03 +00:00
|
|
|
return IPSet(self.ip_range)
|
2017-07-21 03:06:53 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def ip_set_as_str(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Renvoie une liste des ip en string"""
|
2017-07-21 03:06:53 +00:00
|
|
|
return [str(x) for x in self.ip_set]
|
|
|
|
|
|
|
|
def ip_objects(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Renvoie tous les objets ipv4 relié à ce type"""
|
2017-07-21 04:49:39 +00:00
|
|
|
return IpList.objects.filter(ip_type=self)
|
|
|
|
|
|
|
|
def free_ip(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Renvoie toutes les ip libres associées au type donné (self)"""
|
2017-10-15 20:10:33 +00:00
|
|
|
return IpList.objects.filter(
|
|
|
|
interface__isnull=True
|
|
|
|
).filter(ip_type=self)
|
2017-07-21 03:06:53 +00:00
|
|
|
|
|
|
|
def gen_ip_range(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Cree les IpList associées au type self. Parcours pédestrement et
|
|
|
|
crée les ip une par une. Si elles existent déjà, met à jour le type
|
|
|
|
associé à l'ip"""
|
2017-07-21 03:06:53 +00:00
|
|
|
# Creation du range d'ip dans les objets iplist
|
2017-08-28 22:35:03 +00:00
|
|
|
networks = []
|
|
|
|
for net in self.ip_range.cidrs():
|
|
|
|
networks += net.iter_hosts()
|
|
|
|
ip_obj = [IpList(ip_type=self, ipv4=str(ip)) for ip in networks]
|
2017-10-15 20:10:33 +00:00
|
|
|
listes_ip = IpList.objects.filter(
|
|
|
|
ipv4__in=[str(ip) for ip in networks]
|
|
|
|
)
|
2017-08-28 23:04:15 +00:00
|
|
|
# Si il n'y a pas d'ip, on les crée
|
|
|
|
if not listes_ip:
|
|
|
|
IpList.objects.bulk_create(ip_obj)
|
|
|
|
# Sinon on update l'ip_type
|
|
|
|
else:
|
|
|
|
listes_ip.update(ip_type=self)
|
2017-08-23 21:26:49 +00:00
|
|
|
return
|
2017-07-21 03:06:53 +00:00
|
|
|
|
|
|
|
def del_ip_range(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Methode dépréciée, IpList est en mode cascade et supprimé
|
|
|
|
automatiquement"""
|
2017-07-21 04:49:39 +00:00
|
|
|
if Interface.objects.filter(ipv4__in=self.ip_objects()):
|
2017-10-15 20:10:33 +00:00
|
|
|
raise ValidationError("Une ou plusieurs ip du range sont\
|
|
|
|
affectées, impossible de supprimer le range")
|
2017-07-21 03:06:53 +00:00
|
|
|
for ip in self.ip_objects():
|
|
|
|
ip.delete()
|
|
|
|
|
2018-01-30 03:51:15 +00:00
|
|
|
def check_replace_prefixv6(self):
|
|
|
|
"""Remplace les prefixv6 des interfaces liées à ce type d'ip"""
|
|
|
|
if not self.prefix_v6:
|
|
|
|
return
|
|
|
|
else:
|
2018-04-13 22:48:44 +00:00
|
|
|
for ipv6 in Ipv6List.objects.filter(
|
2018-04-14 18:19:02 +00:00
|
|
|
interface__in=Interface.objects.filter(
|
|
|
|
type__in=MachineType.objects.filter(ip_type=self)
|
|
|
|
)
|
|
|
|
):
|
2018-01-30 03:51:15 +00:00
|
|
|
ipv6.check_and_replace_prefix(prefix=self.prefix_v6)
|
|
|
|
|
2017-07-21 03:06:53 +00:00
|
|
|
def clean(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Nettoyage. Vérifie :
|
|
|
|
- Que ip_stop est après ip_start
|
|
|
|
- Qu'on ne crée pas plus gros qu'un /16
|
|
|
|
- Que le range crée ne recoupe pas un range existant
|
|
|
|
- Formate l'ipv6 donnée en /64"""
|
2017-08-28 22:35:03 +00:00
|
|
|
if IPAddress(self.domaine_ip_start) > IPAddress(self.domaine_ip_stop):
|
|
|
|
raise ValidationError("Domaine end doit être après start...")
|
|
|
|
# On ne crée pas plus grand qu'un /16
|
|
|
|
if self.ip_range.size > 65536:
|
2017-10-15 20:10:33 +00:00
|
|
|
raise ValidationError("Le range est trop gros, vous ne devez\
|
|
|
|
pas créer plus grand qu'un /16")
|
2017-07-21 03:06:53 +00:00
|
|
|
# On check que les / ne se recoupent pas
|
2017-08-26 13:10:18 +00:00
|
|
|
for element in IpType.objects.all().exclude(pk=self.pk):
|
2017-07-21 03:06:53 +00:00
|
|
|
if not self.ip_set.isdisjoint(element.ip_set):
|
2017-10-15 20:10:33 +00:00
|
|
|
raise ValidationError("Le range indiqué n'est pas disjoint\
|
|
|
|
des ranges existants")
|
2017-10-03 00:36:39 +00:00
|
|
|
# On formate le prefix v6
|
|
|
|
if self.prefix_v6:
|
|
|
|
self.prefix_v6 = str(IPNetwork(self.prefix_v6 + '/64').network)
|
2017-07-21 03:06:53 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.clean()
|
|
|
|
super(IpType, self).save(*args, **kwargs)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_use_all(user_request, *_args, **_kwargs):
|
2018-04-13 22:48:44 +00:00
|
|
|
"""Superdroit qui permet d'utiliser toutes les extensions sans
|
|
|
|
restrictions
|
2017-12-30 00:48:23 +00:00
|
|
|
:param user_request: instance user qui fait l'edition
|
|
|
|
:return: True ou False avec la raison de l'échec le cas échéant"""
|
2017-12-31 18:47:22 +00:00
|
|
|
return user_request.has_perm('machines.use_all_iptype'), None
|
2017-12-30 00:48:23 +00:00
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.type
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Vlan(RevMixin, AclMixin, models.Model):
|
2017-10-15 18:37:21 +00:00
|
|
|
""" Un vlan : vlan_id et nom
|
|
|
|
On limite le vlan id entre 0 et 4096, comme défini par la norme"""
|
2017-08-26 13:10:18 +00:00
|
|
|
PRETTY_NAME = "Vlans"
|
|
|
|
|
2017-10-15 18:37:21 +00:00
|
|
|
vlan_id = models.PositiveIntegerField(validators=[MaxValueValidator(4095)])
|
2017-08-26 13:10:18 +00:00
|
|
|
name = models.CharField(max_length=256)
|
|
|
|
comment = models.CharField(max_length=256, blank=True)
|
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_vlan", "Peut voir un objet vlan"),
|
|
|
|
)
|
|
|
|
|
2017-08-26 13:10:18 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Nas(RevMixin, AclMixin, models.Model):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Les nas. Associé à un machine_type.
|
2017-10-05 01:21:36 +00:00
|
|
|
Permet aussi de régler le port_access_mode (802.1X ou mac-address) pour
|
|
|
|
le radius. Champ autocapture de la mac à true ou false"""
|
2017-09-10 22:33:45 +00:00
|
|
|
PRETTY_NAME = "Correspondance entre les nas et les machines connectées"
|
|
|
|
|
2017-09-13 13:04:09 +00:00
|
|
|
default_mode = '802.1X'
|
|
|
|
AUTH = (
|
|
|
|
('802.1X', '802.1X'),
|
|
|
|
('Mac-address', 'Mac-address'),
|
|
|
|
)
|
|
|
|
|
2017-09-10 22:33:45 +00:00
|
|
|
name = models.CharField(max_length=255, unique=True)
|
2017-10-15 20:10:33 +00:00
|
|
|
nas_type = models.ForeignKey(
|
|
|
|
'MachineType',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='nas_type'
|
|
|
|
)
|
|
|
|
machine_type = models.ForeignKey(
|
|
|
|
'MachineType',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='machinetype_on_nas'
|
|
|
|
)
|
|
|
|
port_access_mode = models.CharField(
|
|
|
|
choices=AUTH,
|
|
|
|
default=default_mode,
|
|
|
|
max_length=32
|
|
|
|
)
|
2017-09-14 13:44:51 +00:00
|
|
|
autocapture_mac = models.BooleanField(default=False)
|
2017-09-10 22:33:45 +00:00
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_nas", "Peut voir un objet Nas"),
|
|
|
|
)
|
|
|
|
|
2017-09-10 22:33:45 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class SOA(RevMixin, AclMixin, models.Model):
|
2017-10-19 23:52:38 +00:00
|
|
|
"""
|
|
|
|
Un enregistrement SOA associé à une extension
|
|
|
|
Les valeurs par défault viennent des recommandations RIPE :
|
|
|
|
https://www.ripe.net/publications/docs/ripe-203
|
|
|
|
"""
|
|
|
|
PRETTY_NAME = "Enregistrement SOA"
|
|
|
|
|
2017-10-21 12:49:03 +00:00
|
|
|
name = models.CharField(max_length=255)
|
2017-10-19 23:52:38 +00:00
|
|
|
mail = models.EmailField(
|
|
|
|
help_text='Email du contact pour la zone'
|
|
|
|
)
|
|
|
|
refresh = models.PositiveIntegerField(
|
2018-04-13 22:48:44 +00:00
|
|
|
default=86400, # 24 hours
|
2017-10-19 23:52:38 +00:00
|
|
|
help_text='Secondes avant que les DNS secondaires doivent demander le\
|
|
|
|
serial du DNS primaire pour détecter une modification'
|
|
|
|
)
|
|
|
|
retry = models.PositiveIntegerField(
|
2018-04-13 22:48:44 +00:00
|
|
|
default=7200, # 2 hours
|
2017-10-19 23:52:38 +00:00
|
|
|
help_text='Secondes avant que les DNS secondaires fassent une nouvelle\
|
|
|
|
demande de serial en cas de timeout du DNS primaire'
|
|
|
|
)
|
|
|
|
expire = models.PositiveIntegerField(
|
2018-04-13 22:48:44 +00:00
|
|
|
default=3600000, # 1000 hours
|
2017-10-19 23:52:38 +00:00
|
|
|
help_text='Secondes après lesquelles les DNS secondaires arrêtent de\
|
|
|
|
de répondre aux requêtes en cas de timeout du DNS primaire'
|
|
|
|
)
|
|
|
|
ttl = models.PositiveIntegerField(
|
|
|
|
default=172800, # 2 days
|
|
|
|
help_text='Time To Live'
|
|
|
|
)
|
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_soa", "Peut voir un objet soa"),
|
|
|
|
)
|
|
|
|
|
2017-10-19 23:52:38 +00:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.name)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def dns_soa_param(self):
|
|
|
|
"""
|
|
|
|
Renvoie la partie de l'enregistrement SOA correspondant aux champs :
|
|
|
|
<refresh> ; refresh
|
|
|
|
<retry> ; retry
|
|
|
|
<expire> ; expire
|
|
|
|
<ttl> ; TTL
|
|
|
|
"""
|
|
|
|
return (
|
2017-10-20 01:30:38 +00:00
|
|
|
' {refresh}; refresh\n'
|
|
|
|
' {retry}; retry\n'
|
|
|
|
' {expire}; expire\n'
|
|
|
|
' {ttl}; TTL'
|
2017-10-19 23:52:38 +00:00
|
|
|
).format(
|
2017-10-20 00:28:47 +00:00
|
|
|
refresh=str(self.refresh).ljust(12),
|
|
|
|
retry=str(self.retry).ljust(12),
|
|
|
|
expire=str(self.expire).ljust(12),
|
|
|
|
ttl=str(self.ttl).ljust(12)
|
2017-10-19 23:52:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def dns_soa_mail(self):
|
|
|
|
""" Renvoie le mail dans l'enregistrement SOA """
|
|
|
|
mail_fields = str(self.mail).split('@')
|
2017-10-20 01:24:14 +00:00
|
|
|
return mail_fields[0].replace('.', '\\.') + '.' + mail_fields[1] + '.'
|
2017-10-19 23:52:38 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def new_default_soa(cls):
|
|
|
|
""" Fonction pour créer un SOA par défaut, utile pour les nouvelles
|
|
|
|
extensions .
|
|
|
|
/!\ Ne jamais supprimer ou renommer cette fonction car elle est
|
|
|
|
utilisée dans les migrations de la BDD. """
|
2018-04-13 22:48:44 +00:00
|
|
|
return cls.objects.get_or_create(
|
|
|
|
name="SOA to edit",
|
|
|
|
mail="postmaser@example.com"
|
|
|
|
)[0].pk
|
2017-10-19 23:52:38 +00:00
|
|
|
|
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Extension(RevMixin, AclMixin, models.Model):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Extension dns type example.org. Précise si tout le monde peut
|
|
|
|
l'utiliser, associé à un origin (ip d'origine)"""
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Extensions dns"
|
|
|
|
|
2017-11-16 01:33:57 +00:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
unique=True,
|
|
|
|
help_text="Nom de la zone, doit commencer par un point (.example.org)"
|
|
|
|
)
|
2016-11-19 22:19:44 +00:00
|
|
|
need_infra = models.BooleanField(default=False)
|
2018-04-09 20:49:44 +00:00
|
|
|
origin = models.ForeignKey(
|
2017-10-15 20:10:33 +00:00
|
|
|
'IpList',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
blank=True,
|
2017-11-16 01:33:57 +00:00
|
|
|
null=True,
|
|
|
|
help_text="Enregistrement A associé à la zone"
|
2017-10-15 20:10:33 +00:00
|
|
|
)
|
2017-10-18 14:27:48 +00:00
|
|
|
origin_v6 = models.GenericIPAddressField(
|
|
|
|
protocol='IPv6',
|
|
|
|
null=True,
|
2017-11-16 01:33:57 +00:00
|
|
|
blank=True,
|
2017-11-16 02:36:05 +00:00
|
|
|
help_text="Enregistrement AAAA associé à la zone"
|
2017-10-18 14:27:48 +00:00
|
|
|
)
|
2017-10-19 23:52:38 +00:00
|
|
|
soa = models.ForeignKey(
|
|
|
|
'SOA',
|
2018-04-29 16:34:05 +00:00
|
|
|
on_delete=models.CASCADE
|
2017-10-19 23:52:38 +00:00
|
|
|
)
|
2016-07-08 15:54:06 +00:00
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_extension", "Peut voir un objet extension"),
|
|
|
|
("use_all_extension", "Peut utiliser toutes les extension"),
|
|
|
|
)
|
|
|
|
|
2017-09-06 09:07:36 +00:00
|
|
|
@cached_property
|
|
|
|
def dns_entry(self):
|
2017-10-18 14:27:48 +00:00
|
|
|
""" Une entrée DNS A et AAAA sur origin (zone self)"""
|
|
|
|
entry = ""
|
|
|
|
if self.origin:
|
2017-10-20 01:24:14 +00:00
|
|
|
entry += "@ IN A " + str(self.origin)
|
2017-10-18 14:27:48 +00:00
|
|
|
if self.origin_v6:
|
|
|
|
if entry:
|
|
|
|
entry += "\n"
|
2017-10-20 01:24:14 +00:00
|
|
|
entry += "@ IN AAAA " + str(self.origin_v6)
|
2017-10-18 14:27:48 +00:00
|
|
|
return entry
|
2017-09-06 09:07:36 +00:00
|
|
|
|
2018-06-10 00:47:25 +00:00
|
|
|
def get_associated_a_records(self):
|
2018-06-23 21:36:01 +00:00
|
|
|
from re2o.utils import all_active_assigned_interfaces
|
|
|
|
return (all_active_assigned_interfaces()
|
2018-06-23 21:19:11 +00:00
|
|
|
.filter(type__ip_type__extension=self)
|
2018-06-23 21:36:01 +00:00
|
|
|
.filter(ipv4__isnull=False))
|
2018-06-10 00:47:25 +00:00
|
|
|
|
|
|
|
def get_associated_aaaa_records(self):
|
2018-06-23 21:36:01 +00:00
|
|
|
from re2o.utils import all_active_interfaces
|
|
|
|
return (all_active_interfaces(full=True)
|
|
|
|
.filter(type__ip_type__extension=self))
|
2018-06-10 00:47:25 +00:00
|
|
|
|
|
|
|
def get_associated_cname_records(self):
|
2018-06-23 21:36:01 +00:00
|
|
|
from re2o.utils import all_active_assigned_interfaces
|
2018-06-23 21:19:11 +00:00
|
|
|
return (Domain.objects
|
|
|
|
.filter(extension=self)
|
|
|
|
.filter(cname__isnull=False)
|
2018-06-23 21:36:01 +00:00
|
|
|
.filter(interface_parent__in=all_active_assigned_interfaces())
|
2018-06-23 21:19:11 +00:00
|
|
|
.prefetch_related('cname'))
|
2018-06-10 00:47:25 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_use_all(user_request, *_args, **_kwargs):
|
2018-04-13 22:48:44 +00:00
|
|
|
"""Superdroit qui permet d'utiliser toutes les extensions sans
|
|
|
|
restrictions
|
2017-12-29 00:42:00 +00:00
|
|
|
:param user_request: instance user qui fait l'edition
|
|
|
|
:return: True ou False avec la raison de l'échec le cas échéant"""
|
2017-12-31 18:47:22 +00:00
|
|
|
return user_request.has_perm('machines.use_all_extension'), None
|
2017-12-29 00:42:00 +00:00
|
|
|
|
2016-07-08 15:54:06 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2018-01-30 02:02:43 +00:00
|
|
|
def clean(self, *args, **kwargs):
|
2017-11-16 01:33:57 +00:00
|
|
|
if self.name and self.name[0] != '.':
|
|
|
|
raise ValidationError("Une extension doit commencer par un point")
|
|
|
|
super(Extension, self).clean(*args, **kwargs)
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Mx(RevMixin, AclMixin, models.Model):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Entrées des MX. Enregistre la zone (extension) associée et la
|
|
|
|
priorité
|
2017-10-05 01:21:36 +00:00
|
|
|
Todo : pouvoir associer un MX à une interface """
|
2016-11-19 16:44:43 +00:00
|
|
|
PRETTY_NAME = "Enregistrements MX"
|
|
|
|
|
|
|
|
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
2017-10-15 18:37:21 +00:00
|
|
|
priority = models.PositiveIntegerField(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
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_mx", "Peut voir un objet mx"),
|
|
|
|
)
|
|
|
|
|
2017-09-06 09:07:36 +00:00
|
|
|
@cached_property
|
|
|
|
def dns_entry(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Renvoie l'entrée DNS complète pour un MX à mettre dans les
|
|
|
|
fichiers de zones"""
|
2018-04-13 22:48:44 +00:00
|
|
|
return "@ IN MX {prior} {name}".format(
|
|
|
|
prior=str(self.priority).ljust(3),
|
2018-04-14 01:53:34 +00:00
|
|
|
name=str(self.name)
|
2018-04-13 22:48:44 +00:00
|
|
|
)
|
2017-09-06 09:07:36 +00:00
|
|
|
|
2016-11-19 16:44:43 +00:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.zone) + ' ' + str(self.priority) + ' ' + str(self.name)
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Ns(RevMixin, AclMixin, models.Model):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Liste des enregistrements name servers par zone considéérée"""
|
2016-11-19 16:44:43 +00:00
|
|
|
PRETTY_NAME = "Enregistrements NS"
|
|
|
|
|
|
|
|
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
2018-05-03 04:40:04 +00:00
|
|
|
ns = models.ForeignKey('Domain', on_delete=models.PROTECT)
|
2016-11-19 16:44:43 +00:00
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2017-12-31 20:00:52 +00:00
|
|
|
("view_ns", "Peut voir un objet ns"),
|
2017-12-31 18:47:22 +00:00
|
|
|
)
|
|
|
|
|
2017-09-06 09:07:36 +00:00
|
|
|
@cached_property
|
|
|
|
def dns_entry(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Renvoie un enregistrement NS complet pour les filezones"""
|
2017-10-22 03:04:49 +00:00
|
|
|
return "@ IN NS " + str(self.ns)
|
2017-09-06 09:07:36 +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
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Txt(RevMixin, AclMixin, models.Model):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Un enregistrement TXT associé à une extension"""
|
2017-10-19 23:52:38 +00:00
|
|
|
PRETTY_NAME = "Enregistrement TXT"
|
2017-09-05 16:18:41 +00:00
|
|
|
|
|
|
|
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
|
|
|
field1 = models.CharField(max_length=255)
|
2017-11-15 02:06:33 +00:00
|
|
|
field2 = models.TextField(max_length=2047)
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_txt", "Peut voir un objet txt"),
|
|
|
|
)
|
|
|
|
|
2017-09-05 16:18:41 +00:00
|
|
|
def __str__(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
return str(self.zone) + " : " + str(self.field1) + " " +\
|
|
|
|
str(self.field2)
|
2017-09-05 16:18:41 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def dns_entry(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Renvoie l'enregistrement TXT complet pour le fichier de zone"""
|
2017-10-20 01:24:14 +00:00
|
|
|
return str(self.field1).ljust(15) + " IN TXT " + str(self.field2)
|
2017-09-05 16:18:41 +00:00
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2018-06-25 14:50:45 +00:00
|
|
|
class DName(RevMixin, AclMixin, models.Model):
|
2018-07-23 21:19:19 +00:00
|
|
|
"""A DNAME entry for the DNS."""
|
2018-06-25 14:50:45 +00:00
|
|
|
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
|
|
|
alias = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2018-07-23 21:19:19 +00:00
|
|
|
("view_dname", "Can see a dname object"),
|
2018-06-25 14:50:45 +00:00
|
|
|
)
|
2018-07-23 21:19:19 +00:00
|
|
|
verbose_name = "DNAME entry"
|
|
|
|
verbose_name_plural = "DNAME entries"
|
2018-06-25 14:50:45 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.zone) + " : " + str(self.alias)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def dns_entry(self):
|
2018-07-23 21:19:19 +00:00
|
|
|
"""Returns the DNAME record for the DNS zone file."""
|
2018-07-29 14:45:12 +00:00
|
|
|
return str(self.alias).ljust(15) + " IN DNAME " + str(self.zone)
|
2018-06-25 14:50:45 +00:00
|
|
|
|
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Srv(RevMixin, AclMixin, models.Model):
|
2018-04-14 18:19:02 +00:00
|
|
|
""" A SRV record """
|
2017-11-16 01:33:57 +00:00
|
|
|
PRETTY_NAME = "Enregistrement Srv"
|
|
|
|
|
|
|
|
TCP = 'TCP'
|
|
|
|
UDP = 'UDP'
|
|
|
|
|
2018-04-13 22:48:44 +00:00
|
|
|
service = models.CharField(max_length=31)
|
2017-11-16 01:33:57 +00:00
|
|
|
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(
|
2017-11-16 01:53:05 +00:00
|
|
|
default=0,
|
2017-11-16 01:33:57 +00:00
|
|
|
validators=[MaxValueValidator(65535)],
|
2018-04-13 22:48:44 +00:00
|
|
|
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)")
|
2017-11-16 01:33:57 +00:00
|
|
|
)
|
|
|
|
weight = models.PositiveIntegerField(
|
2017-11-16 01:53:05 +00:00
|
|
|
default=0,
|
2017-11-16 01:33:57 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2018-04-15 23:52:24 +00:00
|
|
|
("view_srv", "Peut voir un objet srv"),
|
2017-12-31 18:47:22 +00:00
|
|
|
)
|
|
|
|
|
2017-11-16 01:33:57 +00:00
|
|
|
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) + '.'
|
|
|
|
|
|
|
|
|
2018-04-13 22:48:44 +00:00
|
|
|
class Interface(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Une interface. Objet clef de l'application machine :
|
|
|
|
- une address mac unique. Possibilité de la rendre unique avec le
|
|
|
|
typemachine
|
2017-10-05 01:21:36 +00:00
|
|
|
- une onetoone vers IpList pour attribution ipv4
|
|
|
|
- le type parent associé au range ip et à l'extension
|
|
|
|
- un objet domain associé contenant son nom
|
|
|
|
- la liste des ports oiuvert"""
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Interface"
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
ipv4 = models.OneToOneField(
|
|
|
|
'IpList',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
blank=True,
|
|
|
|
null=True
|
|
|
|
)
|
2016-07-04 00:48:24 +00:00
|
|
|
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)
|
2017-10-02 02:19:07 +00:00
|
|
|
port_lists = models.ManyToManyField('OuverturePortList', blank=True)
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_interface", "Peut voir un objet interface"),
|
2018-04-13 22:48:44 +00:00
|
|
|
("change_interface_machine",
|
|
|
|
"Peut changer le propriétaire d'une interface"),
|
2017-12-31 18:47:22 +00:00
|
|
|
)
|
|
|
|
|
2017-05-26 22:43:51 +00:00
|
|
|
@cached_property
|
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
|
2017-07-18 01:49:36 +00:00
|
|
|
return machine.active and user.has_access()
|
2016-07-18 21:22:59 +00:00
|
|
|
|
2017-10-03 00:36:39 +00:00
|
|
|
@cached_property
|
2018-01-29 02:53:49 +00:00
|
|
|
def ipv6_slaac(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Renvoie un objet type ipv6 à partir du prefix associé à
|
|
|
|
l'iptype parent"""
|
2017-10-03 00:36:39 +00:00
|
|
|
if self.type.ip_type.prefix_v6:
|
2017-10-15 20:10:33 +00:00
|
|
|
return EUI(self.mac_address).ipv6(
|
|
|
|
IPNetwork(self.type.ip_type.prefix_v6).network
|
|
|
|
)
|
2017-10-03 00:36:39 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2018-01-30 05:33:56 +00:00
|
|
|
@cached_property
|
|
|
|
def gen_ipv6_dhcpv6(self):
|
|
|
|
"""Cree une ip, à assigner avec dhcpv6 sur une machine"""
|
|
|
|
prefix_v6 = self.type.ip_type.prefix_v6
|
|
|
|
if not prefix_v6:
|
|
|
|
return None
|
2018-04-13 22:48:44 +00:00
|
|
|
return IPv6Address(
|
|
|
|
IPv6Address(prefix_v6).exploded[:20] +
|
|
|
|
IPv6Address(self.id).exploded[20:]
|
|
|
|
)
|
2018-01-30 05:33:56 +00:00
|
|
|
|
|
|
|
def sync_ipv6_dhcpv6(self):
|
|
|
|
"""Affecte une ipv6 dhcpv6 calculée à partir de l'id de la machine"""
|
|
|
|
ipv6_dhcpv6 = self.gen_ipv6_dhcpv6
|
|
|
|
if not ipv6_dhcpv6:
|
|
|
|
return
|
|
|
|
ipv6 = Ipv6List.objects.filter(ipv6=str(ipv6_dhcpv6)).first()
|
|
|
|
if not ipv6:
|
|
|
|
ipv6 = Ipv6List(ipv6=str(ipv6_dhcpv6))
|
|
|
|
ipv6.interface = self
|
|
|
|
ipv6.save()
|
|
|
|
return
|
|
|
|
|
2018-01-29 02:53:49 +00:00
|
|
|
def sync_ipv6_slaac(self):
|
|
|
|
"""Cree, mets à jour et supprime si il y a lieu l'ipv6 slaac associée
|
|
|
|
à la machine
|
|
|
|
Sans prefixe ipv6, on return
|
|
|
|
Si l'ip slaac n'est pas celle qu'elle devrait être, on maj"""
|
|
|
|
ipv6_slaac = self.ipv6_slaac
|
|
|
|
if not ipv6_slaac:
|
|
|
|
return
|
2018-04-13 22:48:44 +00:00
|
|
|
ipv6_object = (Ipv6List.objects
|
|
|
|
.filter(interface=self, slaac_ip=True)
|
|
|
|
.first())
|
2018-01-29 02:53:49 +00:00
|
|
|
if not ipv6_object:
|
|
|
|
ipv6_object = Ipv6List(interface=self, slaac_ip=True)
|
|
|
|
if ipv6_object.ipv6 != str(ipv6_slaac):
|
|
|
|
ipv6_object.ipv6 = str(ipv6_slaac)
|
|
|
|
ipv6_object.save()
|
|
|
|
|
2018-01-30 05:33:56 +00:00
|
|
|
def sync_ipv6(self):
|
|
|
|
"""Cree et met à jour l'ensemble des ipv6 en fonction du mode choisi"""
|
2018-04-13 22:48:44 +00:00
|
|
|
if (preferences.models.OptionalMachine
|
|
|
|
.get_cached_value('ipv6_mode') == 'SLAAC'):
|
2018-01-30 05:33:56 +00:00
|
|
|
self.sync_ipv6_slaac()
|
2018-04-13 22:48:44 +00:00
|
|
|
elif (preferences.models.OptionalMachine
|
|
|
|
.get_cached_value('ipv6_mode') == 'DHCPV6'):
|
2018-01-30 05:33:56 +00:00
|
|
|
self.sync_ipv6_dhcpv6()
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
2017-10-03 00:36:39 +00:00
|
|
|
def ipv6(self):
|
2018-01-29 02:53:49 +00:00
|
|
|
""" Renvoie le queryset de la liste des ipv6
|
2018-04-13 22:48:44 +00:00
|
|
|
On renvoie l'ipv6 slaac que si le mode slaac est activé
|
|
|
|
(et non dhcpv6)"""
|
|
|
|
if (preferences.models.OptionalMachine
|
|
|
|
.get_cached_value('ipv6_mode') == 'SLAAC'):
|
2018-01-30 22:07:43 +00:00
|
|
|
return self.ipv6list.all()
|
2018-04-13 22:48:44 +00:00
|
|
|
elif (preferences.models.OptionalMachine
|
|
|
|
.get_cached_value('ipv6_mode') == 'DHCPV6'):
|
2018-01-30 22:07:43 +00:00
|
|
|
return self.ipv6list.filter(slaac_ip=False)
|
2018-01-30 06:36:22 +00:00
|
|
|
else:
|
|
|
|
return None
|
2017-10-03 00:36:39 +00:00
|
|
|
|
2016-11-20 16:57:10 +00:00
|
|
|
def mac_bare(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Formatage de la mac type mac_bare"""
|
2016-11-20 16:57:10 +00:00
|
|
|
return str(EUI(self.mac_address, dialect=mac_bare)).lower()
|
|
|
|
|
2017-09-08 14:02:11 +00:00
|
|
|
def filter_macaddress(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Tente un formatage mac_bare, si échoue, lève une erreur de
|
|
|
|
validation"""
|
2017-09-19 20:09:12 +00:00
|
|
|
try:
|
|
|
|
self.mac_address = str(EUI(self.mac_address))
|
2017-10-15 20:10:33 +00:00
|
|
|
except:
|
2017-09-08 14:02:11 +00:00
|
|
|
raise ValidationError("La mac donnée est invalide")
|
|
|
|
|
2016-11-12 08:09:28 +00:00
|
|
|
def clean(self, *args, **kwargs):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Formate l'addresse mac en mac_bare (fonction filter_mac)
|
|
|
|
et assigne une ipv4 dans le bon range si inexistante ou incohérente"""
|
2017-10-26 21:36:09 +00:00
|
|
|
# If type was an invalid value, django won't create an attribute type
|
|
|
|
# but try clean() as we may be able to create it from another value
|
|
|
|
# so even if the error as yet been detected at this point, django
|
|
|
|
# continues because the error might not prevent us from creating the
|
|
|
|
# instance.
|
|
|
|
# But in our case, it's impossible to create a type value so we raise
|
|
|
|
# the error.
|
2018-04-13 22:48:44 +00:00
|
|
|
if not hasattr(self, 'type'):
|
2017-10-26 21:36:09 +00:00
|
|
|
raise ValidationError("Le type d'ip choisi n'est pas valide")
|
2017-09-08 14:02:11 +00:00
|
|
|
self.filter_macaddress()
|
2017-08-07 22:01:45 +00:00
|
|
|
self.mac_address = str(EUI(self.mac_address)) or None
|
2017-07-23 02:22:22 +00:00
|
|
|
if not self.ipv4 or self.type.ip_type != self.ipv4.ip_type:
|
2017-07-21 04:49:39 +00:00
|
|
|
self.assign_ipv4()
|
2017-08-07 22:01:45 +00:00
|
|
|
super(Interface, self).clean(*args, **kwargs)
|
2017-07-21 04:49:39 +00:00
|
|
|
|
|
|
|
def assign_ipv4(self):
|
|
|
|
""" Assigne une ip à l'interface """
|
|
|
|
free_ips = self.type.ip_type.free_ip()
|
|
|
|
if free_ips:
|
|
|
|
self.ipv4 = free_ips[0]
|
|
|
|
else:
|
2017-10-15 20:10:33 +00:00
|
|
|
raise ValidationError("Il n'y a plus d'ip disponibles\
|
|
|
|
dans le slash")
|
2017-07-21 04:49:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def unassign_ipv4(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Sans commentaire, désassigne une ipv4"""
|
2017-07-21 04:49:39 +00:00
|
|
|
self.ipv4 = None
|
2016-11-12 08:09:28 +00:00
|
|
|
|
2017-07-23 03:03:56 +00:00
|
|
|
def update_type(self):
|
|
|
|
""" Lorsque le machinetype est changé de type d'ip, on réassigne"""
|
|
|
|
self.clean()
|
|
|
|
self.save()
|
|
|
|
|
2017-07-23 02:22:22 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2017-09-08 14:02:11 +00:00
|
|
|
self.filter_macaddress()
|
2017-07-23 02:22:22 +00:00
|
|
|
# On verifie la cohérence en forçant l'extension par la méthode
|
2017-10-19 05:08:10 +00:00
|
|
|
if self.ipv4:
|
|
|
|
if self.type.ip_type != self.ipv4.ip_type:
|
|
|
|
raise ValidationError("L'ipv4 et le type de la machine ne\
|
|
|
|
correspondent pas")
|
2017-07-23 02:22:22 +00:00
|
|
|
super(Interface, self).save(*args, **kwargs)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_create(user_request, machineid, *_args, **_kwargs):
|
2017-12-26 23:18:39 +00:00
|
|
|
"""Verifie que l'user a les bons droits infra pour créer
|
|
|
|
une interface, ou bien que la machine appartient bien à l'user
|
|
|
|
:param macineid: Id de la machine parente de l'interface
|
|
|
|
:param user_request: instance utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
2017-11-29 00:53:32 +00:00
|
|
|
try:
|
2017-11-30 20:38:16 +00:00
|
|
|
machine = Machine.objects.get(pk=machineid)
|
2017-11-29 00:53:32 +00:00
|
|
|
except Machine.DoesNotExist:
|
|
|
|
return False, u"Machine inexistante"
|
2017-12-31 18:47:22 +00:00
|
|
|
if not user_request.has_perm('machines.add_interface'):
|
2018-04-13 22:48:44 +00:00
|
|
|
if not (preferences.models.OptionalMachine
|
|
|
|
.get_cached_value('create_machine')):
|
2018-02-28 15:36:59 +00:00
|
|
|
return False, u"Vous ne pouvez pas ajouter une machine"
|
2018-04-13 22:48:44 +00:00
|
|
|
max_lambdauser_interfaces = (preferences.models.OptionalMachine
|
|
|
|
.get_cached_value(
|
|
|
|
'max_lambdauser_interfaces'
|
|
|
|
))
|
2017-11-29 00:53:32 +00:00
|
|
|
if machine.user != user_request:
|
|
|
|
return False, u"Vous ne pouvez pas ajouter une interface à une\
|
|
|
|
machine d'un autre user que vous sans droit"
|
2018-04-13 22:48:44 +00:00
|
|
|
if (machine.user.user_interfaces().count() >=
|
|
|
|
max_lambdauser_interfaces):
|
2017-11-29 00:53:32 +00:00
|
|
|
return False, u"Vous avez atteint le maximum d'interfaces\
|
|
|
|
autorisées que vous pouvez créer vous même (%s) "\
|
|
|
|
% max_lambdauser_interfaces
|
|
|
|
return True, None
|
|
|
|
|
2018-01-08 22:57:19 +00:00
|
|
|
@staticmethod
|
2018-04-14 18:19:02 +00:00
|
|
|
def can_change_machine(user_request, *_args, **_kwargs):
|
|
|
|
"""Check if a user can change the machine associated with an
|
|
|
|
Interface object """
|
2018-04-13 22:48:44 +00:00
|
|
|
return (user_request.has_perm('machines.change_interface_machine'),
|
|
|
|
"Droit requis pour changer la machine")
|
2018-01-08 22:57:19 +00:00
|
|
|
|
2017-11-30 20:48:32 +00:00
|
|
|
def can_edit(self, user_request, *args, **kwargs):
|
2017-12-26 23:18:39 +00:00
|
|
|
"""Verifie que l'user a les bons droits infra pour editer
|
|
|
|
cette instance interface, ou qu'elle lui appartient
|
|
|
|
:param self: Instance interface à editer
|
|
|
|
:param user_request: Utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
2018-01-08 00:21:24 +00:00
|
|
|
if self.machine.user != user_request:
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.change_interface') or
|
|
|
|
not self.machine.user.can_edit(
|
|
|
|
user_request,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
)[0]):
|
|
|
|
return False, (u"Vous ne pouvez pas éditer une machine "
|
|
|
|
"d'un autre user que vous sans droit")
|
2017-11-29 23:45:53 +00:00
|
|
|
return True, None
|
|
|
|
|
2017-12-09 01:15:11 +00:00
|
|
|
def can_delete(self, user_request, *args, **kwargs):
|
2018-01-06 17:21:50 +00:00
|
|
|
"""Verifie que l'user a les bons droits delete object pour del
|
2017-12-26 23:18:39 +00:00
|
|
|
cette instance interface, ou qu'elle lui appartient
|
|
|
|
:param self: Instance interface à del
|
|
|
|
:param user_request: Utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
2018-01-08 00:21:24 +00:00
|
|
|
if self.machine.user != user_request:
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.change_interface') or
|
|
|
|
not self.machine.user.can_edit(
|
|
|
|
user_request,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
)[0]):
|
|
|
|
return False, (u"Vous ne pouvez pas éditer une machine "
|
|
|
|
"d'un autre user que vous sans droit")
|
2017-12-03 15:32:43 +00:00
|
|
|
return True, None
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
def can_view(self, user_request, *_args, **_kwargs):
|
2017-12-26 23:18:39 +00:00
|
|
|
"""Vérifie qu'on peut bien voir cette instance particulière avec
|
2018-01-06 17:21:50 +00:00
|
|
|
droit view objet ou qu'elle appartient à l'user
|
2017-12-26 23:18:39 +00:00
|
|
|
:param self: instance interface à voir
|
|
|
|
:param user_request: instance user qui fait l'edition
|
|
|
|
:return: True ou False avec la raison de l'échec le cas échéant"""
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.view_interface') and
|
|
|
|
self.machine.user != user_request):
|
|
|
|
return False, (u"Vous n'avez pas le droit de voir des machines "
|
|
|
|
"autre que les vôtres")
|
2017-12-03 16:43:07 +00:00
|
|
|
return True, None
|
|
|
|
|
2018-01-08 22:57:19 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Interface, self).__init__(*args, **kwargs)
|
|
|
|
self.field_permissions = {
|
2018-04-13 22:48:44 +00:00
|
|
|
'machine': self.can_change_machine,
|
2018-01-08 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-10-01 22:08:07 +00:00
|
|
|
def has_private_ip(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" True si l'ip associée est privée"""
|
2017-10-02 02:54:40 +00:00
|
|
|
if self.ipv4:
|
2017-10-02 02:19:07 +00:00
|
|
|
return IPAddress(str(self.ipv4)).is_private()
|
|
|
|
else:
|
|
|
|
return False
|
2017-10-01 22:08:07 +00:00
|
|
|
|
2017-10-02 02:19:07 +00:00
|
|
|
def may_have_port_open(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" True si l'interface a une ip et une ip publique.
|
2017-10-15 20:10:33 +00:00
|
|
|
Permet de ne pas exporter des ouvertures sur des ip privées
|
|
|
|
(useless)"""
|
2017-10-02 02:54:40 +00:00
|
|
|
return self.ipv4 and not self.has_private_ip()
|
2017-09-30 08:04:18 +00:00
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Ipv6List(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model):
|
2018-04-14 18:19:02 +00:00
|
|
|
""" A list of IPv6 """
|
2018-01-29 02:53:49 +00:00
|
|
|
PRETTY_NAME = 'Enregistrements Ipv6 des machines'
|
|
|
|
|
|
|
|
ipv6 = models.GenericIPAddressField(
|
|
|
|
protocol='IPv6',
|
|
|
|
unique=True
|
|
|
|
)
|
2018-04-13 22:48:44 +00:00
|
|
|
interface = models.ForeignKey(
|
|
|
|
'Interface',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='ipv6list'
|
|
|
|
)
|
2018-01-29 02:53:49 +00:00
|
|
|
slaac_ip = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_ipv6list", "Peut voir un objet ipv6"),
|
2018-04-13 22:48:44 +00:00
|
|
|
("change_ipv6list_slaac_ip",
|
|
|
|
"Peut changer la valeur slaac sur une ipv6"),
|
2018-01-29 02:53:49 +00:00
|
|
|
)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_create(user_request, interfaceid, *_args, **_kwargs):
|
2018-01-29 02:53:49 +00:00
|
|
|
"""Verifie que l'user a les bons droits infra pour créer
|
|
|
|
une ipv6, ou possède l'interface associée
|
|
|
|
:param interfaceid: Id de l'interface associée à cet objet domain
|
|
|
|
:param user_request: instance utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
|
|
|
try:
|
|
|
|
interface = Interface.objects.get(pk=interfaceid)
|
|
|
|
except Interface.DoesNotExist:
|
|
|
|
return False, u"Interface inexistante"
|
|
|
|
if not user_request.has_perm('machines.add_ipv6list'):
|
|
|
|
if interface.machine.user != user_request:
|
|
|
|
return False, u"Vous ne pouvez pas ajouter un alias à une\
|
|
|
|
machine d'un autre user que vous sans droit"
|
|
|
|
return True, None
|
|
|
|
|
|
|
|
@staticmethod
|
2018-04-14 18:19:02 +00:00
|
|
|
def can_change_slaac_ip(user_request, *_args, **_kwargs):
|
|
|
|
""" Check if a user can change the slaac value """
|
2018-04-13 22:48:44 +00:00
|
|
|
return (user_request.has_perm('machines.change_ipv6list_slaac_ip'),
|
|
|
|
"Droit requis pour changer la valeur slaac ip")
|
2018-01-29 02:53:49 +00:00
|
|
|
|
|
|
|
def can_edit(self, user_request, *args, **kwargs):
|
|
|
|
"""Verifie que l'user a les bons droits infra pour editer
|
|
|
|
cette instance interface, ou qu'elle lui appartient
|
|
|
|
:param self: Instance interface à editer
|
|
|
|
:param user_request: Utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
|
|
|
if self.interface.machine.user != user_request:
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.change_ipv6list') or
|
|
|
|
not self.interface.machine.user.can_edit(
|
|
|
|
user_request,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
)[0]):
|
|
|
|
return False, (u"Vous ne pouvez pas éditer une machine "
|
|
|
|
"d'un autre user que vous sans droit")
|
2018-01-29 02:53:49 +00:00
|
|
|
return True, None
|
|
|
|
|
|
|
|
def can_delete(self, user_request, *args, **kwargs):
|
|
|
|
"""Verifie que l'user a les bons droits delete object pour del
|
|
|
|
cette instance interface, ou qu'elle lui appartient
|
|
|
|
:param self: Instance interface à del
|
|
|
|
:param user_request: Utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
|
|
|
if self.interface.machine.user != user_request:
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.change_ipv6list') or
|
|
|
|
not self.interface.machine.user.can_edit(
|
|
|
|
user_request,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
)[0]):
|
|
|
|
return False, (u"Vous ne pouvez pas éditer une machine "
|
|
|
|
"d'un autre user que vous sans droit")
|
2018-01-29 02:53:49 +00:00
|
|
|
return True, None
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
def can_view(self, user_request, *_args, **_kwargs):
|
2018-01-29 02:53:49 +00:00
|
|
|
"""Vérifie qu'on peut bien voir cette instance particulière avec
|
|
|
|
droit view objet ou qu'elle appartient à l'user
|
|
|
|
:param self: instance interface à voir
|
|
|
|
:param user_request: instance user qui fait l'edition
|
|
|
|
:return: True ou False avec la raison de l'échec le cas échéant"""
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.view_ipv6list') and
|
|
|
|
self.interface.machine.user != user_request):
|
|
|
|
return False, (u"Vous n'avez pas le droit de voir des machines "
|
|
|
|
"autre que les vôtres")
|
2018-01-29 02:53:49 +00:00
|
|
|
return True, None
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Ipv6List, self).__init__(*args, **kwargs)
|
|
|
|
self.field_permissions = {
|
2018-04-13 22:48:44 +00:00
|
|
|
'slaac_ip': self.can_change_slaac_ip,
|
2018-01-29 02:53:49 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 03:51:15 +00:00
|
|
|
def check_and_replace_prefix(self, prefix=None):
|
|
|
|
"""Si le prefixe v6 est incorrect, on maj l'ipv6"""
|
2018-01-30 05:33:56 +00:00
|
|
|
prefix_v6 = prefix or self.interface.type.ip_type.prefix_v6
|
|
|
|
if not prefix_v6:
|
|
|
|
return
|
2018-04-13 22:48:44 +00:00
|
|
|
if (IPv6Address(self.ipv6).exploded[:20] !=
|
|
|
|
IPv6Address(prefix_v6).exploded[:20]):
|
|
|
|
self.ipv6 = IPv6Address(
|
|
|
|
IPv6Address(prefix_v6).exploded[:20] +
|
|
|
|
IPv6Address(self.ipv6).exploded[20:]
|
|
|
|
)
|
2018-01-30 03:51:15 +00:00
|
|
|
self.save()
|
|
|
|
|
2018-01-30 02:01:06 +00:00
|
|
|
def clean(self, *args, **kwargs):
|
2018-04-13 22:48:44 +00:00
|
|
|
if self.slaac_ip and (Ipv6List.objects
|
|
|
|
.filter(interface=self.interface, slaac_ip=True)
|
|
|
|
.exclude(id=self.id)):
|
2018-01-30 02:01:06 +00:00
|
|
|
raise ValidationError("Une ip slaac est déjà enregistrée")
|
2018-01-30 05:33:56 +00:00
|
|
|
prefix_v6 = self.interface.type.ip_type.prefix_v6
|
|
|
|
if prefix_v6:
|
2018-04-13 22:48:44 +00:00
|
|
|
if (IPv6Address(self.ipv6).exploded[:20] !=
|
|
|
|
IPv6Address(prefix_v6).exploded[:20]):
|
|
|
|
raise ValidationError(
|
|
|
|
"Le prefixv6 est incorrect et ne correspond pas au type "
|
|
|
|
"associé à la machine"
|
|
|
|
)
|
2018-01-30 02:01:06 +00:00
|
|
|
super(Ipv6List, self).clean(*args, **kwargs)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
"""Force à avoir appellé clean avant"""
|
|
|
|
self.full_clean()
|
|
|
|
super(Ipv6List, self).save(*args, **kwargs)
|
|
|
|
|
2018-01-29 02:53:49 +00:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.ipv6)
|
|
|
|
|
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Domain(RevMixin, AclMixin, models.Model):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Objet domain. Enregistrement A et CNAME en même temps : permet de
|
|
|
|
stocker les alias et les nom de machines, suivant si interface_parent
|
|
|
|
ou cname sont remplis"""
|
2016-12-24 17:07:09 +00:00
|
|
|
PRETTY_NAME = "Domaine dns"
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2017-10-15 20:10:33 +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)
|
2017-10-15 20:10:33 +00:00
|
|
|
cname = models.ForeignKey(
|
|
|
|
'self',
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
related_name='related_domain'
|
|
|
|
)
|
2016-11-18 16:36:30 +00:00
|
|
|
|
|
|
|
class Meta:
|
2017-08-01 04:57:56 +00:00
|
|
|
unique_together = (("name", "extension"),)
|
2017-12-31 18:47:22 +00:00
|
|
|
permissions = (
|
|
|
|
("view_domain", "Peut voir un objet domain"),
|
|
|
|
)
|
2016-11-18 16:36:30 +00:00
|
|
|
|
2017-07-23 02:22:22 +00:00
|
|
|
def get_extension(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Retourne l'extension de l'interface parente si c'est un A
|
|
|
|
Retourne l'extension propre si c'est un cname, renvoie None sinon"""
|
2017-07-23 02:22:22 +00:00
|
|
|
if self.interface_parent:
|
|
|
|
return self.interface_parent.type.ip_type.extension
|
2017-10-15 20:10:33 +00:00
|
|
|
elif hasattr(self, 'extension'):
|
2017-07-23 02:22:22 +00:00
|
|
|
return self.extension
|
2017-08-01 04:57:56 +00:00
|
|
|
else:
|
|
|
|
return None
|
2017-07-23 02:22:22 +00:00
|
|
|
|
2016-12-24 17:07:09 +00:00
|
|
|
def clean(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Validation :
|
2017-10-05 01:21:36 +00:00
|
|
|
- l'objet est bien soit A soit CNAME
|
|
|
|
- le cname est pas pointé sur lui-même
|
2017-10-15 20:10:33 +00:00
|
|
|
- le nom contient bien les caractères autorisés par la norme
|
|
|
|
dns et moins de 63 caractères au total
|
2017-10-05 01:21:36 +00:00
|
|
|
- le couple nom/extension est bien unique"""
|
2017-08-01 04:57:56 +00:00
|
|
|
if self.get_extension():
|
2017-10-15 20:10:33 +00:00
|
|
|
self.extension = self.get_extension()
|
2016-12-24 17:07:09 +00:00
|
|
|
if self.interface_parent and self.cname:
|
|
|
|
raise ValidationError("On ne peut créer à la fois A et CNAME")
|
2017-10-15 20:10:33 +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")
|
2017-10-15 20:10:33 +00:00
|
|
|
HOSTNAME_LABEL_PATTERN = re.compile(
|
2018-04-14 18:19:02 +00:00
|
|
|
r"(?!-)[A-Z\d-]+(?<!-)$",
|
2017-10-15 20:10:33 +00:00
|
|
|
re.IGNORECASE
|
|
|
|
)
|
2017-07-21 03:06:53 +00:00
|
|
|
dns = self.name.lower()
|
|
|
|
if len(dns) > 63:
|
2017-10-15 20:10:33 +00:00
|
|
|
raise ValidationError("Le nom de domaine %s est trop long\
|
|
|
|
(maximum de 63 caractères)." % dns)
|
2017-07-21 03:06:53 +00:00
|
|
|
if not HOSTNAME_LABEL_PATTERN.match(dns):
|
2017-10-15 20:10:33 +00:00
|
|
|
raise ValidationError("Ce nom de domaine %s contient des\
|
|
|
|
carractères interdits." % dns)
|
2017-08-01 04:57:56 +00:00
|
|
|
self.validate_unique()
|
|
|
|
super(Domain, self).clean()
|
|
|
|
|
2017-09-06 09:07:36 +00:00
|
|
|
@cached_property
|
|
|
|
def dns_entry(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Une entrée DNS"""
|
2017-09-06 09:07:36 +00:00
|
|
|
if self.cname:
|
2018-04-13 22:48:44 +00:00
|
|
|
return "{name} IN CNAME {cname}.".format(
|
|
|
|
name=str(self.name).ljust(15),
|
|
|
|
cname=str(self.cname)
|
|
|
|
)
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2017-07-23 02:22:22 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Empèche le save sans extension valide.
|
|
|
|
Force à avoir appellé clean avant"""
|
2017-08-01 04:57:56 +00:00
|
|
|
if not self.get_extension():
|
|
|
|
raise ValidationError("Extension invalide")
|
|
|
|
self.full_clean()
|
2017-07-23 02:22:22 +00:00
|
|
|
super(Domain, self).save(*args, **kwargs)
|
|
|
|
|
2017-12-27 00:59:48 +00:00
|
|
|
@cached_property
|
|
|
|
def get_source_interface(self):
|
|
|
|
"""Renvoie l'interface source :
|
|
|
|
- l'interface reliée si c'est un A
|
|
|
|
- si c'est un cname, suit le cname jusqu'à atteindre le A
|
|
|
|
et renvoie l'interface parente
|
|
|
|
Fonction récursive"""
|
|
|
|
if self.interface_parent:
|
|
|
|
return self.interface_parent
|
|
|
|
else:
|
|
|
|
return self.cname.get_parent_interface()
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_create(user_request, interfaceid, *_args, **_kwargs):
|
2017-12-26 23:18:39 +00:00
|
|
|
"""Verifie que l'user a les bons droits infra pour créer
|
|
|
|
un domain, ou possède l'interface associée
|
|
|
|
:param interfaceid: Id de l'interface associée à cet objet domain
|
|
|
|
:param user_request: instance utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
2017-11-29 00:53:32 +00:00
|
|
|
try:
|
2017-11-30 20:38:16 +00:00
|
|
|
interface = Interface.objects.get(pk=interfaceid)
|
2017-11-29 00:53:32 +00:00
|
|
|
except Interface.DoesNotExist:
|
|
|
|
return False, u"Interface inexistante"
|
2017-12-31 18:47:22 +00:00
|
|
|
if not user_request.has_perm('machines.add_domain'):
|
2018-04-13 22:48:44 +00:00
|
|
|
max_lambdauser_aliases = (preferences.models.OptionalMachine
|
|
|
|
.get_cached_value(
|
|
|
|
'max_lambdauser_aliases'
|
|
|
|
))
|
2017-11-29 00:53:32 +00:00
|
|
|
if interface.machine.user != user_request:
|
2018-04-13 22:48:44 +00:00
|
|
|
return False, (u"Vous ne pouvez pas ajouter un alias à une "
|
|
|
|
"machine d'un autre user que vous sans droit")
|
2017-11-29 00:53:32 +00:00
|
|
|
if Domain.objects.filter(
|
2018-04-14 18:19:02 +00:00
|
|
|
cname__in=Domain.objects.filter(
|
|
|
|
interface_parent__in=(interface.machine.user
|
|
|
|
.user_interfaces())
|
|
|
|
)
|
|
|
|
).count() >= max_lambdauser_aliases:
|
2018-04-13 22:48:44 +00:00
|
|
|
return False, (u"Vous avez atteint le maximum d'alias "
|
|
|
|
"autorisés que vous pouvez créer vous même "
|
|
|
|
"(%s) " % max_lambdauser_aliases)
|
2017-11-29 00:53:32 +00:00
|
|
|
return True, None
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
def can_edit(self, user_request, *_args, **_kwargs):
|
2018-01-06 17:21:50 +00:00
|
|
|
"""Verifie que l'user a les bons droits pour editer
|
2017-12-26 23:18:39 +00:00
|
|
|
cette instance domain
|
|
|
|
:param self: Instance domain à editer
|
|
|
|
:param user_request: Utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.change_domain') and
|
|
|
|
self.get_source_interface.machine.user != user_request):
|
|
|
|
return False, (u"Vous ne pouvez pas editer un alias à une machine "
|
|
|
|
"d'un autre user que vous sans droit")
|
2017-11-29 23:45:53 +00:00
|
|
|
return True, None
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
def can_delete(self, user_request, *_args, **_kwargs):
|
2018-01-06 17:21:50 +00:00
|
|
|
"""Verifie que l'user a les bons droits delete object pour del
|
2017-12-26 23:18:39 +00:00
|
|
|
cette instance domain, ou qu'elle lui appartient
|
|
|
|
:param self: Instance domain à del
|
|
|
|
:param user_request: Utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.delete_domain') and
|
|
|
|
self.get_source_interface.machine.user != user_request):
|
|
|
|
return False, (u"Vous ne pouvez pas supprimer un alias à une "
|
|
|
|
"machine d'un autre user que vous sans droit")
|
2017-12-03 15:32:43 +00:00
|
|
|
return True, None
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
def can_view(self, user_request, *_args, **_kwargs):
|
2017-12-26 23:18:39 +00:00
|
|
|
"""Vérifie qu'on peut bien voir cette instance particulière avec
|
2018-01-06 17:21:50 +00:00
|
|
|
droit view objet ou qu'elle appartient à l'user
|
2017-12-26 23:18:39 +00:00
|
|
|
:param self: instance domain à voir
|
|
|
|
:param user_request: instance user qui fait l'edition
|
|
|
|
:return: True ou False avec la raison de l'échec le cas échéant"""
|
2018-04-13 22:48:44 +00:00
|
|
|
if (not user_request.has_perm('machines.view_domain') and
|
|
|
|
self.get_source_interface.machine.user != user_request):
|
|
|
|
return False, (u"Vous n'avez pas le droit de voir des machines "
|
|
|
|
"autre que les vôtres")
|
2017-12-03 16:43:07 +00:00
|
|
|
return True, None
|
|
|
|
|
2016-11-01 01:14:06 +00:00
|
|
|
def __str__(self):
|
2017-08-06 18:00:29 +00:00
|
|
|
return str(self.name) + str(self.extension)
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class IpList(RevMixin, AclMixin, models.Model):
|
2018-04-14 18:19:02 +00:00
|
|
|
""" A list of IPv4 """
|
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)
|
2017-07-21 03:06:53 +00:00
|
|
|
ip_type = models.ForeignKey('IpType', on_delete=models.CASCADE)
|
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_iplist", "Peut voir un objet iplist"),
|
|
|
|
)
|
|
|
|
|
2017-07-21 03:06:53 +00:00
|
|
|
@cached_property
|
|
|
|
def need_infra(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Permet de savoir si un user basique peut assigner cette ip ou
|
|
|
|
non"""
|
2017-07-21 03:06:53 +00:00
|
|
|
return self.ip_type.need_infra
|
|
|
|
|
|
|
|
def clean(self):
|
2017-10-05 01:21:36 +00:00
|
|
|
""" Erreur si l'ip_type est incorrect"""
|
2017-07-21 03:06:53 +00:00
|
|
|
if not str(self.ipv4) in self.ip_type.ip_set_as_str:
|
2017-10-15 20:10:33 +00:00
|
|
|
raise ValidationError("L'ipv4 et le range de l'iptype ne\
|
|
|
|
correspondent pas!")
|
2017-07-21 03:06:53 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.clean()
|
|
|
|
super(IpList, self).save(*args, **kwargs)
|
2016-07-03 14:49:29 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-07-04 00:48:24 +00:00
|
|
|
return self.ipv4
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Service(RevMixin, AclMixin, models.Model):
|
2017-08-08 04:56:19 +00:00
|
|
|
""" Definition d'un service (dhcp, dns, etc)"""
|
2017-10-27 20:12:55 +00:00
|
|
|
PRETTY_NAME = "Services à générer (dhcp, dns, etc)"
|
|
|
|
|
2017-08-08 04:56:19 +00:00
|
|
|
service_type = models.CharField(max_length=255, blank=True, unique=True)
|
2017-10-15 20:10:33 +00:00
|
|
|
min_time_regen = models.DurationField(
|
|
|
|
default=timedelta(minutes=1),
|
|
|
|
help_text="Temps minimal avant nouvelle génération du service"
|
|
|
|
)
|
|
|
|
regular_time_regen = models.DurationField(
|
|
|
|
default=timedelta(hours=1),
|
|
|
|
help_text="Temps maximal avant nouvelle génération du service"
|
|
|
|
)
|
2017-08-08 04:56:19 +00:00
|
|
|
servers = models.ManyToManyField('Interface', through='Service_link')
|
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_service", "Peut voir un objet service"),
|
|
|
|
)
|
|
|
|
|
2017-08-08 04:56:19 +00:00
|
|
|
def ask_regen(self):
|
2017-08-08 17:40:30 +00:00
|
|
|
""" Marque à True la demande de régénération pour un service x """
|
2017-10-15 20:10:33 +00:00
|
|
|
Service_link.objects.filter(service=self).exclude(asked_regen=True)\
|
|
|
|
.update(asked_regen=True)
|
2017-08-18 22:01:06 +00:00
|
|
|
return
|
2017-08-08 04:56:19 +00:00
|
|
|
|
|
|
|
def process_link(self, servers):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Django ne peut créer lui meme les relations manytomany avec table
|
|
|
|
intermediaire explicite"""
|
|
|
|
for serv in servers.exclude(
|
2018-04-14 18:19:02 +00:00
|
|
|
pk__in=Interface.objects.filter(service=self)
|
|
|
|
):
|
2017-08-08 04:56:19 +00:00
|
|
|
link = Service_link(service=self, server=serv)
|
|
|
|
link.save()
|
2017-10-15 20:10:33 +00:00
|
|
|
Service_link.objects.filter(service=self).exclude(server__in=servers)\
|
|
|
|
.delete()
|
2017-08-08 04:56:19 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
super(Service, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.service_type)
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-08-08 17:40:30 +00:00
|
|
|
def regen(service):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Fonction externe pour régérération d'un service, prend un objet service
|
|
|
|
en arg"""
|
2018-05-14 21:58:05 +00:00
|
|
|
obj = Service.objects.filter(service_type=service)
|
|
|
|
if obj:
|
|
|
|
obj[0].ask_regen()
|
2017-08-08 17:40:30 +00:00
|
|
|
return
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Service_link(RevMixin, AclMixin, models.Model):
|
2017-08-08 04:56:19 +00:00
|
|
|
""" Definition du lien entre serveurs et services"""
|
2017-10-27 20:12:55 +00:00
|
|
|
PRETTY_NAME = "Relation entre service et serveur"
|
|
|
|
|
2017-08-08 04:56:19 +00:00
|
|
|
service = models.ForeignKey('Service', on_delete=models.CASCADE)
|
2018-05-14 21:58:05 +00:00
|
|
|
server = models.ForeignKey('Interface', on_delete=models.CASCADE)
|
2017-08-08 04:56:19 +00:00
|
|
|
last_regen = models.DateTimeField(auto_now_add=True)
|
|
|
|
asked_regen = models.BooleanField(default=False)
|
|
|
|
|
2018-05-25 20:10:31 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_service_link", "Peut voir un objet service_link"),
|
|
|
|
)
|
|
|
|
|
2017-08-08 17:40:30 +00:00
|
|
|
def done_regen(self):
|
|
|
|
""" Appellé lorsqu'un serveur a regénéré son service"""
|
|
|
|
self.last_regen = timezone.now()
|
|
|
|
self.asked_regen = False
|
|
|
|
self.save()
|
2017-08-08 04:56:19 +00:00
|
|
|
|
2018-05-26 21:08:03 +00:00
|
|
|
@property
|
2017-08-08 04:56:19 +00:00
|
|
|
def need_regen(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
""" Décide si le temps minimal écoulé est suffisant pour provoquer une
|
|
|
|
régénération de service"""
|
|
|
|
return bool(
|
|
|
|
(self.asked_regen and (
|
|
|
|
self.last_regen + self.service.min_time_regen
|
|
|
|
) < timezone.now()
|
|
|
|
) or (
|
|
|
|
self.last_regen + self.service.regular_time_regen
|
|
|
|
) < timezone.now()
|
|
|
|
)
|
2017-08-08 04:56:19 +00:00
|
|
|
|
2018-05-26 21:08:03 +00:00
|
|
|
@need_regen.setter
|
|
|
|
def need_regen(self, value):
|
|
|
|
"""
|
|
|
|
Force to set the need_regen value. True means a regen is asked and False
|
|
|
|
means a regen has been done.
|
|
|
|
|
|
|
|
:param value: (bool) The value to set to
|
|
|
|
"""
|
|
|
|
if not value:
|
|
|
|
self.last_regen = timezone.now()
|
|
|
|
self.asked_regen = value
|
|
|
|
self.save()
|
|
|
|
|
2017-08-08 04:56:19 +00:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.server) + " " + str(self.service)
|
|
|
|
|
2017-09-28 15:19:33 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class OuverturePortList(RevMixin, AclMixin, models.Model):
|
2017-09-28 15:19:33 +00:00
|
|
|
"""Liste des ports ouverts sur une interface."""
|
2017-10-27 20:12:55 +00:00
|
|
|
PRETTY_NAME = "Profil d'ouverture de ports"
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
name = models.CharField(
|
|
|
|
help_text="Nom de la configuration des ports.",
|
|
|
|
max_length=255
|
|
|
|
)
|
2017-09-28 15:19:33 +00:00
|
|
|
|
2017-12-31 18:47:22 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("view_ouvertureportlist", "Peut voir un objet ouvertureport"),
|
|
|
|
)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
def can_delete(self, user_request, *_args, **_kwargs):
|
2017-12-26 23:18:39 +00:00
|
|
|
"""Verifie que l'user a les bons droits bureau pour delete
|
|
|
|
cette instance ouvertureportlist
|
|
|
|
:param self: Instance ouvertureportlist à delete
|
|
|
|
:param user_request: Utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
2017-12-31 18:47:22 +00:00
|
|
|
if not user_request.has_perm('machines.delete_ouvertureportlist'):
|
2018-04-13 22:48:44 +00:00
|
|
|
return False, (u"Vous n'avez pas le droit de supprimer une "
|
|
|
|
"ouverture de port")
|
2017-12-09 01:15:11 +00:00
|
|
|
if self.interface_set.all():
|
|
|
|
return False, u"Cette liste de ports est utilisée"
|
|
|
|
return True, None
|
2017-12-03 15:32:43 +00:00
|
|
|
|
2017-09-28 15:19:33 +00:00
|
|
|
def __str__(self):
|
2017-10-01 19:57:10 +00:00
|
|
|
return self.name
|
2017-09-28 15:19:33 +00:00
|
|
|
|
2017-10-01 09:39:39 +00:00
|
|
|
def tcp_ports_in(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Renvoie la liste des ports ouverts en TCP IN pour ce profil"""
|
|
|
|
return self.ouvertureport_set.filter(
|
|
|
|
protocole=OuverturePort.TCP,
|
|
|
|
io=OuverturePort.IN
|
|
|
|
)
|
|
|
|
|
2017-10-01 09:39:39 +00:00
|
|
|
def udp_ports_in(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Renvoie la liste des ports ouverts en UDP IN pour ce profil"""
|
|
|
|
return self.ouvertureport_set.filter(
|
|
|
|
protocole=OuverturePort.UDP,
|
|
|
|
io=OuverturePort.IN
|
|
|
|
)
|
2017-10-01 09:39:39 +00:00
|
|
|
|
|
|
|
def tcp_ports_out(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Renvoie la liste des ports ouverts en TCP OUT pour ce profil"""
|
|
|
|
return self.ouvertureport_set.filter(
|
|
|
|
protocole=OuverturePort.TCP,
|
|
|
|
io=OuverturePort.OUT
|
|
|
|
)
|
|
|
|
|
2017-10-01 09:39:39 +00:00
|
|
|
def udp_ports_out(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Renvoie la liste des ports ouverts en UDP OUT pour ce profil"""
|
|
|
|
return self.ouvertureport_set.filter(
|
|
|
|
protocole=OuverturePort.UDP,
|
|
|
|
io=OuverturePort.OUT
|
|
|
|
)
|
2017-10-01 09:39:39 +00:00
|
|
|
|
2017-09-29 20:28:48 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class OuverturePort(RevMixin, AclMixin, models.Model):
|
2017-09-28 15:19:33 +00:00
|
|
|
"""
|
|
|
|
Représente un simple port ou une plage de ports.
|
2017-10-15 20:10:33 +00:00
|
|
|
|
|
|
|
Les ports de la plage sont compris entre begin et en inclus.
|
2017-09-28 15:19:33 +00:00
|
|
|
Si begin == end alors on ne représente qu'un seul port.
|
2017-10-15 18:37:21 +00:00
|
|
|
|
|
|
|
On limite les ports entre 0 et 65535, tels que défini par la RFC
|
2017-09-28 15:19:33 +00:00
|
|
|
"""
|
2017-10-27 20:12:55 +00:00
|
|
|
PRETTY_NAME = "Plage de port ouverte"
|
|
|
|
|
2017-09-28 15:19:33 +00:00
|
|
|
TCP = 'T'
|
|
|
|
UDP = 'U'
|
2017-10-01 09:39:39 +00:00
|
|
|
IN = 'I'
|
|
|
|
OUT = 'O'
|
2017-10-15 18:37:21 +00:00
|
|
|
begin = models.PositiveIntegerField(validators=[MaxValueValidator(65535)])
|
|
|
|
end = models.PositiveIntegerField(validators=[MaxValueValidator(65535)])
|
2017-10-15 20:10:33 +00:00
|
|
|
port_list = models.ForeignKey(
|
|
|
|
'OuverturePortList',
|
|
|
|
on_delete=models.CASCADE
|
|
|
|
)
|
2017-09-28 15:19:33 +00:00
|
|
|
protocole = models.CharField(
|
2017-10-15 20:10:33 +00:00
|
|
|
max_length=1,
|
|
|
|
choices=(
|
|
|
|
(TCP, 'TCP'),
|
|
|
|
(UDP, 'UDP'),
|
|
|
|
),
|
|
|
|
default=TCP,
|
2017-09-28 15:19:33 +00:00
|
|
|
)
|
2017-10-01 09:39:39 +00:00
|
|
|
io = models.CharField(
|
2017-10-15 20:10:33 +00:00
|
|
|
max_length=1,
|
|
|
|
choices=(
|
|
|
|
(IN, 'IN'),
|
|
|
|
(OUT, 'OUT'),
|
|
|
|
),
|
|
|
|
default=OUT,
|
2017-10-01 09:39:39 +00:00
|
|
|
)
|
2017-09-28 15:19:33 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
if self.begin == self.end:
|
2017-09-29 20:28:48 +00:00
|
|
|
return str(self.begin)
|
|
|
|
return '-'.join([str(self.begin), str(self.end)])
|
2017-09-28 15:19:33 +00:00
|
|
|
|
2017-09-30 08:04:18 +00:00
|
|
|
def show_port(self):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Formatage plus joli, alias pour str"""
|
2017-09-30 08:04:18 +00:00
|
|
|
return str(self)
|
|
|
|
|
2017-09-28 15:19:33 +00:00
|
|
|
|
2017-03-09 23:05:53 +00:00
|
|
|
@receiver(post_save, sender=Machine)
|
2018-04-15 01:00:05 +00:00
|
|
|
def machine_post_save(**kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Synchronisation ldap et régen parefeu/dhcp lors de la modification
|
|
|
|
d'une machine"""
|
2017-03-09 23:05:53 +00:00
|
|
|
user = kwargs['instance'].user
|
|
|
|
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
|
2017-08-08 17:40:30 +00:00
|
|
|
regen('dhcp')
|
2017-08-24 19:36:59 +00:00
|
|
|
regen('mac_ip_list')
|
2017-03-09 23:05:53 +00:00
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-03-09 23:05:53 +00:00
|
|
|
@receiver(post_delete, sender=Machine)
|
2018-04-15 01:00:05 +00:00
|
|
|
def machine_post_delete(**kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Synchronisation ldap et régen parefeu/dhcp lors de la suppression
|
|
|
|
d'une machine"""
|
2017-03-09 23:05:53 +00:00
|
|
|
machine = kwargs['instance']
|
|
|
|
user = machine.user
|
|
|
|
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
|
2017-09-01 01:15:54 +00:00
|
|
|
regen('dhcp')
|
|
|
|
regen('mac_ip_list')
|
2016-07-25 21:54:40 +00:00
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2016-07-25 21:54:40 +00:00
|
|
|
@receiver(post_save, sender=Interface)
|
2018-04-15 01:00:05 +00:00
|
|
|
def interface_post_save(**kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Synchronisation ldap et régen parefeu/dhcp lors de la modification
|
|
|
|
d'une interface"""
|
2017-07-21 04:49:39 +00:00
|
|
|
interface = kwargs['instance']
|
2018-01-30 05:33:56 +00:00
|
|
|
interface.sync_ipv6()
|
2017-07-21 04:49:39 +00:00
|
|
|
user = interface.machine.user
|
2016-11-20 15:53:59 +00:00
|
|
|
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
|
2017-10-02 02:19:07 +00:00
|
|
|
# Regen services
|
2017-08-08 17:40:30 +00:00
|
|
|
regen('dhcp')
|
2017-08-24 19:36:59 +00:00
|
|
|
regen('mac_ip_list')
|
2016-07-25 21:54:40 +00:00
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2016-07-25 21:54:40 +00:00
|
|
|
@receiver(post_delete, sender=Interface)
|
2018-04-15 01:00:05 +00:00
|
|
|
def interface_post_delete(**kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Synchronisation ldap et régen parefeu/dhcp lors de la suppression
|
|
|
|
d'une interface"""
|
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
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-07-21 03:06:53 +00:00
|
|
|
@receiver(post_save, sender=IpType)
|
2018-04-15 01:00:05 +00:00
|
|
|
def iptype_post_save(**kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Generation des objets ip après modification d'un range ip"""
|
2017-07-21 03:06:53 +00:00
|
|
|
iptype = kwargs['instance']
|
2017-08-28 23:04:15 +00:00
|
|
|
iptype.gen_ip_range()
|
2018-01-30 03:51:15 +00:00
|
|
|
iptype.check_replace_prefixv6()
|
2017-07-21 03:06:53 +00:00
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-07-23 03:03:56 +00:00
|
|
|
@receiver(post_save, sender=MachineType)
|
2018-04-15 01:00:05 +00:00
|
|
|
def machinetype_post_save(**kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Mise à jour des interfaces lorsque changement d'attribution
|
|
|
|
d'une machinetype (changement iptype parent)"""
|
2017-07-23 03:03:56 +00:00
|
|
|
machinetype = kwargs['instance']
|
|
|
|
for interface in machinetype.all_interfaces():
|
|
|
|
interface.update_type()
|
2017-09-01 16:47:34 +00:00
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-09-01 16:47:34 +00:00
|
|
|
@receiver(post_save, sender=Domain)
|
2018-04-15 01:00:05 +00:00
|
|
|
def domain_post_save(**_kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Regeneration dns après modification d'un domain object"""
|
2017-09-01 16:47:34 +00:00
|
|
|
regen('dns')
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-09-05 16:23:25 +00:00
|
|
|
@receiver(post_delete, sender=Domain)
|
2018-04-15 01:00:05 +00:00
|
|
|
def domain_post_delete(**_kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Regeneration dns après suppression d'un domain object"""
|
2017-09-05 16:23:25 +00:00
|
|
|
regen('dns')
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-09-08 23:07:28 +00:00
|
|
|
@receiver(post_save, sender=Extension)
|
2018-04-15 01:00:05 +00:00
|
|
|
def extension_post_save(**_kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Regeneration dns après modification d'une extension"""
|
2017-09-08 23:07:28 +00:00
|
|
|
regen('dns')
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-09-08 23:07:28 +00:00
|
|
|
@receiver(post_delete, sender=Extension)
|
2018-04-15 01:00:05 +00:00
|
|
|
def extension_post_selete(**_kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Regeneration dns après suppression d'une extension"""
|
2017-09-08 23:07:28 +00:00
|
|
|
regen('dns')
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-10-19 23:52:38 +00:00
|
|
|
@receiver(post_save, sender=SOA)
|
2018-04-15 01:00:05 +00:00
|
|
|
def soa_post_save(**_kwargs):
|
2017-10-19 23:52:38 +00:00
|
|
|
"""Regeneration dns après modification d'un SOA"""
|
|
|
|
regen('dns')
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=SOA)
|
2018-04-15 01:00:05 +00:00
|
|
|
def soa_post_delete(**_kwargs):
|
2017-10-19 23:52:38 +00:00
|
|
|
"""Regeneration dns après suppresson d'un SOA"""
|
|
|
|
regen('dns')
|
|
|
|
|
|
|
|
|
2017-09-05 16:23:25 +00:00
|
|
|
@receiver(post_save, sender=Mx)
|
2018-04-15 01:00:05 +00:00
|
|
|
def mx_post_save(**_kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Regeneration dns après modification d'un MX"""
|
2017-09-05 16:23:25 +00:00
|
|
|
regen('dns')
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-09-05 16:23:25 +00:00
|
|
|
@receiver(post_delete, sender=Mx)
|
2018-04-15 01:00:05 +00:00
|
|
|
def mx_post_delete(**_kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Regeneration dns après suppresson d'un MX"""
|
2017-09-05 16:23:25 +00:00
|
|
|
regen('dns')
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-09-05 16:23:25 +00:00
|
|
|
@receiver(post_save, sender=Ns)
|
2018-04-15 01:00:05 +00:00
|
|
|
def ns_post_save(**_kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Regeneration dns après modification d'un NS"""
|
2017-09-05 16:23:25 +00:00
|
|
|
regen('dns')
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-09-05 16:23:25 +00:00
|
|
|
@receiver(post_delete, sender=Ns)
|
2018-04-15 01:00:05 +00:00
|
|
|
def ns_post_delete(**_kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Regeneration dns après modification d'un NS"""
|
2017-09-05 16:23:25 +00:00
|
|
|
regen('dns')
|
|
|
|
|
2017-10-15 20:10:33 +00:00
|
|
|
|
2017-11-15 14:17:22 +00:00
|
|
|
@receiver(post_save, sender=Txt)
|
2018-04-15 01:00:05 +00:00
|
|
|
def text_post_save(**_kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Regeneration dns après modification d'un TXT"""
|
2017-09-05 16:23:25 +00:00
|
|
|
regen('dns')
|
|
|
|
|
2018-07-23 21:19:19 +00:00
|
|
|
|
2017-11-15 14:17:22 +00:00
|
|
|
@receiver(post_delete, sender=Txt)
|
2018-04-15 01:00:05 +00:00
|
|
|
def text_post_delete(**_kwargs):
|
2017-10-15 20:10:33 +00:00
|
|
|
"""Regeneration dns après modification d'un TX"""
|
2017-09-01 16:47:34 +00:00
|
|
|
regen('dns')
|
2017-11-16 01:45:49 +00:00
|
|
|
|
2018-07-23 21:19:19 +00:00
|
|
|
|
2018-06-25 14:50:45 +00:00
|
|
|
@receiver(post_save, sender=DName)
|
|
|
|
def dname_post_save(**_kwargs):
|
2018-07-23 21:19:19 +00:00
|
|
|
"""Updates the DNS regen after modification of a DName object."""
|
2018-06-25 14:50:45 +00:00
|
|
|
regen('dns')
|
|
|
|
|
2018-07-23 21:19:19 +00:00
|
|
|
|
2018-06-25 14:50:45 +00:00
|
|
|
@receiver(post_delete, sender=DName)
|
2018-07-23 21:19:19 +00:00
|
|
|
def dname_post_delete(**_kwargs):
|
|
|
|
"""Updates the DNS regen after deletion of a DName object."""
|
2018-06-25 14:50:45 +00:00
|
|
|
regen('dns')
|
|
|
|
|
2017-11-16 01:45:49 +00:00
|
|
|
|
|
|
|
@receiver(post_save, sender=Srv)
|
2018-04-15 01:00:05 +00:00
|
|
|
def srv_post_save(**_kwargs):
|
2017-11-16 01:45:49 +00:00
|
|
|
"""Regeneration dns après modification d'un SRV"""
|
|
|
|
regen('dns')
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Srv)
|
2018-04-15 01:00:05 +00:00
|
|
|
def srv_post_delete(**_kwargs):
|
2017-11-16 01:45:49 +00:00
|
|
|
"""Regeneration dns après modification d'un SRV"""
|
|
|
|
regen('dns')
|