Proyecto

General

Perfil

Descargar (20,7 KB) Estadísticas
| Rama: | Revisión:
23a384b9 Elisa
unit demonio ;

{$mode objfpc}{$H+}

interface

uses
unix,clservidor,globales,Classes , SysUtils , FileUtil , LResources, IdCmdTCPServer, IdCommandHandlers,IniFiles;

type

{ TDataModule1 }

TDataModule1 = class (TDataModule )
IdCmdTCPServer1:TIdCmdTCPServer;
procedure activarAccesoInternetCommand(ASender: TIdCommand);
procedure apagarEquipoCommand (ASender : TIdCommand );
procedure DataModuleCreate(Sender: TObject);
procedure desactivarAccesoInternetCommand(ASender: TIdCommand);
procedure hallarHostMacCommand(ASender: TIdCommand);
procedure iniciarCanonCommand(ASender: TIdCommand);
procedure terminarCanonCommand(ASender: TIdCommand);
procedure videoVLCCommand(ASender: TIdCommand);
procedure videoVLCNoCommand(ASender: TIdCommand);
private
function CrearArchivoMensaje(TipoMensaje, AsuntoMensaje,
ContenidoMensaje: string; ArchivoMensaje: String='0'): boolean;
procedure Ejecutar1 (comando : string );
function hallaXaut(): String;
//procedure IdCmdTCPServer1Connect(AContext: TIdContext);
procedure IniciarDemonio(iport: integer; ihost: string);
procedure mensajeCommand(ASender: TIdCommand);
procedure mensajeNoCommand(ASender: TIdCommand);
procedure mostrarCommand(ASender: TIdCommand);
procedure mostrarNoCommand(ASender: TIdCommand);
procedure ratonCommand(ASender: TIdCommand);
procedure ratonNoCommand(ASender: TIdCommand);
{ private declarations }
public
{ public declarations }
end ;

var
DataModule1 : TDataModule1 ;

implementation

{ TDataModule1 }
procedure TDataModule1.IniciarDemonio(iport:integer;ihost:string);
var
aCommand:TIdCommandHandler;
begin
Ejecutar1('fuser -k -n tcp 36999 >/dev/null 2>&1');
sleep(1000);

IdCmdTCPServer1:=TIdCmdTCPServer.Create(nil);
with IdCmdTCPServer1 do
begin
Bindings.Clear;
Bindings.Add;
Bindings.items[0].IP:=ihost;
Bindings.Items[0].Port:=iport;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'apagarEquipo';
aCommand.OnCommand := @apagarEquipoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='302';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'hallarHostMac';
aCommand.OnCommand := @hallarHostMacCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='308';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'iniciarCanon';
aCommand.OnCommand := @iniciarCanonCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='313';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'terminarCanon';
aCommand.OnCommand := @terminarCanonCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='314';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'activarAccesoInternet';
aCommand.OnCommand := @activarAccesoInternetCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='315';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'desactivarAccesoInternet';
aCommand.OnCommand := @desactivarAccesoInternetCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='316';
aCommand.Enabled:=true;


aCommand:=CommandHandlers.Add;
aCommand.Command := 'videoVLC';
aCommand.OnCommand := @videoVLCCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='311';
aCommand.Enabled:=true;



aCommand:=CommandHandlers.Add;
aCommand.Command := 'videoVLCNo';
aCommand.OnCommand := @videoVLCNoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='312';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'mostrar';
aCommand.OnCommand := @mostrarCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='317';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'mostrarNo';
aCommand.OnCommand := @mostrarNoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='318';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'raton';
aCommand.OnCommand := @ratonCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='319';
aCommand.Enabled:=true;


aCommand:=CommandHandlers.Add;
aCommand.Command := 'ratonNo';
aCommand.OnCommand := @ratonNoCommand;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='320';
aCommand.Enabled:=true;
aCommand.Disconnect:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'mensaje';
aCommand.OnCommand := @mensajeCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:='$';
aCommand.ParamDelimiter:='$';
aCommand.NormalReply.Code:='321';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'mensajeNo';
aCommand.OnCommand := @mensajeNoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='322';
aCommand.Enabled:=true;



DefaultPort:=iport;
MaxConnections:=0;
ListenQueue:=15;
TerminateWaitTime:=5000;
active:=true;

end;
end;
procedure TDataModule1.apagarEquipoCommand (ASender : TIdCommand );
begin
try
miservidor.apagarEquipo;
Asender.Reply.SetReply(302,'Apagando equipo');
ASender.SendReply;
if IdCmdTCPServer1.Active then IdCmdTCPServer1.Active:=false;
halt;
except
// writeln('Error al apagar equipo');
end;
end;

procedure TDataModule1.activarAccesoInternetCommand(ASender: TIdCommand);
const
ORDEN_ACTIVAR0:string='iptables-save| grep "owner %s"|head -1';
ORDEN_ACTIVAR1:string='iptables %s >/dev/null 2>&1';
var
datosorden:string;
usuario:string;
begin
try
if Asender.Params.Count=1 then
begin
usuario:= trim(Asender.Params[0]);
datosorden:=Format(ORDEN_ACTIVAR0,[usuario]);
datosorden:=miservidor.HallarDato(datosorden);
if datosorden ='' then exit;
datosorden:=StringReplace(datosorden, '-A ', '-D ',[rfReplaceAll]);
datosorden:= Format(ORDEN_ACTIVAR1,[datosorden]);
Ejecutar1(datosorden);
Asender.Reply.SetReply(304,'el usuario '+ usuario + ' tiene ya la salida internet activada');
ASender.SendReply;
end
else
begin
//writeln('activar internet Orden o número parametros incorrectos');
end;
except
//writeln('Error al activar Internet');
end;
end;
procedure TDataModule1.desactivarAccesoInternetCommand(ASender: TIdCommand);
const
ORDEN_DESACTIVAR:string='iptables -A OUTPUT -p tcp -m multiport --dport 21,23,80,143,194,443,445 -m owner --uid-owner %s -j DROP >/dev/null 2>&1';
var
ordenDesactivar,usuario:string;
begin
try
if Asender.Params.Count=1 then
begin
usuario:= trim(Asender.Params[0]);
ordenDesactivar:=Format(ORDEN_DESACTIVAR,[usuario]);
Ejecutar1(ordenDesactivar);
Asender.Reply.SetReply(303,'Alumno '+ usuario +' tiene la salida a internet desactivada');
ASender.SendReply;
end
else
begin
//writeln('desactivar internet Orden o número parametros incorrectos');
end;
except
//writeln('Error al desactivar Internet');
end;
end;

procedure TDataModule1.DataModuleCreate(Sender: TObject);
const
ORDEN_CERRAR_PUERTO:string='fuser -k -n tcp 36999';
begin
Ejecutar1(ORDEN_CERRAR_PUERTO);
sleep(1000);
IniciarDemonio(36999,'0.0.0.0');
end;

procedure TDataModule1.Ejecutar1 (comando:string);
begin;
unix.fpSystem(comando);
end;

procedure TDataModule1.hallarHostMacCommand(ASender: TIdCommand);
var
ip,mac,hostmac:string;
begin
try
ip:=miservidor.ObtenerHost();
mac:=miservidor.ObtenerMiMAC();
hostmac:=ip+'#'+mac;
Asender.Reply.SetReply(308,hostmac);
ASender.SendReply;
except
//writeln('Error al detectar el mac');
end;
end;

procedure TDataModule1.iniciarCanonCommand(ASender: TIdCommand);
const
ORD_CIERRA_LDM:String='killall -9 ldm;killall -9 /usr/lib/ldm/ldmgtkgreet';
ORD_INICIA_USUARIO:string='(export LDM_AUTOLOGIN=true; export LDM_USERNAME=%s;export LDM_SERVER=192.168.0.254;export LDM_PASSWORD=%s; ldm vt7 :7 -depth 16) &';
var
usuario,contrasena,miorden:string;
begin
try
if ASender.Params.Count = 2 then
begin
usuario:=ASender.Params[0];
contrasena:=ASender.Params[1];
Ejecutar1(ORD_CIERRA_LDM);
sleep(2000);
miorden:=Format(ORD_INICIA_USUARIO,[usuario,contrasena]);
Ejecutar1(miorden);
end
else
begin
//writeln('Numero de parámetros incorrectos')
end;
except
//writeln('Error al iniciar cañón')
end;
end;

procedure TDataModule1.terminarCanonCommand(ASender: TIdCommand);
const
ORD_TERMINAR_CANON:string='(killall -9 ldm;export LDM_AUTOLOGIN=false;export LDM_SERVER=192.168.0.254;ldm vt7 :7 -depth 16) &';
begin
try
if ASender.Params.Count = 0 then
begin
Ejecutar1(ORD_TERMINAR_CANON);
end
else
begin
//writeln('Numero de parámetros incorrectos')
end;
except
//writeln('Error al cerrar cañón')
end;
end;

procedure TDataModule1.mensajeCommand (ASender : TIdCommand );
const
ORDEN_MENSAJE:string='su -c " export DISPLAY=:7.0 XAUTHORITY=%s;mensaje-ltsp" nobody &';
var
xauthority,orden,tipo,asunto, mensaje:String;
begin
try
if ASender.Params.Count =3 then
begin
xauthority:= hallaXaut();
if xauthority='' then exit;
tipo:=Asender.Params[0];
asunto:= Asender.Params[1];
mensaje:=Asender.Params[2];
if CrearArchivoMensaje(tipo,asunto,mensaje,'0') then
begin
orden:=Format(ORDEN_MENSAJE,[xauthority]);
ejecutar1(orden);
end;
end
else
begin
writeln('orden o número de parámetros incorrecto')
end;
except
writeln('Error al enviar el mensaje')
end;
end;


procedure TDataModule1.mensajeNoCommand (ASender : TIdCommand );
begin
try
Ejecutar1('killall -9 mensaje-ltsp >/dev/null 2>&1');
except
writeln('Error al cerrar mensaje');
end;
end;
function TDataModule1.CrearArchivoMensaje(TipoMensaje,AsuntoMensaje,ContenidoMensaje:string; ArchivoMensaje:String='0'):boolean;
const
NOMBRE_ALUMNO:string='aulalinex-alumno'; var
Mifichero:TMemIniFile;
begin
result:=true;
if fileexists(g_dirmensaje) then DeleteFile(g_dirmensaje);
try
Mifichero := TMemIniFile.Create(g_dirmensaje);
with Mifichero do
begin
WriteString('MENSAJE','mensaje',ContenidoMensaje);
WriteString('MENSAJE','tipo', TipoMensaje);
WriteString('MENSAJE','titulo',AsuntoMensaje);
WriteString('MENSAJE','tiempo','30');
WriteString('MENSAJE','usuario',NOMBRE_ALUMNO);
WriteString('MENSAJE','archivo',ArchivoMensaje);
UpdateFile;
Free;
end;
sleep(2000);
Ejecutar1('chmod 777 ' + g_dirmensaje);
except
begin
result:=false;
writeLn('Error al crear archivo');
end;
end;
end;
procedure TDataModule1.ratonCommand (ASender : TIdCommand );
const
ORDEN_CREAR_CONTRA:STRING='aulaconlinex-ltps %s %s/.seral456512';
ORDEN_MOSTRAR:string='su -c " export DISPLAY=:7.0 XAUTHORITY=%s;xvnc4viewer -UseLocalCursor=0 -LowColourLevel=1 -MenuKey F15 -Shared -Fullscreen -passwd %s 192.168.0.254:5899" nobody &';
ORDEN_MATAR_VNC='killall xvnc4viewer >/dev/null 2>&1';
var
xauthority,archivocontra,orden,contra:string;
begin
try
if ASender.Params.count=1 then
begin
xauthority:= hallaXaut();
if xauthority='' then exit;
ejecutar1(ORDEN_MATAR_VNC);
archivocontra:='/tmp/.seral456512';
contra:=Asender.Params[0];
orden:=Format(ORDEN_CREAR_CONTRA,[contra,'/tmp']);
ejecutar1(orden);
sleep(100);
ejecutar1('chmod 777 /tmp/.seral456512');
orden:=Format(ORDEN_MOSTRAR,[xauthority, archivocontra]);
ejecutar1(orden);
end
else
begin
writeln('Error al mostrar');
end;
except
writeln('orden o número de parámetros incorrecto');
end;
end;

procedure TDataModule1.ratonNoCommand (ASender : TIdCommand );
const
ORDEN_CREAR_CONTRA:STRING='aulaconlinex-ltps %s %s/.seral456512';
ORDEN_MOSTRAR:string='su -c " export DISPLAY=:7.0 XAUTHORITY=%s;xvnc4viewer -UseLocalCursor=0 -ViewOnly -LowColourLevel=1 -MenuKey F15 -Shared -Fullscreen -passwd %s 192.168.0.254:5899" nobody &';
ORDEN_MATAR_VNC='killall xvnc4viewer >/dev/null 2>&1';
var
xauthority,archivocontra,orden,contra:string;
begin
try
if ASender.Params.count=1 then
begin
xauthority:= hallaXaut();
if xauthority='' then exit;
ejecutar1(ORDEN_MATAR_VNC);
archivocontra:='/tmp/.seral456512';
contra:=Asender.Params[0];
orden:=Format(ORDEN_CREAR_CONTRA,[contra,'/tmp']);
ejecutar1(orden);
sleep(100);
ejecutar1('chmod 777 /tmp/.seral456512');
orden:=Format(ORDEN_MOSTRAR,[xauthority, archivocontra]);
ejecutar1(orden);
end
else
begin
writeln('Error al mostrar');
end;
except
writeln('orden o número de parámetros incorrecto');
end;
end;
function TDataModule1.hallaXaut ():String;
const
ORD_HALLA_XAUTHORITY:String='ls /var/run/ | grep ldm-xauth';
var
directorio, xauthority:String;
begin
xauthority:='';
xauthority:=Trim(miservidor.HallarDato(ORD_HALLA_XAUTHORITY));
if (xauthority <>'') then
begin
directorio:='/var/run/'+xauthority;
if DirectoryExists(directorio) then Ejecutar1('chmod 777 -R ' + directorio);;
xauthority:='/var/run/'+xauthority+'/Xauthority';

end;
Result:=xauthority;
end;

procedure TDataModule1.mostrarCommand (ASender : TIdCommand );
const
ORDEN_CREAR_CONTRA:STRING='aulaconlinex-ltsp %s %s/.seral456512';
ORDEN_MOSTRAR:string='su -c " export DISPLAY=:7.0 XAUTHORITY=%s;xvnc4viewer -ViewOnly -UseLocalCursor=0 -LowColourLevel=1 -MenuKey F15 -Shared -Fullscreen -passwd %s 192.168.0.254:5899" nobody &';
var
xauthority,archivocontra,orden,contra:string;
begin
try
if ASender.Params.count=1 then
begin
xauthority:= hallaXaut();
if xauthority='' then exit;
archivocontra:='/tmp/.seral456512';
contra:=Asender.Params[0];
orden:=Format(ORDEN_CREAR_CONTRA,[contra,'/tmp']);
ejecutar1(orden);
sleep(100);
ejecutar1('chmod 777 /tmp/.seral456512');
orden:=Format(ORDEN_MOSTRAR,[xauthority, archivocontra]);
ejecutar1(orden);
end
else
begin
writeln('numero de parametros incorrecto');
end;
except
writeln('Error al mostrar el profesor');
end;
end;

procedure TDataModule1.mostrarNoCommand (ASender : TIdCommand );
begin
try
Ejecutar1('killall -9 xvnc4viewer >/dev/null 2>&1');
except
writeln('Error al detener mostrar alumno');
end;
end;

procedure TDataModule1.videoVLCCommand(ASender: TIdCommand);
const
ORD_VIDEO:String='su -c "export DISPLAY=:7.0 XAUTHORITY=%s; vlc --video-on-top --skip-frames --no-audio -f rtp://@%s:%s" nobody &';
var
xauthority,pvvlc,ipgw,ipvlc,miorden:string;
begin
try
if ASender.Params.Count = 3 then
begin
xauthority:= hallaXaut();
if xauthority='' then exit;
ipvlc:=ASender.Params[0];
ipgw:=ASender.Params[1];
pvvlc:=ASender.Params[2];
miorden:=Format(ORD_VIDEO,[xauthority,ipvlc,pvvlc]);
Ejecutar1(miorden);
end
else
begin
//writeln('videoVLC Numero de parámetros incorrectos')
end;
except
//writeln('Error al iniciar vlc')
end;
end;

procedure TDataModule1.videoVLCNoCommand(ASender: TIdCommand);
const
ORD_VLC_NO:string='killall -9 vlc >/dev/null 2>&1';
begin
try
Ejecutar1(ORD_VLC_NO);
except
// writeln('Error al iniciar vlc')
end;
end;

initialization
{$I demonio.lrs}
miservidor:=Tservidor.create();
DataModule1:=TDataModule1.create(nil);
miservidor.EnviarDatosEquipo();
Finalization
FreeAndNil(DataModule1);
FreeAndNil(miservidor);;

end .