1 | // ******************************************************************************************************** |
---|
2 | // Cliernte: ogAdmLnxClient |
---|
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: ogAdmLnxClient.c |
---|
7 | // Descripción :Este fichero implementa el cliente windows del sistema |
---|
8 | // ******************************************************************************************************** |
---|
9 | #include "ogAdmLnxClient.h" |
---|
10 | #include "ogAdmLib.c" |
---|
11 | //________________________________________________________________________________________________________ |
---|
12 | // Función: tomaConfiguracion |
---|
13 | // |
---|
14 | // Descripción: |
---|
15 | // Lee el fichero de configuración del servicio |
---|
16 | // Parámetros: |
---|
17 | // filecfg : Ruta completa al fichero de configuración |
---|
18 | // Devuelve: |
---|
19 | // TRUE: Si el proceso es correcto |
---|
20 | // FALSE: En caso de ocurrir algún error |
---|
21 | //________________________________________________________________________________________________________ |
---|
22 | BOOLEAN tomaConfiguracion(char* filecfg) |
---|
23 | { |
---|
24 | char modulo[] = "tomaConfiguracion()"; |
---|
25 | |
---|
26 | if (filecfg == NULL || strlen(filecfg) == 0) { |
---|
27 | errorLog(modulo, 1, FALSE); // Fichero de configuración del cliente vacío |
---|
28 | return (FALSE); |
---|
29 | } |
---|
30 | FILE *fcfg; |
---|
31 | int lSize; |
---|
32 | char * buffer, *lineas[MAXPRM], *dualparametro[2]; |
---|
33 | int i, numlin, resul; |
---|
34 | |
---|
35 | fcfg = fopen(filecfg, "rt"); |
---|
36 | if (fcfg == NULL) { |
---|
37 | errorLog(modulo, 2, FALSE); // No existe fichero de configuración del cliente |
---|
38 | return (FALSE); |
---|
39 | } |
---|
40 | |
---|
41 | fseek(fcfg, 0, SEEK_END); |
---|
42 | lSize = ftell(fcfg); // Obtiene tamaño del fichero. |
---|
43 | rewind(fcfg); |
---|
44 | buffer = (char*) reservaMemoria(lSize+1); // Toma memoria para el buffer de lectura. |
---|
45 | if (buffer == NULL) { // No hay memoria suficiente para el buffer |
---|
46 | errorLog(modulo, 3, FALSE); |
---|
47 | return (FALSE); |
---|
48 | } |
---|
49 | lSize=fread(buffer, 1, lSize, fcfg); // Lee contenido del fichero |
---|
50 | buffer[lSize]=CHARNULL; |
---|
51 | fclose(fcfg); |
---|
52 | |
---|
53 | /* Inicializar variables globales */ |
---|
54 | servidoradm[0]=CHARNULL; |
---|
55 | puerto[0] = CHARNULL; |
---|
56 | IPlocal[0]=CHARNULL; |
---|
57 | |
---|
58 | numlin = splitCadena(lineas, buffer, '\n'); |
---|
59 | for (i = 0; i < numlin; i++){ |
---|
60 | splitCadena(dualparametro, lineas[i], '='); |
---|
61 | |
---|
62 | resul = strcmp(StrToUpper(dualparametro[0]), "SERVIDORADM"); |
---|
63 | if (resul == 0) |
---|
64 | strcpy(servidoradm, dualparametro[1]); |
---|
65 | |
---|
66 | resul = strcmp(StrToUpper(dualparametro[0]), "PUERTO"); |
---|
67 | if (resul == 0) |
---|
68 | strcpy(puerto, dualparametro[1]); |
---|
69 | |
---|
70 | resul = strcmp(StrToUpper(dualparametro[0]), "IPLOCAL"); |
---|
71 | if (resul == 0) |
---|
72 | strcpy(IPlocal, dualparametro[1]); |
---|
73 | } |
---|
74 | |
---|
75 | if (servidoradm[0] == CHARNULL) { |
---|
76 | errorLog(modulo,4, FALSE); // Falta parámetro SERVIDORADM |
---|
77 | return (FALSE); |
---|
78 | } |
---|
79 | |
---|
80 | if (puerto[0] == CHARNULL) { |
---|
81 | errorLog(modulo,5, FALSE); // Falta parámetro PUERTO |
---|
82 | return (FALSE); |
---|
83 | } |
---|
84 | if (IPlocal[0] == CHARNULL) { |
---|
85 | errorLog(modulo, 92, FALSE); // Falta parámetro IPLOCAL |
---|
86 | return (FALSE); |
---|
87 | } |
---|
88 | return (TRUE); |
---|
89 | } |
---|
90 | //______________________________________________________________________________________________________ |
---|
91 | // Función: InclusionClienteWinLnx |
---|
92 | // Descripción: |
---|
93 | // Abre una sesión en el servidor de administración y registra al cliente en el sistema |
---|
94 | // Parámetros: |
---|
95 | // Ninguno |
---|
96 | // Devuelve: |
---|
97 | // TRUE: Si el proceso es correcto |
---|
98 | // FALSE: En caso de ocurrir algún error |
---|
99 | //______________________________________________________________________________________________________ |
---|
100 | BOOLEAN InclusionClienteWinLnx(TRAMA* ptrTrama) |
---|
101 | { |
---|
102 | int lon; |
---|
103 | SOCKET socket_c; |
---|
104 | char modulo[] = "InclusionClienteWinLnx()"; |
---|
105 | |
---|
106 | initParametros(ptrTrama,0); |
---|
107 | lon=sprintf(ptrTrama->parametros,"nfn=InclusionClienteWinLnx\r"); // Nombre de la función a ejecutar en el servidor |
---|
108 | |
---|
109 | if(!enviaMensajeServidor(&socket_c,ptrTrama,MSG_PETICION)){ |
---|
110 | errorLog(modulo,37,FALSE); |
---|
111 | return(FALSE); |
---|
112 | } |
---|
113 | ptrTrama=recibeMensaje(&socket_c); |
---|
114 | if(!ptrTrama){ |
---|
115 | errorLog(modulo,22,FALSE); |
---|
116 | return(FALSE); |
---|
117 | } |
---|
118 | close(socket_c); |
---|
119 | |
---|
120 | if(!gestionaTrama(ptrTrama)){ // Análisis de la trama |
---|
121 | errorLog(modulo,39,FALSE); |
---|
122 | return(FALSE); |
---|
123 | } |
---|
124 | |
---|
125 | return(TRUE); |
---|
126 | } |
---|
127 | //______________________________________________________________________________________________________ |
---|
128 | // Función: RESPUESTA_InclusionClienteWinLnx |
---|
129 | // |
---|
130 | // Descripción: |
---|
131 | // Respuesta del servidor de administración a la petición de inicio |
---|
132 | // enviando los datos identificativos del cliente |
---|
133 | // Parámetros: |
---|
134 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
135 | // Devuelve: |
---|
136 | // TRUE: Si el proceso es correcto |
---|
137 | // FALSE: En caso de ocurrir algún error |
---|
138 | //______________________________________________________________________________________________________ |
---|
139 | BOOLEAN RESPUESTA_InclusionClienteWinLnx(TRAMA* ptrTrama) |
---|
140 | { |
---|
141 | char* res; |
---|
142 | char modulo[] = "RESPUESTA_InclusionClienteWinLnx()"; |
---|
143 | int err; |
---|
144 | |
---|
145 | res=copiaParametro("res",ptrTrama); // Resultado del proceso de inclusión |
---|
146 | err=(int)atoi(res); // Código de error devuelto por el servidor |
---|
147 | if(err>0){ // Error en el proceso de inclusión |
---|
148 | errorLog(modulo,41,FALSE); |
---|
149 | errorLog(modulo,err,FALSE); |
---|
150 | return (FALSE); |
---|
151 | } |
---|
152 | strcpy(idordenador,copiaParametro("ido",ptrTrama)); // Identificador del ordenador |
---|
153 | strcpy(nombreordenador,copiaParametro("npc",ptrTrama)); // Nombre del ordenador |
---|
154 | |
---|
155 | if(idordenador==NULL || nombreordenador==NULL){ |
---|
156 | errorLog(modulo,40,FALSE); |
---|
157 | return (FALSE); |
---|
158 | } |
---|
159 | return(TRUE); |
---|
160 | } |
---|
161 | //______________________________________________________________________________________________________ |
---|
162 | // Función: ProcesaComandos |
---|
163 | // |
---|
164 | // Descripción: |
---|
165 | // Espera comando desde el Servidor de Administración para ejecutarlos |
---|
166 | // Parámetros: |
---|
167 | // Ninguno |
---|
168 | // Devuelve: |
---|
169 | // TRUE: Si el proceso es correcto |
---|
170 | // FALSE: En caso de ocurrir algún error |
---|
171 | // ________________________________________________________________________________________________________ |
---|
172 | void procesaComandos(TRAMA* ptrTrama) |
---|
173 | { |
---|
174 | int lon; |
---|
175 | SOCKET socket_c; |
---|
176 | char modulo[] = "procesaComandos()"; |
---|
177 | |
---|
178 | initParametros(ptrTrama,0); |
---|
179 | while(TRUE){ |
---|
180 | lon=sprintf(ptrTrama->parametros,"nfn=DisponibilidadComandos\r"); |
---|
181 | lon+=sprintf(ptrTrama->parametros+lon,"tpc=%s\r",CLIENTE_LNX); // Activar disponibilidad |
---|
182 | if(!enviaMensajeServidor(&socket_c,ptrTrama,MSG_INFORMACION)){ |
---|
183 | errorLog(modulo,43,FALSE); |
---|
184 | return; |
---|
185 | } |
---|
186 | infoLog(19); // Disponibilidad de cliente activada |
---|
187 | ptrTrama=recibeMensaje(&socket_c); |
---|
188 | if(!ptrTrama){ |
---|
189 | errorLog(modulo,46,FALSE); |
---|
190 | return; |
---|
191 | } |
---|
192 | |
---|
193 | close(socket_c); |
---|
194 | |
---|
195 | if(!gestionaTrama(ptrTrama)){ // Análisis de la trama |
---|
196 | errorLog(modulo,39,FALSE); |
---|
197 | return; |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | //_____________________________________________________________________________________________________ |
---|
202 | // Función: Apagar |
---|
203 | // |
---|
204 | // Descripción: |
---|
205 | // Apaga el cliente |
---|
206 | // Parámetros: |
---|
207 | // ptrTrama: contenido del mensaje |
---|
208 | // Devuelve: |
---|
209 | // TRUE: Si el proceso es correcto |
---|
210 | // FALSE: En caso de ocurrir algún error |
---|
211 | //_____________________________________________________________________________________________________ |
---|
212 | BOOLEAN Apagar(TRAMA* ptrTrama) |
---|
213 | { |
---|
214 | int lon; |
---|
215 | char *ids,msglog[LONSTD]; |
---|
216 | char modulo[] = "Apagar()"; |
---|
217 | |
---|
218 | if (ndebug>=DEBUG_MAXIMO) { |
---|
219 | sprintf(msglog, "%s:%s",tbMensajes[21],modulo); |
---|
220 | infoDebug(msglog); |
---|
221 | } |
---|
222 | ids=copiaParametro("ids",ptrTrama); |
---|
223 | |
---|
224 | initParametros(ptrTrama,0); |
---|
225 | lon=sprintf(ptrTrama->parametros,"nfn=%s\r","RESPUESTA_Apagar"); |
---|
226 | respuestaEjecucionComando(ptrTrama,0,ids); |
---|
227 | |
---|
228 | system("shutdown -h now"); |
---|
229 | return(TRUE); |
---|
230 | } |
---|
231 | //_____________________________________________________________________________________________________ |
---|
232 | // Función: Reiniciar |
---|
233 | // |
---|
234 | // Descripción: |
---|
235 | // Apaga el cliente |
---|
236 | // Parámetros: |
---|
237 | // ptrTrama: contenido del mensaje |
---|
238 | // Devuelve: |
---|
239 | // TRUE: Si el proceso es correcto |
---|
240 | // FALSE: En caso de ocurrir algún errorservidoradm |
---|
241 | //_____________________________________________________________________________________________________ |
---|
242 | BOOLEAN Reiniciar(TRAMA* ptrTrama) |
---|
243 | { |
---|
244 | int lon; |
---|
245 | char *ids,msglog[LONSTD]; |
---|
246 | char modulo[] = "Reiniciar()"; |
---|
247 | |
---|
248 | if (ndebug>=DEBUG_MAXIMO) { |
---|
249 | sprintf(msglog, "%s:%s",tbMensajes[21],modulo); |
---|
250 | infoDebug(msglog); |
---|
251 | } |
---|
252 | ids=copiaParametro("ids",ptrTrama); |
---|
253 | |
---|
254 | initParametros(ptrTrama,0); |
---|
255 | lon=sprintf(ptrTrama->parametros,"nfn=%s\r","RESPUESTA_Reiniciar"); |
---|
256 | respuestaEjecucionComando(ptrTrama,0,ids); |
---|
257 | |
---|
258 | system("shutdown -r now"); |
---|
259 | |
---|
260 | return(TRUE); |
---|
261 | } |
---|
262 | //______________________________________________________________________________________________________ |
---|
263 | // Función: Sondeo |
---|
264 | // |
---|
265 | // Descripción: |
---|
266 | // Envía al servidor una confirmación de que está dentro del sistema |
---|
267 | // Parámetros: |
---|
268 | // ptrTrama: contenido del mensajede |
---|
269 | // Devuelve: |
---|
270 | // TRUE: Si el proceso es correcto |
---|
271 | // FALSE: En caso de ocurrir algún error |
---|
272 | //______________________________________________________________________________________________________ |
---|
273 | BOOLEAN Sondeo(TRAMA* ptrTrama) |
---|
274 | { |
---|
275 | return(TRUE); |
---|
276 | } |
---|
277 | //______________________________________________________________________________________________________ |
---|
278 | // Función: respuestaEjecucionComando |
---|
279 | // |
---|
280 | // Descripción: |
---|
281 | // Envia una respuesta a una ejecucion de comando al servidor de Administración |
---|
282 | // Parámetros: |
---|
283 | // - ptrTrama: contenido del mensaje |
---|
284 | // - res: Resultado de la ejecución (Código de error devuelto por el script ejecutado) |
---|
285 | // - ids: Identificador de la sesion (En caso de no haber seguimiento es NULO) |
---|
286 | // Devuelve: |
---|
287 | // TRUE: Si el proceso es correcto |
---|
288 | // FALSE: En caso de ocurrir algún error |
---|
289 | // ________________________________________________________________________________________________________ |
---|
290 | BOOLEAN respuestaEjecucionComando(TRAMA* ptrTrama,int res,char *ids) |
---|
291 | { |
---|
292 | int lon; |
---|
293 | SOCKET socket_c; |
---|
294 | char modulo[] = "respuestaEjecucionComando()"; |
---|
295 | |
---|
296 | lon=strlen(ptrTrama->parametros); |
---|
297 | if(ids){ // Existe seguimiento |
---|
298 | lon+=sprintf(ptrTrama->parametros+lon,"ids=%s\r",ids); // Añade identificador de la sesión |
---|
299 | } |
---|
300 | if (res==0){ // Resultado satisfactorio |
---|
301 | lon+=sprintf(ptrTrama->parametros+lon,"res=%s\r","1"); |
---|
302 | lon+=sprintf(ptrTrama->parametros+lon,"der=%s\r",""); |
---|
303 | } |
---|
304 | else{ // Algún error |
---|
305 | lon+=sprintf(ptrTrama->parametros+lon,"res=%s\r","2"); |
---|
306 | lon+=sprintf(ptrTrama->parametros+lon,"der=%s\r",tbErrores[res]);// Descripción del error |
---|
307 | } |
---|
308 | if(!(enviaMensajeServidor(&socket_c,ptrTrama,MSG_NOTIFICACION))){ |
---|
309 | errorLog(modulo,44,FALSE); |
---|
310 | return(FALSE); |
---|
311 | } |
---|
312 | close(socket_c); |
---|
313 | return(TRUE); |
---|
314 | } |
---|
315 | // ________________________________________________________________________________________________________ |
---|
316 | // Función: gestionaTrama |
---|
317 | // |
---|
318 | // Descripción: |
---|
319 | // Procesa las tramas recibidas.servidoradm |
---|
320 | // Parametros: |
---|
321 | // ptrTrama: contenido del mensaje |
---|
322 | // Devuelve: |
---|
323 | // TRUE: Si el proceso es correcto |
---|
324 | // FALSE: En caso de ocurrir algún error |
---|
325 | // ________________________________________________________________________________________________________ |
---|
326 | BOOLEAN gestionaTrama(TRAMA *ptrTrama) |
---|
327 | { |
---|
328 | int i, res; |
---|
329 | char *nfn; |
---|
330 | char modulo[] = "gestionaTrama()"; |
---|
331 | |
---|
332 | INTROaFINCAD(ptrTrama); |
---|
333 | nfn = copiaParametro("nfn", ptrTrama); // Toma nombre de función |
---|
334 | for (i = 0; i < MAXIMAS_FUNCIONES; i++) { // Recorre funciones que procesan las tramas |
---|
335 | res = strcmp(tbfuncionesClient[i].nf, nfn); |
---|
336 | if (res == 0) { // Encontrada la función que procesa el mensaje |
---|
337 | return(tbfuncionesClient[i].fptr(ptrTrama)); // Invoca la función |
---|
338 | } |
---|
339 | } |
---|
340 | errorLog(modulo, 18, FALSE); |
---|
341 | return (FALSE); |
---|
342 | } |
---|
343 | //______________________________________________________________________________________________________ |
---|
344 | // Función: enviaMensajeServidor |
---|
345 | // |
---|
346 | // Descripción: |
---|
347 | // Envia un mensaje al servidor de Administración |
---|
348 | // Parámetros: |
---|
349 | // - socket_c: (Salida) Socket utilizado para el envío |
---|
350 | // - ptrTrama: contenido del mensaje |
---|
351 | // - tipo: Tipo de mensaje |
---|
352 | // C=Comando, N=Respuesta a un comando, P=Peticion,R=Respuesta a una petición, I=Informacion |
---|
353 | // Devuelve: |
---|
354 | // TRUE: Si el proceso es correcto |
---|
355 | // FALSE: En caso de ocurrir algún error |
---|
356 | // ________________________________________________________________________________________________________ |
---|
357 | BOOLEAN enviaMensajeServidor(SOCKET *socket_c,TRAMA *ptrTrama,char tipo) |
---|
358 | { |
---|
359 | int lon; |
---|
360 | char modulo[] = "enviaMensajeServidor()"; |
---|
361 | |
---|
362 | *socket_c=abreConexion(); |
---|
363 | if(*socket_c==INVALID_SOCKET){ |
---|
364 | errorLog(modulo,38,FALSE); // Error de conexión con el servidor |
---|
365 | return(FALSE); |
---|
366 | } |
---|
367 | ptrTrama->arroba='@'; // Cabecera de la trama |
---|
368 | strncpy(ptrTrama->identificador,"JMMLCAMDJ_MCDJ",14); // identificador de la trama |
---|
369 | ptrTrama->tipo=tipo; // Tipo de mensaje |
---|
370 | lon=strlen(ptrTrama->parametros); // Compone la trama |
---|
371 | lon+=sprintf(ptrTrama->parametros+lon,"iph=%s\r",IPlocal); // Ip del ordenador |
---|
372 | lon+=sprintf(ptrTrama->parametros+lon,"ido=%s\r",idordenador); // Identificador del ordenador |
---|
373 | lon+=sprintf(ptrTrama->parametros+lon,"npc=%s\r",nombreordenador); // Nombre del ordenador |
---|
374 | |
---|
375 | if (!mandaTrama(socket_c,ptrTrama)) { |
---|
376 | errorLog(modulo,26,FALSE); |
---|
377 | return (FALSE); |
---|
378 | } |
---|
379 | return(TRUE); |
---|
380 | } |
---|
381 | // ******************************************************************************************************** |
---|
382 | // PROGRAMA PRINCIPAL (CLIENTE) |
---|
383 | // ******************************************************************************************************** |
---|
384 | int main(int argc, char *argv[]) |
---|
385 | { |
---|
386 | TRAMA *ptrTrama; |
---|
387 | char modulo[] = "main()"; |
---|
388 | |
---|
389 | ptrTrama=(TRAMA *)reservaMemoria(sizeof(TRAMA)); |
---|
390 | if (ptrTrama == NULL) { // No hay memoria suficiente para el bufer de las tramas |
---|
391 | errorLog(modulo, 3, FALSE); |
---|
392 | exit(EXIT_FAILURE); |
---|
393 | } |
---|
394 | /*-------------------------------------------------------------------------------------------------------- |
---|
395 | Validación de parámetros de ejecución y fichero de configuración |
---|
396 | ---------------------------------------------------------------------------------------------------------*/ |
---|
397 | /*-------------------------------------------------------------------------------------------------------- |
---|
398 | Validación de parámetros de ejecución y fichero de configuración |
---|
399 | ---------------------------------------------------------------------------------------------------------*/ |
---|
400 | if (!validacionParametros(argc, argv,6)) // Valida parámetros de ejecución |
---|
401 | exit(EXIT_FAILURE); |
---|
402 | |
---|
403 | if (!tomaConfiguracion(szPathFileCfg)) // Toma parametros de configuración |
---|
404 | exit(EXIT_FAILURE); |
---|
405 | |
---|
406 | /*-------------------------------------------------------------------------------------------------------- |
---|
407 | Carga catálogo de funciones que procesan las tramas |
---|
408 | ---------------------------------------------------------------------------------------------------------*/ |
---|
409 | int cf = 0; |
---|
410 | |
---|
411 | strcpy(tbfuncionesClient[cf].nf, "RESPUESTA_InclusionClienteWinLnx"); |
---|
412 | tbfuncionesClient[cf++].fptr = &RESPUESTA_InclusionClienteWinLnx; |
---|
413 | |
---|
414 | strcpy(tbfuncionesClient[cf].nf, "Apagar"); |
---|
415 | tbfuncionesClient[cf++].fptr = &Apagar; |
---|
416 | |
---|
417 | strcpy(tbfuncionesClient[cf].nf, "Reiniciar"); |
---|
418 | tbfuncionesClient[cf++].fptr = &Reiniciar; |
---|
419 | |
---|
420 | strcpy(tbfuncionesClient[cf].nf, "Sondeo"); |
---|
421 | tbfuncionesClient[cf++].fptr = &Sondeo; |
---|
422 | |
---|
423 | /*-------------------------------------------------------------------------------------------------------- |
---|
424 | Inicio de sesión |
---|
425 | ---------------------------------------------------------------------------------------------------------*/ |
---|
426 | infoLog(1); // Inicio de sesión |
---|
427 | infoLog(3); // Abriendo sesión en el servidor de Administración; |
---|
428 | /*-------------------------------------------------------------------------------------------------------- |
---|
429 | Inclusión del cliente en el sistema |
---|
430 | ---------------------------------------------------------------------------------------------------------*/ |
---|
431 | if(!InclusionClienteWinLnx(ptrTrama)){ // Ha habido algún problema al abrir sesión |
---|
432 | errorLog(modulo,0,FALSE); |
---|
433 | exit(EXIT_FAILURE); |
---|
434 | } |
---|
435 | infoLog(4); // Cliente iniciado |
---|
436 | procesaComandos(ptrTrama); // Bucle para procesar comandos interactivos |
---|
437 | exit(EXIT_SUCCESS); |
---|
438 | } |
---|