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:
pauget 2004-12-12 00:50:03 +01:00
parent 291139c9a7
commit ab52211b22

View file

@ -16,37 +16,15 @@
# REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE # REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
# MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR # MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
# PURPOSE. # PURPOSE.
""" classe Firewall de Komaz """ """ Firewall de Komaz """
import syslog import syslog
import iptools,config from lock import *
import ldap_crans from ldap_crans import crans_ldap, ann_scol, machine
from affich_tools import * from affich_tools import *
from commands import getstatusoutput from commands import getstatusoutput
from iptools import AddrInNet from iptools import AddrInNet
syslog.openlog('firewall') 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): class IptablesError(Exception):
""" Gestion des erreurs d'iptables """ """ Gestion des erreurs d'iptables """
def __init__(self,cmd,err_code,output): 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) return "%s\n status : %s\n %s" % (self.cmd,self.err_code,self.output)
def iptables(cmd): def iptables(cmd):
""" Interface à iptables  """ """ Interface à iptables """
syslog.syslog(syslog.LOG_INFO,cmd) syslog.syslog(syslog.LOG_INFO,cmd)
status,output=getstatusoutput("/sbin/iptables "+cmd) status,output=getstatusoutput("/sbin/iptables "+cmd)
if status: if status:
raise IptablesError(cmd,status,output) raise IptablesError(cmd,status,output)
return output
class ErrorNoSuchIp(ErrorIp): class firewall_komaz :
"""
User defined exception
Personne n'a cette ip
"""
pass
class firewall:
""" """
Structure du firewall : Structure du firewall :
table nat : table nat :
@ -145,14 +117,44 @@ class firewall:
machines = [] machines = []
debug = 1 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) : def __machines(self) :
""" Liste des machines du crans """ """ Liste des machines du crans """
if not self.machines : if not self.machines :
self.anim = anim(" Interrogation de la base LDAP") 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 print OK
return self.machines 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) : def start(self) :
""" Construction du firewall """ """ Construction du firewall """
cprint('Démarrage firewall','gras') cprint('Démarrage firewall','gras')
@ -161,11 +163,11 @@ class firewall:
print "Abandon" print "Abandon"
return return
self.anim = anim(' Construction structure de la table nat') def procedure() :
for chaine in [ 'LOG_VIRUS', 'LOG_FLOOD', 'TEST_VIRUS_FLOOD' , 'TEST_MAC-IP' , 'RESEAUX_NON_ROUTABLES_SRC', 'RESEAUX_NON_ROUTABLES_DST' ] : self.anim = anim(' Structure de la table nat')
iptables('-t nat -N %s' % chaine) 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 :
iptables("-t nat -P PREROUTING ACCEPT") iptables("-t nat -P PREROUTING ACCEPT")
iptables("-t nat -A PREROUTING -i lo -j ACCEPT") iptables("-t nat -A PREROUTING -i lo -j ACCEPT")
iptables("-t nat -A PREROUTING -s ! %s -j TEST_VIRUS_FLOOD" % self.zone_serveur) 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") iptables("-t nat -P PREROUTING DROP")
print OK 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' ] : for chaine in [ 'EXT_VERS_SERVEURS', 'SERVEURS_VERS_EXT' , 'EXT_VERS_CRANS', 'CRANS_VERS_EXT', 'BLACKLIST_SRC', 'BLACKLIST_DST' ] :
iptables('-N %s' % chaine) iptables('-N %s' % chaine)
@ -193,33 +195,16 @@ class firewall:
iptables("-A FORWARD -o %s -j CRANS_VERS_EXT" % self.eth_ext ) iptables("-A FORWARD -o %s -j CRANS_VERS_EXT" % self.eth_ext )
print OK print OK
except IptablesError, c : # Initialisation
print ERREUR self.__exception_catcher(procedure)
if self.debug : print c
self.stop()
return 1
except :
print ERREUR
self.stop()
if self.debug :
import traceback
traceback.print_exc()
return 1
# Remplisage
for tache in [ self.log_chaines, self.test_virus_flood, self.reseaux_non_routables, 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.blacklist , self.serveurs_vers_ext, self.ext_vers_serveurs,
self.crans_vers_ext, self.ext_vers_crans, self.test_mac_ip ] : self.crans_vers_ext, self.ext_vers_crans, self.test_mac_ip ] :
try : self.__exception_catcher(tache)
tache()
except IptablesError, c : cprint(" -> fin de la procédure de démarrage",'vert')
self.anim.reinit()
print ERREUR
if self.debug : print c
except :
self.anim.reinit()
print ERREUR
import traceback
if self.debug : traceback.print_exc()
def log_chaines(self) : def log_chaines(self) :
""" Construction des chaines de log (LOG_VIRUS et LOG_FLOOD) """ """ Construction des chaines de log (LOG_VIRUS et LOG_FLOOD) """
@ -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. 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() self.anim.cycle()
iptables('-t nat -A TEST_VIRUS_FLOOD -j LOG_FLOOD') iptables('-t nat -A TEST_VIRUS_FLOOD -j LOG_FLOOD')
self.anim.reinit()
print OK
def stop(self): def stop(self):
""" Arrête le firewall """ """ Arrête le firewall """
self.anim = anim(" Arrêt du firewall") self.anim = anim(" Arrêt du firewall")
try : def procedure() :
iptables("-t nat -P PREROUTING ACCEPT") iptables("-t nat -P PREROUTING ACCEPT")
iptables("-F") iptables("-F")
iptables("-t nat -F") iptables("-t nat -F")
iptables("-X") iptables("-X")
iptables("-t nat -X") iptables("-t nat -X")
print OK print OK
return 0
except IptablesError, c : return self.__exception_catcher(procedure)
print ERREUR
if self.debug : print c
except :
print ERREUR
if self.debug :
import traceback
traceback.print_exc()
return 1
def test_mac_ip(self) : def test_mac_ip(self) :
""" Reconstruit la correspondance MAC-IP des machines des adhérents """ """ 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 -P PREROUTING ACCEPT")
iptables("-t nat -F TEST_MAC-IP") iptables("-t nat -F TEST_MAC-IP")
self.anim.cycle() self.anim.cycle()
try:
def procedure() :
for machine in self.__machines() : for machine in self.__machines() :
self.__test_mac_ip(machine) self.__test_mac_ip(machine)
self.anim.cycle() self.anim.cycle()
iptables("-t nat -P PREROUTING DROP") iptables("-t nat -P PREROUTING DROP")
self.anim.reinit() self.anim.reinit()
print OK print OK
except IptablesError, c :
self.anim.reinit() self.__exception_catcher(procedure)
print ERREUR
if self.debug : print c
except :
self.anim.reinit()
print ERREUR
if self.debug :
import traceback
traceback.print_exc()
def serveurs_vers_ext(self) : def serveurs_vers_ext(self) :
""" Reconstruit la chaine SERVEURS_VERS_EXT """ """ 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() self.anim.reinit()
print OK print OK
def ext_vers_serveurs(self) : def ext_vers_serveurs(self) :
""" Reconstruit la chaine EXT_VERS_SERVEURS """ """ 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() self.anim.reinit()
print OK print OK
@ -323,8 +292,9 @@ class firewall:
self.__build_chaine_adherent('EXT_VERS_CRANS',self.__ext_vers_crans) self.__build_chaine_adherent('EXT_VERS_CRANS',self.__ext_vers_crans)
def __build_chaine_adherent(self,chaine,methode): def __build_chaine_adherent(self,chaine,methode):
if self.__build_chaine(chaine, methode) : # On construit d'abord les autorisations particulières
# Défauts if not self.__build_chaine(chaine, methode) :
# Puis si pas de problèmes les autorisation par défaut
self.anim.reinit() self.anim.reinit()
for proto in [ 'tcp' , 'udp' ] : for proto in [ 'tcp' , 'udp' ] :
for port in self.ports_default["%s_%s" % ( proto, chaine) ] : for port in self.ports_default["%s_%s" % ( proto, chaine) ] :
@ -334,33 +304,16 @@ class firewall:
print OK print OK
def __build_chaine(self,chaine, methode) : 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) iptables("-F %s" % chaine)
self.anim.cycle() self.anim.cycle()
try: def procedure() :
for machine in self.__machines() : for machine in self.__machines() :
methode(machine) methode(machine)
self.anim.cycle() self.anim.cycle()
iptables("-A %s -j REJECT" % chaine) 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()
def add_machine(self,machine): return self.__exception_catcher(procedure)
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): def __serveurs_vers_ext(self,machine):
ip=machine.ip() ip=machine.ip()
@ -433,17 +386,17 @@ class firewall:
def blacklist(self): def blacklist(self):
""" Construit les chaines de blackliste (BLACKLIST_{DST,SRC}) """ """ Construit les chaines de blackliste (BLACKLIST_{DST,SRC}) """
self.anim = anim(" Contruction blackliste") self.anim = anim(" Blackliste")
iptables('-F BLACKLIST_DST') iptables('-F BLACKLIST_DST')
iptables('-F BLACKLIST_SRC') iptables('-F BLACKLIST_SRC')
blacklist=[] 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']: for entite in search['adherent']+search['club']+search['machine']:
self.anim.cycle() self.anim.cycle()
sanctions = entite.blacklist_actif() sanctions = entite.blacklist_actif()
if 'upload' in sanctions or 'warez' in sanctions : if 'upload' in sanctions or 'warez' in sanctions :
if search.__class__ == ldap_crans.machine: if search.__class__ == machine:
blacklist+=[entite] blacklist+=[entite]
else: else:
blacklist+=entite.machines() blacklist+=entite.machines()
@ -455,21 +408,126 @@ class firewall:
self.anim.reinit() self.anim.reinit()
print OK print OK
def del_entree(self,ip): def port_maj(self,ip_list) :
""" Détruit toutes les occurences à l'IP dans le firewall """ """ Mise à jour des ports pour les ip fournies """
for table in [ 'filter' , 'nat' ] : # Note : système bourrin (on efface les chaines et on refait)
for chaine in iptables("-t %s -L -n --line-numbers" % table).split('Chain ') : # mais rapide et efficace (si qqn veut se casser le cul à
nom_chaine = chaine.split(' ',1)[0] # un système aussi délicat que pour la correspondance MAC-IP...)
to_del=[] # -- Fred
for regle in chaine.split('\n')[1:] : serveur_maj = False
# On regarde les règles une à une adh_maj = False
regle = regle.split() for ip in ip_list :
if ip in regle : if AddrInNet(ip,self.zone_serveur) :
# Règle à dértuire serveur_maj = True
to_del.append(int(regle[0])) else :
# Destruction des règles de cette chaîne adh_maj = True
to_del.sort() if serveur_maj and adh_maj :
to_del.reverse() break
for i in to_del : to_do=[]
iptables('-t %s -D %s %s' % (table, nom_chaine, i) ) 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 ]
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()
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 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)