1 | // ********************************************************************************************************
|
---|
2 | // Cliente: ogAdmWinClient
|
---|
3 | // Autor: José Manuel Alonso (E.T.S.I.I.) Universidad de Sevilla
|
---|
4 | // Fecha Creación: Febrero-2012
|
---|
5 | // Fecha Última modificación: Febrero-2012
|
---|
6 | // Nombre del fichero: ogAdmWinClient.cpp
|
---|
7 | // Descripción :Este fichero implementa el cliente windows del sistema
|
---|
8 | // ********************************************************************************************************
|
---|
9 | #include "ogAdmWinClient.h"
|
---|
10 | #include "ogAdmLib.c"
|
---|
11 | #include "registrow.c"
|
---|
12 | //________________________________________________________________________________________________________
|
---|
13 | // Función: tomaConfiguracion
|
---|
14 | //
|
---|
15 | // Descripción:
|
---|
16 | // Lee el fichero de configuración del servicio
|
---|
17 | // Parámetros:
|
---|
18 | // filecfg : Ruta completa al fichero de configuración
|
---|
19 | // Devuelve:
|
---|
20 | // TRUE: Si el proceso es correcto
|
---|
21 | // FALSE: En caso de ocurrir algún error
|
---|
22 | //________________________________________________________________________________________________________
|
---|
23 | BOOLEAN tomaConfiguracion(char* filecfg)
|
---|
24 | {
|
---|
25 | char modulo[] = "tomaConfiguracion()";
|
---|
26 |
|
---|
27 | if (filecfg == NULL || strlen(filecfg) == 0) {
|
---|
28 | errorLog(modulo, 1, FALSE); // Fichero de configuración del cliente vacío
|
---|
29 | return (FALSE);
|
---|
30 | }
|
---|
31 | FILE *fcfg;
|
---|
32 | int lSize;
|
---|
33 | char * buffer, *lineas[MAXPRM], *dualparametro[2];
|
---|
34 | int i, numlin, resul;
|
---|
35 |
|
---|
36 | fcfg = fopen(filecfg, "rt");
|
---|
37 | if (fcfg == NULL) {
|
---|
38 | errorLog(modulo, 2, FALSE); // No existe fichero de configuración del cliente
|
---|
39 | return (FALSE);
|
---|
40 | }
|
---|
41 |
|
---|
42 | fseek(fcfg, 0, SEEK_END);
|
---|
43 | lSize = ftell(fcfg); // Obtiene tamaño del fichero.
|
---|
44 | rewind(fcfg);
|
---|
45 | buffer = (char*) reservaMemoria(lSize+1); // Toma memoria para el buffer de lectura.
|
---|
46 | if (buffer == NULL) { // No hay memoria suficiente para el buffer
|
---|
47 | errorLog(modulo, 3, FALSE);
|
---|
48 | return (FALSE);
|
---|
49 | }
|
---|
50 | lSize=fread(buffer, 1, lSize, fcfg); // Lee contenido del fichero
|
---|
51 | buffer[lSize]=CHARNULL;
|
---|
52 | fclose(fcfg);
|
---|
53 |
|
---|
54 | /* Inicializar variables globales */
|
---|
55 | servidoradm[0]=CHARNULL;
|
---|
56 | puerto[0] = CHARNULL;
|
---|
57 | IPlocal[0]=CHARNULL;
|
---|
58 |
|
---|
59 | numlin = splitCadena(lineas, buffer, '\n');
|
---|
60 | for (i = 0; i < numlin; i++){
|
---|
61 | splitCadena(dualparametro, lineas[i], '=');
|
---|
62 |
|
---|
63 | resul = strcmp(StrToUpper(dualparametro[0]), "SERVIDORADM");
|
---|
64 | if (resul == 0)
|
---|
65 | strcpy(servidoradm, dualparametro[1]);
|
---|
66 |
|
---|
67 | resul = strcmp(StrToUpper(dualparametro[0]), "PUERTO");
|
---|
68 | if (resul == 0)
|
---|
69 | strcpy(puerto, dualparametro[1]);
|
---|
70 |
|
---|
71 | resul = strcmp(StrToUpper(dualparametro[0]), "IPLOCAL");
|
---|
72 | if (resul == 0)
|
---|
73 | strcpy(IPlocal, dualparametro[1]);
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (servidoradm[0] == CHARNULL) {
|
---|
77 | errorLog(modulo,4, FALSE); // Falta parámetro SERVIDORADM
|
---|
78 | return (FALSE);
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (puerto[0] == CHARNULL) {
|
---|
82 | errorLog(modulo,5, FALSE); // Falta parámetro PUERTO
|
---|
83 | return (FALSE);
|
---|
84 | }
|
---|
85 | if (IPlocal[0] == CHARNULL) {
|
---|
86 | errorLog(modulo, 92, FALSE); // Falta parámetro IPLOCAL
|
---|
87 | return (FALSE);
|
---|
88 | }
|
---|
89 | return (TRUE);
|
---|
90 | }
|
---|
91 | //______________________________________________________________________________________________________
|
---|
92 | // Función: InclusionClienteWinLnx
|
---|
93 | // Descripción:
|
---|
94 | // Abre una sesión en el servidor de administración y registra al cliente en el sistema
|
---|
95 | // Parámetros:
|
---|
96 | // Ninguno
|
---|
97 | // Devuelve:
|
---|
98 | // TRUE: Si el proceso es correcto
|
---|
99 | // FALSE: En caso de ocurrir algún error
|
---|
100 | //______________________________________________________________________________________________________
|
---|
101 | BOOLEAN InclusionClienteWinLnx(TRAMA* ptrTrama)
|
---|
102 | {
|
---|
103 | int lon;
|
---|
104 | SOCKET socket_c;
|
---|
105 | char modulo[] = "InclusionClienteWinLnx()";
|
---|
106 |
|
---|
107 | initParametros(ptrTrama,0);
|
---|
108 | lon=sprintf(ptrTrama->parametros,"nfn=InclusionClienteWinLnx\r"); // Nombre de la función a ejecutar en el servidor
|
---|
109 |
|
---|
110 | if(!enviaMensajeServidor(&socket_c,ptrTrama,MSG_PETICION)){
|
---|
111 | errorLog(modulo,37,FALSE);
|
---|
112 | return(FALSE);
|
---|
113 | }
|
---|
114 | ptrTrama=recibeMensaje(&socket_c);
|
---|
115 | if(!ptrTrama){
|
---|
116 | errorLog(modulo,22,FALSE);
|
---|
117 | return(FALSE);
|
---|
118 | }
|
---|
119 | closesocket(socket_c);
|
---|
120 |
|
---|
121 | if(!gestionaTrama(ptrTrama)){ // Análisis de la trama
|
---|
122 | errorLog(modulo,39,FALSE);
|
---|
123 | return(FALSE);
|
---|
124 | }
|
---|
125 |
|
---|
126 | return(TRUE);
|
---|
127 | }
|
---|
128 | //______________________________________________________________________________________________________
|
---|
129 | // Función: RESPUESTA_InclusionClienteWinLnx
|
---|
130 | //
|
---|
131 | // Descripción:
|
---|
132 | // Respuesta del servidor de administración a la petición de inicio
|
---|
133 | // enviando los datos identificativos del cliente
|
---|
134 | // Parámetros:
|
---|
135 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros
|
---|
136 | // Devuelve:
|
---|
137 | // TRUE: Si el proceso es correcto
|
---|
138 | // FALSE: En caso de ocurrir algún error
|
---|
139 | //______________________________________________________________________________________________________
|
---|
140 | BOOLEAN RESPUESTA_InclusionClienteWinLnx(TRAMA* ptrTrama)
|
---|
141 | {
|
---|
142 | char* res;
|
---|
143 | char modulo[] = "RESPUESTA_InclusionClienteWinLnx()";
|
---|
144 | int err;
|
---|
145 |
|
---|
146 | res=copiaParametro("res",ptrTrama); // Resultado del proceso de inclusión
|
---|
147 | err=(int)atoi(res); // Código de error devuelto por el servidor
|
---|
148 | if(err>0){ // Error en el proceso de inclusión
|
---|
149 | errorLog(modulo,41,FALSE);
|
---|
150 | errorLog(modulo,err,FALSE);
|
---|
151 | return (FALSE);
|
---|
152 | }
|
---|
153 | strcpy(idordenador,copiaParametro("ido",ptrTrama)); // Identificador del ordenador
|
---|
154 | strcpy(nombreordenador,copiaParametro("npc",ptrTrama)); // Nombre del ordenador
|
---|
155 |
|
---|
156 | if(idordenador==NULL || nombreordenador==NULL){
|
---|
157 | errorLog(modulo,40,FALSE);
|
---|
158 | return (FALSE);
|
---|
159 | }
|
---|
160 | return(TRUE);
|
---|
161 | }
|
---|
162 | //______________________________________________________________________________________________________
|
---|
163 | // Función: ProcesaComandos
|
---|
164 | //
|
---|
165 | // Descripción:
|
---|
166 | // Espera comando desde el Servidor de Administración para ejecutarlos
|
---|
167 | // Parámetros:
|
---|
168 | // Ninguno
|
---|
169 | // Devuelve:
|
---|
170 | // TRUE: Si el proceso es correcto
|
---|
171 | // FALSE: En caso de ocurrir algún error
|
---|
172 | // ________________________________________________________________________________________________________
|
---|
173 | void procesaComandos(TRAMA* ptrTrama)
|
---|
174 | {
|
---|
175 | int lon;
|
---|
176 | SOCKET socket_c;
|
---|
177 | char modulo[] = "procesaComandos()";
|
---|
178 |
|
---|
179 | initParametros(ptrTrama,0);
|
---|
180 | while(TRUE){
|
---|
181 | lon=sprintf(ptrTrama->parametros,"nfn=DisponibilidadComandos\r");
|
---|
182 | lon+=sprintf(ptrTrama->parametros+lon,"tpc=%s\r",CLIENTE_WIN); // Activar disponibilidad
|
---|
183 | if(!enviaMensajeServidor(&socket_c,ptrTrama,MSG_INFORMACION)){
|
---|
184 | errorLog(modulo,43,FALSE);
|
---|
185 | return;
|
---|
186 | }
|
---|
187 | infoLog(19); // Disponibilidad de cliente activada
|
---|
188 | ptrTrama=recibeMensaje(&socket_c);
|
---|
189 | if(!ptrTrama){
|
---|
190 | errorLog(modulo,46,FALSE);
|
---|
191 | return;
|
---|
192 | }
|
---|
193 |
|
---|
194 | closesocket(socket_c);
|
---|
195 |
|
---|
196 | if(!gestionaTrama(ptrTrama)){ // Análisis de la trama
|
---|
197 | errorLog(modulo,39,FALSE);
|
---|
198 | return;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 | //_____________________________________________________________________________________________________
|
---|
203 | // Función: Apagar
|
---|
204 | //
|
---|
205 | // Descripción:
|
---|
206 | // Apaga el cliente
|
---|
207 | // Parámetros:
|
---|
208 | // ptrTrama: contenido del mensaje
|
---|
209 | // Devuelve:
|
---|
210 | // TRUE: Si el proceso es correcto
|
---|
211 | // FALSE: En caso de ocurrir algún error
|
---|
212 | //_____________________________________________________________________________________________________
|
---|
213 | BOOLEAN Apagar(TRAMA* ptrTrama)
|
---|
214 | {
|
---|
215 | int lon;
|
---|
216 | char *ids,msglog[LONSTD];
|
---|
217 | char modulo[] = "Apagar()";
|
---|
218 |
|
---|
219 | if (ndebug>=DEBUG_MAXIMO) {
|
---|
220 | sprintf(msglog, "%s:%s",tbMensajes[21],modulo);
|
---|
221 | infoDebug(msglog);
|
---|
222 | }
|
---|
223 | ids=copiaParametro("ids",ptrTrama);
|
---|
224 |
|
---|
225 | initParametros(ptrTrama,0);
|
---|
226 | lon=sprintf(ptrTrama->parametros,"nfn=%s\r","RESPUESTA_Apagar");
|
---|
227 | respuestaEjecucionComando(ptrTrama,0,ids);
|
---|
228 |
|
---|
229 | if(versionWin<5)
|
---|
230 | ApagarReiniciar(EWX_POWEROFF | EWX_FORCE,ids,0);
|
---|
231 | else
|
---|
232 | ApagarReiniciar(EWX_SHUTDOWN | EWX_FORCE,ids,0);
|
---|
233 | return(TRUE);
|
---|
234 | }
|
---|
235 | //_____________________________________________________________________________________________________
|
---|
236 | // Función: Reiniciar
|
---|
237 | //
|
---|
238 | // Descripción:
|
---|
239 | // Apaga el cliente
|
---|
240 | // Parámetros:
|
---|
241 | // ptrTrama: contenido del mensaje
|
---|
242 | // Devuelve:
|
---|
243 | // TRUE: Si el proceso es correcto
|
---|
244 | // FALSE: En caso de ocurrir algún errorservidoradm
|
---|
245 | //_____________________________________________________________________________________________________
|
---|
246 | BOOLEAN Reiniciar(TRAMA* ptrTrama)
|
---|
247 | {
|
---|
248 | int lon;
|
---|
249 | char *ids,msglog[LONSTD];
|
---|
250 | char modulo[] = "Reiniciar()";
|
---|
251 |
|
---|
252 | if (ndebug>=DEBUG_MAXIMO) {
|
---|
253 | sprintf(msglog, "%s:%s",tbMensajes[21],modulo);
|
---|
254 | infoDebug(msglog);
|
---|
255 | }
|
---|
256 | ids=copiaParametro("ids",ptrTrama);
|
---|
257 |
|
---|
258 | initParametros(ptrTrama,0);
|
---|
259 | lon=sprintf(ptrTrama->parametros,"nfn=%s\r","RESPUESTA_Reiniciar");
|
---|
260 | respuestaEjecucionComando(ptrTrama,0,ids);
|
---|
261 |
|
---|
262 | if(versionWin<5)
|
---|
263 | ApagarReiniciar(EWX_REBOOT | EWX_FORCE,ids,1);
|
---|
264 | else
|
---|
265 | ApagarReiniciar(EWX_REBOOT | EWX_FORCE,ids,1);
|
---|
266 |
|
---|
267 | return(TRUE);
|
---|
268 | }
|
---|
269 | // _____________________________________________________________________________________________________________
|
---|
270 | //
|
---|
271 | // Función: ApagarReiniciar
|
---|
272 | //
|
---|
273 | // Descripción:
|
---|
274 | // Apaga o reinicia el ordenador o bien hace logout del usuario
|
---|
275 | //
|
---|
276 | // Parámetros:
|
---|
277 | // - uFlags : Especifica el tipo de shutdown
|
---|
278 | // _____________________________________________________________________________________________________________
|
---|
279 | BOOLEAN ApagarReiniciar(UINT uFlags,char *ids,int sw)
|
---|
280 | {
|
---|
281 | HANDLE hToken;
|
---|
282 | TOKEN_PRIVILEGES tkp;
|
---|
283 | char modulo[] = "Reiniciar()";
|
---|
284 |
|
---|
285 | if (versionWin>4){
|
---|
286 | if (!ExitWindowsEx(uFlags, 0)) {
|
---|
287 | errorLog(modulo,86,FALSE);
|
---|
288 | return(FALSE);
|
---|
289 | }
|
---|
290 | return TRUE;
|
---|
291 | }
|
---|
292 | // Get a token for this process.
|
---|
293 | if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
|
---|
294 | errorLog(modulo,86,FALSE);
|
---|
295 | return(FALSE);
|
---|
296 | }
|
---|
297 |
|
---|
298 | // Get the LUID for the shutdown privilege.
|
---|
299 | LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
|
---|
300 |
|
---|
301 | tkp.PrivilegeCount = 1; // one privilege to set
|
---|
302 | tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
---|
303 |
|
---|
304 | // Get the shutdown privilege for this process.
|
---|
305 | AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
|
---|
306 |
|
---|
307 | if (GetLastError() != ERROR_SUCCESS) {
|
---|
308 | errorLog(modulo,86,FALSE);
|
---|
309 | return(FALSE);
|
---|
310 | }
|
---|
311 |
|
---|
312 | // Shut down the system and force all applications to close.
|
---|
313 | if (!ExitWindowsEx(uFlags, 0)) {
|
---|
314 | errorLog(modulo,86,FALSE);
|
---|
315 | return(FALSE);
|
---|
316 | }
|
---|
317 |
|
---|
318 | return TRUE;
|
---|
319 | }
|
---|
320 | // _____________________________________________________________________________________________________________
|
---|
321 | //
|
---|
322 | // Función: TomaVersionWindows
|
---|
323 | //
|
---|
324 | // Descripción:
|
---|
325 | // Toma la versión del sistema operativo
|
---|
326 | //
|
---|
327 | // Valores de retorno:
|
---|
328 | // 1.- Microsoft Windows Server 2003
|
---|
329 | // 2.- Microsoft Windows XP
|
---|
330 | // 3.- Microsoft Windows 2000
|
---|
331 | // 4.- Microsoft Windows NT
|
---|
332 | // 5.- Microsoft Windows 95
|
---|
333 | // 6.- Microsoft Windows 98
|
---|
334 | // 7.- Microsoft Windows Millennium Edition
|
---|
335 | // _____________________________________________________________________________________________________________
|
---|
336 | int TomaVersionWindows()
|
---|
337 | {
|
---|
338 | OSVERSIONINFOEX osvi;
|
---|
339 | BOOL bOsVersionInfoEx;
|
---|
340 |
|
---|
341 | // Intenta tomar la version usando la estructura OSVERSIONINFOEX
|
---|
342 | // Si falla lo intentausando la estructura OSVERSIONINFO.
|
---|
343 |
|
---|
344 | ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
|
---|
345 | osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
|
---|
346 |
|
---|
347 | if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) ){
|
---|
348 | osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
|
---|
349 | if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
|
---|
350 | return -1;
|
---|
351 | }
|
---|
352 | switch (osvi.dwPlatformId){
|
---|
353 | // Test for the Windows NT product family.
|
---|
354 | case VER_PLATFORM_WIN32_NT:
|
---|
355 | if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
|
---|
356 | return(1); // Microsoft Windows Server 2003
|
---|
357 | if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
|
---|
358 | return(2); // Microsoft Windows XP
|
---|
359 | if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
|
---|
360 | return(3); // Microsoft Windows 2000
|
---|
361 | if ( osvi.dwMajorVersion <= 4 )
|
---|
362 | return(4); // Microsoft Windows NT
|
---|
363 | break;
|
---|
364 |
|
---|
365 | // Test for the Windows Me/98/95.
|
---|
366 | case VER_PLATFORM_WIN32_WINDOWS:
|
---|
367 | if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
|
---|
368 | return(5); // Microsoft Windows 95
|
---|
369 | if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
|
---|
370 | return(6); // Microsoft Windows 98
|
---|
371 | if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
|
---|
372 | return(7); // Microsoft Windows Millennium Edition
|
---|
373 | break;
|
---|
374 | }
|
---|
375 | return -1;
|
---|
376 | }
|
---|
377 | //______________________________________________________________________________________________________
|
---|
378 | // Función: Sondeo
|
---|
379 | //
|
---|
380 | // Descripción:
|
---|
381 | // Envía al servidor una confirmación de que está dentro del sistema
|
---|
382 | // Parámetros:
|
---|
383 | // ptrTrama: contenido del mensajede
|
---|
384 | // Devuelve:
|
---|
385 | // TRUE: Si el proceso es correcto
|
---|
386 | // FALSE: En caso de ocurrir algún error
|
---|
387 | //______________________________________________________________________________________________________
|
---|
388 | BOOLEAN Sondeo(TRAMA* ptrTrama)
|
---|
389 | {
|
---|
390 | return(TRUE);
|
---|
391 | }
|
---|
392 | //______________________________________________________________________________________________________
|
---|
393 | // Función: Actualizar
|
---|
394 | //
|
---|
395 | // Descripción:
|
---|
396 | // Envía al servidor una confirmación de que está dentro del sistema
|
---|
397 | // Parámetros:
|
---|
398 | // ptrTrama: contenido del mensajede
|
---|
399 | // Devuelve:
|
---|
400 | // TRUE: Si el proceso es correcto
|
---|
401 | // FALSE: En caso de ocurrir algún error
|
---|
402 | //______________________________________________________________________________________________________
|
---|
403 | BOOLEAN Actualizar(TRAMA* ptrTrama)
|
---|
404 | {
|
---|
405 | return(TRUE);
|
---|
406 | }
|
---|
407 | //______________________________________________________________________________________________________
|
---|
408 | // Función: respuestaEjecucionComando
|
---|
409 | //
|
---|
410 | // Descripción:
|
---|
411 | // Envia una respuesta a una ejecucion de comando al servidor de Administración
|
---|
412 | // Parámetros:
|
---|
413 | // - ptrTrama: contenido del mensaje
|
---|
414 | // - res: Resultado de la ejecución (Código de error devuelto por el script ejecutado)
|
---|
415 | // - ids: Identificador de la sesion (En caso de no haber seguimiento es NULO)
|
---|
416 | // Devuelve:
|
---|
417 | // TRUE: Si el proceso es correcto
|
---|
418 | // FALSE: En caso de ocurrir algún error
|
---|
419 | // ________________________________________________________________________________________________________
|
---|
420 | BOOLEAN respuestaEjecucionComando(TRAMA* ptrTrama,int res,char *ids)
|
---|
421 | {
|
---|
422 | int lon;
|
---|
423 | SOCKET socket_c;
|
---|
424 | char modulo[] = "respuestaEjecucionComando()";
|
---|
425 |
|
---|
426 | lon=strlen(ptrTrama->parametros);
|
---|
427 | if(ids){ // Existe seguimiento
|
---|
428 | lon+=sprintf(ptrTrama->parametros+lon,"ids=%s\r",ids); // Añade identificador de la sesión
|
---|
429 | }
|
---|
430 | if (res==0){ // Resultado satisfactorio
|
---|
431 | lon+=sprintf(ptrTrama->parametros+lon,"res=%s\r","1");
|
---|
432 | lon+=sprintf(ptrTrama->parametros+lon,"der=%s\r","");
|
---|
433 | }
|
---|
434 | else{ // Algún error
|
---|
435 | lon+=sprintf(ptrTrama->parametros+lon,"res=%s\r","2");
|
---|
436 | lon+=sprintf(ptrTrama->parametros+lon,"der=%s\r",tbErrores[res]);// Descripción del error
|
---|
437 | }
|
---|
438 | if(!(enviaMensajeServidor(&socket_c,ptrTrama,MSG_NOTIFICACION))){
|
---|
439 | errorLog(modulo,44,FALSE);
|
---|
440 | return(FALSE);
|
---|
441 | }
|
---|
442 | closesocket(socket_c);
|
---|
443 | return(TRUE);
|
---|
444 | }
|
---|
445 | // ________________________________________________________________________________________________________
|
---|
446 | // Función: gestionaTrama
|
---|
447 | //
|
---|
448 | // Descripción:
|
---|
449 | // Procesa las tramas recibidas.servidoradm
|
---|
450 | // Parametros:
|
---|
451 | // ptrTrama: contenido del mensaje
|
---|
452 | // Devuelve:
|
---|
453 | // TRUE: Si el proceso es correcto
|
---|
454 | // FALSE: En caso de ocurrir algún error
|
---|
455 | // ________________________________________________________________________________________________________
|
---|
456 | BOOLEAN gestionaTrama(TRAMA *ptrTrama)
|
---|
457 | {
|
---|
458 | int i, res;
|
---|
459 | char *nfn;
|
---|
460 | char modulo[] = "gestionaTrama()";
|
---|
461 |
|
---|
462 | INTROaFINCAD(ptrTrama);
|
---|
463 | nfn = copiaParametro("nfn", ptrTrama); // Toma nombre de función
|
---|
464 | for (i = 0; i < MAXIMAS_FUNCIONES; i++) { // Recorre funciones que procesan las tramas
|
---|
465 | res = strcmp(tbfuncionesClient[i].nf, nfn);
|
---|
466 | if (res == 0) { // Encontrada la función que procesa el mensaje
|
---|
467 | return(tbfuncionesClient[i].fptr(ptrTrama)); // Invoca la función
|
---|
468 | }
|
---|
469 | }
|
---|
470 | errorLog(modulo, 18, FALSE);
|
---|
471 | return (FALSE);
|
---|
472 | }
|
---|
473 | //______________________________________________________________________________________________________
|
---|
474 | // Función: enviaMensajeServidor
|
---|
475 | //
|
---|
476 | // Descripción:
|
---|
477 | // Envia un mensaje al servidor de Administración
|
---|
478 | // Parámetros:
|
---|
479 | // - socket_c: (Salida) Socket utilizado para el envío
|
---|
480 | // - ptrTrama: contenido del mensaje
|
---|
481 | // - tipo: Tipo de mensaje
|
---|
482 | // C=Comando, N=Respuesta a un comando, P=Peticion,R=Respuesta a una petición, I=Informacion
|
---|
483 | // Devuelve:
|
---|
484 | // TRUE: Si el proceso es correcto
|
---|
485 | // FALSE: En caso de ocurrir algún error
|
---|
486 | // ________________________________________________________________________________________________________
|
---|
487 | BOOLEAN enviaMensajeServidor(SOCKET *socket_c,TRAMA *ptrTrama,char tipo)
|
---|
488 | {
|
---|
489 | int lon;
|
---|
490 | char modulo[] = "enviaMensajeServidor()";
|
---|
491 |
|
---|
492 | *socket_c=abreConexion();
|
---|
493 | if(*socket_c==INVALID_SOCKET){
|
---|
494 | errorLog(modulo,38,FALSE); // Error de conexión con el servidor
|
---|
495 | return(FALSE);
|
---|
496 | }
|
---|
497 | ptrTrama->arroba='@'; // Cabecera de la trama
|
---|
498 | strncpy(ptrTrama->identificador,"JMMLCAMDJ_MCDJ",14); // identificador de la trama
|
---|
499 | ptrTrama->tipo=tipo; // Tipo de mensaje
|
---|
500 | lon=strlen(ptrTrama->parametros); // Compone la trama
|
---|
501 | lon+=sprintf(ptrTrama->parametros+lon,"iph=%s\r",IPlocal); // Ip del ordenador
|
---|
502 | lon+=sprintf(ptrTrama->parametros+lon,"ido=%s\r",idordenador); // Identificador del ordenador
|
---|
503 | lon+=sprintf(ptrTrama->parametros+lon,"npc=%s\r",nombreordenador); // Nombre del ordenador
|
---|
504 |
|
---|
505 | if (!mandaTrama(socket_c,ptrTrama)) {
|
---|
506 | errorLog(modulo,26,FALSE);
|
---|
507 | return (FALSE);
|
---|
508 | }
|
---|
509 | return(TRUE);
|
---|
510 | }
|
---|
511 | // _____________________________________________________________________________________________________________
|
---|
512 | // Función: TomaParametrosReg
|
---|
513 | //
|
---|
514 | // Descripción:
|
---|
515 | // Toma los parámetros de conexión del registro
|
---|
516 | // Devuelve:
|
---|
517 | // TRUE: Si el proceso es correcto
|
---|
518 | // FALSE: En caso de ocurrir algún error
|
---|
519 | // _____________________________________________________________________________________________________________
|
---|
520 | BOOLEAN TomaParametrosReg()
|
---|
521 | {
|
---|
522 | if(!ReadRegistryString(HIVE,BASE,"servidoradm",servidoradm,20))
|
---|
523 | return(FALSE);
|
---|
524 |
|
---|
525 | if(!ReadRegistryString(HIVE,BASE,"puerto",puerto,20))
|
---|
526 | return(FALSE);
|
---|
527 |
|
---|
528 | if(!ReadRegistryString(HIVE,BASE,"Iplocal",IPlocal,20))
|
---|
529 | return(FALSE);
|
---|
530 |
|
---|
531 | return(TRUE);
|
---|
532 | }
|
---|
533 | //______________________________________________________________________________________________________
|
---|
534 | // Función: TomaIP
|
---|
535 | //
|
---|
536 | // Descripción:
|
---|
537 | // Recupera la ip de la máquina
|
---|
538 | // Parámetros:
|
---|
539 | // Ninguno
|
---|
540 | // Devuelve:
|
---|
541 | // TRUE: Si el proceso es correcto
|
---|
542 | // FALSE: En caso de ocurrir algún error
|
---|
543 | // ________________________________________________________________________________________________________
|
---|
544 | BOOLEAN TomaIP(char* ip)
|
---|
545 | {
|
---|
546 | char ac[80];
|
---|
547 | struct in_addr addr;
|
---|
548 | BOOLEAN ipv;
|
---|
549 |
|
---|
550 | if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
|
---|
551 | return (FALSE);
|
---|
552 | }
|
---|
553 | struct hostent *phe = gethostbyname(ac);
|
---|
554 | if (phe == 0) {
|
---|
555 | return (FALSE);
|
---|
556 | }
|
---|
557 | ipv=FALSE;
|
---|
558 | for (int i = 0; phe->h_addr_list[i] != 0 && !ipv; ++i) {
|
---|
559 | memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
|
---|
560 | strcpy(ip,inet_ntoa(addr));
|
---|
561 | if(strcmp(IPlocal,"127.0.0.1")==0)
|
---|
562 | ipv=FALSE;
|
---|
563 | else{
|
---|
564 | ipv=true; // IP válida distinta a loop
|
---|
565 | break;
|
---|
566 | }
|
---|
567 | }
|
---|
568 |
|
---|
569 | return(ipv);
|
---|
570 | }
|
---|
571 | // ********************************************************************************************************
|
---|
572 | // PROGRAMA PRINCIPAL (CLIENTE)
|
---|
573 | // ********************************************************************************************************
|
---|
574 | //int main(int argc, char *argv[])
|
---|
575 | VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)
|
---|
576 | {
|
---|
577 | //___________________________________________________________
|
---|
578 | //
|
---|
579 | // Service initialization (Report the status to the service control manager)
|
---|
580 | //___________________________________________________________
|
---|
581 | //
|
---|
582 | if (!ReportStatusToSCMgr(
|
---|
583 | SERVICE_START_PENDING, // service state
|
---|
584 | NO_ERROR, // exit code
|
---|
585 | 3000)) // wait hint
|
---|
586 | return;
|
---|
587 | //__________________________________________
|
---|
588 |
|
---|
589 | TRAMA *ptrTrama;
|
---|
590 | char modulo[] = "main()";
|
---|
591 |
|
---|
592 | strcpy(szPathFileLog, "ogAdmWinClient.log"); // de configuración y de logs
|
---|
593 |
|
---|
594 |
|
---|
595 | WSADATA wsd;
|
---|
596 | if (WSAStartup(MAKEWORD(2,2),&wsd)!=0){ // Carga librería Winsock
|
---|
597 | errorLog(modulo, 93, FALSE);
|
---|
598 | exit(EXIT_FAILURE);
|
---|
599 | }
|
---|
600 |
|
---|
601 | ptrTrama=(TRAMA *)reservaMemoria(sizeof(TRAMA));
|
---|
602 | if (ptrTrama == NULL) { // No hay memoria suficiente para el bufer de las tramas
|
---|
603 | errorLog(modulo, 3, FALSE);
|
---|
604 | exit(EXIT_FAILURE);
|
---|
605 | }
|
---|
606 | /*--------------------------------------------------------------------------------------------------------
|
---|
607 | Validación de parámetros de ejecución y fichero de configuración
|
---|
608 | ---------------------------------------------------------------------------------------------------------*/
|
---|
609 | if(!TomaParametrosReg()) // Toma parametros de configuracion
|
---|
610 | exit(EXIT_FAILURE);
|
---|
611 |
|
---|
612 | //___________________________________________________________
|
---|
613 | //
|
---|
614 | // Service initialization (Report the status to the service control manager)
|
---|
615 | //___________________________________________________________
|
---|
616 | //
|
---|
617 | if (!ReportStatusToSCMgr(
|
---|
618 | SERVICE_START_PENDING, // service state
|
---|
619 | NO_ERROR, // exit code
|
---|
620 | 3000)) // wait hint
|
---|
621 | return;
|
---|
622 | //__________________________________________
|
---|
623 | //
|
---|
624 | // Toma IP local
|
---|
625 | //___________________________________________
|
---|
626 | //
|
---|
627 | // Toma la ip también del registro
|
---|
628 | /*if(!TomaIP(IPlocal)){
|
---|
629 | errorLog(modulo,85,FALSE);
|
---|
630 | exit(EXIT_FAILURE);
|
---|
631 | }
|
---|
632 | */
|
---|
633 | versionWin=TomaVersionWindows(); // Toma versión de windows
|
---|
634 |
|
---|
635 | /*--------------------------------------------------------------------------------------------------------
|
---|
636 | Carga catálogo de funciones que procesan las tramas
|
---|
637 | ---------------------------------------------------------------------------------------------------------*/
|
---|
638 | int cf = 0;
|
---|
639 |
|
---|
640 | strcpy(tbfuncionesClient[cf].nf, "RESPUESTA_InclusionClienteWinLnx");
|
---|
641 | tbfuncionesClient[cf++].fptr = &RESPUESTA_InclusionClienteWinLnx;
|
---|
642 |
|
---|
643 | strcpy(tbfuncionesClient[cf].nf, "Apagar");
|
---|
644 | tbfuncionesClient[cf++].fptr = &Apagar;
|
---|
645 |
|
---|
646 | strcpy(tbfuncionesClient[cf].nf, "Reiniciar");
|
---|
647 | tbfuncionesClient[cf++].fptr = &Reiniciar;
|
---|
648 |
|
---|
649 | strcpy(tbfuncionesClient[cf].nf, "Sondeo");
|
---|
650 | tbfuncionesClient[cf++].fptr = &Sondeo;
|
---|
651 |
|
---|
652 | strcpy(tbfuncionesClient[cf].nf, "Actualizar");
|
---|
653 | tbfuncionesClient[cf++].fptr = &Actualizar;
|
---|
654 |
|
---|
655 | /*--------------------------------------------------------------------------------------------------------
|
---|
656 | Inicio de sesión
|
---|
657 | ---------------------------------------------------------------------------------------------------------*/
|
---|
658 | infoLog(1); // Inicio de sesión
|
---|
659 | infoLog(3); // Abriendo sesión en el servidor de Administración;
|
---|
660 | /*--------------------------------------------------------------------------------------------------------
|
---|
661 | Inclusión del cliente en el sistema
|
---|
662 | ---------------------------------------------------------------------------------------------------------*/
|
---|
663 | if(!InclusionClienteWinLnx(ptrTrama)){ // Ha habido algún problema al abrir sesión
|
---|
664 | errorLog(modulo,0,FALSE);
|
---|
665 | exit(EXIT_FAILURE);
|
---|
666 | }
|
---|
667 | infoLog(4); // Cliente iniciado
|
---|
668 |
|
---|
669 | //__________________________________________
|
---|
670 | // El servicio está instalado y debe ser iniciado
|
---|
671 | // -- report the status to the service control manager.--
|
---|
672 | //
|
---|
673 | if (!ReportStatusToSCMgr(
|
---|
674 | SERVICE_RUNNING, // service state
|
---|
675 | NO_ERROR, // exit code
|
---|
676 | 0)) // wait hint
|
---|
677 | return;
|
---|
678 | // End of initialization
|
---|
679 | //__________________________________________
|
---|
680 |
|
---|
681 | //
|
---|
682 | // Service is now running, perform work until shutdown
|
---|
683 | //
|
---|
684 | while (ssStatus.dwCurrentState == SERVICE_RUNNING) { // Bucle para escuchar peticiones de clientes
|
---|
685 | procesaComandos(ptrTrama); // Bucle para procesar comandos interactivos
|
---|
686 | }
|
---|
687 | WSACleanup();
|
---|
688 | // El servicio de detiene
|
---|
689 | }
|
---|
690 | /* _____________________________________________________________________________________________________________
|
---|
691 | FUNCTION: ServiceStop
|
---|
692 |
|
---|
693 | PURPOSE: Stops the service
|
---|
694 | PARAMETERS:
|
---|
695 | none
|
---|
696 |
|
---|
697 | RETURN VALUE:
|
---|
698 | none
|
---|
699 |
|
---|
700 | COMMENTS:
|
---|
701 |
|
---|
702 | If a ServiceStop procedure is going to
|
---|
703 | take longer than 3 seconds to execute,
|
---|
704 | it should spawn a thread to execute the
|
---|
705 | stop code, and return. Otherwise, the
|
---|
706 | ServiceControlManager will believe that
|
---|
707 | the service has stopped responding.
|
---|
708 | _____________________________________________________________________________________________________________*/
|
---|
709 | VOID ServiceStop()
|
---|
710 | {
|
---|
711 | // Incluir aquí el código necesario antes de parar el servicio
|
---|
712 | ReportStatusToSCMgr(SERVICE_STOPPED, NO_ERROR, 0);
|
---|
713 | } |
---|