Systme de mise jour de la correspondance MAC-IP ou modifs des ports ouverts.
Intgration du script de dmarrage. Gestion des erreurs centralise. darcs-hash:20041211235003-41617-de56319b24d0ad5c6c0068e6d18817e374554888.gz
This commit is contained in:
parent
291139c9a7
commit
ab52211b22
1 changed files with 193 additions and 135 deletions
|
@ -16,37 +16,15 @@
|
|||
# REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
|
||||
# MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
|
||||
# PURPOSE.
|
||||
""" classe Firewall de Komaz """
|
||||
""" Firewall de Komaz """
|
||||
import syslog
|
||||
import iptools,config
|
||||
import ldap_crans
|
||||
from lock import *
|
||||
from ldap_crans import crans_ldap, ann_scol, machine
|
||||
from affich_tools import *
|
||||
from commands import getstatusoutput
|
||||
from iptools import AddrInNet
|
||||
syslog.openlog('firewall')
|
||||
|
||||
class ErrorArgument(Exception):
|
||||
"""
|
||||
User defined exception
|
||||
Erreur sur les arguments d'appel du firewall
|
||||
"""
|
||||
pass
|
||||
|
||||
class ErrorIp(Exception):
|
||||
"""
|
||||
User defined exception
|
||||
Erreur dans les fonctions Ips
|
||||
"""
|
||||
def __init__(self, ip):
|
||||
self.ip=ip
|
||||
|
||||
class ErrorMoreThanOneIp(ErrorIp):
|
||||
"""
|
||||
User defined exception
|
||||
Au moins deux utilisateurs ont la même ip
|
||||
"""
|
||||
pass
|
||||
|
||||
class IptablesError(Exception):
|
||||
""" Gestion des erreurs d'iptables """
|
||||
def __init__(self,cmd,err_code,output):
|
||||
|
@ -58,20 +36,14 @@ class IptablesError(Exception):
|
|||
return "%s\n status : %s\n %s" % (self.cmd,self.err_code,self.output)
|
||||
|
||||
def iptables(cmd):
|
||||
""" Interface à iptables """
|
||||
""" Interface à iptables """
|
||||
syslog.syslog(syslog.LOG_INFO,cmd)
|
||||
status,output=getstatusoutput("/sbin/iptables "+cmd)
|
||||
if status:
|
||||
raise IptablesError(cmd,status,output)
|
||||
|
||||
class ErrorNoSuchIp(ErrorIp):
|
||||
"""
|
||||
User defined exception
|
||||
Personne n'a cette ip
|
||||
"""
|
||||
pass
|
||||
return output
|
||||
|
||||
class firewall:
|
||||
class firewall_komaz :
|
||||
"""
|
||||
Structure du firewall :
|
||||
table nat :
|
||||
|
@ -145,14 +117,44 @@ class firewall:
|
|||
machines = []
|
||||
debug = 1
|
||||
|
||||
def __exception_catcher(self,tache) :
|
||||
""" Excétute la tache founie en gérant les diverses exceptions
|
||||
pouvant survenir
|
||||
Retoune 1 en cas d'erreur et 0 sinon """
|
||||
try :
|
||||
tache()
|
||||
return 0
|
||||
except IptablesError, c :
|
||||
self.anim.reinit()
|
||||
print ERREUR
|
||||
if self.debug : print c
|
||||
except :
|
||||
self.anim.reinit()
|
||||
print ERREUR
|
||||
import traceback
|
||||
if self.debug : traceback.print_exc()
|
||||
return 1
|
||||
|
||||
def __machines(self) :
|
||||
""" Liste des machines du crans """
|
||||
if not self.machines :
|
||||
self.anim = anim(" Interrogation de la base LDAP")
|
||||
self.machines = ldap_crans.crans_ldap().search('ip=*')['machine']
|
||||
self.machines = crans_ldap().search('ip=*')['machine']
|
||||
print OK
|
||||
return self.machines
|
||||
|
||||
def __init__(self) :
|
||||
""" Pose un lock """
|
||||
make_lock('firewall')
|
||||
|
||||
def __del__(self) :
|
||||
""" Destruction du lock """
|
||||
remove_lock('firewall')
|
||||
|
||||
def restart(self):
|
||||
""" Idem start """
|
||||
self.start()
|
||||
|
||||
def start(self) :
|
||||
""" Construction du firewall """
|
||||
cprint('Démarrage firewall','gras')
|
||||
|
@ -160,12 +162,12 @@ class firewall:
|
|||
if not self.__machines() or self.stop() :
|
||||
print "Abandon"
|
||||
return
|
||||
|
||||
self.anim = anim(' Construction structure de la table nat')
|
||||
for chaine in [ 'LOG_VIRUS', 'LOG_FLOOD', 'TEST_VIRUS_FLOOD' , 'TEST_MAC-IP' , 'RESEAUX_NON_ROUTABLES_SRC', 'RESEAUX_NON_ROUTABLES_DST' ] :
|
||||
iptables('-t nat -N %s' % chaine)
|
||||
|
||||
try :
|
||||
|
||||
def procedure() :
|
||||
self.anim = anim(' Structure de la table nat')
|
||||
for chaine in [ 'LOG_VIRUS', 'LOG_FLOOD', 'TEST_VIRUS_FLOOD' , 'TEST_MAC-IP' , 'RESEAUX_NON_ROUTABLES_SRC', 'RESEAUX_NON_ROUTABLES_DST' ] :
|
||||
iptables('-t nat -N %s' % chaine)
|
||||
|
||||
iptables("-t nat -P PREROUTING ACCEPT")
|
||||
iptables("-t nat -A PREROUTING -i lo -j ACCEPT")
|
||||
iptables("-t nat -A PREROUTING -s ! %s -j TEST_VIRUS_FLOOD" % self.zone_serveur)
|
||||
|
@ -178,7 +180,7 @@ class firewall:
|
|||
iptables("-t nat -P PREROUTING DROP")
|
||||
print OK
|
||||
|
||||
self.anim = anim(' Construction structure de la table filter')
|
||||
self.anim = anim(' Structure de la table filter')
|
||||
for chaine in [ 'EXT_VERS_SERVEURS', 'SERVEURS_VERS_EXT' , 'EXT_VERS_CRANS', 'CRANS_VERS_EXT', 'BLACKLIST_SRC', 'BLACKLIST_DST' ] :
|
||||
iptables('-N %s' % chaine)
|
||||
|
||||
|
@ -193,34 +195,17 @@ class firewall:
|
|||
iptables("-A FORWARD -o %s -j CRANS_VERS_EXT" % self.eth_ext )
|
||||
print OK
|
||||
|
||||
except IptablesError, c :
|
||||
print ERREUR
|
||||
if self.debug : print c
|
||||
self.stop()
|
||||
return 1
|
||||
except :
|
||||
print ERREUR
|
||||
self.stop()
|
||||
if self.debug :
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return 1
|
||||
|
||||
# Initialisation
|
||||
self.__exception_catcher(procedure)
|
||||
|
||||
# Remplisage
|
||||
for tache in [ self.log_chaines, self.test_virus_flood, self.reseaux_non_routables,
|
||||
self.blacklist , self.serveurs_vers_ext, self.ext_vers_serveurs,
|
||||
self.crans_vers_ext, self.ext_vers_crans, self.test_mac_ip ] :
|
||||
try :
|
||||
tache()
|
||||
except IptablesError, c :
|
||||
self.anim.reinit()
|
||||
print ERREUR
|
||||
if self.debug : print c
|
||||
except :
|
||||
self.anim.reinit()
|
||||
print ERREUR
|
||||
import traceback
|
||||
if self.debug : traceback.print_exc()
|
||||
self.__exception_catcher(tache)
|
||||
|
||||
cprint(" -> fin de la procédure de démarrage",'vert')
|
||||
|
||||
def log_chaines(self) :
|
||||
""" Construction des chaines de log (LOG_VIRUS et LOG_FLOOD) """
|
||||
self.anim = anim(' Création des chaines de log')
|
||||
|
@ -254,63 +239,47 @@ class firewall:
|
|||
iptables('-t nat -A TEST_VIRUS_FLOOD %s -j RETURN' % self.filtre_flood) # Les limites en négatif ca ne marche pas.
|
||||
self.anim.cycle()
|
||||
iptables('-t nat -A TEST_VIRUS_FLOOD -j LOG_FLOOD')
|
||||
self.anim.reinit()
|
||||
print OK
|
||||
|
||||
def stop(self):
|
||||
""" Arrête le firewall """
|
||||
self.anim = anim(" Arrêt du firewall")
|
||||
try :
|
||||
def procedure() :
|
||||
iptables("-t nat -P PREROUTING ACCEPT")
|
||||
iptables("-F")
|
||||
iptables("-t nat -F")
|
||||
iptables("-X")
|
||||
iptables("-t nat -X")
|
||||
print OK
|
||||
return 0
|
||||
except IptablesError, c :
|
||||
print ERREUR
|
||||
if self.debug : print c
|
||||
except :
|
||||
print ERREUR
|
||||
if self.debug :
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return 1
|
||||
|
||||
return self.__exception_catcher(procedure)
|
||||
|
||||
|
||||
def test_mac_ip(self) :
|
||||
""" Reconstruit la correspondance MAC-IP des machines des adhérents """
|
||||
self.anim = anim(' Construction de la chaîne TEST_MAC-IP',len(self.__machines())+1)
|
||||
self.anim = anim(' Chaîne TEST_MAC-IP',len(self.__machines())+1)
|
||||
iptables("-t nat -P PREROUTING ACCEPT")
|
||||
iptables("-t nat -F TEST_MAC-IP")
|
||||
self.anim.cycle()
|
||||
try:
|
||||
|
||||
def procedure() :
|
||||
for machine in self.__machines() :
|
||||
self.__test_mac_ip(machine)
|
||||
self.anim.cycle()
|
||||
iptables("-t nat -P PREROUTING DROP")
|
||||
self.anim.reinit()
|
||||
print OK
|
||||
except IptablesError, c :
|
||||
self.anim.reinit()
|
||||
print ERREUR
|
||||
if self.debug : print c
|
||||
except :
|
||||
self.anim.reinit()
|
||||
print ERREUR
|
||||
if self.debug :
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
self.__exception_catcher(procedure)
|
||||
|
||||
def serveurs_vers_ext(self) :
|
||||
""" Reconstruit la chaine SERVEURS_VERS_EXT """
|
||||
if self.__build_chaine('SERVEURS_VERS_EXT', self.__serveurs_vers_ext) :
|
||||
if not self.__build_chaine('SERVEURS_VERS_EXT', self.__serveurs_vers_ext) :
|
||||
self.anim.reinit()
|
||||
print OK
|
||||
|
||||
def ext_vers_serveurs(self) :
|
||||
""" Reconstruit la chaine EXT_VERS_SERVEURS """
|
||||
if self.__build_chaine('EXT_VERS_SERVEURS', self.__ext_vers_serveurs) :
|
||||
if not self.__build_chaine('EXT_VERS_SERVEURS', self.__ext_vers_serveurs) :
|
||||
self.anim.reinit()
|
||||
print OK
|
||||
|
||||
|
@ -323,8 +292,9 @@ class firewall:
|
|||
self.__build_chaine_adherent('EXT_VERS_CRANS',self.__ext_vers_crans)
|
||||
|
||||
def __build_chaine_adherent(self,chaine,methode):
|
||||
if self.__build_chaine(chaine, methode) :
|
||||
# Défauts
|
||||
# On construit d'abord les autorisations particulières
|
||||
if not self.__build_chaine(chaine, methode) :
|
||||
# Puis si pas de problèmes les autorisation par défaut
|
||||
self.anim.reinit()
|
||||
for proto in [ 'tcp' , 'udp' ] :
|
||||
for port in self.ports_default["%s_%s" % ( proto, chaine) ] :
|
||||
|
@ -334,34 +304,17 @@ class firewall:
|
|||
print OK
|
||||
|
||||
def __build_chaine(self,chaine, methode) :
|
||||
self.anim = anim(' Construction de la chaîne %s' % chaine,len(self.__machines())+1)
|
||||
self.anim = anim(' Chaîne %s' % chaine,len(self.__machines())+1)
|
||||
iptables("-F %s" % chaine)
|
||||
self.anim.cycle()
|
||||
try:
|
||||
def procedure() :
|
||||
for machine in self.__machines() :
|
||||
methode(machine)
|
||||
self.anim.cycle()
|
||||
iptables("-A %s -j REJECT" % chaine)
|
||||
self.anim.reinit()
|
||||
return 1
|
||||
except IptablesError, c :
|
||||
self.anim.reinit()
|
||||
print ERREUR
|
||||
if self.debug : print c
|
||||
except :
|
||||
self.anim.reinit()
|
||||
print ERREUR
|
||||
if self.debug :
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
return self.__exception_catcher(procedure)
|
||||
|
||||
def add_machine(self,machine):
|
||||
self.__serveurs_vers_ext(machine)
|
||||
self.__ext_vers_serveurs(machine)
|
||||
self.__crans_vers_ext(machine)
|
||||
self.__ext_vers_crans(machine)
|
||||
self.__test_mac_ip(machine)
|
||||
|
||||
def __serveurs_vers_ext(self,machine):
|
||||
ip=machine.ip()
|
||||
if not AddrInNet(ip,self.zone_serveur):
|
||||
|
@ -433,17 +386,17 @@ class firewall:
|
|||
|
||||
def blacklist(self):
|
||||
""" Construit les chaines de blackliste (BLACKLIST_{DST,SRC}) """
|
||||
self.anim = anim(" Contruction blackliste")
|
||||
self.anim = anim(" Blackliste")
|
||||
iptables('-F BLACKLIST_DST')
|
||||
iptables('-F BLACKLIST_SRC')
|
||||
|
||||
blacklist=[]
|
||||
search=ldap_crans.crans_ldap().search('blacklist=*&paiement=%s'%ldap_crans.ann_scol)
|
||||
search = crans_ldap().search('blacklist=*&paiement=%s'% ann_scol)
|
||||
for entite in search['adherent']+search['club']+search['machine']:
|
||||
self.anim.cycle()
|
||||
sanctions = entite.blacklist_actif()
|
||||
if 'upload' in sanctions or 'warez' in sanctions :
|
||||
if search.__class__ == ldap_crans.machine:
|
||||
if search.__class__ == machine:
|
||||
blacklist+=[entite]
|
||||
else:
|
||||
blacklist+=entite.machines()
|
||||
|
@ -454,22 +407,127 @@ class firewall:
|
|||
|
||||
self.anim.reinit()
|
||||
print OK
|
||||
|
||||
def port_maj(self,ip_list) :
|
||||
""" Mise à jour des ports pour les ip fournies """
|
||||
# Note : système bourrin (on efface les chaines et on refait)
|
||||
# mais rapide et efficace (si qqn veut se casser le cul à
|
||||
# un système aussi délicat que pour la correspondance MAC-IP...)
|
||||
# -- Fred
|
||||
serveur_maj = False
|
||||
adh_maj = False
|
||||
for ip in ip_list :
|
||||
if AddrInNet(ip,self.zone_serveur) :
|
||||
serveur_maj = True
|
||||
else :
|
||||
adh_maj = True
|
||||
if serveur_maj and adh_maj :
|
||||
break
|
||||
to_do=[]
|
||||
if serveur_maj :
|
||||
to_do += [ self.serveurs_vers_ext, self.ext_vers_serveurs ]
|
||||
|
||||
if adh_maj :
|
||||
to_do += [ self.crans_vers_ext, self.ext_vers_crans ]
|
||||
|
||||
def del_entree(self,ip):
|
||||
""" Détruit toutes les occurences à l'IP dans le firewall """
|
||||
for table in [ 'filter' , 'nat' ] :
|
||||
for chaine in iptables("-t %s -L -n --line-numbers" % table).split('Chain ') :
|
||||
nom_chaine = chaine.split(' ',1)[0]
|
||||
to_del=[]
|
||||
for regle in chaine.split('\n')[1:] :
|
||||
# On regarde les règles une à une
|
||||
for tache in to_do :
|
||||
self.__exception_catcher(tache)
|
||||
|
||||
def mac_ip_maj(self,ip_list) :
|
||||
""" Mise à jour de la correspondance MAC-IP pour les ip fournies """
|
||||
## Que faut-il faire ?
|
||||
self.anim = anim(' Analyse travail à effectuer')
|
||||
mac_ip_maj = {}
|
||||
serveur_maj = False
|
||||
for ip in ip_list :
|
||||
machine = crans_ldap().search('ip=%s'% ip)['machine']
|
||||
if not machine :
|
||||
# Destruction des occurences
|
||||
if AddrInNet(ip,self.zone_serveur) :
|
||||
serveur_maj = True
|
||||
else :
|
||||
mac_ip_maj[ip] = None
|
||||
|
||||
elif len(machine) == 1 :
|
||||
# Mise à jour de la machine
|
||||
if AddrInNet(ip,self.zone_serveur) :
|
||||
serveur_maj = True
|
||||
else :
|
||||
mac_ip_maj[ip] = machine[0]
|
||||
else :
|
||||
print WARNING
|
||||
if debug :
|
||||
sys.stderr.write("Plusieurs machines avec l'IP %s" % ip)
|
||||
|
||||
print OK
|
||||
|
||||
## Traitement
|
||||
# Seveurs
|
||||
if serveur_maj :
|
||||
# Bourrin mais vu la rapidité...
|
||||
self.__exception_catcher(self.serveurs_vers_ext)
|
||||
|
||||
# Correspondance MAC-IP
|
||||
if mac_ip_maj :
|
||||
to_del = []
|
||||
to_add = []
|
||||
def procedure() :
|
||||
self.anim = anim(' Actualisation TEST_MAC-IP')
|
||||
for regle in iptables("-t nat -L TEST_MAC-IP -n --line-numbers").split('\n')[2:] :
|
||||
regle = regle.split()
|
||||
if ip in regle :
|
||||
# Règle à dértuire
|
||||
to_del.append(int(regle[0]))
|
||||
# Destruction des règles de cette chaîne
|
||||
to_del.sort()
|
||||
to_del.reverse()
|
||||
num = regle[0]
|
||||
ip = regle[4]
|
||||
mac = regle[7].lower()
|
||||
if ip in mac_ip_maj.keys() :
|
||||
machine = mac_ip_maj.pop(ip)
|
||||
if not machine :
|
||||
to_del.append(num)
|
||||
elif ( machine.ipsec() and mac!=self.mac_wifi ) or \
|
||||
( not machine.ipsec() and mac != machine.mac() ) :
|
||||
to_del.append(num)
|
||||
to_add.append(machine)
|
||||
|
||||
for i in to_del :
|
||||
iptables('-t %s -D %s %s' % (table, nom_chaine, i) )
|
||||
|
||||
iptables('-t nat -D TEST_MAC-IP %s' % i )
|
||||
for machine in ( to_add + mac_ip_maj.values() ) :
|
||||
if machine :
|
||||
self.__test_mac_ip(machine)
|
||||
print OK
|
||||
|
||||
self.__exception_catcher(procedure)
|
||||
|
||||
|
||||
if __name__ == '__main__' :
|
||||
# Chaines pouvant être recontruites
|
||||
global chaines
|
||||
chaines = [ 'log_chaines' , 'test_virus_flood', 'reseaux_non_routables',
|
||||
'test_mac_ip' , 'blacklist' , 'ext_vers_serveurs' , 'serveurs_vers_ext',
|
||||
'ext_vers_crans', 'crans_vers_ext' ]
|
||||
|
||||
def __usage(txt=None) :
|
||||
if txt!=None : cprint(txt,'gras')
|
||||
|
||||
print """Usage:
|
||||
%(p)s start : Construction du firewall.
|
||||
%(p)s restart : Reconstruction du firewall.
|
||||
%(p)s stop : Arrêt du firewall.
|
||||
%(p)s chaine <noms de chaines> : recontruit les chaines spécifiées
|
||||
Les chaines pouvant être reconstruites sont :
|
||||
%(chaines)s
|
||||
Pour reconfiguration d'IPs particulières, utiliser generate. """ % \
|
||||
{ 'p' : sys.argv[0].split('/')[-1] , 'chaines' : '\n '.join(chaines) }
|
||||
sys.exit(-1)
|
||||
|
||||
# Bons arguments ?
|
||||
if len(sys.argv) == 1 :
|
||||
__usage()
|
||||
for arg in sys.argv[1:] :
|
||||
if arg in [ 'stop', 'restart', 'start' ] and len(sys.argv) != 2 :
|
||||
__usage("L'argument %s ne peut être employé que seul." % arg)
|
||||
|
||||
if arg not in [ 'stop', 'restart', 'start' ] + chaines :
|
||||
__usage("L'argument %s est inconnu." % arg)
|
||||
|
||||
fw = firewall_komaz()
|
||||
for arg in sys.argv[1:] :
|
||||
eval('fw.%s()' % arg)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue