Import initial
darcs-hash:20040906090607-d1718-88ed25aacc058dd937f6a2f5899bd9e01773047f.gz
This commit is contained in:
parent
a9c7f1b23f
commit
d5cfbd4698
1 changed files with 440 additions and 0 deletions
440
gestion/gen_confs/wifi.py
Executable file
440
gestion/gen_confs/wifi.py
Executable file
|
@ -0,0 +1,440 @@
|
|||
#! /usr/bin/env python
|
||||
# -*- coding: iso-8859-15 -*-
|
||||
|
||||
import time, commands
|
||||
from gen_confs import gen_config, ERREUR, OK, anim
|
||||
|
||||
class conf_wifi(gen_config) :
|
||||
""" Génération de la configuration de isakmpd dans ISAKMPD_CONF
|
||||
Le fichier est constitué en 5 parties :
|
||||
1) Configuration générale insérée telle quelle
|
||||
2) Phase 1 : une ligne par host suivant template
|
||||
3) Phase 2 : une entrée par machine
|
||||
4) Bloc par machine suivant template
|
||||
5) Ajout de net_crans
|
||||
"""
|
||||
######################################PARTIE DE CONFIGURATION
|
||||
|
||||
# Fichiers à écrire
|
||||
# Répertoire d'écriture des fichiers de zone
|
||||
ISAKMPD_CONF='/tmp/isakmpd.conf'
|
||||
|
||||
# Config générale
|
||||
general="""
|
||||
[General]
|
||||
Listen-on= 138.231.148.1
|
||||
Retransmits= 5
|
||||
Exchange-max-time= 29
|
||||
Logverbose= 1
|
||||
Loglevel= A=90
|
||||
Check-interval= 60
|
||||
Default-phase-1-lifetime= 1200,90:86400
|
||||
Default-phase-2-lifetime= 1800,120:86400
|
||||
|
||||
[Default-main-mode]
|
||||
DOI= IPSEC
|
||||
EXCHANGE_TYPE= ID_PROT
|
||||
Transforms= 3DES-SHA,3DES-MD5
|
||||
|
||||
[Default-quick-mode]
|
||||
DOI= IPSEC
|
||||
EXCHANGE_TYPE= QUICK_MODE
|
||||
Suites= QM-ESP-AES-SHA-SUITE, QM-ESP-AES-MD5-SUITE, \\
|
||||
QM-ESP-BLF-SHA-SUITE, QM-ESP-BLF-MD5-SUITE, \\
|
||||
QM-ESP-3DES-SHA-SUITE, QM-ESP-3DES-MD5-SUITE, \\
|
||||
QM-ESP-AES-SHA-PFS-SUITE, QM-ESP-AES-MD5-PFS-SUITE, \\
|
||||
QM-ESP-BLF-SHA-PFS-SUITE, QM-ESP-BLF-MD5-PFS-SUITE, \\
|
||||
QM-ESP-3DES-SHA-PFS-SUITE, QM-ESP-3DES-MD5-PFS-SUITE
|
||||
|
||||
"""
|
||||
|
||||
# Phase 1
|
||||
phase1_debut="[Phase 1]\n"
|
||||
phase1_template="%(IP)s= ISAKMP-peer-%(HOST)s\n"
|
||||
|
||||
# Phase 2
|
||||
phase2_debut="[Phase 2]\nPassive-connections= "
|
||||
phase2_template="IPsec-%(HOST)s"
|
||||
phase2_sep=', \\\n '
|
||||
|
||||
#Lignes necessaires pour chaque machine
|
||||
host_template="""
|
||||
[ISAKMP-peer-%(HOST)s]
|
||||
Phase= 1
|
||||
Transport= udp
|
||||
Address= %(IP)s
|
||||
Configuration= Default-main-mode
|
||||
Authentication=%(KEY)s
|
||||
|
||||
[IPsec-%(HOST)s]
|
||||
Phase= 2
|
||||
ISAKMP-peer= ISAKMP-peer-%(HOST)s
|
||||
Configuration= Default-quick-mode
|
||||
Local-ID= Net-crans
|
||||
Remote-ID= Net-%(HOST)s
|
||||
|
||||
[Net-%(HOST)s]
|
||||
ID-type= IPV4_ADDR
|
||||
Address= %(HOST)s
|
||||
"""
|
||||
# Dernière partie du fichier
|
||||
net_crans="""
|
||||
[Net-crans]
|
||||
ID-type= IPV4_ADDR_SUBNET
|
||||
Network= 0.0.0.0
|
||||
Netmask= 0.0.0.0
|
||||
"""
|
||||
restart_cmd = 'kill -HUP $(cat /var/run/isakmpd.pid 2>/dev/null )'
|
||||
|
||||
######################################FIN PARTIE DE CONFIGURATION
|
||||
|
||||
def __str__(self) :
|
||||
return 'wifi'
|
||||
|
||||
def _gen(self) :
|
||||
phase1 = self.phase1_debut
|
||||
phase2 = self.phase2_debut
|
||||
blocs=''
|
||||
|
||||
for machine in self.base.search('ipsec=*&paiement=%s' % self.ann_scol)['machine'] :
|
||||
self.anim.cycle()
|
||||
if 'bloq' in machine.blacklist_actif() : continue
|
||||
data = { 'HOST' : machine.nom().split('.')[0] ,
|
||||
'IP' : machine.ip() ,
|
||||
'KEY' : machine.ipsec() }
|
||||
|
||||
# Phase 1
|
||||
phase1 += self.phase1_template % data
|
||||
|
||||
# Phase 2
|
||||
if blocs != '' :
|
||||
# Ce n'est pas la première machine, il faut insérer un séparateur
|
||||
phase2 += self.phase2_sep
|
||||
phase2 += self.phase2_template % data
|
||||
|
||||
# Blocs machine
|
||||
blocs += self.host_template % data
|
||||
|
||||
# Ecriture du fichier
|
||||
fd = self._open_conf(self.ISAKMPD_CONF,'#')
|
||||
fd.write(self.general)
|
||||
fd.write(phase1)
|
||||
fd.write('\n')
|
||||
fd.write(phase2)
|
||||
fd.write('\n')
|
||||
fd.write(blocs)
|
||||
fd.write(self.net_crans)
|
||||
fd.close()
|
||||
|
||||
|
||||
|
||||
class bornes_wifi(gen_config) :
|
||||
""" Reconfiguration bornes wifi
|
||||
Si nom de borne fourni à l'initialisation restart uniquement celle-ci
|
||||
"""
|
||||
|
||||
clef = '/etc/wifi/ssh/wifi' # Fichier clef ssh
|
||||
|
||||
# Fichier
|
||||
AUTOEXEC = '/tmp/autoexec.sh'
|
||||
|
||||
autoexec = """#! /bin/sh
|
||||
#***********************************************************
|
||||
# Ce fichier est généré dans wifi.py
|
||||
# Les données proviennent de la base LDAP et de la conf
|
||||
# présente au début du script.
|
||||
#
|
||||
# Génération : %(date)s
|
||||
# Fichier : %(AUTOEXEC)s
|
||||
#
|
||||
# NE PAS EDITER
|
||||
#
|
||||
#***********************************************************
|
||||
|
||||
## On configure la nvram de la borne selon son adresse MAC (celle inscrite sur la borne)
|
||||
## Modifier bornes.cf et non ce fichier qui sera modifié selon le contenu de bornes.cf
|
||||
cat <<EOF | grep -i $(nvram get et0macaddr) > /tmp/params
|
||||
%(BORNES)s
|
||||
EOF
|
||||
(
|
||||
|
||||
NECTARIS=138.231.148.1
|
||||
|
||||
echo -n "Mise en place de syslog..."
|
||||
|
||||
killall syslogd 2> /dev/null
|
||||
killall klogd 2> /dev/null
|
||||
/sbin/syslogd -R $NECTARIS
|
||||
/sbin/klogd
|
||||
|
||||
echo " ok"
|
||||
|
||||
read mac ip canal puissance nom < /tmp/params
|
||||
echo "Mac : $mac"
|
||||
echo "IP : $ip"
|
||||
echo "Canal : $canal"
|
||||
echo "Puiss : $puissance mW"
|
||||
echo "Nom : $nom"
|
||||
|
||||
if [ "$mac" = "" ]; then
|
||||
echo "Borne inconnue"
|
||||
ping -c 1 138.231.148.1
|
||||
sleep 120
|
||||
reboot
|
||||
fi
|
||||
|
||||
change=0
|
||||
|
||||
echo -n IP...
|
||||
if [ "$(nvram get lan_ipaddr)" != "$ip" ]; then
|
||||
ifconfig br0 $ip netmask 255.255.252.0
|
||||
nvram set lan_ipaddr=$ip
|
||||
change=1
|
||||
fi
|
||||
echo " ok"
|
||||
|
||||
echo -n Canal...
|
||||
if [ "$(nvram get wl_channel)" != "$canal" ]; then
|
||||
nvram set wl_channel=$canal
|
||||
change=1
|
||||
fi
|
||||
echo " ok"
|
||||
|
||||
echo -n Puissance...
|
||||
if [ "$(nvram get txpwr)" != "$puissance" ]; then
|
||||
nvram set txpwr=$puissance
|
||||
change=1
|
||||
fi
|
||||
echo " ok"
|
||||
|
||||
echo -n Nom...
|
||||
if [ "$(nvram get wan_hostname)" != "$nom" ]; then
|
||||
nvram set wan_hostname=$nom
|
||||
change=1
|
||||
fi
|
||||
echo " ok"
|
||||
|
||||
echo -n Désactivation de cron...
|
||||
if [ "$(nvram get cron_enable)" != "0" ]; then
|
||||
nvram set cron_enable=0
|
||||
change=1
|
||||
fi
|
||||
echo " ok"
|
||||
|
||||
echo -n Commit des changements...
|
||||
if [ $change -eq 1 ]; then
|
||||
nvram commit
|
||||
sleep 1
|
||||
# On se casse pas la tete
|
||||
reboot
|
||||
fi
|
||||
echo " ok"
|
||||
|
||||
# On place un prompt pour la machine
|
||||
cd /tmp
|
||||
|
||||
cat <<EOF > .profile
|
||||
export PS1="$nom ($ip) # "
|
||||
echo "
|
||||
----------------------------------
|
||||
|
||||
WIFI @ CRANS
|
||||
|
||||
Nom: $nom
|
||||
IP: $ip
|
||||
Puissance: $puissance
|
||||
Canal: $canal
|
||||
|
||||
---------------------------------"
|
||||
EOF
|
||||
|
||||
|
||||
|
||||
echo -n Démarrage du firewall...
|
||||
## Firewall
|
||||
|
||||
DHCP=138.231.148.253
|
||||
WEB=138.231.136.6
|
||||
FTP=138.231.136.10
|
||||
WIFI=138.231.148.0/22
|
||||
flt="iptables -A FORWARD -i br0"
|
||||
|
||||
iptables -F FORWARD
|
||||
|
||||
$flt --destination 224.0.0.0/4 -j DROP
|
||||
$flt -m state --state ESTABLISHED,RELATED -j ACCEPT
|
||||
|
||||
# DHCP
|
||||
$flt -p udp --sport bootpc --dport bootps --source 0.0.0.0 --destination 255.255.255.255 -j ACCEPT
|
||||
$flt -p udp --sport bootpc --dport bootps --source 0.0.0.0 --destination $DHCP -j ACCEPT
|
||||
$flt -p udp --sport bootpc --dport bootps --source $WIFI --destination 255.255.255.255 -j ACCEPT
|
||||
$flt -p udp --sport bootps --dport bootpc --source $DHCP --destination $WIFI -j ACCEPT
|
||||
$flt -p udp --sport bootps --dport bootpc --source $DHCP --destination 255.255.255.255 -j ACCEPT
|
||||
# Pour compter ce qui ne passe pas (et penser un jour à utiliser tcpdump pour voir ce que c'est)
|
||||
#$flt -p udp --sport bootpc --dport bootps -j LOG # Devrait être 0
|
||||
#$flt -p udp --sport bootps --dport bootpc -j LOG # Devrait être 0
|
||||
#$flt -p udp --sport bootpc --dport bootps -j ACCEPT # Devrait être 0
|
||||
#$flt -p udp --sport bootps --dport bootpc -j ACCEPT # Devrait être 0
|
||||
|
||||
# ISAKMP
|
||||
$flt -p udp --source $WIFI --destination $NECTARIS --dport 500 --sport 500 -j ACCEPT
|
||||
#$flt -p udp --destination $WIFI --source $NECTARIS --dport 500 --sport 500 -j LOG # Doit être 0
|
||||
#$flt -p udp --destination $WIFI --source $NECTARIS --dport 500 --sport 500 -j ACCEPT # Doit être 0
|
||||
|
||||
# DNS + Web vers Zamok + FTP sila
|
||||
$flt -p udp --source $WIFI --destination $NECTARIS --dport 53 --sport 1024: -j ACCEPT
|
||||
$flt -p tcp --source $WIFI --destination $WEB --syn --dport 443 --sport 1024: -j ACCEPT
|
||||
$flt -p tcp --source $WIFI --destination $WEB --syn --dport 80 --sport 1024: -j ACCEPT
|
||||
$flt -p tcp --source $WIFI --destination $FTP --syn --dport 21 --sport 1024: -j ACCEPT
|
||||
|
||||
# ESP
|
||||
$flt -p ESP --source $WIFI --destination $NECTARIS -j ACCEPT # A 0, mais a priori, rien n'empêche d'utiliser cette ligne
|
||||
$flt -p ESP --destination $WIFI --source $NECTARIS -j ACCEPT
|
||||
|
||||
iptables -P FORWARD DROP
|
||||
|
||||
echo " ok"
|
||||
|
||||
echo -n "Mise en place du resolv.conf..."
|
||||
|
||||
cat > /tmp/myresolv.conf <<EOF
|
||||
search wifi.crans.org crans.org
|
||||
nameserver $NECTARIS
|
||||
EOF
|
||||
|
||||
cp /tmp/myresolv.conf /tmp/resolv.conf
|
||||
|
||||
echo " ok"
|
||||
|
||||
echo -n "Mise à l'heure..."
|
||||
|
||||
ntpclient -h $NECTARIS -s
|
||||
date
|
||||
|
||||
echo " ok"
|
||||
|
||||
echo -n "Mise en place de cron..."
|
||||
|
||||
killall cron 2> /dev/null
|
||||
|
||||
mkdir /tmp/cron.d 2> /dev/null
|
||||
mkdir /var/spool 2> /dev/null
|
||||
mkdir /var/spool/cron 2> /dev/null
|
||||
chmod 700 /tmp/cron.d /var/spool /var/spool/cron
|
||||
|
||||
cat > /tmp/cron.updatedns <<EOF
|
||||
#! /bin/sh
|
||||
if ! cmp /tmp/myresolv.conf /tmp/resolv.conf; then
|
||||
cp /tmp/myresolv.conf /tmp/resolv.conf
|
||||
rm /etc/cron.d/dns
|
||||
sleep 61 # Sinon, il devient fou
|
||||
fi
|
||||
exit 0
|
||||
EOF
|
||||
chmod +x /tmp/cron.updatedns
|
||||
|
||||
cat > /tmp/cron.ping <<EOF
|
||||
#! /bin/sh
|
||||
/usr/sbin/wl assoclist | grep -c '^assoc' | logger -t "Clients"
|
||||
ping -c 1 $NECTARIS 2> /dev/null
|
||||
sleep 61 # Idem
|
||||
exit 0
|
||||
EOF
|
||||
chmod +x /tmp/cron.ping
|
||||
|
||||
cat > /tmp/cron.uptime <<EOF
|
||||
#! /bin/sh
|
||||
uptime | logger -t "Uptime"
|
||||
sleep 61 # idem
|
||||
exit 0
|
||||
EOF
|
||||
chmod +x /tmp/cron.uptime
|
||||
|
||||
# klogd semble tomber en panne regulierement...
|
||||
# Ou alors, c'est syslog ?
|
||||
cat > /tmp/cron.klogd <<EOF
|
||||
#! /bin/sh
|
||||
killall klogd
|
||||
killall syslogd
|
||||
/sbin/syslogd -R $NECTARIS
|
||||
/sbin/klogd
|
||||
sleep 61 # Idem
|
||||
exit 0
|
||||
EOF
|
||||
chmod +x /tmp/cron.klogd
|
||||
|
||||
# Remet en place le plus rapidement possible le resolv.conf (écrasé par la suite du démarrage)
|
||||
cat > /etc/cron.d/dns <<EOF
|
||||
*/2 * * * * root /tmp/cron.updatedns
|
||||
EOF
|
||||
|
||||
# Relance certains services à intervalles réguliers
|
||||
cat > /etc/cron.d/services <<EOF
|
||||
12 * * * * root /tmp/cron.klogd
|
||||
EOF
|
||||
|
||||
# Pingue toutes les 5 minutes egon, cela peut servir un jour...
|
||||
cat > /etc/cron.d/ping <<EOF
|
||||
*/5 * * * * root /tmp/cron.ping
|
||||
2 * * * * root /tmp/cron.uptime
|
||||
EOF
|
||||
|
||||
# On lance cron
|
||||
cron
|
||||
|
||||
echo " ok"
|
||||
|
||||
) >> /tmp/autoexec.log 2>> /tmp/autoexec.log
|
||||
# Pas sûr que cela reste ordonné
|
||||
|
||||
|
||||
# On donne le tout à syslog
|
||||
echo "Fin du boot" >> /tmp/autoexec.log
|
||||
cat /tmp/autoexec.log | busybox logger -t "Statut"
|
||||
|
||||
|
||||
"""
|
||||
|
||||
"#" # Ligne ici juste pour avoir un comportement correct de mon éditeur......
|
||||
|
||||
def __str__(self) :
|
||||
return 'bornes wifi'
|
||||
|
||||
def __init__(self,borne='') :
|
||||
self.borne = borne
|
||||
|
||||
def _gen(self) :
|
||||
date = time.strftime('%A %d %B %Y %H:%M')
|
||||
BORNES = '## Liste des bornes -- source : base LDAP\n'
|
||||
BORNES = '## MAC - IP - Canal - Puissance - Nom\n'
|
||||
for b in self.base.search('puissance=*')['machine'] :
|
||||
self.anim.cycle()
|
||||
BORNES += '%s %s %s %s %s\n' % ( b.mac(), b.ip(), b.canal(), b.puissance(), b.nom() )
|
||||
|
||||
BORNES += '## Fin liste des bornes'
|
||||
|
||||
AUTOEXEC = self.AUTOEXEC
|
||||
fd = self._open_conf(self.AUTOEXEC)
|
||||
fd.write( self.autoexec % vars() )
|
||||
fd.close()
|
||||
|
||||
def restart(self) :
|
||||
self.lock()
|
||||
if self.borne :
|
||||
bornes = [ self.borne ]
|
||||
else :
|
||||
bornes = []
|
||||
for b in self.base.search('puissance=*')['machine'] :
|
||||
bornes.append( b.nom() )
|
||||
|
||||
for borne in bornes :
|
||||
anim('\treboot de %s' % borne)
|
||||
status, output = commands.getstatusoutput('ssh -i %s -o StrictHostKeyChecking=no root@%s reboot' % ( self.clef, borne ) )
|
||||
if status :
|
||||
print ERREUR
|
||||
if self.debug :
|
||||
print output
|
||||
else :
|
||||
print OK
|
||||
|
||||
self.unlock()
|
Loading…
Add table
Add a link
Reference in a new issue