Proyecto

General

Perfil

Descargar (11,2 KB) Estadísticas
| Rama: | Revisión:
unit demonio ;

{$mode objfpc}{$H+}

interface

uses
clcomandosroot,clservidor,globales,Classes , SysUtils , FileUtil , LResources , IdCmdTCPServer, IdCommandHandlers,IdContext, IdSync,
IdSSLOpenSSL, IdComponent,IdSSL,HTTPDefs;

type

{ TDataModule1 }

TDataModule1 = class (TDataModule )
IdCmdTCPOrdenes : TIdCmdTCPServer ;
IdServerIOHandlerSSLOpenSSL1: TIdServerIOHandlerSSLOpenSSL;
procedure archivoCommand (ASender : TIdCommand );
procedure carpetaCommand (ASender : TIdCommand );
procedure IdCmdTCPOrdenesConnect(AContext: TIdContext);
procedure IdServerIOHandlerSSLOpenSSL1GetPassword(var Password: String);
procedure iniciarCuestionCommand (ASender : TIdCommand );
procedure mensajeNoCommand (ASender : TIdCommand );
procedure mensajeCommand (ASender : TIdCommand );
private
procedure CerrarServidor();
procedure IniciarDemonio(iport:integer;ihost:string);
procedure IniciarDemonio1(iport: integer; ihost: string);
{ private declarations }
public
{ public declarations }
end ;

var
DataModule1 : TDataModule1 ;
miservidor:Tservidor;
CONST
ORD_NUMERO_INCORRECTA:string='Orden o número parámetros incorrectos';

implementation

{ TDataModule1 }
procedure TDataModule1.IniciarDemonio1(iport:integer;ihost:string);
var
aCommand:TIdCommandHandler;
begin
try
IdCmdTCPOrdenes:=TIdCmdTCPServer.Create(nil);
IdCmdTCPOrdenes.IOHandler:=IdServerIOHandlerSSLOpenSSL1;
with IdCmdTCPOrdenes do
begin
Bindings.Clear;
Bindings.Add;
Bindings.items[0].IP:=ihost;
Bindings.Items[0].Port:=iport;
DefaultPort:=iport;
MaxConnections:=0;
ListenQueue:=15;
TerminateWaitTime:=5000;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'archivo';
aCommand.OnCommand :=@archivoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='252';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'carpeta';
aCommand.OnCommand :=@carpetaCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='253';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'iniciarCuestion';
aCommand.OnCommand :=@iniciarCuestionCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='250';
aCommand.Enabled:=true;

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

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

OnConnect:=@IdCmdTCPOrdenesConnect;
if active then active:=false;
active:=true;

end;

except
//
end;
end;
procedure TDataModule1.IdCmdTCPOrdenesConnect(AContext: TIdContext);
var
ipOrden:string;
begin
if (AContext.Connection.IOHandler is TIdSSLIOHandlerSocketBase) then
TIdSSLIOHandlerSocketBase(AContext.Connection.IOHandler).PassThrough:=false;
ipOrden:=trim(AContext.Connection.Socket.Binding.PeerIP);
if (ipOrden<>miservidor.IPAdministrador) then AContext.Connection.Disconnect;
end;

procedure TDataModule1.IniciarDemonio(iport:integer;ihost:string);
var
orden, usuarioA:String;
begin
usuarioA:= GetEnvironmentVariable('USER');
orden:= 'cierraServidorAlumno ' + usuarioA;
try
IdServerIOHandlerSSLOpenSSL1:= TIdServerIOHandlerSSLOpenSSL.Create(nil);
IdServerIOHandlerSSLOpenSSL1.SSLOptions.KeyFile:='/var/lib/adtos/privkey.key';
IdServerIOHandlerSSLOpenSSL1.SSLOptions.CertFile:= '/var/lib/adtos/cacert.crt';
IdServerIOHandlerSSLOpenSSL1.SSLOptions.RootCertFile:= '/var/lib/adtos/cacertroot.pem';
IdServerIOHandlerSSLOpenSSL1.SSLOptions.Method := sslvSSLv3;
IdServerIOHandlerSSLOpenSSL1.SSLOptions.SSLVersions:=[sslvSSLv3];
IdServerIOHandlerSSLOpenSSL1.SSLOptions.Mode := sslmServer;
IdServerIOHandlerSSLOpenSSL1.SSLOptions.VerifyDepth := 0;
IdServerIOHandlerSSLOpenSSL1.SSLOptions.VerifyMode := [];
IdServerIOHandlerSSLOpenSSL1.OnGetPassword:=@IdServerIOHandlerSSLOpenSSL1GetPassword;
IniciarDemonio1(iport, ihost);
except
try
with ThrComandosRoot.create('localhost',37999,400,orden) do Resume;
sleep(5000);
IniciarDemonio1(iport, ihost);
except
IdCmdTCPOrdenes.active:=false;
writeln('Error al iniciar servidor');
Halt;
end;
end;

end;

procedure TDataModule1.CerrarServidor();
var
lista:Tlist;
i:integer;
begin
lista:=IdCmdTCPOrdenes.Contexts.LockList;
try
for i:=(lista.Count -1) downto 0 do
TIdContext(lista.items[i]).Connection.Disconnect();
finally
IdCmdTCPOrdenes.Contexts.UnlockList;
end;
sleep(IdCmdTCPOrdenes.TerminateWaitTime);
IdCmdTCPOrdenes.active:=false;
IdCmdTCPOrdenes.Bindings.Clear;
end;

procedure TDataModule1.archivoCommand (ASender : TIdCommand );
var
orden,titulo,mensaje,archivo1:string;
stream:TFileStream;
begin
if fapagando then exit;
try
if (Asender.Params.Count=2) then
begin
archivo1:=fdirpersonal+'/recibidos_administrador/'+HTTPDecode(Asender.Params[0]);
; mensaje:=Asender.Params[1];
titulo:='Aviso';
Asender.sendReply;
stream:=TfileStream.create(archivo1,fmcreate);
try
asender.Context.Connection.IOHandler.ReadStream(stream,-1,false);
if CrearArchivoMensaje('1',titulo,mensaje,'1') then
begin
orden:='/usr/bin/adtosmensajes &';
ejecutar1(orden);
end;
finally
freeandnil(stream)
end;
end
else
begin
writeln(ORD_NUMERO_INCORRECTA);
end;
except
writeln('Error al enviar archivo');
end;
end;

procedure TDataModule1.carpetaCommand (ASender : TIdCommand );
const
ORD_DESCOMPRIMIR:STRING= '(cd %s; tar xzvf adtos-carpeta.tar.gz;rm -f %s) &';
var
titulo:string;
mensaje:string;
orden,archivo:string;
stream:TFileStream;
begin
if fapagando then exit;
try
if (Asender.Params.Count=1) then
begin
archivo:=fdirpersonal+'/recibidos_administrador/adtos-carpeta.tar.gz';
mensaje:=Asender.Params[0];
titulo:='Aviso';
Asender.sendReply;
stream:=TfileStream.create(archivo,fmcreate);
try
asender.Context.Connection.IOHandler.ReadStream(stream,-1,false);
orden:=Format(ORD_DESCOMPRIMIR,[fdirrecibidos,archivo]);
ejecutar1(orden);
if CrearArchivoMensaje('1',titulo,mensaje,'1') then
begin
orden:='/usr/bin/adtosmensajes &';
ejecutar1(orden);
end;
finally
freeandnil(stream)
end;
end
else
begin
writeln(ORD_NUMERO_INCORRECTA);
end;
except
writeln('Error al recibir carpeta');
end;
end;



procedure TDataModule1.iniciarCuestionCommand (ASender : TIdCommand );
const
ORD_ALUMNO_CUESTION:string='/usr/bin/adtoscuestion &';
var
archivo1:string;
stream:TFileStream;
begin
if fapagando then exit;
try
archivo1:=fdirpersonal+'/recibidos_administrador/.cuestion.ini';
asender.sendReply;
stream:=TfileStream.create(archivo1,fmcreate);
try
asender.Context.Connection.IOHandler.ReadStream(stream,-1,false);
finally
freeandnil(stream)

end;
sleep(3000);
Ejecutar1('chmod 777 ' + archivo1);
ejecutar1(ORD_ALUMNO_CUESTION);
except
writeln(ORD_NUMERO_INCORRECTA);
end;
end;
procedure TDataModule1.IdServerIOHandlerSSLOpenSSL1GetPassword(
var Password: String);
begin
Password:='aaaa';
end;

procedure TDataModule1.mensajeCommand (ASender : TIdCommand );
var
orden,tipo,asunto, mensaje:String;
begin
try

if ASender.Params.Count =3 then
begin
tipo:=Asender.Params[0];
asunto:=Asender.Params[1];
mensaje:=Asender.Params[2];
if CrearArchivoMensaje(tipo,asunto,mensaje,'0') then
begin
orden:='/usr/bin/adtosmensajes &';
ejecutar1(orden);
end;
end
else
begin
writeln(ORD_NUMERO_INCORRECTA)
end;
except
writeln('Error al recibir mensaje el mensaje')
end;
end;


procedure TDataModule1.mensajeNoCommand (ASender : TIdCommand );
begin
try
MatarAplicacion('adtosmensajes');
except
//writeln('Error al cerrar mensaje');
end;
end;

initialization
{$I demonio.lrs}
DataModule1:=TDataModule1.create(nil);
miservidor:=Tservidor.create;
fusuario:=miservidor.UsuarioEquipoProfesor;
fmipuerto:=miservidor.Puerto;
DataModule1.IniciarDemonio(PUERTO_USUARIO,'0.0.0.0');
fdirpersonal:=miservidor.HomeUsuario;
fdirrecibidos:=miservidor.DirRecibidos;
if not directoryexists(fdirrecibidos) then
createdir(fdirrecibidos);
Ejecutar1('chmod 755 ' +fdirrecibidos +' &');
g_dirmensaje:=fdirrecibidos+'/.mensaje.ini';

Finalization
FreeAndNil(DataModule1);
FreeAndNil(miservidor);

end .

(6-6/11)