Ménage dans la partie wifi
A priori, le dossier wifi n'a pas de dépendance (enfin, un grep "bornes" -r | grep import ne m'a rient donné). wifiweb, n'en parlons pas.
This commit is contained in:
parent
574a2aadf0
commit
21fc3e42d6
11 changed files with 1 additions and 0 deletions
26
archive/wifi/wifiweb/adduser.py
Executable file
26
archive/wifi/wifiweb/adduser.py
Executable file
|
@ -0,0 +1,26 @@
|
|||
#! /usr/bin/env python
|
||||
# -*- coding: iso-8859-15 -*-
|
||||
|
||||
# ce script ajoute une ligne à la fin du fichier des utilisateurs
|
||||
# avec le mot de passe déja hashé, il faut ensuite l'integrer au
|
||||
# dictionnaire
|
||||
|
||||
import sha, sys
|
||||
from getpass import getpass
|
||||
|
||||
sys.stdout.write('Nom d\'utilisateur (au format prenom.nom) : ')
|
||||
while True :
|
||||
user = sys.stdin.readline().strip()
|
||||
if user :
|
||||
break
|
||||
|
||||
while True :
|
||||
pass1 = getpass('Mot de passe : ')
|
||||
pass2 = getpass('Retappez le mot de passe : ')
|
||||
if pass1 == pass2 :
|
||||
break
|
||||
print 'Les deux mot de passe ne correpondent pas !'
|
||||
|
||||
f = open('/usr/scripts/wifiweb/utilisateurs.py','a+')
|
||||
f.write('# %s:%s\n' % (user , sha.new(pass1).hexdigest()) )
|
||||
f.close()
|
245
archive/wifi/wifiweb/bornes.py
Normal file
245
archive/wifi/wifiweb/bornes.py
Normal file
|
@ -0,0 +1,245 @@
|
|||
#! /usr/bin/env python
|
||||
# -*- coding: iso-8859-15 -*-
|
||||
|
||||
import cgi, sys, os, sha, time, re
|
||||
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')
|
||||
|
||||
# url
|
||||
try :
|
||||
url = os.environ['HTTP_REFERER']
|
||||
if '?' in url :
|
||||
url = url.split('?')[0]
|
||||
except :
|
||||
url = ''
|
||||
|
||||
# création de la session
|
||||
try :
|
||||
sess = session(sid)
|
||||
except :
|
||||
sess = session()
|
||||
sid = sess.sid
|
||||
|
||||
######################################################
|
||||
# quelques fontions html
|
||||
|
||||
def message (texte, color='black'):
|
||||
return '<font color="%s">%s</font><br>' % (color,texte)
|
||||
|
||||
def message_ok (texte) :
|
||||
return message (texte,'green')
|
||||
|
||||
def message_nok (texte) :
|
||||
return message (texte,'red')
|
||||
|
||||
######################################################
|
||||
# quelques variables html
|
||||
|
||||
def bouton (url='', sid='', action='', caption='') :
|
||||
txt = ''
|
||||
if url :
|
||||
txt += '<form action="%s" method="POST">\n' % url
|
||||
else :
|
||||
txt += '<form method="POST">\n'
|
||||
if sid :
|
||||
txt += '<input type="hidden" name="sid" value="%s">\n' % sid
|
||||
if action :
|
||||
txt += '<input type="hidden" name="action" value="%s">' % action
|
||||
txt += '<input type="submit" value="%s">\n</form>' % caption
|
||||
return txt
|
||||
|
||||
bouton_quitter = bouton (url, sid, 'logout', 'Quitter')
|
||||
bouton_menu = bouton (url, sid, 'index', 'Menu principal')
|
||||
bouton_retour = bouton ('javascript:history.go(-1)', '', '', 'Retour')
|
||||
|
||||
######################################################
|
||||
# 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 = 'index'
|
||||
|
||||
if not sess.data.has_key('user') or not sess.data.has_key('password') :
|
||||
action = 'auth'
|
||||
|
||||
if action != 'auth' :
|
||||
|
||||
if users.has_key( sess.data['user'] + ':' + sess.data['password'] ) :
|
||||
# droits de l'utilisateur
|
||||
#########################
|
||||
|
||||
# construction de la liste des bornes modifiables
|
||||
bornes_modifiables = []
|
||||
if len(users[ sess.data['user'] + ':' + sess.data['password'] ]) :
|
||||
for lieu in users[ sess.data['user'] + ':' + sess.data['password'] ] :
|
||||
for borne in db.search('canal=*&info=<lieu>%s*' % lieu )['machine'] :
|
||||
nom = borne.nom().encode('iso-8859-15').split('.')[0]
|
||||
if nom not in bornes_modifiables :
|
||||
bornes_modifiables.append(nom)
|
||||
|
||||
else :
|
||||
# erreur d'authentification
|
||||
###########################
|
||||
|
||||
log(sess.data['user'] + ' erreur d\'authentification' )
|
||||
page.add('<font color="red">Erreur d\'authentification !</font><br><br>')
|
||||
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>Utilisateur : </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 )
|
||||
action = ''
|
||||
|
||||
######################################################
|
||||
# désactivation d'un borne
|
||||
|
||||
if action == 'desactive' and bornes_modifiables :
|
||||
if form.getvalue('borne','') in bornes_modifiables :
|
||||
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()
|
||||
action = 'liste-bornes'
|
||||
else :
|
||||
log(sess.data['user'] + ' a tenté de désactiver %s' % form.getvalue('borne','') )
|
||||
action = 'erreur-droits'
|
||||
|
||||
######################################################
|
||||
# activation d'un borne
|
||||
|
||||
if action == 'active' and bornes_modifiables :
|
||||
if form.getvalue('borne','') in bornes_modifiables :
|
||||
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()
|
||||
action = 'liste-bornes'
|
||||
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 = 'erreur-droits'
|
||||
|
||||
######################################################
|
||||
# page : liste des bornes
|
||||
|
||||
if action == 'liste-bornes' and bornes_modifiables :
|
||||
page.sous_titre('Liste des bornes')
|
||||
|
||||
for b in bornes_modifiables :
|
||||
try :
|
||||
borne = db.search('host=%s.wifi.crans.org' % b)['machine'][0]
|
||||
except :
|
||||
log('borne non existante : %s' % b)
|
||||
continue
|
||||
|
||||
# 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().encode('iso-8859-15'))
|
||||
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().encode('iso-8859-15'))
|
||||
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( filter(lambda x : re.match("^\<.*\>", x) == None, borne.info() ) ).encode('iso-8859-15'))
|
||||
page.add('</td></tr></table>')
|
||||
|
||||
# bouton de validation
|
||||
if '-' in borne.puissance() :
|
||||
page.add('<input type="submit" value="Réactiver %s">' % b)
|
||||
else :
|
||||
page.add('<input type="submit" value="Désactiver %s">' % b)
|
||||
|
||||
# fin du formulaire
|
||||
page.add('</form>')
|
||||
|
||||
# menu de fin de page
|
||||
page.add( "<center><table><tr><td>%s</td><td>%s</td><td>%s</td></tr></center>" % ( bouton(url,sid,'liste-bornes','Actualiser'), bouton_menu, bouton_quitter ) )
|
||||
action = ''
|
||||
|
||||
######################################################
|
||||
# page : erreur de droits
|
||||
|
||||
# si on a encore quelque chose à afficher c'est qu'il y une erreur
|
||||
# on colle ca sur la faute des droits
|
||||
if action not in [ 'index', 'logout', '' ] :
|
||||
page.add("<font color=\"red\">Vous n'avez pas le droits d'effectuer cette opération</font><br><br>\n")
|
||||
page.add('<center><a href="javascript:history.go(-1)">Retour</a></center><br>')
|
||||
action = ''
|
||||
|
||||
######################################################
|
||||
# page : index
|
||||
|
||||
if action == 'index' :
|
||||
# menu principal
|
||||
page.sous_titre("Menu principal")
|
||||
if bornes_modifiables :
|
||||
page.add('<a href="?sid=%s&action=liste-bornes">Activation/désactivation d\'une borne</a><br>' % sid )
|
||||
# menu de bas de page
|
||||
page.add("<center>%s</center>" % bouton_quitter )
|
||||
|
||||
######################################################
|
||||
# page : logout
|
||||
|
||||
if action == 'logout' :
|
||||
log(sess.data['user'] + ' s\'est déconnecté' )
|
||||
page.sous_titre('Session fermée...')
|
||||
sess.destroy()
|
||||
|
||||
######################################################
|
||||
# fin du script
|
||||
page.sendtobrowser()
|
101
archive/wifi/wifiweb/html.py
Normal file
101
archive/wifi/wifiweb/html.py
Normal 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://www.crans.org/styles/style2_left.png\" leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\" style=\"background-repeat:repeat-y\">\n"
|
||||
page += "<img src=\"http://www.crans.org/styles/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
|
||||
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
archive/wifi/wifiweb/session.py
Normal file
91
archive/wifi/wifiweb/session.py
Normal 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
|
24
archive/wifi/wifiweb/utilisateurs.py
Normal file
24
archive/wifi/wifiweb/utilisateurs.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
#! /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()"
|
||||
|
||||
# Liste des utilisateurs
|
||||
########################
|
||||
# premier champ : autorisation d'inscrire des machines temporaires True|False
|
||||
# champs suivants : préfixe des lieux
|
||||
|
||||
users = {
|
||||
'bilou:b6831110716ea7782b636469b31dc3a695b26386' : ['ens'],
|
||||
'vince||:7bc07c05eebf6726b48f557fcb60b434364034cd' : ['ens'],
|
||||
'xabi:4f1da4cacfd69622c2123d83007a92f9e3de9722' : ['ens'],
|
||||
# Jean-Marc Roussel, pour le laboratoire d'automtique du dgm
|
||||
'labo_auto:920eb1d6bc608a3e8d3a20ccc49bee6c849ccb8b': ['ens_vinci_autom'],
|
||||
# Cécile Durieu
|
||||
'durieu:897712550705c3e02e795e3eea579b0e40c90903' : ['ens_alembert'],
|
||||
# farid.benboudjema, pour le DGC
|
||||
'farid:c710e92d2d15f292f2d5f8c5901fcf91a778590a' : ['ens_vinci_dgc']
|
||||
}
|
||||
|
||||
#
|
Loading…
Add table
Add a link
Reference in a new issue