242 lines
9.8 KiB
Python
Executable file
242 lines
9.8 KiB
Python
Executable file
#! /usr/bin/env python
|
||
# -*- coding: iso-8859-15 -*-
|
||
# ##################################################################################################### #
|
||
# Machines
|
||
# ##################################################################################################### #
|
||
# Description:
|
||
# Affiche la liste des machies, les infons sur une machine et permet des modifications
|
||
# Informations:
|
||
#
|
||
# Pages:
|
||
# index:liste des machines + le reste (AJAX)
|
||
#
|
||
# ##################################################################################################### #
|
||
import cherrypy, sys, os, datetime
|
||
from time import strftime, localtime, time
|
||
# libraries crans
|
||
sys.path.append('/usr/scripts/gestion/')
|
||
|
||
if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
|
||
from ldap_crans_test import *
|
||
# print("mesmachines : unsing test ldap : env=" + cherrypy.config.configMap["global"]["server.environment"])
|
||
else:
|
||
from ldap_crans import *
|
||
# print("mesmachines : unsing prod ldap : env=" + cherrypy.config.configMap["global"]["server.environment"])
|
||
|
||
|
||
|
||
class root:
|
||
|
||
def AJAXListeMachines(self):
|
||
adh = cherrypy.session['LDAP'].search('uid=' + cherrypy.session['uid'])['adherent'][0]
|
||
machines = []
|
||
for une_machine in adh.machines():
|
||
machineInfos = {}
|
||
# nom, mac, mid, ip
|
||
machineInfos['nom'] = une_machine.nom()
|
||
machineInfos['nomCourt'] = une_machine.nom().split('.',1)[0]
|
||
machineInfos['mid'] = une_machine.id()
|
||
# type
|
||
if une_machine.objectClass == 'machineFixe':
|
||
machineInfos['type'] = 'fixe'
|
||
#machineInfos['type'] = 'Machine fixe'
|
||
else:
|
||
machineInfos['type'] = 'wifi'
|
||
#machineInfos['type'] = 'Machine wifi'
|
||
# clef ipsec
|
||
machines.append(machineInfos)
|
||
|
||
return {"machines":machines}
|
||
AJAXListeMachines.exposed = True
|
||
|
||
def AJAXMachineInfo(self, mid):
|
||
try:
|
||
machine = cherrypy.session['LDAP'].search('mid=' + mid)['machine'][0]
|
||
if machine.proprietaire().mail() != cherrypy.session['uid']:
|
||
raise Exception
|
||
# zamok -> pour tester l'affichage des ports, des alias
|
||
#machine = cherrypy.session['LDAP'].search('mid=896')['machine'][0]
|
||
machineInfos = {}
|
||
# nom, mac, mid, ip
|
||
machineInfos['nom'] = machine.nom()
|
||
machineInfos['nomCourt'] = machine.nom().split('.',1)[0]
|
||
machineInfos['mac'] = machine.mac()
|
||
machineInfos['mid'] = machine.id()
|
||
machineInfos['ip'] = machine.ip()
|
||
# type
|
||
if machine.objectClass == 'machineFixe':
|
||
machineInfos['type'] = 'fixe'
|
||
else:
|
||
machineInfos['type'] = 'wifi'
|
||
# clef ipsec
|
||
try:
|
||
machineInfos['ipsec'] = machine.ipsec()
|
||
except:
|
||
machineInfos['ipsec'] = ''
|
||
# alias
|
||
machineInfos['alias'] = machine.alias()
|
||
# blacklists
|
||
machineInfos['blacklist'] = []
|
||
for blacklist_type in machine.blacklist_all()[0].keys():
|
||
for (begin, end) in machine.blacklist_all()[0][blacklist_type]:
|
||
blacklist = {}
|
||
blacklist['begin'] = strftime('%d/%m/%Y %H:%M', localtime(int(begin)))
|
||
blacklist['end'] = strftime('%d/%m/%Y %H:%M', localtime(int(end)))
|
||
blacklist['type'] = blacklist_type
|
||
blacklist['actif'] = 1
|
||
machineInfos['blacklist'].append(blacklist)
|
||
for blacklist_type in machine.blacklist_all()[1].keys():
|
||
for (begin, end) in machine.blacklist_all()[1][blacklist_type]:
|
||
blacklist = {}
|
||
blacklist['begin'] = strftime('%d/%m/%Y %H:%M', localtime(int(begin)))
|
||
blacklist['end'] = strftime('%d/%m/%Y %H:%M', localtime(int(end)))
|
||
blacklist['type'] = blacklist_type
|
||
blacklist['actif'] = 0
|
||
machineInfos['blacklist'].append(blacklist)
|
||
# ports
|
||
machineInfos['ports'] = []
|
||
if machine.portTCPin() != []:
|
||
machineInfos['ports'].append(
|
||
{
|
||
'titre':'Ports TCP ouvert ext->machine',
|
||
'ports':machine.portTCPin()
|
||
}
|
||
)
|
||
if machine.portTCPout() != []:
|
||
machineInfos['ports'].append(
|
||
{
|
||
'titre':'Ports TCP ouvert machine->ext',
|
||
'ports':machine.portTCPout()
|
||
}
|
||
)
|
||
if machine.portUDPin() != []:
|
||
machineInfos['ports'].append(
|
||
{
|
||
'titre':'Ports UDP ouvert ext->machine',
|
||
'ports':machine.portUDPin()
|
||
}
|
||
)
|
||
if machine.portUDPout() != []:
|
||
machineInfos['ports'].append(
|
||
{
|
||
'titre':'Ports TCP ouvert machine->ext',
|
||
'ports':machine.portUDPout()
|
||
}
|
||
)
|
||
|
||
return machineInfos
|
||
except Exception, e:
|
||
return {"erreur":str(e)}
|
||
AJAXMachineInfo.exposed = True
|
||
|
||
##########################
|
||
# affichage
|
||
##########################
|
||
#
|
||
# methode qui affiche la template
|
||
#
|
||
def index(self):
|
||
return {
|
||
'template' :'machines',
|
||
'values' :{},
|
||
'stylesheets' :['machines.css'],
|
||
'scripts':['machines.js'],
|
||
}
|
||
index.exposed = True
|
||
|
||
|
||
###########################################################################
|
||
# methodes pour changer
|
||
# des valeurs
|
||
###########################################################################
|
||
#
|
||
|
||
##########################
|
||
# machine:nom
|
||
##########################
|
||
def AJAXChangerNom(self, mid, nouveauNom):
|
||
try:
|
||
adh = cherrypy.session['LDAP'].search('uid=' + cherrypy.session['uid'])['adherent'][0]
|
||
mach = cherrypy.session['LDAP'].search('mid=' + mid, 'w')['machine'][0]
|
||
# tester si c'est bien la machine de l'adherent
|
||
if mach.proprietaire().compte() != cherrypy.session['uid']:
|
||
del adh, mach
|
||
raise Exception(u"L'uid de l'adherent ne correspond mas au proprietaire de la machine.")
|
||
mach.nom(nouveauNom)
|
||
mach.save()
|
||
del mach
|
||
except ValueError, e:
|
||
raise e
|
||
#return {'error':str(e)}
|
||
cherrypy.log("Changer nom machine : %s" % nouveauNom, "MESMACHINES")
|
||
return {'message':u"Modification r<>ussie", 'mid':mid}
|
||
AJAXChangerNom.exposed = True
|
||
|
||
##########################
|
||
# machine:mac
|
||
##########################
|
||
def AJAXchangerMAC(self, mid, nouvelleMAC):
|
||
try:
|
||
adh = cherrypy.session['LDAP'].search('uid=' + cherrypy.session['uid'])['adherent'][0]
|
||
mach = cherrypy.session['LDAP'].search('mid=' + mid, 'w')['machine'][0]
|
||
# tester si c'est bien la machine de l'adherent
|
||
if mach.proprietaire().compte() != cherrypy.session['uid']:
|
||
del adh, mach
|
||
raise Exception(u"L'uid de l'adherent ne correspond mas au proprietaire de la machine.")
|
||
mach.mac(nouvelleMAC)
|
||
mach.save()
|
||
del mach
|
||
except ValueError, e:
|
||
return {'error':e.args[0]}
|
||
cherrypy.log("Change mac machine", "MESMACHINES")
|
||
return {'message':u"Modification r<>ussie", 'mid':mid}
|
||
AJAXchangerMAC.exposed = True
|
||
|
||
|
||
|
||
##########################
|
||
# machine:suppression
|
||
##########################
|
||
def AJAXSupprimerMachine(self, mid):
|
||
try:
|
||
adh = cherrypy.session['LDAP'].search('uid=' + cherrypy.session['uid'])['adherent'][0]
|
||
mach = cherrypy.session['LDAP'].search('mid=' + mid, 'w')['machine'][0]
|
||
# tester si c'est bien la machine de l'adherent
|
||
if mach.proprietaire().compte() != cherrypy.session['uid']:
|
||
del adh, mach
|
||
raise Exception(u"L'uid de l'adherent ne correspond mas au proprietaire de la machine.")
|
||
mach.delete()
|
||
except ValueError, e:
|
||
return {'error':e.args[0]}
|
||
cherrypy.log("Machine supprimee", "MACHINES")
|
||
return {'message':u"Machine supprim<69>e"}
|
||
AJAXSupprimerMachine.exposed = True
|
||
|
||
##########################
|
||
# machine:creation
|
||
##########################
|
||
def AJAXCreerMachine(self, nomNouvelleMachine, MACNouvelleMachine, typeNouvelleMachine):
|
||
adh = cherrypy.session['LDAP'].search('uid=' + cherrypy.session['uid'])['adherent'][0]
|
||
if typeNouvelleMachine=='fixe' and adh.droits() == [] and adh.machines_fixes() != []:
|
||
return {'error':'Vous avez deja une machine fixe. Vous ne pouvez ajouter que des machines WiFi.'}
|
||
try:
|
||
if typeNouvelleMachine=='wifi':
|
||
m = MachineWifi(adh)
|
||
elif typeNouvelleMachine=='fixe':
|
||
m = MachineFixe(adh)
|
||
else:
|
||
raise Exception, "type de machine inconnu : %s " % typeNouvelleMachine
|
||
m.nom(nomNouvelleMachine)
|
||
m.mac(MACNouvelleMachine)
|
||
m.ip("<automatique>")
|
||
message = m.save()
|
||
del m
|
||
except ValueError, e:
|
||
del m
|
||
return {'error':e.args[0].replace("\n","\\n")}
|
||
cherrypy.log("Nouvelle machine %s" % nomNouvelleMachine, "MACHINES")
|
||
return {'message':u"Machine enregistr<74>e avec succ<63>s"}
|
||
AJAXCreerMachine.exposed = True
|
||
|
||
|
||
|