gestionnaire qui permet de desactiver les bornes wifi avec un einterface web
darcs-hash:20050224011102-4ec08-29a8144812c52abe1bb6e18a42280d21a2f4204b.gz
This commit is contained in:
parent
452e021675
commit
dcdf92ee6a
4 changed files with 378 additions and 0 deletions
176
wifiweb/bornes.py
Executable file
176
wifiweb/bornes.py
Executable file
|
@ -0,0 +1,176 @@
|
||||||
|
#! /usr/bin/env python
|
||||||
|
# -*- coding: iso-8859-15 -*-
|
||||||
|
|
||||||
|
import cgi, sys, os, sha, time
|
||||||
|
from html import html
|
||||||
|
from session import session
|
||||||
|
from utilisateurs import users
|
||||||
|
|
||||||
|
sys.path.append('/usr/scripts/gestion')
|
||||||
|
from ldap_crans import crans_ldap
|
||||||
|
db = crans_ldap()
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# fontion pour logguer un truc
|
||||||
|
def log (message) :
|
||||||
|
f = open('/var/log/wifiweb.log','a+')
|
||||||
|
f.write( time.strftime('%d/%m/%Y %H:%M:%S',time.localtime(time.time())) + ' ' + message + '\n')
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# initialisation des variables
|
||||||
|
|
||||||
|
# initialisation de la page html
|
||||||
|
page = html()
|
||||||
|
page.sendheaderstobrowser()
|
||||||
|
page.titre("Gestion des bornes wifi")
|
||||||
|
|
||||||
|
# récupération des données du formulaire
|
||||||
|
form = cgi.FieldStorage()
|
||||||
|
sid = form.getvalue('sid')
|
||||||
|
action = form.getvalue('action','auth')
|
||||||
|
|
||||||
|
# création de la session
|
||||||
|
try :
|
||||||
|
sess = session(sid)
|
||||||
|
except :
|
||||||
|
sess = session()
|
||||||
|
sid = sess.sid
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# authentification
|
||||||
|
|
||||||
|
if action == 'valid-auth' :
|
||||||
|
sess.data['user'] = form.getvalue('user','')
|
||||||
|
sess.data['password'] = sha.new(form.getvalue('password','')).hexdigest()
|
||||||
|
if users.has_key( sess.data['user'] + ':' + sess.data['password'] ) :
|
||||||
|
log(sess.data['user'] + ' s\'est connecté' )
|
||||||
|
action = 'liste'
|
||||||
|
|
||||||
|
if action != 'auth' :
|
||||||
|
try :
|
||||||
|
if not users.has_key( sess.data['user'] + ':' + sess.data['password'] ) :
|
||||||
|
log(sess.data['user'] + ' mauvais user/pass' )
|
||||||
|
raise ValueError, 'Mauvais user/pass'
|
||||||
|
bornes = users[ sess.data['user'] + ':' + sess.data['password'] ]
|
||||||
|
except :
|
||||||
|
page.add('<font color="red">Erreur d\'authentification !</font><br><br>')
|
||||||
|
log(sess.data['user'] + ' erreur d\'authentification' )
|
||||||
|
action = 'auth'
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# page : authentification
|
||||||
|
|
||||||
|
if action == 'auth' :
|
||||||
|
page.add("""<center>
|
||||||
|
<form method="POST">
|
||||||
|
<input type="hidden" name="sid" value="%s">
|
||||||
|
<input type="hidden" name="action" value="valid-auth">
|
||||||
|
<table>
|
||||||
|
<tr><td>Utiliateur : </td><td><input type="text" name="user">
|
||||||
|
<tr><td>Mot de passe : </td><td><input type="password" name="password"><br></td></tr>
|
||||||
|
<tr><td collspan="2" align="center"><input type="submit" value="Valider"></td></tr>
|
||||||
|
</table>
|
||||||
|
</form>
|
||||||
|
</center>
|
||||||
|
""" % sid )
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# désactivation d'un borne
|
||||||
|
|
||||||
|
if action == 'desactive' :
|
||||||
|
if form.getvalue('borne','') in bornes :
|
||||||
|
log(sess.data['user'] + ' a désactivé %s' % form.getvalue('borne','') )
|
||||||
|
page.add('<font color="blue">La borne <b>%s</b> sera désactivée dans quelques instants</font><br><br>' % form.getvalue('borne','') )
|
||||||
|
borne = db.search('host=%s.wifi.crans.org' % form.getvalue('borne',''), 'w' )['machine'][0]
|
||||||
|
if int(borne.puissance()) > 0 :
|
||||||
|
borne.puissance(-int(borne.puissance()))
|
||||||
|
borne.save()
|
||||||
|
else :
|
||||||
|
log(sess.data['user'] + ' a tenté de désactiver %s' % form.getvalue('borne','') )
|
||||||
|
page.add('<font color="red">Vous n\'êtes pas authorisé à modifier la borne <b>%s</b></font><br><br>' % form.getvalue('borne','') )
|
||||||
|
action = 'liste'
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# activation d'un borne
|
||||||
|
|
||||||
|
if action == 'active' :
|
||||||
|
if form.getvalue('borne','') in bornes :
|
||||||
|
log(sess.data['user'] + ' a activé %s' % form.getvalue('borne','') )
|
||||||
|
page.add('<font color="blue">La borne <b>%s</b> sera réactivée dans quelques instants</font><br><br>' % form.getvalue('borne','') )
|
||||||
|
borne = db.search('host=%s.wifi.crans.org' % form.getvalue('borne',''),'w' )['machine'][0]
|
||||||
|
if int(borne.puissance()) < 0 :
|
||||||
|
borne.puissance(int(borne.puissance().replace('-','')))
|
||||||
|
borne.save()
|
||||||
|
else :
|
||||||
|
log(sess.data['user'] + ' a tenté d\'activer %s' % form.getvalue('borne','') )
|
||||||
|
page.add('<font color="red">Vous n\'êtes pas authorisé à modifier la borne <b>%s</b></font><br><br>' % form.getvalue('borne','') )
|
||||||
|
action = 'liste'
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# page : liste des bornes
|
||||||
|
|
||||||
|
if action == 'liste' :
|
||||||
|
page.sous_titre('Liste des bornes')
|
||||||
|
|
||||||
|
for b in bornes :
|
||||||
|
borne = db.search('host=%s.wifi.crans.org' % b)['machine'][0]
|
||||||
|
|
||||||
|
# formulaire
|
||||||
|
page.add('<form method=\"POST\">')
|
||||||
|
page.add('<input type="hidden" name="sid" value="%s">' % sid )
|
||||||
|
page.add('<input type="hidden" name="borne" value="%s">' % b)
|
||||||
|
|
||||||
|
# titre
|
||||||
|
if '-' in borne.puissance() :
|
||||||
|
# réactivation
|
||||||
|
page.add('<b><u>%s</u></b> <font color="red">(borne désactivée)</font>' % borne.Nom())
|
||||||
|
page.add('<input type="hidden" name="action" value="active">')
|
||||||
|
else :
|
||||||
|
# désctivation
|
||||||
|
page.add('<b><u>%s</u></b> <font color="green">(borne activée)</font>' % borne.Nom())
|
||||||
|
page.add('<input type="hidden" name="action" value="desactive">')
|
||||||
|
page.add('<br>')
|
||||||
|
|
||||||
|
# commentaires
|
||||||
|
page.add('<table><tr><td width=\"20\"> </td><td>')
|
||||||
|
page.add('<br>'.join(borne.info()))
|
||||||
|
page.add('</td></tr></table>')
|
||||||
|
|
||||||
|
# bouton de validation
|
||||||
|
if '-' in borne.puissance() :
|
||||||
|
page.add(u'<input type="submit" value="Réactiver %s">' % b)
|
||||||
|
else :
|
||||||
|
page.add(u'<input type="submit" value="Désactiver %s">' % b)
|
||||||
|
|
||||||
|
# fin du formulaire
|
||||||
|
page.add('</form>')
|
||||||
|
|
||||||
|
# bouton quitter
|
||||||
|
page.add("""<center>
|
||||||
|
<table><tr>
|
||||||
|
<td><form method="POST">
|
||||||
|
<input type="hidden" name="sid" value="%s">
|
||||||
|
<input type="hidden" name="action" value="liste">
|
||||||
|
<input type="submit" value="Actualiser">
|
||||||
|
</form></td>
|
||||||
|
<td><form method="POST">
|
||||||
|
<input type="hidden" name="sid" value="%s">
|
||||||
|
<input type="hidden" name="action" value="logout">
|
||||||
|
<input type="submit" value="Quitter">
|
||||||
|
</form></td>
|
||||||
|
</tr></table>
|
||||||
|
</center>
|
||||||
|
""" % (sid, sid) )
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# page : logout
|
||||||
|
|
||||||
|
if action == 'logout' :
|
||||||
|
log(sess.data['user'] + ' s\'est déconnecté' )
|
||||||
|
page.sous_titre('Seen you soon')
|
||||||
|
sess.destroy()
|
||||||
|
|
||||||
|
######################################################
|
||||||
|
# fin du script
|
||||||
|
page.sendtobrowser()
|
101
wifiweb/html.py
Executable file
101
wifiweb/html.py
Executable file
|
@ -0,0 +1,101 @@
|
||||||
|
#! /usr/bin/env python
|
||||||
|
# -*- coding: iso-8859-15 -*-
|
||||||
|
|
||||||
|
class html :
|
||||||
|
|
||||||
|
def __init__ (self) :
|
||||||
|
self._titre = ''
|
||||||
|
self._sous_titre = ''
|
||||||
|
self._corp = ''
|
||||||
|
self._refresh = 0
|
||||||
|
|
||||||
|
def titre(self, titre = None) :
|
||||||
|
""" Définit ou retourne le titre """
|
||||||
|
if titre != None :
|
||||||
|
self._titre = titre
|
||||||
|
return self._titre
|
||||||
|
|
||||||
|
def sous_titre (self, sous_titre = None) :
|
||||||
|
""" Définit ou retourne le sous titre """
|
||||||
|
if sous_titre != None :
|
||||||
|
self._sous_titre = sous_titre
|
||||||
|
return self._sous_titre
|
||||||
|
|
||||||
|
def refresh (self, refresh = None) :
|
||||||
|
""" Définit ou retourne la durée du refresh """
|
||||||
|
|
||||||
|
if _refresh != None :
|
||||||
|
self._refresh = _refresh
|
||||||
|
return self._refresh
|
||||||
|
|
||||||
|
def corp (self, corp = None) :
|
||||||
|
""" Définit ou retourne le contenu du corp """
|
||||||
|
if corp != None :
|
||||||
|
self._corp = corp
|
||||||
|
return self._corp
|
||||||
|
|
||||||
|
def add (self, string) :
|
||||||
|
""" Ajoute une ligne au corp de la page """
|
||||||
|
self._corp += string + "\n"
|
||||||
|
|
||||||
|
def make (self) :
|
||||||
|
""" Génère la page HTML finiale """
|
||||||
|
|
||||||
|
page = ""
|
||||||
|
|
||||||
|
page += "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
|
||||||
|
page += "<html lang=\"fr\">\n"
|
||||||
|
|
||||||
|
# en-têtes de la page
|
||||||
|
#####################
|
||||||
|
|
||||||
|
page += "<head>\n"
|
||||||
|
page += " <title>%s</title>\n" % self._titre
|
||||||
|
if self._refresh :
|
||||||
|
page += " <meta http-equiv=\"refresh\" content=\"%s\">\n" % str(self._refresh)
|
||||||
|
page += " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n"
|
||||||
|
page += "</head>\n\n"
|
||||||
|
|
||||||
|
# début du corp de la page
|
||||||
|
##########################
|
||||||
|
|
||||||
|
page += "<body background=\"http://bilou/style2_left.png\" leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\" style=\"background-repeat:repeat-y\">\n"
|
||||||
|
page += "<img src=\"http://bilou/style2_top.png\" alt=\"En_tete\"><br>\n\n"
|
||||||
|
|
||||||
|
# division du titre
|
||||||
|
page += "<div id=\"Titre\" style=\"position:absolute; left:580px; top:70px; width:400px;height:34px; z-index:2\">\n"
|
||||||
|
page += "<font size=\"5\"><b>%s</b></font>\n" % self._titre
|
||||||
|
page += "</div>\n\n"
|
||||||
|
|
||||||
|
# division du sous titre
|
||||||
|
page += "<div id=\"Sous-titre\" style=\"position:absolute; left:380px; top:140px; width:500px;height:34px; z-index:2\">\n"
|
||||||
|
page += "<font size=\"4\"><u>%s</u></font>\n" % self._sous_titre
|
||||||
|
page += "</div>\n\n"
|
||||||
|
|
||||||
|
# division du contenu
|
||||||
|
page += "<div id=\"Contenu\" style=\"position:absolute; left:245px; top:190px; right:16px; z-index:1; overflow: visible; visibility: visible; background-color: #FFFFFF; layer-background-color: #FFFFFF;\">\n"
|
||||||
|
page += self._corp.encode('iso-8859-15')
|
||||||
|
page += "</div>\n\n"
|
||||||
|
|
||||||
|
# fin de la page
|
||||||
|
################
|
||||||
|
|
||||||
|
page += "</body>\n"
|
||||||
|
page += "</html>\n"
|
||||||
|
|
||||||
|
return page
|
||||||
|
|
||||||
|
def sendheaderstobrowser (self) :
|
||||||
|
""" Envoie les entetes au navigateur """
|
||||||
|
print "content-type: text/html"
|
||||||
|
print
|
||||||
|
|
||||||
|
def sendtobrowser (self) :
|
||||||
|
""" Envoie la page au navigateur """
|
||||||
|
print self.make()
|
||||||
|
|
||||||
|
def savetofile (self, fichier) :
|
||||||
|
""" Enregistre la page dans un fichier """
|
||||||
|
f = open(fichier,'w')
|
||||||
|
f.write( self.make() )
|
||||||
|
f.close()
|
91
wifiweb/session.py
Executable file
91
wifiweb/session.py
Executable file
|
@ -0,0 +1,91 @@
|
||||||
|
#! /usr/bin/env python
|
||||||
|
# -*- coding: iso-8859-15 -*-
|
||||||
|
|
||||||
|
import os, random
|
||||||
|
from time import time
|
||||||
|
|
||||||
|
class session :
|
||||||
|
|
||||||
|
def __init__ (self, sid = None) :
|
||||||
|
"""
|
||||||
|
Si sid est fournit, on regarde si la session est valide ;
|
||||||
|
on soulève un exeption si il y a un problème
|
||||||
|
|
||||||
|
Si sid n'est pas fourni un créé une nouvelle session
|
||||||
|
"""
|
||||||
|
self.save = True
|
||||||
|
|
||||||
|
if sid :
|
||||||
|
|
||||||
|
# on vérifie la validité
|
||||||
|
if not os.access( self._sess_file(sid), os.W_OK ) :
|
||||||
|
raise ValueError, 'Session inconnue'
|
||||||
|
|
||||||
|
# on exporte le sid
|
||||||
|
self.sid = sid
|
||||||
|
|
||||||
|
# on lit les données
|
||||||
|
self.data = {}
|
||||||
|
f = open(self._sess_file(sid))
|
||||||
|
for i in f.readlines() :
|
||||||
|
if not i.strip() :
|
||||||
|
continue
|
||||||
|
key = i.split(' ')[0]
|
||||||
|
value = ' '.join(i.split(' ')[1:]).strip()
|
||||||
|
self.data[key] = value
|
||||||
|
|
||||||
|
if int(self.data['perime']) < int(time()) :
|
||||||
|
self.destroy()
|
||||||
|
raise ValueError, 'Session périmée'
|
||||||
|
|
||||||
|
else :
|
||||||
|
|
||||||
|
# on créé un nouveau sid
|
||||||
|
self.data = {}
|
||||||
|
ok = False
|
||||||
|
while not ok :
|
||||||
|
sid = ''.join( [ random.choice('abcdefghijklmnopqrstuvwxyz0123456789') for i in range(0,30) ])
|
||||||
|
# est ce que la session existe ?
|
||||||
|
if not os.path.exists(self._sess_file(sid)) :
|
||||||
|
# on créé un nouveau fichier avec un timeout de 60
|
||||||
|
f = os.open(self._sess_file(sid), os.O_WRONLY + os.O_CREAT , 0600)
|
||||||
|
f
|
||||||
|
# on valide
|
||||||
|
ok = True
|
||||||
|
self.sid = sid
|
||||||
|
|
||||||
|
# on initialise les données
|
||||||
|
self.data = {'timeout' : '600'}
|
||||||
|
|
||||||
|
def _sess_file (self, sid = None) :
|
||||||
|
""" Retourne le nom du fichier correspondant à la session """
|
||||||
|
if not sid :
|
||||||
|
sid = self.sid
|
||||||
|
return '/tmp/pysession-%s' % sid
|
||||||
|
|
||||||
|
def __del__ (self) :
|
||||||
|
""" On enregsitre la session à la destruction de l'instance """
|
||||||
|
|
||||||
|
if self.save :
|
||||||
|
# epok de peromption
|
||||||
|
self.data['perime'] = str(int(time() + int(self.data['timeout'])))
|
||||||
|
|
||||||
|
f = open(self._sess_file(), 'w')
|
||||||
|
for k in self.data.keys() :
|
||||||
|
f.write( '%s %s\n' % (k,self.data[k]) )
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
def destroy(self) :
|
||||||
|
""" Supprime la session """
|
||||||
|
self.save = False
|
||||||
|
os.remove( self._sess_file() )
|
||||||
|
|
||||||
|
# on supprime toutes les vieilles sessions
|
||||||
|
for file in os.listdir('/tmp') :
|
||||||
|
if file[0:10] == 'pysession-' :
|
||||||
|
#print file[10:]
|
||||||
|
try :
|
||||||
|
s = session(file[10:])
|
||||||
|
s.save = False
|
||||||
|
except :
|
||||||
|
continue
|
10
wifiweb/utilisateurs.py
Executable file
10
wifiweb/utilisateurs.py
Executable file
|
@ -0,0 +1,10 @@
|
||||||
|
#! /usr/bin/env python
|
||||||
|
# -*- coding: iso-8859-15 -*-
|
||||||
|
|
||||||
|
# les mots de passe sont cryptés avec la commande :
|
||||||
|
# python -c "import sha ; print sha.new('***').hexdigest()"
|
||||||
|
|
||||||
|
users = {
|
||||||
|
'bilou:b6831110716ea7782b636469b31dc3a695b26386' : ['valhalla','aegir']
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue