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.
2017-09-10 23:29:24 +00:00
from __future__ import unicode_literals
2017-10-01 14:42:55 +00:00
import re
2016-07-06 20:49:16 +00:00
from django . forms import ModelForm , Form , ValidationError
2016-07-07 11:19:03 +00:00
from django import forms
2017-10-01 14:42:55 +00:00
from . models import Domain , Machine , Interface , IpList , MachineType , Extension , Mx , Text , Ns , Service , Vlan , Nas , IpType , PortList , Port
2016-11-30 03:48:47 +00:00
from django . db . models import Q
2017-01-14 11:52:23 +00:00
from django . core . validators import validate_email
2016-07-06 20:49:16 +00:00
2017-05-27 02:39:53 +00:00
from users . models import User
2016-07-06 20:49:16 +00:00
class EditMachineForm ( ModelForm ) :
class Meta :
model = Machine
fields = ' __all__ '
def __init__ ( self , * args , * * kwargs ) :
super ( EditMachineForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' name ' ] . label = ' Nom de la machine '
class NewMachineForm ( EditMachineForm ) :
class Meta ( EditMachineForm . Meta ) :
2016-07-18 17:14:48 +00:00
fields = [ ' name ' ]
2016-07-06 20:49:16 +00:00
2016-07-09 17:51:37 +00:00
class BaseEditMachineForm ( EditMachineForm ) :
class Meta ( EditMachineForm . Meta ) :
2016-07-18 17:14:48 +00:00
fields = [ ' name ' , ' active ' ]
2016-07-09 17:51:37 +00:00
2016-07-06 20:49:16 +00:00
class EditInterfaceForm ( ModelForm ) :
class Meta :
model = Interface
fields = ' __all__ '
def __init__ ( self , * args , * * kwargs ) :
super ( EditInterfaceForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' mac_address ' ] . label = ' Adresse mac '
2016-07-18 17:14:48 +00:00
self . fields [ ' type ' ] . label = ' Type de machine '
self . fields [ ' type ' ] . empty_label = " Séléctionner un type de machine "
2017-05-28 17:22:45 +00:00
if " machine " in self . fields :
self . fields [ ' machine ' ] . queryset = Machine . objects . all ( ) . select_related ( ' user ' )
2016-07-06 20:49:16 +00:00
class AddInterfaceForm ( EditInterfaceForm ) :
class Meta ( EditInterfaceForm . Meta ) :
2016-12-24 19:04:53 +00:00
fields = [ ' ipv4 ' , ' mac_address ' , ' type ' , ' details ' ]
2016-07-06 20:49:16 +00:00
2016-07-07 17:15:33 +00:00
def __init__ ( self , * args , * * kwargs ) :
2016-10-13 00:11:23 +00:00
infra = kwargs . pop ( ' infra ' )
2016-07-07 17:15:33 +00:00
super ( AddInterfaceForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' ipv4 ' ] . empty_label = " Assignation automatique de l ' ipv4 "
2016-10-13 00:11:23 +00:00
if not infra :
2016-12-14 02:08:57 +00:00
self . fields [ ' type ' ] . queryset = MachineType . objects . filter ( ip_type__in = IpType . objects . filter ( need_infra = False ) )
2017-07-21 03:06:53 +00:00
self . fields [ ' ipv4 ' ] . queryset = IpList . objects . filter ( interface__isnull = True ) . filter ( ip_type__in = IpType . objects . filter ( need_infra = False ) )
2016-11-30 03:48:47 +00:00
else :
self . fields [ ' ipv4 ' ] . queryset = IpList . objects . filter ( interface__isnull = True )
2016-07-07 17:15:33 +00:00
2016-07-06 20:49:16 +00:00
class NewInterfaceForm ( EditInterfaceForm ) :
class Meta ( EditInterfaceForm . Meta ) :
2016-12-24 19:04:53 +00:00
fields = [ ' mac_address ' , ' type ' , ' details ' ]
2016-07-06 20:49:16 +00:00
2016-07-09 17:51:37 +00:00
class BaseEditInterfaceForm ( EditInterfaceForm ) :
class Meta ( EditInterfaceForm . Meta ) :
2016-12-24 19:04:53 +00:00
fields = [ ' ipv4 ' , ' mac_address ' , ' type ' , ' details ' ]
2016-07-09 17:51:37 +00:00
2016-10-24 17:52:29 +00:00
def __init__ ( self , * args , * * kwargs ) :
2016-10-13 00:11:23 +00:00
infra = kwargs . pop ( ' infra ' )
2016-07-09 17:51:37 +00:00
super ( BaseEditInterfaceForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' ipv4 ' ] . empty_label = " Assignation automatique de l ' ipv4 "
2016-10-13 00:11:23 +00:00
if not infra :
2016-12-14 02:08:57 +00:00
self . fields [ ' type ' ] . queryset = MachineType . objects . filter ( ip_type__in = IpType . objects . filter ( need_infra = False ) )
2017-07-21 03:06:53 +00:00
self . fields [ ' ipv4 ' ] . queryset = IpList . objects . filter ( interface__isnull = True ) . filter ( ip_type__in = IpType . objects . filter ( need_infra = False ) )
2016-11-30 03:48:47 +00:00
else :
self . fields [ ' ipv4 ' ] . queryset = IpList . objects . filter ( interface__isnull = True )
2016-07-09 17:51:37 +00:00
2016-11-19 20:15:43 +00:00
class AliasForm ( ModelForm ) :
2016-11-01 01:14:06 +00:00
class Meta :
2016-12-24 19:04:53 +00:00
model = Domain
fields = [ ' name ' , ' extension ' ]
2016-11-01 01:14:06 +00:00
2016-11-19 22:19:44 +00:00
def __init__ ( self , * args , * * kwargs ) :
2017-01-13 21:25:03 +00:00
if ' infra ' in kwargs :
infra = kwargs . pop ( ' infra ' )
2017-07-23 02:22:22 +00:00
super ( AliasForm , self ) . __init__ ( * args , * * kwargs )
class DomainForm ( AliasForm ) :
class Meta ( AliasForm . Meta ) :
fields = [ ' name ' ]
def __init__ ( self , * args , * * kwargs ) :
2017-01-09 15:20:13 +00:00
if ' name_user ' in kwargs :
name_user = kwargs . pop ( ' name_user ' )
nb_machine = kwargs . pop ( ' nb_machine ' )
initial = kwargs . get ( ' initial ' , { } )
initial [ ' name ' ] = name_user . lower ( ) + str ( nb_machine )
kwargs [ ' initial ' ] = initial
2017-07-23 02:22:22 +00:00
super ( DomainForm , self ) . __init__ ( * args , * * kwargs )
2016-11-19 22:19:44 +00:00
2017-08-18 22:01:06 +00:00
class DelAliasForm ( Form ) :
2016-12-24 19:04:53 +00:00
alias = forms . ModelMultipleChoiceField ( queryset = Domain . objects . all ( ) , label = " Alias actuels " , widget = forms . CheckboxSelectMultiple )
2016-11-19 20:15:43 +00:00
def __init__ ( self , * args , * * kwargs ) :
interface = kwargs . pop ( ' interface ' )
super ( DelAliasForm , self ) . __init__ ( * args , * * kwargs )
2016-12-24 19:04:53 +00:00
self . fields [ ' alias ' ] . queryset = Domain . objects . filter ( cname__in = Domain . objects . filter ( interface_parent = interface ) )
2016-11-19 16:44:43 +00:00
2016-07-07 11:19:03 +00:00
class MachineTypeForm ( ModelForm ) :
class Meta :
model = MachineType
2016-10-22 22:55:58 +00:00
fields = [ ' type ' , ' ip_type ' ]
2016-07-07 11:19:03 +00:00
def __init__ ( self , * args , * * kwargs ) :
super ( MachineTypeForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' type ' ] . label = ' Type de machine à ajouter '
2016-10-22 22:55:58 +00:00
self . fields [ ' ip_type ' ] . label = " Type d ' ip relié "
2016-07-07 11:19:03 +00:00
2017-08-08 04:56:19 +00:00
class DelMachineTypeForm ( Form ) :
2016-07-07 11:19:03 +00:00
machinetypes = forms . ModelMultipleChoiceField ( queryset = MachineType . objects . all ( ) , label = " Types de machines actuelles " , widget = forms . CheckboxSelectMultiple )
2016-10-22 22:55:58 +00:00
class IpTypeForm ( ModelForm ) :
class Meta :
model = IpType
2017-08-28 22:35:03 +00:00
fields = [ ' type ' , ' extension ' , ' need_infra ' , ' domaine_ip_start ' , ' domaine_ip_stop ' , ' vlan ' ]
2016-10-22 22:55:58 +00:00
def __init__ ( self , * args , * * kwargs ) :
super ( IpTypeForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' type ' ] . label = ' Type ip à ajouter '
2017-07-21 03:06:53 +00:00
class EditIpTypeForm ( IpTypeForm ) :
class Meta ( IpTypeForm . Meta ) :
2017-08-26 13:10:18 +00:00
fields = [ ' extension ' , ' type ' , ' need_infra ' , ' vlan ' ]
2016-10-22 22:55:58 +00:00
2017-08-08 04:56:19 +00:00
class DelIpTypeForm ( Form ) :
2017-07-21 03:06:53 +00:00
iptypes = forms . ModelMultipleChoiceField ( queryset = IpType . objects . all ( ) , label = " Types d ' ip actuelles " , widget = forms . CheckboxSelectMultiple )
2016-10-22 22:55:58 +00:00
2016-07-08 15:54:06 +00:00
class ExtensionForm ( ModelForm ) :
class Meta :
model = Extension
2016-11-19 22:19:44 +00:00
fields = [ ' name ' , ' need_infra ' , ' origin ' ]
2016-07-08 15:54:06 +00:00
def __init__ ( self , * args , * * kwargs ) :
super ( ExtensionForm , self ) . __init__ ( * args , * * kwargs )
2016-10-26 12:05:40 +00:00
self . fields [ ' name ' ] . label = ' Extension à ajouter '
2016-11-19 17:52:59 +00:00
self . fields [ ' origin ' ] . label = ' Enregistrement A origin '
2016-07-08 15:54:06 +00:00
2017-08-18 22:01:06 +00:00
class DelExtensionForm ( Form ) :
2016-07-08 15:54:06 +00:00
extensions = forms . ModelMultipleChoiceField ( queryset = Extension . objects . all ( ) , label = " Extensions actuelles " , widget = forms . CheckboxSelectMultiple )
2016-11-19 16:44:43 +00:00
class MxForm ( ModelForm ) :
class Meta :
model = Mx
fields = [ ' zone ' , ' priority ' , ' name ' ]
2017-01-05 22:48:45 +00:00
def __init__ ( self , * args , * * kwargs ) :
super ( MxForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' name ' ] . queryset = Domain . objects . exclude ( interface_parent = None )
2017-08-08 04:56:19 +00:00
class DelMxForm ( Form ) :
2016-11-19 16:44:43 +00:00
mx = forms . ModelMultipleChoiceField ( queryset = Mx . objects . all ( ) , label = " MX actuels " , widget = forms . CheckboxSelectMultiple )
2016-11-19 17:21:05 +00:00
class NsForm ( ModelForm ) :
class Meta :
model = Ns
2016-12-26 16:43:41 +00:00
fields = [ ' zone ' , ' ns ' ]
2016-11-19 17:21:05 +00:00
2016-12-26 18:45:43 +00:00
def __init__ ( self , * args , * * kwargs ) :
super ( NsForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' ns ' ] . queryset = Domain . objects . exclude ( interface_parent = None )
2017-08-08 04:56:19 +00:00
class DelNsForm ( Form ) :
2016-11-19 17:21:05 +00:00
ns = forms . ModelMultipleChoiceField ( queryset = Ns . objects . all ( ) , label = " Enregistrements NS actuels " , widget = forms . CheckboxSelectMultiple )
2017-09-05 16:18:41 +00:00
class TextForm ( ModelForm ) :
class Meta :
model = Text
fields = ' __all__ '
class DelTextForm ( Form ) :
text = forms . ModelMultipleChoiceField ( queryset = Text . objects . all ( ) , label = " Enregistrements Text actuels " , widget = forms . CheckboxSelectMultiple )
2017-09-10 22:33:45 +00:00
class NasForm ( ModelForm ) :
class Meta :
model = Nas
fields = ' __all__ '
class DelNasForm ( Form ) :
nas = forms . ModelMultipleChoiceField ( queryset = Nas . objects . all ( ) , label = " Enregistrements Nas actuels " , widget = forms . CheckboxSelectMultiple )
2017-08-08 04:56:19 +00:00
class ServiceForm ( ModelForm ) :
2016-11-19 17:21:05 +00:00
class Meta :
2017-08-08 04:56:19 +00:00
model = Service
fields = ' __all__ '
def save ( self , commit = True ) :
instance = super ( ServiceForm , self ) . save ( commit = False )
if commit :
instance . save ( )
instance . process_link ( self . cleaned_data . get ( ' servers ' ) )
return instance
class DelServiceForm ( Form ) :
service = forms . ModelMultipleChoiceField ( queryset = Service . objects . all ( ) , label = " Services actuels " , widget = forms . CheckboxSelectMultiple )
2017-08-26 13:10:18 +00:00
class VlanForm ( ModelForm ) :
class Meta :
model = Vlan
fields = ' __all__ '
class DelVlanForm ( Form ) :
vlan = forms . ModelMultipleChoiceField ( queryset = Vlan . objects . all ( ) , label = " Vlan actuels " , widget = forms . CheckboxSelectMultiple )
2017-09-30 08:04:18 +00:00
class EditPortListForm ( ModelForm ) :
2017-10-01 09:39:39 +00:00
tcp_ports_in = forms . CharField ( required = False , label = " Ports TCP (entrée) " )
udp_ports_in = forms . CharField ( required = False , label = " Ports UDP (entrée) " )
tcp_ports_out = forms . CharField ( required = False , label = " Ports TCP (sortie) " )
udp_ports_out = forms . CharField ( required = False , label = " Ports UDP (sortie) " )
2017-09-30 08:04:18 +00:00
class Meta :
model = PortList
fields = [ ' name ' ]
2017-08-26 13:10:18 +00:00
2017-10-01 14:42:55 +00:00
def __init__ ( self , * args , * * kwargs ) :
super ( EditPortListForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' name ' ] . label = " Nom de la liste "
if ' instance ' in kwargs . keys ( ) :
p = kwargs [ ' instance ' ]
self . fields [ ' tcp_ports_in ' ] . initial = ' , ' . join ( map ( str , p . tcp_ports_in ( ) ) )
self . fields [ ' tcp_ports_out ' ] . initial = ' , ' . join ( map ( str , p . tcp_ports_out ( ) ) )
self . fields [ ' udp_ports_in ' ] . initial = ' , ' . join ( map ( str , p . udp_ports_in ( ) ) )
self . fields [ ' udp_ports_out ' ] . initial = ' , ' . join ( map ( str , p . udp_ports_out ( ) ) )
def save ( self , commit = False ) :
"""
Sauvegarde l ' instance. Le commit est obligatoire à cause des ForeignKey.
"""
instance = super ( EditPortListForm , self ) . save ( commit = False )
# Suppression des anciens ports.
for port in instance . port_set . all ( ) :
port . delete ( )
split = r ' , \ s+ '
ip_range = r ' \ d+- \ d+ '
def add_port ( string , protocole , mode ) :
for p in re . split ( split , string ) :
if not p :
continue
if re . match ( ip_range , p ) :
a , b = p . split ( ' - ' )
a , b = int ( a ) , int ( b )
begin , end = min ( a , b ) , max ( a , b )
else :
begin = end = int ( p . strip ( ) )
port = Port ( )
port . begin = begin
port . end = end
port . port_list = instance
port . protocole = protocole
port . io = mode
port . save ( )
# Ajout des ports TCP en entrée
add_port ( self . cleaned_data [ ' tcp_ports_in ' ] , Port . TCP , Port . IN )
# Ajout des ports TCP en sortie
add_port ( self . cleaned_data [ ' tcp_ports_out ' ] , Port . TCP , Port . OUT )
# Ajout des ports UDP en entrée
add_port ( self . cleaned_data [ ' tcp_ports_in ' ] , Port . UDP , Port . IN )
# Ajout des ports UDP en sortie
add_port ( self . cleaned_data [ ' tcp_ports_in ' ] , Port . UDP , Port . OUT )
if commit :
instance . save ( )
return instance
2017-08-26 13:10:18 +00:00