1 | // ******************************************************************************************************* |
---|
2 | // Servicio: ogAdmServer |
---|
3 | // Autor: José Manuel Alonso (E.T.S.I.I.) Universidad de Sevilla |
---|
4 | // Fecha Creación: Marzo-2010 |
---|
5 | // Fecha Última modificación: Marzo-2010 |
---|
6 | // Nombre del fichero: ogAdmServer.cpp |
---|
7 | // Descripción :Este fichero implementa el servicio de administración general del sistema |
---|
8 | // ******************************************************************************************************* |
---|
9 | #include "ogAdmServer.h" |
---|
10 | #include "ogAdmLib.c" |
---|
11 | #include <ev.h> |
---|
12 | #include <syslog.h> |
---|
13 | #include <sys/ioctl.h> |
---|
14 | #include <ifaddrs.h> |
---|
15 | #include <sys/types.h> |
---|
16 | #include <sys/stat.h> |
---|
17 | #include <fcntl.h> |
---|
18 | #include <jansson.h> |
---|
19 | |
---|
20 | static char usuario[LONPRM]; // Usuario de acceso a la base de datos |
---|
21 | static char pasguor[LONPRM]; // Password del usuario |
---|
22 | static char datasource[LONPRM]; // Dirección IP del gestor de base de datos |
---|
23 | static char catalog[LONPRM]; // Nombre de la base de datos |
---|
24 | static char interface[LONPRM]; // Interface name |
---|
25 | static char auth_token[LONPRM]; // API token |
---|
26 | |
---|
27 | //________________________________________________________________________________________________________ |
---|
28 | // Función: tomaConfiguracion |
---|
29 | // |
---|
30 | // Descripción: |
---|
31 | // Lee el fichero de configuración del servicio |
---|
32 | // Parámetros: |
---|
33 | // filecfg : Ruta completa al fichero de configuración |
---|
34 | // Devuelve: |
---|
35 | // true: Si el proceso es correcto |
---|
36 | // false: En caso de ocurrir algún error |
---|
37 | //________________________________________________________________________________________________________ |
---|
38 | static bool tomaConfiguracion(const char *filecfg) |
---|
39 | { |
---|
40 | char buf[1024], *line; |
---|
41 | char *key, *value; |
---|
42 | FILE *fcfg; |
---|
43 | |
---|
44 | if (filecfg == NULL || strlen(filecfg) == 0) { |
---|
45 | syslog(LOG_ERR, "No configuration file has been specified\n"); |
---|
46 | return false; |
---|
47 | } |
---|
48 | |
---|
49 | fcfg = fopen(filecfg, "rt"); |
---|
50 | if (fcfg == NULL) { |
---|
51 | syslog(LOG_ERR, "Cannot open configuration file `%s'\n", |
---|
52 | filecfg); |
---|
53 | return false; |
---|
54 | } |
---|
55 | |
---|
56 | servidoradm[0] = (char) NULL; //inicializar variables globales |
---|
57 | |
---|
58 | line = fgets(buf, sizeof(buf), fcfg); |
---|
59 | while (line != NULL) { |
---|
60 | const char *delim = "="; |
---|
61 | |
---|
62 | line[strlen(line) - 1] = '\0'; |
---|
63 | |
---|
64 | key = strtok(line, delim); |
---|
65 | value = strtok(NULL, delim); |
---|
66 | |
---|
67 | if (!strcmp(StrToUpper(key), "SERVIDORADM")) |
---|
68 | snprintf(servidoradm, sizeof(servidoradm), "%s", value); |
---|
69 | else if (!strcmp(StrToUpper(key), "PUERTO")) |
---|
70 | snprintf(puerto, sizeof(puerto), "%s", value); |
---|
71 | else if (!strcmp(StrToUpper(key), "USUARIO")) |
---|
72 | snprintf(usuario, sizeof(usuario), "%s", value); |
---|
73 | else if (!strcmp(StrToUpper(key), "PASSWORD")) |
---|
74 | snprintf(pasguor, sizeof(pasguor), "%s", value); |
---|
75 | else if (!strcmp(StrToUpper(key), "DATASOURCE")) |
---|
76 | snprintf(datasource, sizeof(datasource), "%s", value); |
---|
77 | else if (!strcmp(StrToUpper(key), "CATALOG")) |
---|
78 | snprintf(catalog, sizeof(catalog), "%s", value); |
---|
79 | else if (!strcmp(StrToUpper(key), "INTERFACE")) |
---|
80 | snprintf(interface, sizeof(interface), "%s", value); |
---|
81 | else if (!strcmp(StrToUpper(key), "APITOKEN")) |
---|
82 | snprintf(auth_token, sizeof(auth_token), "%s", value); |
---|
83 | |
---|
84 | line = fgets(buf, sizeof(buf), fcfg); |
---|
85 | } |
---|
86 | |
---|
87 | fclose(fcfg); |
---|
88 | |
---|
89 | if (!servidoradm[0]) { |
---|
90 | syslog(LOG_ERR, "Missing SERVIDORADM in configuration file\n"); |
---|
91 | return false; |
---|
92 | } |
---|
93 | if (!puerto[0]) { |
---|
94 | syslog(LOG_ERR, "Missing PUERTO in configuration file\n"); |
---|
95 | return false; |
---|
96 | } |
---|
97 | if (!usuario[0]) { |
---|
98 | syslog(LOG_ERR, "Missing USUARIO in configuration file\n"); |
---|
99 | return false; |
---|
100 | } |
---|
101 | if (!pasguor[0]) { |
---|
102 | syslog(LOG_ERR, "Missing PASSWORD in configuration file\n"); |
---|
103 | return false; |
---|
104 | } |
---|
105 | if (!datasource[0]) { |
---|
106 | syslog(LOG_ERR, "Missing DATASOURCE in configuration file\n"); |
---|
107 | return false; |
---|
108 | } |
---|
109 | if (!catalog[0]) { |
---|
110 | syslog(LOG_ERR, "Missing CATALOG in configuration file\n"); |
---|
111 | return false; |
---|
112 | } |
---|
113 | if (!interface[0]) |
---|
114 | syslog(LOG_ERR, "Missing INTERFACE in configuration file\n"); |
---|
115 | |
---|
116 | return true; |
---|
117 | } |
---|
118 | |
---|
119 | enum og_client_state { |
---|
120 | OG_CLIENT_RECEIVING_HEADER = 0, |
---|
121 | OG_CLIENT_RECEIVING_PAYLOAD, |
---|
122 | OG_CLIENT_PROCESSING_REQUEST, |
---|
123 | }; |
---|
124 | |
---|
125 | /* Shut down connection if there is no complete message after 10 seconds. */ |
---|
126 | #define OG_CLIENT_TIMEOUT 10 |
---|
127 | |
---|
128 | struct og_client { |
---|
129 | struct ev_io io; |
---|
130 | struct ev_timer timer; |
---|
131 | struct sockaddr_in addr; |
---|
132 | enum og_client_state state; |
---|
133 | char buf[4096]; |
---|
134 | unsigned int buf_len; |
---|
135 | unsigned int msg_len; |
---|
136 | int keepalive_idx; |
---|
137 | bool rest; |
---|
138 | unsigned int content_length; |
---|
139 | char auth_token[64]; |
---|
140 | }; |
---|
141 | |
---|
142 | static inline int og_client_socket(const struct og_client *cli) |
---|
143 | { |
---|
144 | return cli->io.fd; |
---|
145 | } |
---|
146 | |
---|
147 | // ________________________________________________________________________________________________________ |
---|
148 | // Función: clienteDisponible |
---|
149 | // |
---|
150 | // Descripción: |
---|
151 | // Comprueba la disponibilidad del cliente para recibir comandos interactivos |
---|
152 | // Parametros: |
---|
153 | // - ip : La ip del cliente a buscar |
---|
154 | // - idx: (Salida) Indice que ocupa el cliente, de estar ya registrado |
---|
155 | // Devuelve: |
---|
156 | // true: Si el cliente está disponible |
---|
157 | // false: En caso contrario |
---|
158 | // ________________________________________________________________________________________________________ |
---|
159 | bool clienteDisponible(char *ip, int* idx) |
---|
160 | { |
---|
161 | int estado; |
---|
162 | |
---|
163 | if (clienteExistente(ip, idx)) { |
---|
164 | estado = strcmp(tbsockets[*idx].estado, CLIENTE_OCUPADO); // Cliente ocupado |
---|
165 | if (estado == 0) |
---|
166 | return false; |
---|
167 | |
---|
168 | estado = strcmp(tbsockets[*idx].estado, CLIENTE_APAGADO); // Cliente apagado |
---|
169 | if (estado == 0) |
---|
170 | return false; |
---|
171 | |
---|
172 | estado = strcmp(tbsockets[*idx].estado, CLIENTE_INICIANDO); // Cliente en proceso de inclusión |
---|
173 | if (estado == 0) |
---|
174 | return false; |
---|
175 | |
---|
176 | return true; // En caso contrario el cliente está disponible |
---|
177 | } |
---|
178 | return false; // Cliente no está registrado en el sistema |
---|
179 | } |
---|
180 | // ________________________________________________________________________________________________________ |
---|
181 | // Función: clienteExistente |
---|
182 | // |
---|
183 | // Descripción: |
---|
184 | // Comprueba si el cliente está registrado en la tabla de socket del sistema |
---|
185 | // Parametros: |
---|
186 | // - ip : La ip del cliente a buscar |
---|
187 | // - idx:(Salida) Indice que ocupa el cliente, de estar ya registrado |
---|
188 | // Devuelve: |
---|
189 | // true: Si el cliente está registrado |
---|
190 | // false: En caso contrario |
---|
191 | // ________________________________________________________________________________________________________ |
---|
192 | bool clienteExistente(char *ip, int* idx) |
---|
193 | { |
---|
194 | int i; |
---|
195 | for (i = 0; i < MAXIMOS_CLIENTES; i++) { |
---|
196 | if (contieneIP(ip, tbsockets[i].ip)) { // Si existe la IP en la cadena |
---|
197 | *idx = i; |
---|
198 | return true; |
---|
199 | } |
---|
200 | } |
---|
201 | return false; |
---|
202 | } |
---|
203 | // ________________________________________________________________________________________________________ |
---|
204 | // Función: hayHueco |
---|
205 | // |
---|
206 | // Descripción: |
---|
207 | // Esta función devuelve true o false dependiendo de que haya hueco en la tabla de sockets para un nuevo cliente. |
---|
208 | // Parametros: |
---|
209 | // - idx: Primer indice libre que se podrn utilizar |
---|
210 | // Devuelve: |
---|
211 | // true: Si el proceso es correcto |
---|
212 | // false: En caso de ocurrir algún error |
---|
213 | // ________________________________________________________________________________________________________ |
---|
214 | static bool hayHueco(int *idx) |
---|
215 | { |
---|
216 | int i; |
---|
217 | |
---|
218 | for (i = 0; i < MAXIMOS_CLIENTES; i++) { |
---|
219 | if (strncmp(tbsockets[i].ip, "\0", 1) == 0) { // Hay un hueco |
---|
220 | *idx = i; |
---|
221 | return true; |
---|
222 | } |
---|
223 | } |
---|
224 | return false; |
---|
225 | } |
---|
226 | // ________________________________________________________________________________________________________ |
---|
227 | // Función: InclusionClienteWin |
---|
228 | // |
---|
229 | // Descripción: |
---|
230 | // Esta función incorpora el socket de un nuevo cliente Windows o Linux a la tabla de clientes |
---|
231 | // Parámetros: |
---|
232 | // - socket_c: Socket del cliente que envió el mensaje |
---|
233 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
234 | // Devuelve: |
---|
235 | // true: Si el proceso es correcto |
---|
236 | // false: En caso de ocurrir algún error |
---|
237 | // ________________________________________________________________________________________________________ |
---|
238 | static bool InclusionClienteWinLnx(TRAMA *ptrTrama, struct og_client *cli) |
---|
239 | { |
---|
240 | int socket_c = og_client_socket(cli); |
---|
241 | int res,idordenador,lon; |
---|
242 | char nombreordenador[LONFIL]; |
---|
243 | |
---|
244 | res = procesoInclusionClienteWinLnx(socket_c, ptrTrama, &idordenador, |
---|
245 | nombreordenador); |
---|
246 | |
---|
247 | // Prepara la trama de respuesta |
---|
248 | |
---|
249 | initParametros(ptrTrama,0); |
---|
250 | ptrTrama->tipo=MSG_RESPUESTA; |
---|
251 | lon = sprintf(ptrTrama->parametros, "nfn=RESPUESTA_InclusionClienteWinLnx\r"); |
---|
252 | lon += sprintf(ptrTrama->parametros + lon, "ido=%d\r", idordenador); |
---|
253 | lon += sprintf(ptrTrama->parametros + lon, "npc=%s\r", nombreordenador); |
---|
254 | lon += sprintf(ptrTrama->parametros + lon, "res=%d\r", res); |
---|
255 | |
---|
256 | if (!mandaTrama(&socket_c, ptrTrama)) { |
---|
257 | syslog(LOG_ERR, "failed to send response to %s:%hu reason=%s\n", |
---|
258 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port), |
---|
259 | strerror(errno)); |
---|
260 | return false; |
---|
261 | } |
---|
262 | return true; |
---|
263 | } |
---|
264 | // ________________________________________________________________________________________________________ |
---|
265 | // Función: procesoInclusionClienteWinLnx |
---|
266 | // |
---|
267 | // Descripción: |
---|
268 | // Implementa el proceso de inclusión en el sistema del Cliente Windows o Linux |
---|
269 | // Parámetros de entrada: |
---|
270 | // - socket_c: Socket del cliente que envió el mensaje |
---|
271 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
272 | // Parámetros de salida: |
---|
273 | // - ido: Identificador del ordenador |
---|
274 | // - nombreordenador: Nombre del ordenador |
---|
275 | // Devuelve: |
---|
276 | // Código del error producido en caso de ocurrir algún error, 0 si el proceso es correcto |
---|
277 | // ________________________________________________________________________________________________________ |
---|
278 | bool procesoInclusionClienteWinLnx(int socket_c, TRAMA *ptrTrama, int *idordenador, char *nombreordenador) |
---|
279 | { |
---|
280 | char msglog[LONSTD], sqlstr[LONSQL]; |
---|
281 | Database db; |
---|
282 | Table tbl; |
---|
283 | char *iph; |
---|
284 | |
---|
285 | // Toma parámetros |
---|
286 | iph = copiaParametro("iph",ptrTrama); // Toma ip |
---|
287 | |
---|
288 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
289 | liberaMemoria(iph); |
---|
290 | db.GetErrorErrStr(msglog); |
---|
291 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
292 | __func__, __LINE__, msglog); |
---|
293 | return false; |
---|
294 | } |
---|
295 | |
---|
296 | // Recupera los datos del cliente |
---|
297 | sprintf(sqlstr, |
---|
298 | "SELECT idordenador,nombreordenador FROM ordenadores " |
---|
299 | " WHERE ordenadores.ip = '%s'", iph); |
---|
300 | |
---|
301 | if (!db.Execute(sqlstr, tbl)) { |
---|
302 | liberaMemoria(iph); |
---|
303 | db.GetErrorErrStr(msglog); |
---|
304 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
305 | __func__, __LINE__, msglog); |
---|
306 | db.Close(); |
---|
307 | return false; |
---|
308 | } |
---|
309 | |
---|
310 | if (tbl.ISEOF()) { |
---|
311 | liberaMemoria(iph); |
---|
312 | syslog(LOG_ERR, "client does not exist in database (%s:%d)\n", |
---|
313 | __func__, __LINE__); |
---|
314 | db.liberaResult(tbl); |
---|
315 | db.Close(); |
---|
316 | return false; |
---|
317 | } |
---|
318 | |
---|
319 | syslog(LOG_DEBUG, "Client %s requesting inclusion\n", iph); |
---|
320 | |
---|
321 | if (!tbl.Get("idordenador", *idordenador)) { |
---|
322 | liberaMemoria(iph); |
---|
323 | db.liberaResult(tbl); |
---|
324 | tbl.GetErrorErrStr(msglog); |
---|
325 | og_info(msglog); |
---|
326 | db.Close(); |
---|
327 | return false; |
---|
328 | } |
---|
329 | if (!tbl.Get("nombreordenador", nombreordenador)) { |
---|
330 | liberaMemoria(iph); |
---|
331 | db.liberaResult(tbl); |
---|
332 | tbl.GetErrorErrStr(msglog); |
---|
333 | og_info(msglog); |
---|
334 | db.Close(); |
---|
335 | return false; |
---|
336 | } |
---|
337 | db.liberaResult(tbl); |
---|
338 | db.Close(); |
---|
339 | |
---|
340 | if (!registraCliente(iph)) { // Incluyendo al cliente en la tabla de sokets |
---|
341 | liberaMemoria(iph); |
---|
342 | syslog(LOG_ERR, "client table is full\n"); |
---|
343 | return false; |
---|
344 | } |
---|
345 | liberaMemoria(iph); |
---|
346 | return true; |
---|
347 | } |
---|
348 | // ________________________________________________________________________________________________________ |
---|
349 | // Función: InclusionCliente |
---|
350 | // |
---|
351 | // Descripción: |
---|
352 | // Esta función incorpora el socket de un nuevo cliente a la tabla de clientes y le devuelve alguna de sus propiedades: |
---|
353 | // nombre, identificador, tamaño de la caché , etc ... |
---|
354 | // Parámetros: |
---|
355 | // - socket_c: Socket del cliente que envió el mensaje |
---|
356 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
357 | // Devuelve: |
---|
358 | // true: Si el proceso es correcto |
---|
359 | // false: En caso de ocurrir algún error |
---|
360 | // ________________________________________________________________________________________________________ |
---|
361 | static bool InclusionCliente(TRAMA *ptrTrama, struct og_client *cli) |
---|
362 | { |
---|
363 | int socket_c = og_client_socket(cli); |
---|
364 | |
---|
365 | if (!procesoInclusionCliente(cli, ptrTrama)) { |
---|
366 | initParametros(ptrTrama,0); |
---|
367 | strcpy(ptrTrama->parametros, "nfn=RESPUESTA_InclusionCliente\rres=0\r"); |
---|
368 | if (!mandaTrama(&socket_c, ptrTrama)) { |
---|
369 | syslog(LOG_ERR, "failed to send response to %s:%hu reason=%s\n", |
---|
370 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port), |
---|
371 | strerror(errno)); |
---|
372 | return false; |
---|
373 | } |
---|
374 | } |
---|
375 | return true; |
---|
376 | } |
---|
377 | // ________________________________________________________________________________________________________ |
---|
378 | // Función: procesoInclusionCliente |
---|
379 | // |
---|
380 | // Descripción: |
---|
381 | // Implementa el proceso de inclusión en el sistema del Cliente |
---|
382 | // Parámetros: |
---|
383 | // - socket_c: Socket del cliente que envió el mensaje |
---|
384 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
385 | // Devuelve: |
---|
386 | // true: Si el proceso es correcto |
---|
387 | // false: En caso de ocurrir algún error |
---|
388 | // ________________________________________________________________________________________________________ |
---|
389 | bool procesoInclusionCliente(struct og_client *cli, TRAMA *ptrTrama) |
---|
390 | { |
---|
391 | int socket_c = og_client_socket(cli); |
---|
392 | char msglog[LONSTD], sqlstr[LONSQL]; |
---|
393 | Database db; |
---|
394 | Table tbl; |
---|
395 | |
---|
396 | char *iph, *cfg; |
---|
397 | char nombreordenador[LONFIL]; |
---|
398 | int lon, resul, idordenador, idmenu, cache, idproautoexec, idaula, idcentro; |
---|
399 | |
---|
400 | // Toma parámetros |
---|
401 | iph = copiaParametro("iph",ptrTrama); // Toma ip |
---|
402 | cfg = copiaParametro("cfg",ptrTrama); // Toma configuracion |
---|
403 | |
---|
404 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
405 | liberaMemoria(iph); |
---|
406 | liberaMemoria(cfg); |
---|
407 | db.GetErrorErrStr(msglog); |
---|
408 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
409 | __func__, __LINE__, msglog); |
---|
410 | return false; |
---|
411 | } |
---|
412 | |
---|
413 | // Recupera los datos del cliente |
---|
414 | sprintf(sqlstr, |
---|
415 | "SELECT ordenadores.*,aulas.idaula,centros.idcentro FROM ordenadores " |
---|
416 | " INNER JOIN aulas ON aulas.idaula=ordenadores.idaula" |
---|
417 | " INNER JOIN centros ON centros.idcentro=aulas.idcentro" |
---|
418 | " WHERE ordenadores.ip = '%s'", iph); |
---|
419 | |
---|
420 | if (!db.Execute(sqlstr, tbl)) { |
---|
421 | db.GetErrorErrStr(msglog); |
---|
422 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
423 | __func__, __LINE__, msglog); |
---|
424 | return false; |
---|
425 | } |
---|
426 | |
---|
427 | if (tbl.ISEOF()) { |
---|
428 | syslog(LOG_ERR, "client does not exist in database (%s:%d)\n", |
---|
429 | __func__, __LINE__); |
---|
430 | return false; |
---|
431 | } |
---|
432 | |
---|
433 | syslog(LOG_DEBUG, "Client %s requesting inclusion\n", iph); |
---|
434 | |
---|
435 | if (!tbl.Get("idordenador", idordenador)) { |
---|
436 | tbl.GetErrorErrStr(msglog); |
---|
437 | og_info(msglog); |
---|
438 | return false; |
---|
439 | } |
---|
440 | if (!tbl.Get("nombreordenador", nombreordenador)) { |
---|
441 | tbl.GetErrorErrStr(msglog); |
---|
442 | og_info(msglog); |
---|
443 | return false; |
---|
444 | } |
---|
445 | if (!tbl.Get("idmenu", idmenu)) { |
---|
446 | tbl.GetErrorErrStr(msglog); |
---|
447 | og_info(msglog); |
---|
448 | return false; |
---|
449 | } |
---|
450 | if (!tbl.Get("cache", cache)) { |
---|
451 | tbl.GetErrorErrStr(msglog); |
---|
452 | og_info(msglog); |
---|
453 | return false; |
---|
454 | } |
---|
455 | if (!tbl.Get("idproautoexec", idproautoexec)) { |
---|
456 | tbl.GetErrorErrStr(msglog); |
---|
457 | og_info(msglog); |
---|
458 | return false; |
---|
459 | } |
---|
460 | if (!tbl.Get("idaula", idaula)) { |
---|
461 | tbl.GetErrorErrStr(msglog); |
---|
462 | og_info(msglog); |
---|
463 | return false; |
---|
464 | } |
---|
465 | if (!tbl.Get("idcentro", idcentro)) { |
---|
466 | tbl.GetErrorErrStr(msglog); |
---|
467 | og_info(msglog); |
---|
468 | return false; |
---|
469 | } |
---|
470 | |
---|
471 | resul = actualizaConfiguracion(db, tbl, cfg, idordenador); // Actualiza la configuración del ordenador |
---|
472 | liberaMemoria(cfg); |
---|
473 | db.Close(); |
---|
474 | |
---|
475 | if (!resul) { |
---|
476 | liberaMemoria(iph); |
---|
477 | syslog(LOG_ERR, "Cannot add client to database\n"); |
---|
478 | return false; |
---|
479 | } |
---|
480 | |
---|
481 | if (!registraCliente(iph)) { // Incluyendo al cliente en la tabla de sokets |
---|
482 | liberaMemoria(iph); |
---|
483 | syslog(LOG_ERR, "client table is full\n"); |
---|
484 | return false; |
---|
485 | } |
---|
486 | |
---|
487 | /*------------------------------------------------------------------------------------------------------------------------------ |
---|
488 | Prepara la trama de respuesta |
---|
489 | -------------------------------------------------------------------------------------------------------------------------------*/ |
---|
490 | initParametros(ptrTrama,0); |
---|
491 | ptrTrama->tipo=MSG_RESPUESTA; |
---|
492 | lon = sprintf(ptrTrama->parametros, "nfn=RESPUESTA_InclusionCliente\r"); |
---|
493 | lon += sprintf(ptrTrama->parametros + lon, "ido=%d\r", idordenador); |
---|
494 | lon += sprintf(ptrTrama->parametros + lon, "npc=%s\r", nombreordenador); |
---|
495 | lon += sprintf(ptrTrama->parametros + lon, "che=%d\r", cache); |
---|
496 | lon += sprintf(ptrTrama->parametros + lon, "exe=%d\r", idproautoexec); |
---|
497 | lon += sprintf(ptrTrama->parametros + lon, "ida=%d\r", idaula); |
---|
498 | lon += sprintf(ptrTrama->parametros + lon, "idc=%d\r", idcentro); |
---|
499 | lon += sprintf(ptrTrama->parametros + lon, "res=%d\r", 1); // Confirmación proceso correcto |
---|
500 | |
---|
501 | if (!mandaTrama(&socket_c, ptrTrama)) { |
---|
502 | syslog(LOG_ERR, "failed to send response to %s:%hu reason=%s\n", |
---|
503 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port), |
---|
504 | strerror(errno)); |
---|
505 | return false; |
---|
506 | } |
---|
507 | liberaMemoria(iph); |
---|
508 | return true; |
---|
509 | } |
---|
510 | // ________________________________________________________________________________________________________ |
---|
511 | // Función: actualizaConfiguracion |
---|
512 | // |
---|
513 | // Descripción: |
---|
514 | // Esta función actualiza la base de datos con la configuracion de particiones de un cliente |
---|
515 | // Parámetros: |
---|
516 | // - db: Objeto base de datos (ya operativo) |
---|
517 | // - tbl: Objeto tabla |
---|
518 | // - cfg: cadena con una Configuración |
---|
519 | // - ido: Identificador del ordenador cliente |
---|
520 | // Devuelve: |
---|
521 | // true: Si el proceso es correcto |
---|
522 | // false: En caso de ocurrir algún error |
---|
523 | // Especificaciones: |
---|
524 | // Los parametros de la configuración son: |
---|
525 | // par= Número de partición |
---|
526 | // cpt= Codigo o tipo de partición |
---|
527 | // sfi= Sistema de ficheros que está implementado en la partición |
---|
528 | // soi= Nombre del sistema de ficheros instalado en la partición |
---|
529 | // tam= Tamaño de la partición |
---|
530 | // ________________________________________________________________________________________________________ |
---|
531 | bool actualizaConfiguracion(Database db, Table tbl, char *cfg, int ido) |
---|
532 | { |
---|
533 | char msglog[LONSTD], sqlstr[LONSQL]; |
---|
534 | int lon, p, c,i, dato, swu, idsoi, idsfi,k; |
---|
535 | char *ptrPar[MAXPAR], *ptrCfg[6], *ptrDual[2], tbPar[LONSTD]; |
---|
536 | char *ser, *disk, *par, *cpt, *sfi, *soi, *tam, *uso; // Parametros de configuración. |
---|
537 | |
---|
538 | lon = 0; |
---|
539 | p = splitCadena(ptrPar, cfg, '\n'); |
---|
540 | for (i = 0; i < p; i++) { |
---|
541 | c = splitCadena(ptrCfg, ptrPar[i], '\t'); |
---|
542 | |
---|
543 | // Si la 1ª línea solo incluye el número de serie del equipo; actualizar BD. |
---|
544 | if (i == 0 && c == 1) { |
---|
545 | splitCadena(ptrDual, ptrCfg[0], '='); |
---|
546 | ser = ptrDual[1]; |
---|
547 | if (strlen(ser) > 0) { |
---|
548 | // Solo actualizar si número de serie no existía. |
---|
549 | sprintf(sqlstr, "UPDATE ordenadores SET numserie='%s'" |
---|
550 | " WHERE idordenador=%d AND numserie IS NULL", |
---|
551 | ser, ido); |
---|
552 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
553 | db.GetErrorErrStr(msglog); |
---|
554 | og_info(msglog); |
---|
555 | return false; |
---|
556 | } |
---|
557 | } |
---|
558 | continue; |
---|
559 | } |
---|
560 | |
---|
561 | // Distribución de particionado. |
---|
562 | disk = par = cpt = sfi = soi = tam = uso = NULL; |
---|
563 | |
---|
564 | splitCadena(ptrDual, ptrCfg[0], '='); |
---|
565 | disk = ptrDual[1]; // Número de disco |
---|
566 | |
---|
567 | splitCadena(ptrDual, ptrCfg[1], '='); |
---|
568 | par = ptrDual[1]; // Número de partición |
---|
569 | |
---|
570 | k=splitCadena(ptrDual, ptrCfg[2], '='); |
---|
571 | if(k==2){ |
---|
572 | cpt = ptrDual[1]; // Código de partición |
---|
573 | }else{ |
---|
574 | cpt = (char*)"0"; |
---|
575 | } |
---|
576 | |
---|
577 | k=splitCadena(ptrDual, ptrCfg[3], '='); |
---|
578 | if(k==2){ |
---|
579 | sfi = ptrDual[1]; // Sistema de ficheros |
---|
580 | /* Comprueba existencia del s0xistema de ficheros instalado */ |
---|
581 | idsfi = checkDato(db, tbl, sfi, "sistemasficheros", "descripcion","idsistemafichero"); |
---|
582 | } |
---|
583 | else |
---|
584 | idsfi=0; |
---|
585 | |
---|
586 | k=splitCadena(ptrDual, ptrCfg[4], '='); |
---|
587 | if(k==2){ // Sistema operativo detecdtado |
---|
588 | soi = ptrDual[1]; // Nombre del S.O. instalado |
---|
589 | /* Comprueba existencia del sistema operativo instalado */ |
---|
590 | idsoi = checkDato(db, tbl, soi, "nombresos", "nombreso", "idnombreso"); |
---|
591 | } |
---|
592 | else |
---|
593 | idsoi=0; |
---|
594 | |
---|
595 | splitCadena(ptrDual, ptrCfg[5], '='); |
---|
596 | tam = ptrDual[1]; // Tamaño de la partición |
---|
597 | |
---|
598 | splitCadena(ptrDual, ptrCfg[6], '='); |
---|
599 | uso = ptrDual[1]; // Porcentaje de uso del S.F. |
---|
600 | |
---|
601 | lon += sprintf(tbPar + lon, "(%s, %s),", disk, par); |
---|
602 | |
---|
603 | sprintf(sqlstr, "SELECT numdisk, numpar, codpar, tamano, uso, idsistemafichero, idnombreso" |
---|
604 | " FROM ordenadores_particiones" |
---|
605 | " WHERE idordenador=%d AND numdisk=%s AND numpar=%s", |
---|
606 | ido, disk, par); |
---|
607 | |
---|
608 | |
---|
609 | if (!db.Execute(sqlstr, tbl)) { |
---|
610 | db.GetErrorErrStr(msglog); |
---|
611 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
612 | __func__, __LINE__, msglog); |
---|
613 | return false; |
---|
614 | } |
---|
615 | if (tbl.ISEOF()) { // Si no existe el registro |
---|
616 | sprintf(sqlstr, "INSERT INTO ordenadores_particiones(idordenador,numdisk,numpar,codpar,tamano,uso,idsistemafichero,idnombreso,idimagen)" |
---|
617 | " VALUES(%d,%s,%s,0x%s,%s,%s,%d,%d,0)", |
---|
618 | ido, disk, par, cpt, tam, uso, idsfi, idsoi); |
---|
619 | |
---|
620 | |
---|
621 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
622 | db.GetErrorErrStr(msglog); |
---|
623 | og_info(msglog); |
---|
624 | return false; |
---|
625 | } |
---|
626 | } else { // Existe el registro |
---|
627 | swu = true; // Se supone que algún dato ha cambiado |
---|
628 | if (!tbl.Get("codpar", dato)) { // Toma dato |
---|
629 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
630 | og_info(msglog); |
---|
631 | return false; |
---|
632 | } |
---|
633 | if (strtol(cpt, NULL, 16) == dato) {// Parámetro tipo de partición (hexadecimal) igual al almacenado (decimal) |
---|
634 | if (!tbl.Get("tamano", dato)) { // Toma dato |
---|
635 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
636 | og_info(msglog); |
---|
637 | return false; |
---|
638 | } |
---|
639 | if (atoi(tam) == dato) {// Parámetro tamaño igual al almacenado |
---|
640 | if (!tbl.Get("idsistemafichero", dato)) { // Toma dato |
---|
641 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
642 | og_info(msglog); |
---|
643 | return false; |
---|
644 | } |
---|
645 | if (idsfi == dato) {// Parámetro sistema de fichero igual al almacenado |
---|
646 | if (!tbl.Get("idnombreso", dato)) { // Toma dato |
---|
647 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
648 | og_info(msglog); |
---|
649 | return false; |
---|
650 | } |
---|
651 | if (idsoi == dato) {// Parámetro sistema de fichero distinto al almacenado |
---|
652 | swu = false; // Todos los parámetros de la partición son iguales, no se actualiza |
---|
653 | } |
---|
654 | } |
---|
655 | } |
---|
656 | } |
---|
657 | if (swu) { // Hay que actualizar los parámetros de la partición |
---|
658 | sprintf(sqlstr, "UPDATE ordenadores_particiones SET " |
---|
659 | " codpar=0x%s," |
---|
660 | " tamano=%s," |
---|
661 | " uso=%s," |
---|
662 | " idsistemafichero=%d," |
---|
663 | " idnombreso=%d," |
---|
664 | " idimagen=0," |
---|
665 | " idperfilsoft=0," |
---|
666 | " fechadespliegue=NULL" |
---|
667 | " WHERE idordenador=%d AND numdisk=%s AND numpar=%s", |
---|
668 | cpt, tam, uso, idsfi, idsoi, ido, disk, par); |
---|
669 | } else { // Actualizar porcentaje de uso. |
---|
670 | sprintf(sqlstr, "UPDATE ordenadores_particiones SET " |
---|
671 | " uso=%s" |
---|
672 | " WHERE idordenador=%d AND numdisk=%s AND numpar=%s", |
---|
673 | uso, ido, disk, par); |
---|
674 | } |
---|
675 | if (!db.Execute(sqlstr, tbl)) { |
---|
676 | db.GetErrorErrStr(msglog); |
---|
677 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
678 | __func__, __LINE__, msglog); |
---|
679 | return false; |
---|
680 | } |
---|
681 | } |
---|
682 | } |
---|
683 | lon += sprintf(tbPar + lon, "(0,0)"); |
---|
684 | // Eliminar particiones almacenadas que ya no existen |
---|
685 | sprintf(sqlstr, "DELETE FROM ordenadores_particiones WHERE idordenador=%d AND (numdisk, numpar) NOT IN (%s)", |
---|
686 | ido, tbPar); |
---|
687 | if (!db.Execute(sqlstr, tbl)) { |
---|
688 | db.GetErrorErrStr(msglog); |
---|
689 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
690 | __func__, __LINE__, msglog); |
---|
691 | return false; |
---|
692 | } |
---|
693 | return true; |
---|
694 | } |
---|
695 | // ________________________________________________________________________________________________________ |
---|
696 | // Función: checkDato |
---|
697 | // |
---|
698 | // Descripción: |
---|
699 | // Esta función comprueba si existe un dato en una tabla y si no es así lo incluye. devuelve en |
---|
700 | // cualquier caso el identificador del registro existenet o del insertado |
---|
701 | // Parámetros: |
---|
702 | // - db: Objeto base de datos (ya operativo) |
---|
703 | // - tbl: Objeto tabla |
---|
704 | // - dato: Dato |
---|
705 | // - tabla: Nombre de la tabla |
---|
706 | // - nomdato: Nombre del dato en la tabla |
---|
707 | // - nomidentificador: Nombre del identificador en la tabla |
---|
708 | // Devuelve: |
---|
709 | // El identificador del registro existente o el del insertado |
---|
710 | // |
---|
711 | // Especificaciones: |
---|
712 | // En caso de producirse algún error se devuelve el valor 0 |
---|
713 | // ________________________________________________________________________________________________________ |
---|
714 | |
---|
715 | int checkDato(Database db, Table tbl, char *dato, const char *tabla, |
---|
716 | const char *nomdato, const char *nomidentificador) |
---|
717 | { |
---|
718 | char msglog[LONSTD], sqlstr[LONSQL]; |
---|
719 | int identificador; |
---|
720 | |
---|
721 | if (strlen(dato) == 0) |
---|
722 | return (0); // EL dato no tiene valor |
---|
723 | sprintf(sqlstr, "SELECT %s FROM %s WHERE %s ='%s'", nomidentificador, |
---|
724 | tabla, nomdato, dato); |
---|
725 | |
---|
726 | // Ejecuta consulta |
---|
727 | if (!db.Execute(sqlstr, tbl)) { |
---|
728 | db.GetErrorErrStr(msglog); |
---|
729 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
730 | __func__, __LINE__, msglog); |
---|
731 | return (0); |
---|
732 | } |
---|
733 | if (tbl.ISEOF()) { // Software NO existente |
---|
734 | sprintf(sqlstr, "INSERT INTO %s (%s) VALUES('%s')", tabla, nomdato, dato); |
---|
735 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
736 | db.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
737 | og_info(msglog); |
---|
738 | return (0); |
---|
739 | } |
---|
740 | // Recupera el identificador del software |
---|
741 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
742 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
743 | db.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
744 | og_info(msglog); |
---|
745 | return (0); |
---|
746 | } |
---|
747 | if (!tbl.ISEOF()) { // Si existe registro |
---|
748 | if (!tbl.Get("identificador", identificador)) { |
---|
749 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
750 | og_info(msglog); |
---|
751 | return (0); |
---|
752 | } |
---|
753 | } |
---|
754 | } else { |
---|
755 | if (!tbl.Get(nomidentificador, identificador)) { // Toma dato |
---|
756 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
757 | og_info(msglog); |
---|
758 | return (0); |
---|
759 | } |
---|
760 | } |
---|
761 | return (identificador); |
---|
762 | } |
---|
763 | // ________________________________________________________________________________________________________ |
---|
764 | // Función: registraCliente |
---|
765 | // |
---|
766 | // Descripción: |
---|
767 | // Incluye al cliente en la tabla de sokets |
---|
768 | // Parámetros: |
---|
769 | // - iph: Dirección ip del cliente |
---|
770 | // Devuelve: |
---|
771 | // true: Si el proceso es correcto |
---|
772 | // false: En caso de ocurrir algún error |
---|
773 | // ________________________________________________________________________________________________________ |
---|
774 | bool registraCliente(char *iph) |
---|
775 | { |
---|
776 | int idx; |
---|
777 | |
---|
778 | if (!clienteExistente(iph, &idx)) { // Si no existe la IP ... |
---|
779 | if (!hayHueco(&idx)) { // Busca hueco para el nuevo cliente |
---|
780 | return false; // No hay huecos |
---|
781 | } |
---|
782 | } |
---|
783 | strcpy(tbsockets[idx].ip, iph); // Copia IP |
---|
784 | strcpy(tbsockets[idx].estado, CLIENTE_INICIANDO); // Actualiza el estado del cliente |
---|
785 | return true; |
---|
786 | } |
---|
787 | // ________________________________________________________________________________________________________ |
---|
788 | // Función: AutoexecCliente |
---|
789 | // |
---|
790 | // Descripción: |
---|
791 | // Envía archivo de autoexec al cliente |
---|
792 | // Parámetros: |
---|
793 | // - socket_c: Socket del cliente que envió el mensaje |
---|
794 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
795 | // Devuelve: |
---|
796 | // true: Si el proceso es correcto |
---|
797 | // false: En caso de ocurrir algún error |
---|
798 | // ________________________________________________________________________________________________________ |
---|
799 | static bool AutoexecCliente(TRAMA *ptrTrama, struct og_client *cli) |
---|
800 | { |
---|
801 | int socket_c = og_client_socket(cli); |
---|
802 | int lon; |
---|
803 | char *iph, *exe, msglog[LONSTD]; |
---|
804 | Database db; |
---|
805 | FILE *fileexe; |
---|
806 | char fileautoexec[LONPRM]; |
---|
807 | char parametros[LONGITUD_PARAMETROS]; |
---|
808 | |
---|
809 | iph = copiaParametro("iph",ptrTrama); // Toma dirección IP del cliente |
---|
810 | exe = copiaParametro("exe",ptrTrama); // Toma identificador del procedimiento inicial |
---|
811 | |
---|
812 | sprintf(fileautoexec, "/tmp/Sautoexec-%s", iph); |
---|
813 | liberaMemoria(iph); |
---|
814 | fileexe = fopen(fileautoexec, "wb"); // Abre fichero de script |
---|
815 | if (fileexe == NULL) { |
---|
816 | syslog(LOG_ERR, "cannot create temporary file\n"); |
---|
817 | return false; |
---|
818 | } |
---|
819 | |
---|
820 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
821 | db.GetErrorErrStr(msglog); |
---|
822 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
823 | __func__, __LINE__, msglog); |
---|
824 | return false; |
---|
825 | } |
---|
826 | initParametros(ptrTrama,0); |
---|
827 | if (recorreProcedimientos(db, parametros, fileexe, exe)) { |
---|
828 | lon = sprintf(ptrTrama->parametros, "nfn=RESPUESTA_AutoexecCliente\r"); |
---|
829 | lon += sprintf(ptrTrama->parametros + lon, "nfl=%s\r", fileautoexec); |
---|
830 | lon += sprintf(ptrTrama->parametros + lon, "res=1\r"); |
---|
831 | } else { |
---|
832 | lon = sprintf(ptrTrama->parametros, "nfn=RESPUESTA_AutoexecCliente\r"); |
---|
833 | lon += sprintf(ptrTrama->parametros + lon, "res=0\r"); |
---|
834 | } |
---|
835 | |
---|
836 | db.Close(); |
---|
837 | fclose(fileexe); |
---|
838 | |
---|
839 | if (!mandaTrama(&socket_c, ptrTrama)) { |
---|
840 | liberaMemoria(exe); |
---|
841 | syslog(LOG_ERR, "failed to send response to %s:%hu reason=%s\n", |
---|
842 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port), |
---|
843 | strerror(errno)); |
---|
844 | return false; |
---|
845 | } |
---|
846 | liberaMemoria(exe); |
---|
847 | return true; |
---|
848 | } |
---|
849 | // ________________________________________________________________________________________________________ |
---|
850 | // Función: recorreProcedimientos |
---|
851 | // |
---|
852 | // Descripción: |
---|
853 | // Crea un archivo con el código de un procedimiento separando cada comando por un salto de linea |
---|
854 | // Parámetros: |
---|
855 | // Database db,char* parametros,FILE* fileexe,char* idp |
---|
856 | // Devuelve: |
---|
857 | // true: Si el proceso es correcto |
---|
858 | // false: En caso de ocurrir algún error |
---|
859 | // ________________________________________________________________________________________________________ |
---|
860 | bool recorreProcedimientos(Database db, char *parametros, FILE *fileexe, char *idp) |
---|
861 | { |
---|
862 | int procedimientoid, lsize; |
---|
863 | char idprocedimiento[LONPRM], msglog[LONSTD], sqlstr[LONSQL]; |
---|
864 | Table tbl; |
---|
865 | |
---|
866 | /* Busca procedimiento */ |
---|
867 | sprintf(sqlstr, |
---|
868 | "SELECT procedimientoid,parametros FROM procedimientos_acciones" |
---|
869 | " WHERE idprocedimiento=%s ORDER BY orden", idp); |
---|
870 | // Ejecuta consulta |
---|
871 | if (!db.Execute(sqlstr, tbl)) { |
---|
872 | db.GetErrorErrStr(msglog); |
---|
873 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
874 | __func__, __LINE__, msglog); |
---|
875 | return false; |
---|
876 | } |
---|
877 | while (!tbl.ISEOF()) { // Recorre procedimientos |
---|
878 | if (!tbl.Get("procedimientoid", procedimientoid)) { // Toma dato |
---|
879 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
880 | og_info(msglog); |
---|
881 | return false; |
---|
882 | } |
---|
883 | if (procedimientoid > 0) { // Procedimiento recursivo |
---|
884 | sprintf(idprocedimiento, "%d", procedimientoid); |
---|
885 | if (!recorreProcedimientos(db, parametros, fileexe, idprocedimiento)) { |
---|
886 | return false; |
---|
887 | } |
---|
888 | } else { |
---|
889 | if (!tbl.Get("parametros", parametros)) { // Toma dato |
---|
890 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
891 | og_info(msglog); |
---|
892 | return false; |
---|
893 | } |
---|
894 | strcat(parametros, "@"); |
---|
895 | lsize = strlen(parametros); |
---|
896 | fwrite(parametros, 1, lsize, fileexe); // Escribe el código a ejecutar |
---|
897 | } |
---|
898 | tbl.MoveNext(); |
---|
899 | } |
---|
900 | return true; |
---|
901 | } |
---|
902 | // ________________________________________________________________________________________________________ |
---|
903 | // Función: ComandosPendientes |
---|
904 | // |
---|
905 | // Descripción: |
---|
906 | // Esta función busca en la base de datos,comandos pendientes de ejecutar por un ordenador concreto |
---|
907 | // Parámetros: |
---|
908 | // - socket_c: Socket del cliente que envió el mensaje |
---|
909 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
910 | // Devuelve: |
---|
911 | // true: Si el proceso es correcto |
---|
912 | // false: En caso de ocurrir algún error |
---|
913 | // ________________________________________________________________________________________________________ |
---|
914 | static bool ComandosPendientes(TRAMA *ptrTrama, struct og_client *cli) |
---|
915 | { |
---|
916 | int socket_c = og_client_socket(cli); |
---|
917 | char *ido,*iph,pids[LONPRM]; |
---|
918 | int ids, idx; |
---|
919 | |
---|
920 | iph = copiaParametro("iph",ptrTrama); // Toma dirección IP |
---|
921 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del ordenador |
---|
922 | |
---|
923 | if (!clienteExistente(iph, &idx)) { // Busca índice del cliente |
---|
924 | liberaMemoria(iph); |
---|
925 | liberaMemoria(ido); |
---|
926 | syslog(LOG_ERR, "client does not exist\n"); |
---|
927 | return false; |
---|
928 | } |
---|
929 | if (buscaComandos(ido, ptrTrama, &ids)) { // Existen comandos pendientes |
---|
930 | ptrTrama->tipo = MSG_COMANDO; |
---|
931 | sprintf(pids, "\rids=%d\r", ids); |
---|
932 | strcat(ptrTrama->parametros, pids); |
---|
933 | strcpy(tbsockets[idx].estado, CLIENTE_OCUPADO); |
---|
934 | } else { |
---|
935 | initParametros(ptrTrama,0); |
---|
936 | strcpy(ptrTrama->parametros, "nfn=NoComandosPtes\r"); |
---|
937 | } |
---|
938 | if (!mandaTrama(&socket_c, ptrTrama)) { |
---|
939 | liberaMemoria(iph); |
---|
940 | liberaMemoria(ido); |
---|
941 | syslog(LOG_ERR, "failed to send response to %s:%hu reason=%s\n", |
---|
942 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port), |
---|
943 | strerror(errno)); |
---|
944 | return false; |
---|
945 | } |
---|
946 | liberaMemoria(iph); |
---|
947 | liberaMemoria(ido); |
---|
948 | return true; |
---|
949 | } |
---|
950 | // ________________________________________________________________________________________________________ |
---|
951 | // Función: buscaComandos |
---|
952 | // |
---|
953 | // Descripción: |
---|
954 | // Busca en la base de datos,comandos pendientes de ejecutar por el cliente |
---|
955 | // Parámetros: |
---|
956 | // - ido: Identificador del ordenador |
---|
957 | // - cmd: Parámetros del comando (Salida) |
---|
958 | // - ids: Identificador de la sesion(Salida) |
---|
959 | // Devuelve: |
---|
960 | // true: Si el proceso es correcto |
---|
961 | // false: En caso de ocurrir algún error |
---|
962 | // ________________________________________________________________________________________________________ |
---|
963 | bool buscaComandos(char *ido, TRAMA *ptrTrama, int *ids) |
---|
964 | { |
---|
965 | char msglog[LONSTD], sqlstr[LONSQL]; |
---|
966 | Database db; |
---|
967 | Table tbl; |
---|
968 | int lonprm; |
---|
969 | |
---|
970 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
971 | db.GetErrorErrStr(msglog); |
---|
972 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
973 | __func__, __LINE__, msglog); |
---|
974 | return false; |
---|
975 | } |
---|
976 | sprintf(sqlstr,"SELECT sesion,parametros,length( parametros) as lonprm"\ |
---|
977 | " FROM acciones WHERE idordenador=%s AND estado='%d' ORDER BY idaccion", ido, ACCION_INICIADA); |
---|
978 | if (!db.Execute(sqlstr, tbl)) { |
---|
979 | db.GetErrorErrStr(msglog); |
---|
980 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
981 | __func__, __LINE__, msglog); |
---|
982 | return false; |
---|
983 | } |
---|
984 | if (tbl.ISEOF()) { |
---|
985 | db.Close(); |
---|
986 | return false; // No hay comandos pendientes |
---|
987 | } else { // Busca entre todas las acciones de diversos ambitos |
---|
988 | if (!tbl.Get("sesion", *ids)) { // Toma identificador de la sesion |
---|
989 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
990 | og_info(msglog); |
---|
991 | return false; |
---|
992 | } |
---|
993 | if (!tbl.Get("lonprm", lonprm)) { // Toma parámetros del comando |
---|
994 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
995 | og_info(msglog); |
---|
996 | return false; |
---|
997 | } |
---|
998 | if(!initParametros(ptrTrama,lonprm+LONGITUD_PARAMETROS)){ |
---|
999 | db.Close(); |
---|
1000 | syslog(LOG_ERR, "%s:%d OOM\n", __FILE__, __LINE__); |
---|
1001 | return false; |
---|
1002 | } |
---|
1003 | if (!tbl.Get("parametros", ptrTrama->parametros)) { // Toma parámetros del comando |
---|
1004 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
1005 | og_info(msglog); |
---|
1006 | return false; |
---|
1007 | } |
---|
1008 | } |
---|
1009 | db.Close(); |
---|
1010 | return true; // Hay comandos pendientes, se toma el primero de la cola |
---|
1011 | } |
---|
1012 | // ________________________________________________________________________________________________________ |
---|
1013 | // Función: DisponibilidadComandos |
---|
1014 | // |
---|
1015 | // Descripción: |
---|
1016 | // Esta función habilita a un cliente para recibir comandos desde la consola |
---|
1017 | // Parámetros: |
---|
1018 | // - socket_c: Socket del cliente que envió el mensaje |
---|
1019 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1020 | // Devuelve: |
---|
1021 | // true: Si el proceso es correcto |
---|
1022 | // false: En caso de ocurrir algún error |
---|
1023 | // ________________________________________________________________________________________________________ |
---|
1024 | // |
---|
1025 | static bool DisponibilidadComandos(TRAMA *ptrTrama, struct og_client *cli) |
---|
1026 | { |
---|
1027 | char *iph, *tpc; |
---|
1028 | int idx; |
---|
1029 | |
---|
1030 | iph = copiaParametro("iph",ptrTrama); // Toma ip |
---|
1031 | if (!clienteExistente(iph, &idx)) { // Busca índice del cliente |
---|
1032 | liberaMemoria(iph); |
---|
1033 | syslog(LOG_ERR, "client does not exist\n"); |
---|
1034 | return false; |
---|
1035 | } |
---|
1036 | tpc = copiaParametro("tpc",ptrTrama); // Tipo de cliente (Plataforma y S.O.) |
---|
1037 | strcpy(tbsockets[idx].estado, tpc); |
---|
1038 | cli->keepalive_idx = idx; |
---|
1039 | liberaMemoria(iph); |
---|
1040 | liberaMemoria(tpc); |
---|
1041 | return true; |
---|
1042 | } |
---|
1043 | // ________________________________________________________________________________________________________ |
---|
1044 | // Función: respuestaEstandar |
---|
1045 | // |
---|
1046 | // Descripción: |
---|
1047 | // Esta función actualiza la base de datos con el resultado de la ejecución de un comando con seguimiento |
---|
1048 | // Parámetros: |
---|
1049 | // - res: resultado de la ejecución del comando |
---|
1050 | // - der: Descripción del error si hubiese habido |
---|
1051 | // - iph: Dirección IP |
---|
1052 | // - ids: identificador de la sesión |
---|
1053 | // - ido: Identificador del ordenador que notifica |
---|
1054 | // - db: Objeto base de datos (operativo) |
---|
1055 | // - tbl: Objeto tabla |
---|
1056 | // Devuelve: |
---|
1057 | // true: Si el proceso es correcto |
---|
1058 | // false: En caso de ocurrir algún error |
---|
1059 | // ________________________________________________________________________________________________________ |
---|
1060 | static bool respuestaEstandar(TRAMA *ptrTrama, char *iph, char *ido, Database db, |
---|
1061 | Table tbl) |
---|
1062 | { |
---|
1063 | char msglog[LONSTD], sqlstr[LONSQL]; |
---|
1064 | char *res, *ids, *der; |
---|
1065 | char fechafin[LONPRM]; |
---|
1066 | struct tm* st; |
---|
1067 | int idaccion; |
---|
1068 | |
---|
1069 | ids = copiaParametro("ids",ptrTrama); // Toma identificador de la sesión |
---|
1070 | |
---|
1071 | if (ids == NULL) // No existe seguimiento de la acción |
---|
1072 | return true; |
---|
1073 | |
---|
1074 | if (atoi(ids) == 0){ // No existe seguimiento de la acción |
---|
1075 | liberaMemoria(ids); |
---|
1076 | return true; |
---|
1077 | } |
---|
1078 | |
---|
1079 | sprintf(sqlstr, |
---|
1080 | "SELECT * FROM acciones WHERE idordenador=%s" |
---|
1081 | " AND sesion=%s ORDER BY idaccion", ido,ids); |
---|
1082 | |
---|
1083 | liberaMemoria(ids); |
---|
1084 | |
---|
1085 | if (!db.Execute(sqlstr, tbl)) { |
---|
1086 | db.GetErrorErrStr(msglog); |
---|
1087 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1088 | __func__, __LINE__, msglog); |
---|
1089 | return false; |
---|
1090 | } |
---|
1091 | if (tbl.ISEOF()) { |
---|
1092 | syslog(LOG_ERR, "no actions available\n"); |
---|
1093 | return true; |
---|
1094 | } |
---|
1095 | if (!tbl.Get("idaccion", idaccion)) { // Toma identificador de la accion |
---|
1096 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
1097 | og_info(msglog); |
---|
1098 | return false; |
---|
1099 | } |
---|
1100 | st = tomaHora(); |
---|
1101 | sprintf(fechafin, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon + 1, |
---|
1102 | st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec); |
---|
1103 | |
---|
1104 | res = copiaParametro("res",ptrTrama); // Toma resultado |
---|
1105 | der = copiaParametro("der",ptrTrama); // Toma descripción del error (si hubiera habido) |
---|
1106 | |
---|
1107 | sprintf(sqlstr, |
---|
1108 | "UPDATE acciones"\ |
---|
1109 | " SET resultado='%s',estado='%d',fechahorafin='%s',descrinotificacion='%s'"\ |
---|
1110 | " WHERE idordenador=%s AND idaccion=%d", |
---|
1111 | res, ACCION_FINALIZADA, fechafin, der, ido, idaccion); |
---|
1112 | |
---|
1113 | if (!db.Execute(sqlstr, tbl)) { // Error al actualizar |
---|
1114 | liberaMemoria(res); |
---|
1115 | liberaMemoria(der); |
---|
1116 | db.GetErrorErrStr(msglog); |
---|
1117 | og_info(msglog); |
---|
1118 | return false; |
---|
1119 | } |
---|
1120 | |
---|
1121 | liberaMemoria(der); |
---|
1122 | |
---|
1123 | if (atoi(res) == ACCION_FALLIDA) { |
---|
1124 | liberaMemoria(res); |
---|
1125 | return false; // Error en la ejecución del comando |
---|
1126 | } |
---|
1127 | |
---|
1128 | liberaMemoria(res); |
---|
1129 | return true; |
---|
1130 | } |
---|
1131 | |
---|
1132 | static bool og_send_cmd(char *ips_array[], int ips_array_len, |
---|
1133 | const char *state, TRAMA *ptrTrama) |
---|
1134 | { |
---|
1135 | int i, idx; |
---|
1136 | |
---|
1137 | for (i = 0; i < ips_array_len; i++) { |
---|
1138 | if (clienteDisponible(ips_array[i], &idx)) { // Si el cliente puede recibir comandos |
---|
1139 | int sock = tbsockets[idx].cli ? tbsockets[idx].cli->io.fd : -1; |
---|
1140 | |
---|
1141 | strcpy(tbsockets[idx].estado, state); // Actualiza el estado del cliente |
---|
1142 | if (!mandaTrama(&sock, ptrTrama)) { |
---|
1143 | syslog(LOG_ERR, "failed to send response to %s:%s\n", |
---|
1144 | ips_array[i], strerror(errno)); |
---|
1145 | return false; |
---|
1146 | } |
---|
1147 | } |
---|
1148 | } |
---|
1149 | return true; |
---|
1150 | } |
---|
1151 | |
---|
1152 | // ________________________________________________________________________________________________________ |
---|
1153 | // Función: enviaComando |
---|
1154 | // |
---|
1155 | // Descripción: |
---|
1156 | // Envía un comando a los clientes |
---|
1157 | // Parámetros: |
---|
1158 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1159 | // - estado: Estado en el se deja al cliente mientras se ejecuta el comando |
---|
1160 | // Devuelve: |
---|
1161 | // true: Si el proceso es correcto |
---|
1162 | // false: En caso de ocurrir algún error |
---|
1163 | // ________________________________________________________________________________________________________ |
---|
1164 | bool enviaComando(TRAMA* ptrTrama, const char *estado) |
---|
1165 | { |
---|
1166 | char *iph, *Ipes, *ptrIpes[MAXIMOS_CLIENTES]; |
---|
1167 | int lon; |
---|
1168 | |
---|
1169 | iph = copiaParametro("iph",ptrTrama); // Toma dirección/es IP |
---|
1170 | lon = strlen(iph); // Calcula longitud de la cadena de direccion/es IPE/S |
---|
1171 | Ipes = (char*) reservaMemoria(lon + 1); |
---|
1172 | if (Ipes == NULL) { |
---|
1173 | syslog(LOG_ERR, "%s:%d OOM\n", __FILE__, __LINE__); |
---|
1174 | return false; |
---|
1175 | } |
---|
1176 | |
---|
1177 | strcpy(Ipes, iph); // Copia cadena de IPES |
---|
1178 | liberaMemoria(iph); |
---|
1179 | |
---|
1180 | lon = splitCadena(ptrIpes, Ipes, ';'); |
---|
1181 | FINCADaINTRO(ptrTrama); |
---|
1182 | |
---|
1183 | if (!og_send_cmd(ptrIpes, lon, estado, ptrTrama)) |
---|
1184 | return false; |
---|
1185 | |
---|
1186 | liberaMemoria(Ipes); |
---|
1187 | return true; |
---|
1188 | } |
---|
1189 | //______________________________________________________________________________________________________ |
---|
1190 | // Función: respuestaConsola |
---|
1191 | // |
---|
1192 | // Descripción: |
---|
1193 | // Envia una respuesta a la consola sobre el resultado de la ejecución de un comando |
---|
1194 | // Parámetros: |
---|
1195 | // - socket_c: (Salida) Socket utilizado para el envío |
---|
1196 | // - res: Resultado del envío del comando |
---|
1197 | // Devuelve: |
---|
1198 | // true: Si el proceso es correcto |
---|
1199 | // false: En caso de ocurrir algún error |
---|
1200 | // ________________________________________________________________________________________________________ |
---|
1201 | bool respuestaConsola(int socket_c, TRAMA *ptrTrama, int res) |
---|
1202 | { |
---|
1203 | initParametros(ptrTrama,0); |
---|
1204 | sprintf(ptrTrama->parametros, "res=%d\r", res); |
---|
1205 | if (!mandaTrama(&socket_c, ptrTrama)) { |
---|
1206 | syslog(LOG_ERR, "%s:%d failed to send response: %s\n", |
---|
1207 | __func__, __LINE__, strerror(errno)); |
---|
1208 | return false; |
---|
1209 | } |
---|
1210 | return true; |
---|
1211 | } |
---|
1212 | // ________________________________________________________________________________________________________ |
---|
1213 | // Función: Levanta |
---|
1214 | // |
---|
1215 | // Descripción: |
---|
1216 | // Enciende ordenadores a través de la red cuyas macs se pasan como parámetro |
---|
1217 | // Parámetros: |
---|
1218 | // - iph: Cadena de direcciones ip separadas por ";" |
---|
1219 | // - mac: Cadena de direcciones mac separadas por ";" |
---|
1220 | // - mar: Método de arranque (1=Broadcast, 2=Unicast) |
---|
1221 | // Devuelve: |
---|
1222 | // true: Si el proceso es correcto |
---|
1223 | // false: En caso de ocurrir algún error |
---|
1224 | // ________________________________________________________________________________________________________ |
---|
1225 | |
---|
1226 | bool Levanta(char *ptrIP[], char *ptrMacs[], int lon, char *mar) |
---|
1227 | { |
---|
1228 | unsigned int on = 1; |
---|
1229 | sockaddr_in local; |
---|
1230 | int i, res; |
---|
1231 | int s; |
---|
1232 | |
---|
1233 | /* Creación de socket para envío de magig packet */ |
---|
1234 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
---|
1235 | if (s < 0) { |
---|
1236 | syslog(LOG_ERR, "cannot create socket for magic packet\n"); |
---|
1237 | return false; |
---|
1238 | } |
---|
1239 | res = setsockopt(s, SOL_SOCKET, SO_BROADCAST, (unsigned int *) &on, |
---|
1240 | sizeof(on)); |
---|
1241 | if (res < 0) { |
---|
1242 | syslog(LOG_ERR, "cannot set broadcast socket\n"); |
---|
1243 | return false; |
---|
1244 | } |
---|
1245 | memset(&local, 0, sizeof(local)); |
---|
1246 | local.sin_family = AF_INET; |
---|
1247 | local.sin_port = htons(PUERTO_WAKEUP); |
---|
1248 | local.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
1249 | |
---|
1250 | for (i = 0; i < lon; i++) { |
---|
1251 | if (!WakeUp(s, ptrIP[i], ptrMacs[i], mar)) { |
---|
1252 | syslog(LOG_ERR, "problem sending magic packet\n"); |
---|
1253 | close(s); |
---|
1254 | return false; |
---|
1255 | } |
---|
1256 | } |
---|
1257 | close(s); |
---|
1258 | return true; |
---|
1259 | } |
---|
1260 | |
---|
1261 | #define OG_WOL_SEQUENCE 6 |
---|
1262 | #define OG_WOL_MACADDR_LEN 6 |
---|
1263 | #define OG_WOL_REPEAT 16 |
---|
1264 | |
---|
1265 | struct wol_msg { |
---|
1266 | char secuencia_FF[OG_WOL_SEQUENCE]; |
---|
1267 | char macbin[OG_WOL_REPEAT][OG_WOL_MACADDR_LEN]; |
---|
1268 | }; |
---|
1269 | |
---|
1270 | static bool wake_up_broadcast(int sd, struct sockaddr_in *client, |
---|
1271 | const struct wol_msg *msg) |
---|
1272 | { |
---|
1273 | struct sockaddr_in *broadcast_addr; |
---|
1274 | struct ifaddrs *ifaddr, *ifa; |
---|
1275 | int ret; |
---|
1276 | |
---|
1277 | if (getifaddrs(&ifaddr) < 0) { |
---|
1278 | syslog(LOG_ERR, "cannot get list of addresses\n"); |
---|
1279 | return false; |
---|
1280 | } |
---|
1281 | |
---|
1282 | client->sin_addr.s_addr = htonl(INADDR_BROADCAST); |
---|
1283 | |
---|
1284 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
---|
1285 | if (ifa->ifa_addr == NULL || |
---|
1286 | ifa->ifa_addr->sa_family != AF_INET || |
---|
1287 | strcmp(ifa->ifa_name, interface) != 0) |
---|
1288 | continue; |
---|
1289 | |
---|
1290 | broadcast_addr = |
---|
1291 | (struct sockaddr_in *)ifa->ifa_ifu.ifu_broadaddr; |
---|
1292 | client->sin_addr.s_addr = broadcast_addr->sin_addr.s_addr; |
---|
1293 | break; |
---|
1294 | } |
---|
1295 | freeifaddrs(ifaddr); |
---|
1296 | |
---|
1297 | ret = sendto(sd, msg, sizeof(*msg), 0, |
---|
1298 | (sockaddr *)client, sizeof(*client)); |
---|
1299 | if (ret < 0) { |
---|
1300 | syslog(LOG_ERR, "failed to send broadcast wol\n"); |
---|
1301 | return false; |
---|
1302 | } |
---|
1303 | |
---|
1304 | return true; |
---|
1305 | } |
---|
1306 | |
---|
1307 | static bool wake_up_unicast(int sd, struct sockaddr_in *client, |
---|
1308 | const struct wol_msg *msg, |
---|
1309 | const struct in_addr *addr) |
---|
1310 | { |
---|
1311 | int ret; |
---|
1312 | |
---|
1313 | client->sin_addr.s_addr = addr->s_addr; |
---|
1314 | |
---|
1315 | ret = sendto(sd, msg, sizeof(*msg), 0, |
---|
1316 | (sockaddr *)client, sizeof(*client)); |
---|
1317 | if (ret < 0) { |
---|
1318 | syslog(LOG_ERR, "failed to send unicast wol\n"); |
---|
1319 | return false; |
---|
1320 | } |
---|
1321 | |
---|
1322 | return true; |
---|
1323 | } |
---|
1324 | |
---|
1325 | enum wol_delivery_type { |
---|
1326 | OG_WOL_BROADCAST = 1, |
---|
1327 | OG_WOL_UNICAST = 2 |
---|
1328 | }; |
---|
1329 | |
---|
1330 | //_____________________________________________________________________________________________________________ |
---|
1331 | // Función: WakeUp |
---|
1332 | // |
---|
1333 | // Descripción: |
---|
1334 | // Enciende el ordenador cuya MAC se pasa como parámetro |
---|
1335 | // Parámetros: |
---|
1336 | // - s : Socket para enviar trama magic packet |
---|
1337 | // - iph : Cadena con la dirección ip |
---|
1338 | // - mac : Cadena con la dirección mac en formato XXXXXXXXXXXX |
---|
1339 | // - mar: Método de arranque (1=Broadcast, 2=Unicast) |
---|
1340 | // Devuelve: |
---|
1341 | // true: Si el proceso es correcto |
---|
1342 | // false: En caso de ocurrir algún error |
---|
1343 | //_____________________________________________________________________________________________________________ |
---|
1344 | // |
---|
1345 | bool WakeUp(int s, char* iph, char *mac, char *mar) |
---|
1346 | { |
---|
1347 | unsigned int macaddr[OG_WOL_MACADDR_LEN]; |
---|
1348 | char HDaddress_bin[OG_WOL_MACADDR_LEN]; |
---|
1349 | struct sockaddr_in WakeUpCliente; |
---|
1350 | struct wol_msg Trama_WakeUp; |
---|
1351 | struct in_addr addr; |
---|
1352 | bool ret; |
---|
1353 | int i; |
---|
1354 | |
---|
1355 | for (i = 0; i < 6; i++) // Primera secuencia de la trama Wake Up (0xFFFFFFFFFFFF) |
---|
1356 | Trama_WakeUp.secuencia_FF[i] = 0xFF; |
---|
1357 | |
---|
1358 | sscanf(mac, "%02x%02x%02x%02x%02x%02x", |
---|
1359 | &macaddr[0], &macaddr[1], &macaddr[2], |
---|
1360 | &macaddr[3], &macaddr[4], &macaddr[5]); |
---|
1361 | |
---|
1362 | for (i = 0; i < 6; i++) |
---|
1363 | HDaddress_bin[i] = (uint8_t)macaddr[i]; |
---|
1364 | |
---|
1365 | for (i = 0; i < 16; i++) // Segunda secuencia de la trama Wake Up , repetir 16 veces su la MAC |
---|
1366 | memcpy(&Trama_WakeUp.macbin[i][0], &HDaddress_bin, 6); |
---|
1367 | |
---|
1368 | /* Creación de socket del cliente que recibe la trama magic packet */ |
---|
1369 | WakeUpCliente.sin_family = AF_INET; |
---|
1370 | WakeUpCliente.sin_port = htons((short) PUERTO_WAKEUP); |
---|
1371 | |
---|
1372 | switch (atoi(mar)) { |
---|
1373 | case OG_WOL_BROADCAST: |
---|
1374 | ret = wake_up_broadcast(s, &WakeUpCliente, &Trama_WakeUp); |
---|
1375 | break; |
---|
1376 | case OG_WOL_UNICAST: |
---|
1377 | if (inet_aton(iph, &addr) < 0) { |
---|
1378 | syslog(LOG_ERR, "bad IP address for unicast wol\n"); |
---|
1379 | ret = false; |
---|
1380 | break; |
---|
1381 | } |
---|
1382 | ret = wake_up_unicast(s, &WakeUpCliente, &Trama_WakeUp, &addr); |
---|
1383 | break; |
---|
1384 | default: |
---|
1385 | syslog(LOG_ERR, "unknown wol type\n"); |
---|
1386 | ret = false; |
---|
1387 | break; |
---|
1388 | } |
---|
1389 | return ret; |
---|
1390 | } |
---|
1391 | // ________________________________________________________________________________________________________ |
---|
1392 | // Función: RESPUESTA_Arrancar |
---|
1393 | // |
---|
1394 | // Descripción: |
---|
1395 | // Respuesta del cliente al comando Arrancar |
---|
1396 | // Parámetros: |
---|
1397 | // - socket_c: Socket del cliente que envió el mensaje |
---|
1398 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1399 | // Devuelve: |
---|
1400 | // true: Si el proceso es correcto |
---|
1401 | // false: En caso de ocurrir algún error |
---|
1402 | // ________________________________________________________________________________________________________ |
---|
1403 | static bool RESPUESTA_Arrancar(TRAMA* ptrTrama, struct og_client *cli) |
---|
1404 | { |
---|
1405 | char msglog[LONSTD]; |
---|
1406 | Database db; |
---|
1407 | Table tbl; |
---|
1408 | int i; |
---|
1409 | char *iph, *ido; |
---|
1410 | char *tpc; |
---|
1411 | |
---|
1412 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
1413 | db.GetErrorErrStr(msglog); |
---|
1414 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
1415 | __func__, __LINE__, msglog); |
---|
1416 | return false; |
---|
1417 | } |
---|
1418 | |
---|
1419 | iph = copiaParametro("iph",ptrTrama); // Toma dirección ip |
---|
1420 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del ordenador |
---|
1421 | |
---|
1422 | if (!respuestaEstandar(ptrTrama, iph, ido, db, tbl)) { |
---|
1423 | liberaMemoria(iph); |
---|
1424 | liberaMemoria(ido); |
---|
1425 | syslog(LOG_ERR, "failed to register notification\n"); |
---|
1426 | return false; |
---|
1427 | } |
---|
1428 | |
---|
1429 | tpc = copiaParametro("tpc",ptrTrama); // Tipo de cliente (Plataforma y S.O.) |
---|
1430 | if (clienteExistente(iph, &i)) // Actualiza estado |
---|
1431 | strcpy(tbsockets[i].estado, tpc); |
---|
1432 | |
---|
1433 | liberaMemoria(iph); |
---|
1434 | liberaMemoria(ido); |
---|
1435 | liberaMemoria(tpc); |
---|
1436 | |
---|
1437 | db.Close(); // Cierra conexión |
---|
1438 | return true; |
---|
1439 | } |
---|
1440 | // ________________________________________________________________________________________________________ |
---|
1441 | // Función: Apagar |
---|
1442 | // |
---|
1443 | // Descripción: |
---|
1444 | // Procesa el comando Apagar |
---|
1445 | // Parámetros: |
---|
1446 | // - socket_c: Socket de la consola al envió el mensaje |
---|
1447 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1448 | // Devuelve: |
---|
1449 | // true: Si el proceso es correcto |
---|
1450 | // false: En caso de ocurrir algún error |
---|
1451 | // ________________________________________________________________________________________________________ |
---|
1452 | static bool Apagar(TRAMA* ptrTrama, struct og_client *cli) |
---|
1453 | { |
---|
1454 | if (!enviaComando(ptrTrama, CLIENTE_OCUPADO)) { |
---|
1455 | respuestaConsola(og_client_socket(cli), ptrTrama, false); |
---|
1456 | return false; |
---|
1457 | } |
---|
1458 | respuestaConsola(og_client_socket(cli), ptrTrama, true); |
---|
1459 | return true; |
---|
1460 | } |
---|
1461 | // ________________________________________________________________________________________________________ |
---|
1462 | // Función: RESPUESTA_Apagar |
---|
1463 | // |
---|
1464 | // Descripción: |
---|
1465 | // Respuesta del cliente al comando Apagar |
---|
1466 | // Parámetros: |
---|
1467 | // - socket_c: Socket del cliente que envió el mensaje |
---|
1468 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1469 | // Devuelve: |
---|
1470 | // true: Si el proceso es correcto |
---|
1471 | // false: En caso de ocurrir algún error |
---|
1472 | // ________________________________________________________________________________________________________ |
---|
1473 | static bool RESPUESTA_Apagar(TRAMA* ptrTrama, struct og_client *cli) |
---|
1474 | { |
---|
1475 | char msglog[LONSTD]; |
---|
1476 | Database db; |
---|
1477 | Table tbl; |
---|
1478 | int i; |
---|
1479 | char *iph, *ido; |
---|
1480 | |
---|
1481 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
1482 | db.GetErrorErrStr(msglog); |
---|
1483 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
1484 | __func__, __LINE__, msglog); |
---|
1485 | return false; |
---|
1486 | } |
---|
1487 | |
---|
1488 | iph = copiaParametro("iph",ptrTrama); // Toma dirección ip |
---|
1489 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del ordenador |
---|
1490 | |
---|
1491 | if (!respuestaEstandar(ptrTrama, iph, ido, db, tbl)) { |
---|
1492 | liberaMemoria(iph); |
---|
1493 | liberaMemoria(ido); |
---|
1494 | syslog(LOG_ERR, "failed to register notification\n"); |
---|
1495 | return false; // Error al registrar notificacion |
---|
1496 | } |
---|
1497 | |
---|
1498 | if (clienteExistente(iph, &i)) // Actualiza estado |
---|
1499 | strcpy(tbsockets[i].estado, CLIENTE_APAGADO); |
---|
1500 | |
---|
1501 | liberaMemoria(iph); |
---|
1502 | liberaMemoria(ido); |
---|
1503 | |
---|
1504 | db.Close(); // Cierra conexión |
---|
1505 | return true; |
---|
1506 | } |
---|
1507 | // ________________________________________________________________________________________________________ |
---|
1508 | // Función: RESPUESTA_Reiniciar |
---|
1509 | // |
---|
1510 | // Descripción: |
---|
1511 | // Respuesta del cliente al comando Reiniciar |
---|
1512 | // Parámetros: |
---|
1513 | // - socket_c: Socket del cliente que envió el mensaje |
---|
1514 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1515 | // Devuelve: |
---|
1516 | // true: Si el proceso es correcto |
---|
1517 | // false: En caso de ocurrir algún error |
---|
1518 | // ________________________________________________________________________________________________________ |
---|
1519 | static bool RESPUESTA_Reiniciar(TRAMA* ptrTrama, struct og_client *cli) |
---|
1520 | { |
---|
1521 | char msglog[LONSTD]; |
---|
1522 | Database db; |
---|
1523 | Table tbl; |
---|
1524 | int i; |
---|
1525 | char *iph, *ido; |
---|
1526 | |
---|
1527 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
1528 | db.GetErrorErrStr(msglog); |
---|
1529 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
1530 | __func__, __LINE__, msglog); |
---|
1531 | return false; |
---|
1532 | } |
---|
1533 | |
---|
1534 | iph = copiaParametro("iph",ptrTrama); // Toma dirección ip |
---|
1535 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del ordenador |
---|
1536 | |
---|
1537 | if (!respuestaEstandar(ptrTrama, iph, ido, db, tbl)) { |
---|
1538 | liberaMemoria(iph); |
---|
1539 | liberaMemoria(ido); |
---|
1540 | syslog(LOG_ERR, "failed to register notification\n"); |
---|
1541 | return false; // Error al registrar notificacion |
---|
1542 | } |
---|
1543 | |
---|
1544 | if (clienteExistente(iph, &i)) // Actualiza estado |
---|
1545 | strcpy(tbsockets[i].estado, CLIENTE_APAGADO); |
---|
1546 | |
---|
1547 | liberaMemoria(iph); |
---|
1548 | liberaMemoria(ido); |
---|
1549 | |
---|
1550 | db.Close(); // Cierra conexión |
---|
1551 | return true; |
---|
1552 | } |
---|
1553 | // ________________________________________________________________________________________________________ |
---|
1554 | // Función: RESPUESTA_IniciarSesion |
---|
1555 | // |
---|
1556 | // Descripción: |
---|
1557 | // Respuesta del cliente al comando Iniciar Sesión |
---|
1558 | // Parámetros: |
---|
1559 | // - socket_c: Socket del cliente que envió el mensaje |
---|
1560 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1561 | // Devuelve: |
---|
1562 | // true: Si el proceso es correcto |
---|
1563 | // false: En caso de ocurrir algún error |
---|
1564 | // ________________________________________________________________________________________________________ |
---|
1565 | static bool RESPUESTA_IniciarSesion(TRAMA* ptrTrama, struct og_client *cli) |
---|
1566 | { |
---|
1567 | char msglog[LONSTD]; |
---|
1568 | Database db; |
---|
1569 | Table tbl; |
---|
1570 | int i; |
---|
1571 | char *iph, *ido; |
---|
1572 | |
---|
1573 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
1574 | db.GetErrorErrStr(msglog); |
---|
1575 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
1576 | __func__, __LINE__, msglog); |
---|
1577 | return false; |
---|
1578 | } |
---|
1579 | |
---|
1580 | iph = copiaParametro("iph",ptrTrama); // Toma dirección ip |
---|
1581 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del ordenador |
---|
1582 | |
---|
1583 | if (!respuestaEstandar(ptrTrama, iph, ido, db, tbl)) { |
---|
1584 | liberaMemoria(iph); |
---|
1585 | liberaMemoria(ido); |
---|
1586 | syslog(LOG_ERR, "failed to register notification\n"); |
---|
1587 | return false; // Error al registrar notificacion |
---|
1588 | } |
---|
1589 | |
---|
1590 | if (clienteExistente(iph, &i)) // Actualiza estado |
---|
1591 | strcpy(tbsockets[i].estado, CLIENTE_APAGADO); |
---|
1592 | |
---|
1593 | liberaMemoria(iph); |
---|
1594 | liberaMemoria(ido); |
---|
1595 | |
---|
1596 | db.Close(); // Cierra conexión |
---|
1597 | return true; |
---|
1598 | } |
---|
1599 | // ________________________________________________________________________________________________________ |
---|
1600 | // Función: CrearImagen |
---|
1601 | // |
---|
1602 | // Descripción: |
---|
1603 | // Crea una imagen de una partición de un disco y la guarda o bien en un repositorio |
---|
1604 | // Parámetros: |
---|
1605 | // - socket_c: Socket de la consola al envió el mensaje |
---|
1606 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1607 | // Devuelve: |
---|
1608 | // true: Si el proceso es correcto |
---|
1609 | // false: En caso de ocurrir algún error |
---|
1610 | // ________________________________________________________________________________________________________ |
---|
1611 | static bool CrearImagen(TRAMA* ptrTrama, struct og_client *cli) |
---|
1612 | { |
---|
1613 | if (!enviaComando(ptrTrama, CLIENTE_OCUPADO)) { |
---|
1614 | respuestaConsola(og_client_socket(cli), ptrTrama, false); |
---|
1615 | return false; |
---|
1616 | } |
---|
1617 | respuestaConsola(og_client_socket(cli), ptrTrama, true); |
---|
1618 | return true; |
---|
1619 | } |
---|
1620 | // ________________________________________________________________________________________________________ |
---|
1621 | // Función: RESPUESTA_CrearImagen |
---|
1622 | // |
---|
1623 | // Descripción: |
---|
1624 | // Respuesta del cliente al comando CrearImagen |
---|
1625 | // Parámetros: |
---|
1626 | // - socket_c: Socket del cliente que envió el mensaje |
---|
1627 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1628 | // Devuelve: |
---|
1629 | // true: Si el proceso es correcto |
---|
1630 | // false: En caso de ocurrir algún error |
---|
1631 | // ________________________________________________________________________________________________________ |
---|
1632 | static bool RESPUESTA_CrearImagen(TRAMA* ptrTrama, struct og_client *cli) |
---|
1633 | { |
---|
1634 | char msglog[LONSTD]; |
---|
1635 | Database db; |
---|
1636 | Table tbl; |
---|
1637 | char *iph, *dsk, *par, *cpt, *ipr, *ido; |
---|
1638 | char *idi; |
---|
1639 | bool res; |
---|
1640 | |
---|
1641 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
1642 | db.GetErrorErrStr(msglog); |
---|
1643 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
1644 | __func__, __LINE__, msglog); |
---|
1645 | return false; |
---|
1646 | } |
---|
1647 | |
---|
1648 | iph = copiaParametro("iph",ptrTrama); // Toma dirección ip |
---|
1649 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del ordenador |
---|
1650 | |
---|
1651 | if (!respuestaEstandar(ptrTrama, iph, ido, db, tbl)) { |
---|
1652 | liberaMemoria(iph); |
---|
1653 | liberaMemoria(ido); |
---|
1654 | syslog(LOG_ERR, "failed to register notification\n"); |
---|
1655 | return false; // Error al registrar notificacion |
---|
1656 | } |
---|
1657 | |
---|
1658 | // Acciones posteriores |
---|
1659 | idi = copiaParametro("idi",ptrTrama); |
---|
1660 | dsk = copiaParametro("dsk",ptrTrama); |
---|
1661 | par = copiaParametro("par",ptrTrama); |
---|
1662 | cpt = copiaParametro("cpt",ptrTrama); |
---|
1663 | ipr = copiaParametro("ipr",ptrTrama); |
---|
1664 | |
---|
1665 | res=actualizaCreacionImagen(db, tbl, idi, dsk, par, cpt, ipr, ido); |
---|
1666 | |
---|
1667 | liberaMemoria(idi); |
---|
1668 | liberaMemoria(par); |
---|
1669 | liberaMemoria(cpt); |
---|
1670 | liberaMemoria(ipr); |
---|
1671 | |
---|
1672 | if(!res){ |
---|
1673 | syslog(LOG_ERR, "Problem processing update\n"); |
---|
1674 | db.Close(); |
---|
1675 | return false; |
---|
1676 | } |
---|
1677 | |
---|
1678 | db.Close(); // Cierra conexión |
---|
1679 | return true; |
---|
1680 | } |
---|
1681 | // ________________________________________________________________________________________________________ |
---|
1682 | // Función: actualizaCreacionImagen |
---|
1683 | // |
---|
1684 | // Descripción: |
---|
1685 | // Esta función actualiza la base de datos con el resultado de la creación de una imagen |
---|
1686 | // Parámetros: |
---|
1687 | // - db: Objeto base de datos (ya operativo) |
---|
1688 | // - tbl: Objeto tabla |
---|
1689 | // - idi: Identificador de la imagen |
---|
1690 | // - dsk: Disco de donde se creó |
---|
1691 | // - par: Partición de donde se creó |
---|
1692 | // - cpt: Código de partición |
---|
1693 | // - ipr: Ip del repositorio |
---|
1694 | // - ido: Identificador del ordenador modelo |
---|
1695 | // Devuelve: |
---|
1696 | // true: Si el proceso es correcto |
---|
1697 | // false: En caso de ocurrir algún error |
---|
1698 | // ________________________________________________________________________________________________________ |
---|
1699 | bool actualizaCreacionImagen(Database db, Table tbl, char *idi, char *dsk, |
---|
1700 | char *par, char *cpt, char *ipr, char *ido) |
---|
1701 | { |
---|
1702 | char msglog[LONSTD], sqlstr[LONSQL]; |
---|
1703 | int idr,ifs; |
---|
1704 | |
---|
1705 | /* Toma identificador del repositorio correspondiente al ordenador modelo */ |
---|
1706 | snprintf(sqlstr, LONSQL, |
---|
1707 | "SELECT repositorios.idrepositorio" |
---|
1708 | " FROM repositorios" |
---|
1709 | " LEFT JOIN ordenadores USING (idrepositorio)" |
---|
1710 | " WHERE repositorios.ip='%s' AND ordenadores.idordenador=%s", ipr, ido); |
---|
1711 | |
---|
1712 | if (!db.Execute(sqlstr, tbl)) { |
---|
1713 | db.GetErrorErrStr(msglog); |
---|
1714 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1715 | __func__, __LINE__, msglog); |
---|
1716 | return false; |
---|
1717 | } |
---|
1718 | if (!tbl.Get("idrepositorio", idr)) { // Toma dato |
---|
1719 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
1720 | og_info(msglog); |
---|
1721 | return false; |
---|
1722 | } |
---|
1723 | |
---|
1724 | /* Toma identificador del perfilsoftware */ |
---|
1725 | snprintf(sqlstr, LONSQL, |
---|
1726 | "SELECT idperfilsoft" |
---|
1727 | " FROM ordenadores_particiones" |
---|
1728 | " WHERE idordenador=%s AND numdisk=%s AND numpar=%s", ido, dsk, par); |
---|
1729 | |
---|
1730 | if (!db.Execute(sqlstr, tbl)) { |
---|
1731 | db.GetErrorErrStr(msglog); |
---|
1732 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1733 | __func__, __LINE__, msglog); |
---|
1734 | return false; |
---|
1735 | } |
---|
1736 | if (!tbl.Get("idperfilsoft", ifs)) { // Toma dato |
---|
1737 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
1738 | og_info(msglog); |
---|
1739 | return false; |
---|
1740 | } |
---|
1741 | |
---|
1742 | /* Actualizar los datos de la imagen */ |
---|
1743 | snprintf(sqlstr, LONSQL, |
---|
1744 | "UPDATE imagenes" |
---|
1745 | " SET idordenador=%s, numdisk=%s, numpar=%s, codpar=%s," |
---|
1746 | " idperfilsoft=%d, idrepositorio=%d," |
---|
1747 | " fechacreacion=NOW(), revision=revision+1" |
---|
1748 | " WHERE idimagen=%s", ido, dsk, par, cpt, ifs, idr, idi); |
---|
1749 | |
---|
1750 | if (!db.Execute(sqlstr, tbl)) { |
---|
1751 | db.GetErrorErrStr(msglog); |
---|
1752 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1753 | __func__, __LINE__, msglog); |
---|
1754 | return false; |
---|
1755 | } |
---|
1756 | /* Actualizar los datos en el cliente */ |
---|
1757 | snprintf(sqlstr, LONSQL, |
---|
1758 | "UPDATE ordenadores_particiones" |
---|
1759 | " SET idimagen=%s, revision=(SELECT revision FROM imagenes WHERE idimagen=%s)," |
---|
1760 | " fechadespliegue=NOW()" |
---|
1761 | " WHERE idordenador=%s AND numdisk=%s AND numpar=%s", |
---|
1762 | idi, idi, ido, dsk, par); |
---|
1763 | if (!db.Execute(sqlstr, tbl)) { |
---|
1764 | db.GetErrorErrStr(msglog); |
---|
1765 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1766 | __func__, __LINE__, msglog); |
---|
1767 | return false; |
---|
1768 | } |
---|
1769 | return true; |
---|
1770 | } |
---|
1771 | // ________________________________________________________________________________________________________ |
---|
1772 | // Función: CrearImagenBasica |
---|
1773 | // |
---|
1774 | // Descripción: |
---|
1775 | // Crea una imagen basica usando sincronización |
---|
1776 | // Parámetros: |
---|
1777 | // - socket_c: Socket de la consola al envió el mensaje |
---|
1778 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1779 | // Devuelve: |
---|
1780 | // true: Si el proceso es correcto |
---|
1781 | // false: En caso de ocurrir algún error |
---|
1782 | // ________________________________________________________________________________________________________ |
---|
1783 | static bool CrearImagenBasica(TRAMA* ptrTrama, struct og_client *cli) |
---|
1784 | { |
---|
1785 | if (!enviaComando(ptrTrama, CLIENTE_OCUPADO)) { |
---|
1786 | respuestaConsola(og_client_socket(cli), ptrTrama, false); |
---|
1787 | return false; |
---|
1788 | } |
---|
1789 | respuestaConsola(og_client_socket(cli), ptrTrama, true); |
---|
1790 | return true; |
---|
1791 | } |
---|
1792 | // ________________________________________________________________________________________________________ |
---|
1793 | // Función: RESPUESTA_CrearImagenBasica |
---|
1794 | // |
---|
1795 | // Descripción: |
---|
1796 | // Respuesta del cliente al comando CrearImagenBasica |
---|
1797 | // Parámetros: |
---|
1798 | // - socket_c: Socket del cliente que envió el mensaje |
---|
1799 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1800 | // Devuelve: |
---|
1801 | // true: Si el proceso es correcto |
---|
1802 | // false: En caso de ocurrir algún error |
---|
1803 | // ________________________________________________________________________________________________________ |
---|
1804 | static bool RESPUESTA_CrearImagenBasica(TRAMA* ptrTrama, struct og_client *cli) |
---|
1805 | { |
---|
1806 | // La misma respuesta que la creación de imagen monolítica |
---|
1807 | return RESPUESTA_CrearImagen(ptrTrama, cli); |
---|
1808 | } |
---|
1809 | // ________________________________________________________________________________________________________ |
---|
1810 | // Función: CrearSoftIncremental |
---|
1811 | // |
---|
1812 | // Descripción: |
---|
1813 | // Crea una imagen incremental entre una partición de un disco y una imagen ya creada guardandola en el |
---|
1814 | // mismo repositorio y en la misma carpeta donde está la imagen básica |
---|
1815 | // Parámetros: |
---|
1816 | // - socket_c: Socket de la consola al envió el mensaje |
---|
1817 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1818 | // Devuelve: |
---|
1819 | // true: Si el proceso es correcto |
---|
1820 | // false: En caso de ocurrir algún error |
---|
1821 | // ________________________________________________________________________________________________________ |
---|
1822 | static bool CrearSoftIncremental(TRAMA* ptrTrama, struct og_client *cli) |
---|
1823 | { |
---|
1824 | if (!enviaComando(ptrTrama, CLIENTE_OCUPADO)) { |
---|
1825 | respuestaConsola(og_client_socket(cli), ptrTrama, false); |
---|
1826 | return false; |
---|
1827 | } |
---|
1828 | respuestaConsola(og_client_socket(cli), ptrTrama, true); |
---|
1829 | return true; |
---|
1830 | } |
---|
1831 | // ________________________________________________________________________________________________________ |
---|
1832 | // Función: RESPUESTA_CrearSoftIncremental |
---|
1833 | // |
---|
1834 | // Descripción: |
---|
1835 | // Respuesta del cliente al comando crearImagenDiferencial |
---|
1836 | // Parámetros: |
---|
1837 | // - socket_c: Socket del cliente que envió el mensaje |
---|
1838 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1839 | // Devuelve: |
---|
1840 | // true: Si el proceso es correcto |
---|
1841 | // false: En caso de ocurrir algún error |
---|
1842 | // ________________________________________________________________________________________________________ |
---|
1843 | static bool RESPUESTA_CrearSoftIncremental(TRAMA* ptrTrama, struct og_client *cli) |
---|
1844 | { |
---|
1845 | Database db; |
---|
1846 | Table tbl; |
---|
1847 | char *iph,*par,*ido,*idf; |
---|
1848 | int ifs; |
---|
1849 | char msglog[LONSTD],sqlstr[LONSQL]; |
---|
1850 | |
---|
1851 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
1852 | db.GetErrorErrStr(msglog); |
---|
1853 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
1854 | __func__, __LINE__, msglog); |
---|
1855 | return false; |
---|
1856 | } |
---|
1857 | |
---|
1858 | iph = copiaParametro("iph",ptrTrama); // Toma dirección ip |
---|
1859 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del ordenador |
---|
1860 | |
---|
1861 | if (!respuestaEstandar(ptrTrama, iph, ido, db, tbl)) { |
---|
1862 | liberaMemoria(iph); |
---|
1863 | liberaMemoria(ido); |
---|
1864 | syslog(LOG_ERR, "failed to register notification\n"); |
---|
1865 | return false; |
---|
1866 | } |
---|
1867 | |
---|
1868 | par = copiaParametro("par",ptrTrama); |
---|
1869 | |
---|
1870 | /* Toma identificador del perfilsoftware creado por el inventario de software */ |
---|
1871 | sprintf(sqlstr,"SELECT idperfilsoft FROM ordenadores_particiones WHERE idordenador=%s AND numpar=%s",ido,par); |
---|
1872 | |
---|
1873 | liberaMemoria(iph); |
---|
1874 | liberaMemoria(ido); |
---|
1875 | liberaMemoria(par); |
---|
1876 | |
---|
1877 | if (!db.Execute(sqlstr, tbl)) { |
---|
1878 | db.GetErrorErrStr(msglog); |
---|
1879 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1880 | __func__, __LINE__, msglog); |
---|
1881 | return false; |
---|
1882 | } |
---|
1883 | if (!tbl.Get("idperfilsoft", ifs)) { // Toma dato |
---|
1884 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
1885 | og_info(msglog); |
---|
1886 | return false; |
---|
1887 | } |
---|
1888 | |
---|
1889 | /* Actualizar los datos de la imagen */ |
---|
1890 | idf = copiaParametro("idf",ptrTrama); |
---|
1891 | sprintf(sqlstr,"UPDATE imagenes SET idperfilsoft=%d WHERE idimagen=%s",ifs,idf); |
---|
1892 | liberaMemoria(idf); |
---|
1893 | |
---|
1894 | if (!db.Execute(sqlstr, tbl)) { |
---|
1895 | db.GetErrorErrStr(msglog); |
---|
1896 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1897 | __func__, __LINE__, msglog); |
---|
1898 | return false; |
---|
1899 | } |
---|
1900 | db.Close(); // Cierra conexión |
---|
1901 | return true; |
---|
1902 | } |
---|
1903 | // ________________________________________________________________________________________________________ |
---|
1904 | // Función: RestaurarImagen |
---|
1905 | // |
---|
1906 | // Descripción: |
---|
1907 | // Restaura una imagen en una partición |
---|
1908 | // Parámetros: |
---|
1909 | // - socket_c: Socket de la consola al envió el mensaje |
---|
1910 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1911 | // Devuelve: |
---|
1912 | // true: Si el proceso es correcto |
---|
1913 | // false: En caso de ocurrir algún error |
---|
1914 | // ________________________________________________________________________________________________________ |
---|
1915 | static bool RestaurarImagen(TRAMA* ptrTrama, struct og_client *cli) |
---|
1916 | { |
---|
1917 | if (!enviaComando(ptrTrama, CLIENTE_OCUPADO)) { |
---|
1918 | respuestaConsola(og_client_socket(cli), ptrTrama, false); |
---|
1919 | return false; |
---|
1920 | } |
---|
1921 | respuestaConsola(og_client_socket(cli), ptrTrama, true); |
---|
1922 | return true; |
---|
1923 | } |
---|
1924 | // ________________________________________________________________________________________________________ |
---|
1925 | // Función: RestaurarImagenBasica |
---|
1926 | // |
---|
1927 | // Descripción: |
---|
1928 | // Restaura una imagen básica en una partición |
---|
1929 | // Parámetros: |
---|
1930 | // - socket_c: Socket de la consola al envió el mensaje |
---|
1931 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1932 | // Devuelve: |
---|
1933 | // true: Si el proceso es correcto |
---|
1934 | // false: En caso de ocurrir algún error |
---|
1935 | // ________________________________________________________________________________________________________ |
---|
1936 | static bool RestaurarImagenBasica(TRAMA* ptrTrama, struct og_client *cli) |
---|
1937 | { |
---|
1938 | if (!enviaComando(ptrTrama, CLIENTE_OCUPADO)) { |
---|
1939 | respuestaConsola(og_client_socket(cli), ptrTrama, false); |
---|
1940 | return false; |
---|
1941 | } |
---|
1942 | respuestaConsola(og_client_socket(cli), ptrTrama, true); |
---|
1943 | return true; |
---|
1944 | } |
---|
1945 | // ________________________________________________________________________________________________________ |
---|
1946 | // Función: RestaurarSoftIncremental |
---|
1947 | // |
---|
1948 | // Descripción: |
---|
1949 | // Restaura una imagen básica junto con software incremental en una partición |
---|
1950 | // Parámetros: |
---|
1951 | // - socket_c: Socket de la consola al envió el mensaje |
---|
1952 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1953 | // Devuelve: |
---|
1954 | // true: Si el proceso es correcto |
---|
1955 | // false: En caso de ocurrir algún error |
---|
1956 | // ________________________________________________________________________________________________________ |
---|
1957 | static bool RestaurarSoftIncremental(TRAMA* ptrTrama, struct og_client *cli) |
---|
1958 | { |
---|
1959 | if (!enviaComando(ptrTrama, CLIENTE_OCUPADO)) { |
---|
1960 | respuestaConsola(og_client_socket(cli), ptrTrama, false); |
---|
1961 | return false; |
---|
1962 | } |
---|
1963 | respuestaConsola(og_client_socket(cli), ptrTrama, true); |
---|
1964 | return true; |
---|
1965 | } |
---|
1966 | // ________________________________________________________________________________________________________ |
---|
1967 | // Función: RESPUESTA_RestaurarImagen |
---|
1968 | // |
---|
1969 | // Descripción: |
---|
1970 | // Respuesta del cliente al comando RestaurarImagen |
---|
1971 | // Parámetros: |
---|
1972 | // - socket_c: Socket del cliente que envió el mensaje |
---|
1973 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
1974 | // Devuelve: |
---|
1975 | // true: Si el proceso es correcto |
---|
1976 | // false: En caso de ocurrir algún error |
---|
1977 | // ________________________________________________________________________________________________________ |
---|
1978 | // |
---|
1979 | static bool RESPUESTA_RestaurarImagen(TRAMA* ptrTrama, struct og_client *cli) |
---|
1980 | { |
---|
1981 | char msglog[LONSTD]; |
---|
1982 | Database db; |
---|
1983 | Table tbl; |
---|
1984 | bool res; |
---|
1985 | char *iph, *ido, *idi, *dsk, *par, *ifs, *cfg; |
---|
1986 | |
---|
1987 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
1988 | db.GetErrorErrStr(msglog); |
---|
1989 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
1990 | __func__, __LINE__, msglog); |
---|
1991 | return false; |
---|
1992 | } |
---|
1993 | |
---|
1994 | iph = copiaParametro("iph",ptrTrama); // Toma dirección ip |
---|
1995 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del ordenador |
---|
1996 | |
---|
1997 | if (!respuestaEstandar(ptrTrama, iph, ido, db, tbl)) { |
---|
1998 | liberaMemoria(iph); |
---|
1999 | liberaMemoria(ido); |
---|
2000 | syslog(LOG_ERR, "failed to register notification\n"); |
---|
2001 | return false; |
---|
2002 | } |
---|
2003 | |
---|
2004 | // Acciones posteriores |
---|
2005 | idi = copiaParametro("idi",ptrTrama); // Toma identificador de la imagen |
---|
2006 | dsk = copiaParametro("dsk",ptrTrama); // Número de disco |
---|
2007 | par = copiaParametro("par",ptrTrama); // Número de partición |
---|
2008 | ifs = copiaParametro("ifs",ptrTrama); // Identificador del perfil software contenido |
---|
2009 | cfg = copiaParametro("cfg",ptrTrama); // Configuración de discos |
---|
2010 | if(cfg){ |
---|
2011 | actualizaConfiguracion(db, tbl, cfg, atoi(ido)); // Actualiza la configuración del ordenador |
---|
2012 | liberaMemoria(cfg); |
---|
2013 | } |
---|
2014 | res=actualizaRestauracionImagen(db, tbl, idi, dsk, par, ido, ifs); |
---|
2015 | |
---|
2016 | liberaMemoria(iph); |
---|
2017 | liberaMemoria(ido); |
---|
2018 | liberaMemoria(idi); |
---|
2019 | liberaMemoria(par); |
---|
2020 | liberaMemoria(ifs); |
---|
2021 | |
---|
2022 | if(!res){ |
---|
2023 | syslog(LOG_ERR, "Problem after restoring image\n"); |
---|
2024 | db.Close(); |
---|
2025 | return false; |
---|
2026 | } |
---|
2027 | |
---|
2028 | db.Close(); // Cierra conexión |
---|
2029 | return true; |
---|
2030 | } |
---|
2031 | // ________________________________________________________________________________________________________ |
---|
2032 | // |
---|
2033 | // Función: RESPUESTA_RestaurarImagenBasica |
---|
2034 | // |
---|
2035 | // Descripción: |
---|
2036 | // Respuesta del cliente al comando RestaurarImagen |
---|
2037 | // Parámetros: |
---|
2038 | // - socket_c: Socket del cliente que envió el mensaje |
---|
2039 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
2040 | // Devuelve: |
---|
2041 | // true: Si el proceso es correcto |
---|
2042 | // false: En caso de ocurrir algún error |
---|
2043 | // ________________________________________________________________________________________________________ |
---|
2044 | // |
---|
2045 | static bool RESPUESTA_RestaurarImagenBasica(TRAMA* ptrTrama, struct og_client *cli) |
---|
2046 | { |
---|
2047 | return RESPUESTA_RestaurarImagen(ptrTrama, cli); |
---|
2048 | } |
---|
2049 | // ________________________________________________________________________________________________________ |
---|
2050 | // Función: RESPUESTA_RestaurarSoftIncremental |
---|
2051 | // |
---|
2052 | // Descripción: |
---|
2053 | // Respuesta del cliente al comando RestaurarSoftIncremental |
---|
2054 | // Parámetros: |
---|
2055 | // - socket_c: Socket del cliente que envió el mensaje |
---|
2056 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
2057 | // Devuelve: |
---|
2058 | // true: Si el proceso es correcto |
---|
2059 | // false: En caso de ocurrir algún error |
---|
2060 | // ________________________________________________________________________________________________________ |
---|
2061 | static bool RESPUESTA_RestaurarSoftIncremental(TRAMA* ptrTrama, struct og_client *cli) |
---|
2062 | { |
---|
2063 | return RESPUESTA_RestaurarImagen(ptrTrama, cli); |
---|
2064 | } |
---|
2065 | // ________________________________________________________________________________________________________ |
---|
2066 | // Función: actualizaRestauracionImagen |
---|
2067 | // |
---|
2068 | // Descripción: |
---|
2069 | // Esta función actualiza la base de datos con el resultado de la restauración de una imagen |
---|
2070 | // Parámetros: |
---|
2071 | // - db: Objeto base de datos (ya operativo) |
---|
2072 | // - tbl: Objeto tabla |
---|
2073 | // - idi: Identificador de la imagen |
---|
2074 | // - dsk: Disco de donde se restauró |
---|
2075 | // - par: Partición de donde se restauró |
---|
2076 | // - ido: Identificador del cliente donde se restauró |
---|
2077 | // - ifs: Identificador del perfil software contenido en la imagen |
---|
2078 | // Devuelve: |
---|
2079 | // true: Si el proceso es correcto |
---|
2080 | // false: En caso de ocurrir algún error |
---|
2081 | // ________________________________________________________________________________________________________ |
---|
2082 | bool actualizaRestauracionImagen(Database db, Table tbl, char *idi, |
---|
2083 | char *dsk, char *par, char *ido, char *ifs) |
---|
2084 | { |
---|
2085 | char msglog[LONSTD], sqlstr[LONSQL]; |
---|
2086 | |
---|
2087 | /* Actualizar los datos de la imagen */ |
---|
2088 | snprintf(sqlstr, LONSQL, |
---|
2089 | "UPDATE ordenadores_particiones" |
---|
2090 | " SET idimagen=%s, idperfilsoft=%s, fechadespliegue=NOW()," |
---|
2091 | " revision=(SELECT revision FROM imagenes WHERE idimagen=%s)," |
---|
2092 | " idnombreso=IFNULL((SELECT idnombreso FROM perfilessoft WHERE idperfilsoft=%s),0)" |
---|
2093 | " WHERE idordenador=%s AND numdisk=%s AND numpar=%s", idi, ifs, idi, ifs, ido, dsk, par); |
---|
2094 | |
---|
2095 | if (!db.Execute(sqlstr, tbl)) { |
---|
2096 | db.GetErrorErrStr(msglog); |
---|
2097 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2098 | __func__, __LINE__, msglog); |
---|
2099 | return false; |
---|
2100 | } |
---|
2101 | return true; |
---|
2102 | } |
---|
2103 | // ________________________________________________________________________________________________________ |
---|
2104 | // Función: Configurar |
---|
2105 | // |
---|
2106 | // Descripción: |
---|
2107 | // Configura la tabla de particiones |
---|
2108 | // Parámetros: |
---|
2109 | // - socket_c: Socket de la consola al envió el mensaje |
---|
2110 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
2111 | // Devuelve: |
---|
2112 | // true: Si el proceso es correcto |
---|
2113 | // false: En caso de ocurrir algún error |
---|
2114 | // ________________________________________________________________________________________________________ |
---|
2115 | static bool Configurar(TRAMA* ptrTrama, struct og_client *cli) |
---|
2116 | { |
---|
2117 | if (!enviaComando(ptrTrama, CLIENTE_OCUPADO)) { |
---|
2118 | respuestaConsola(og_client_socket(cli), ptrTrama, false); |
---|
2119 | return false; |
---|
2120 | } |
---|
2121 | respuestaConsola(og_client_socket(cli), ptrTrama, true); |
---|
2122 | return true; |
---|
2123 | } |
---|
2124 | // ________________________________________________________________________________________________________ |
---|
2125 | // Función: RESPUESTA_Configurar |
---|
2126 | // |
---|
2127 | // Descripción: |
---|
2128 | // Respuesta del cliente al comando Configurar |
---|
2129 | // Parámetros: |
---|
2130 | // - socket_c: Socket del cliente que envió el mensaje |
---|
2131 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
2132 | // Devuelve: |
---|
2133 | // true: Si el proceso es correcto |
---|
2134 | // false: En caso de ocurrir algún error |
---|
2135 | // ________________________________________________________________________________________________________ |
---|
2136 | // |
---|
2137 | static bool RESPUESTA_Configurar(TRAMA* ptrTrama, struct og_client *ci) |
---|
2138 | { |
---|
2139 | char msglog[LONSTD]; |
---|
2140 | Database db; |
---|
2141 | Table tbl; |
---|
2142 | bool res; |
---|
2143 | char *iph, *ido,*cfg; |
---|
2144 | |
---|
2145 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
2146 | db.GetErrorErrStr(msglog); |
---|
2147 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
2148 | __func__, __LINE__, msglog); |
---|
2149 | return false; |
---|
2150 | } |
---|
2151 | |
---|
2152 | iph = copiaParametro("iph",ptrTrama); // Toma dirección ip |
---|
2153 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del ordenador |
---|
2154 | |
---|
2155 | if (!respuestaEstandar(ptrTrama, iph, ido, db, tbl)) { |
---|
2156 | liberaMemoria(iph); |
---|
2157 | liberaMemoria(ido); |
---|
2158 | syslog(LOG_ERR, "failed to register notification\n"); |
---|
2159 | return false; |
---|
2160 | } |
---|
2161 | |
---|
2162 | cfg = copiaParametro("cfg",ptrTrama); // Toma configuración de particiones |
---|
2163 | res=actualizaConfiguracion(db, tbl, cfg, atoi(ido)); // Actualiza la configuración del ordenador |
---|
2164 | |
---|
2165 | liberaMemoria(iph); |
---|
2166 | liberaMemoria(ido); |
---|
2167 | liberaMemoria(cfg); |
---|
2168 | |
---|
2169 | if(!res){ |
---|
2170 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
2171 | return false; |
---|
2172 | } |
---|
2173 | |
---|
2174 | db.Close(); // Cierra conexión |
---|
2175 | return true; |
---|
2176 | } |
---|
2177 | // ________________________________________________________________________________________________________ |
---|
2178 | // Función: EjecutarScript |
---|
2179 | // |
---|
2180 | // Descripción: |
---|
2181 | // Ejecuta un script de código |
---|
2182 | // Parámetros: |
---|
2183 | // - socket_c: Socket de la consola al envió el mensaje |
---|
2184 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
2185 | // Devuelve: |
---|
2186 | // true: Si el proceso es correcto |
---|
2187 | // false: En caso de ocurrir algún error |
---|
2188 | // ________________________________________________________________________________________________________ |
---|
2189 | static bool EjecutarScript(TRAMA* ptrTrama, struct og_client *cli) |
---|
2190 | { |
---|
2191 | if (!enviaComando(ptrTrama, CLIENTE_OCUPADO)) { |
---|
2192 | respuestaConsola(og_client_socket(cli), ptrTrama, false); |
---|
2193 | return false; |
---|
2194 | } |
---|
2195 | respuestaConsola(og_client_socket(cli), ptrTrama, true); |
---|
2196 | return true; |
---|
2197 | } |
---|
2198 | // ________________________________________________________________________________________________________ |
---|
2199 | // Función: RESPUESTA_EjecutarScript |
---|
2200 | // |
---|
2201 | // Descripción: |
---|
2202 | // Respuesta del cliente al comando EjecutarScript |
---|
2203 | // Parámetros: |
---|
2204 | // - socket_c: Socket del cliente que envió el mensaje |
---|
2205 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
2206 | // Devuelve: |
---|
2207 | // true: Si el proceso es correcto |
---|
2208 | // false: En caso de ocurrir algún error |
---|
2209 | // ________________________________________________________________________________________________________ |
---|
2210 | static bool RESPUESTA_EjecutarScript(TRAMA* ptrTrama, struct og_client *cli) |
---|
2211 | { |
---|
2212 | char msglog[LONSTD]; |
---|
2213 | Database db; |
---|
2214 | Table tbl; |
---|
2215 | char *iph, *ido,*cfg; |
---|
2216 | |
---|
2217 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
2218 | db.GetErrorErrStr(msglog); |
---|
2219 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
2220 | __func__, __LINE__, msglog); |
---|
2221 | return false; |
---|
2222 | } |
---|
2223 | |
---|
2224 | iph = copiaParametro("iph",ptrTrama); // Toma dirección ip |
---|
2225 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del ordenador |
---|
2226 | |
---|
2227 | if (!respuestaEstandar(ptrTrama, iph, ido, db, tbl)) { |
---|
2228 | liberaMemoria(iph); |
---|
2229 | liberaMemoria(ido); |
---|
2230 | syslog(LOG_ERR, "failed to register notification\n"); |
---|
2231 | return false; |
---|
2232 | } |
---|
2233 | |
---|
2234 | cfg = copiaParametro("cfg",ptrTrama); // Toma configuración de particiones |
---|
2235 | if(cfg){ |
---|
2236 | actualizaConfiguracion(db, tbl, cfg, atoi(ido)); // Actualiza la configuración del ordenador |
---|
2237 | liberaMemoria(cfg); |
---|
2238 | } |
---|
2239 | |
---|
2240 | liberaMemoria(iph); |
---|
2241 | liberaMemoria(ido); |
---|
2242 | |
---|
2243 | |
---|
2244 | db.Close(); // Cierra conexión |
---|
2245 | return true; |
---|
2246 | } |
---|
2247 | // ________________________________________________________________________________________________________ |
---|
2248 | // Función: InventarioHardware |
---|
2249 | // |
---|
2250 | // Descripción: |
---|
2251 | // Solicita al cliente un inventario de su hardware |
---|
2252 | // Parámetros: |
---|
2253 | // - socket_c: Socket de la consola al envió el mensaje |
---|
2254 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
2255 | // Devuelve: |
---|
2256 | // true: Si el proceso es correcto |
---|
2257 | // false: En caso de ocurrir algún error |
---|
2258 | // ________________________________________________________________________________________________________ |
---|
2259 | static bool InventarioHardware(TRAMA* ptrTrama, struct og_client *cli) |
---|
2260 | { |
---|
2261 | if (!enviaComando(ptrTrama, CLIENTE_OCUPADO)) { |
---|
2262 | respuestaConsola(og_client_socket(cli), ptrTrama, false); |
---|
2263 | return false; |
---|
2264 | } |
---|
2265 | respuestaConsola(og_client_socket(cli), ptrTrama, true); |
---|
2266 | return true; |
---|
2267 | } |
---|
2268 | // ________________________________________________________________________________________________________ |
---|
2269 | // Función: RESPUESTA_InventarioHardware |
---|
2270 | // |
---|
2271 | // Descripción: |
---|
2272 | // Respuesta del cliente al comando InventarioHardware |
---|
2273 | // Parámetros: |
---|
2274 | // - socket_c: Socket del cliente que envió el mensaje |
---|
2275 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
2276 | // Devuelve: |
---|
2277 | // true: Si el proceso es correcto |
---|
2278 | // false: En caso de ocurrir algún error |
---|
2279 | // ________________________________________________________________________________________________________ |
---|
2280 | static bool RESPUESTA_InventarioHardware(TRAMA* ptrTrama, struct og_client *cli) |
---|
2281 | { |
---|
2282 | char msglog[LONSTD]; |
---|
2283 | Database db; |
---|
2284 | Table tbl; |
---|
2285 | bool res; |
---|
2286 | char *iph, *ido, *idc, *npc, *hrd, *buffer; |
---|
2287 | |
---|
2288 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
2289 | db.GetErrorErrStr(msglog); |
---|
2290 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
2291 | __func__, __LINE__, msglog); |
---|
2292 | return false; |
---|
2293 | } |
---|
2294 | |
---|
2295 | iph = copiaParametro("iph",ptrTrama); // Toma dirección ip del cliente |
---|
2296 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del cliente |
---|
2297 | |
---|
2298 | if (!respuestaEstandar(ptrTrama, iph, ido, db, tbl)) { |
---|
2299 | liberaMemoria(iph); |
---|
2300 | liberaMemoria(ido); |
---|
2301 | syslog(LOG_ERR, "failed to register notification\n"); |
---|
2302 | return false; |
---|
2303 | } |
---|
2304 | // Lee archivo de inventario enviado anteriormente |
---|
2305 | hrd = copiaParametro("hrd",ptrTrama); |
---|
2306 | buffer = rTrim(leeArchivo(hrd)); |
---|
2307 | |
---|
2308 | npc = copiaParametro("npc",ptrTrama); |
---|
2309 | idc = copiaParametro("idc",ptrTrama); // Toma identificador del Centro |
---|
2310 | |
---|
2311 | if (buffer) |
---|
2312 | res=actualizaHardware(db, tbl, buffer, ido, npc, idc); |
---|
2313 | |
---|
2314 | liberaMemoria(iph); |
---|
2315 | liberaMemoria(ido); |
---|
2316 | liberaMemoria(npc); |
---|
2317 | liberaMemoria(idc); |
---|
2318 | liberaMemoria(buffer); |
---|
2319 | |
---|
2320 | if(!res){ |
---|
2321 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
2322 | return false; |
---|
2323 | } |
---|
2324 | |
---|
2325 | db.Close(); // Cierra conexión |
---|
2326 | return true; |
---|
2327 | } |
---|
2328 | // ________________________________________________________________________________________________________ |
---|
2329 | // Función: actualizaHardware |
---|
2330 | // |
---|
2331 | // Descripción: |
---|
2332 | // Actualiza la base de datos con la configuracion hardware del cliente |
---|
2333 | // Parámetros: |
---|
2334 | // - db: Objeto base de datos (ya operativo) |
---|
2335 | // - tbl: Objeto tabla |
---|
2336 | // - hrd: cadena con el inventario hardware |
---|
2337 | // - ido: Identificador del ordenador |
---|
2338 | // - npc: Nombre del ordenador |
---|
2339 | // - idc: Identificador del centro o Unidad organizativa |
---|
2340 | // ________________________________________________________________________________________________________ |
---|
2341 | // |
---|
2342 | bool actualizaHardware(Database db, Table tbl, char *hrd, char *ido, char *npc, |
---|
2343 | char *idc) |
---|
2344 | { |
---|
2345 | char msglog[LONSTD], sqlstr[LONSQL]; |
---|
2346 | int idtipohardware, idperfilhard; |
---|
2347 | int lon, i, j, aux; |
---|
2348 | bool retval; |
---|
2349 | char *whard; |
---|
2350 | int tbidhardware[MAXHARDWARE]; |
---|
2351 | char *tbHardware[MAXHARDWARE],*dualHardware[2], descripcion[250], strInt[LONINT], *idhardwares; |
---|
2352 | |
---|
2353 | /* Toma Centro (Unidad Organizativa) */ |
---|
2354 | sprintf(sqlstr, "SELECT * FROM ordenadores WHERE idordenador=%s", ido); |
---|
2355 | |
---|
2356 | if (!db.Execute(sqlstr, tbl)) { |
---|
2357 | db.GetErrorErrStr(msglog); |
---|
2358 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2359 | __func__, __LINE__, msglog); |
---|
2360 | return false; |
---|
2361 | } |
---|
2362 | if (!tbl.Get("idperfilhard", idperfilhard)) { // Toma dato |
---|
2363 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
2364 | og_info(msglog); |
---|
2365 | return false; |
---|
2366 | } |
---|
2367 | whard=escaparCadena(hrd); // Codificar comillas simples |
---|
2368 | if(!whard) |
---|
2369 | return false; |
---|
2370 | /* Recorre componentes hardware*/ |
---|
2371 | lon = splitCadena(tbHardware, whard, '\n'); |
---|
2372 | if (lon > MAXHARDWARE) |
---|
2373 | lon = MAXHARDWARE; // Limita el número de componentes hardware |
---|
2374 | /* |
---|
2375 | for (i=0;i<lon;i++){ |
---|
2376 | sprintf(msglog,"Linea de inventario: %s",tbHardware[i]); |
---|
2377 | RegistraLog(msglog,false); |
---|
2378 | } |
---|
2379 | */ |
---|
2380 | for (i = 0; i < lon; i++) { |
---|
2381 | splitCadena(dualHardware, rTrim(tbHardware[i]), '='); |
---|
2382 | //sprintf(msglog,"nemonico: %s",dualHardware[0]); |
---|
2383 | //RegistraLog(msglog,false); |
---|
2384 | //sprintf(msglog,"valor: %s",dualHardware[1]); |
---|
2385 | //RegistraLog(msglog,false); |
---|
2386 | sprintf(sqlstr, "SELECT idtipohardware,descripcion FROM tipohardwares " |
---|
2387 | " WHERE nemonico='%s'", dualHardware[0]); |
---|
2388 | if (!db.Execute(sqlstr, tbl)) { |
---|
2389 | db.GetErrorErrStr(msglog); |
---|
2390 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2391 | __func__, __LINE__, msglog); |
---|
2392 | return false; |
---|
2393 | } |
---|
2394 | if (tbl.ISEOF()) { // Tipo de Hardware NO existente |
---|
2395 | sprintf(msglog, "%s: %s)", tbErrores[54], dualHardware[0]); |
---|
2396 | og_info(msglog); |
---|
2397 | return false; |
---|
2398 | } else { // Tipo de Hardware Existe |
---|
2399 | if (!tbl.Get("idtipohardware", idtipohardware)) { // Toma dato |
---|
2400 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
2401 | og_info(msglog); |
---|
2402 | return false; |
---|
2403 | } |
---|
2404 | if (!tbl.Get("descripcion", descripcion)) { // Toma dato |
---|
2405 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
2406 | og_info(msglog); |
---|
2407 | return false; |
---|
2408 | } |
---|
2409 | |
---|
2410 | sprintf(sqlstr, "SELECT idhardware FROM hardwares " |
---|
2411 | " WHERE idtipohardware=%d AND descripcion='%s'", |
---|
2412 | idtipohardware, dualHardware[1]); |
---|
2413 | |
---|
2414 | if (!db.Execute(sqlstr, tbl)) { |
---|
2415 | db.GetErrorErrStr(msglog); |
---|
2416 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2417 | __func__, __LINE__, msglog); |
---|
2418 | return false; |
---|
2419 | } |
---|
2420 | |
---|
2421 | if (tbl.ISEOF()) { // Hardware NO existente |
---|
2422 | sprintf(sqlstr, "INSERT hardwares (idtipohardware,descripcion,idcentro,grupoid) " |
---|
2423 | " VALUES(%d,'%s',%s,0)", idtipohardware, |
---|
2424 | dualHardware[1], idc); |
---|
2425 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2426 | db.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
2427 | og_info(msglog); |
---|
2428 | return false; |
---|
2429 | } |
---|
2430 | // Recupera el identificador del hardware |
---|
2431 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
2432 | if (!db.Execute(sqlstr, tbl)) { |
---|
2433 | db.GetErrorErrStr(msglog); |
---|
2434 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2435 | __func__, __LINE__, msglog); |
---|
2436 | return false; |
---|
2437 | } |
---|
2438 | if (!tbl.ISEOF()) { // Si existe registro |
---|
2439 | if (!tbl.Get("identificador", tbidhardware[i])) { |
---|
2440 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
2441 | og_info(msglog); |
---|
2442 | return false; |
---|
2443 | } |
---|
2444 | } |
---|
2445 | } else { |
---|
2446 | if (!tbl.Get("idhardware", tbidhardware[i])) { // Toma dato |
---|
2447 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
2448 | og_info(msglog); |
---|
2449 | return false; |
---|
2450 | } |
---|
2451 | } |
---|
2452 | } |
---|
2453 | } |
---|
2454 | // Ordena tabla de identificadores para cosultar si existe un pefil con esas especificaciones |
---|
2455 | |
---|
2456 | for (i = 0; i < lon - 1; i++) { |
---|
2457 | for (j = i + 1; j < lon; j++) { |
---|
2458 | if (tbidhardware[i] > tbidhardware[j]) { |
---|
2459 | aux = tbidhardware[i]; |
---|
2460 | tbidhardware[i] = tbidhardware[j]; |
---|
2461 | tbidhardware[j] = aux; |
---|
2462 | } |
---|
2463 | } |
---|
2464 | } |
---|
2465 | /* Crea cadena de identificadores de componentes hardware separados por coma */ |
---|
2466 | sprintf(strInt, "%d", tbidhardware[lon - 1]); // Pasa a cadena el último identificador que es de mayor longitud |
---|
2467 | aux = strlen(strInt); // Calcula longitud de cadena para reservar espacio a todos los perfiles |
---|
2468 | idhardwares = reservaMemoria(sizeof(aux) * lon + lon); |
---|
2469 | if (idhardwares == NULL) { |
---|
2470 | syslog(LOG_ERR, "%s:%d OOM\n", __FILE__, __LINE__); |
---|
2471 | return false; |
---|
2472 | } |
---|
2473 | aux = sprintf(idhardwares, "%d", tbidhardware[0]); |
---|
2474 | for (i = 1; i < lon; i++) |
---|
2475 | aux += sprintf(idhardwares + aux, ",%d", tbidhardware[i]); |
---|
2476 | |
---|
2477 | if (!cuestionPerfilHardware(db, tbl, idc, ido, idperfilhard, idhardwares, |
---|
2478 | npc, tbidhardware, lon)) { |
---|
2479 | syslog(LOG_ERR, "Problem updating client hardware\n"); |
---|
2480 | retval=false; |
---|
2481 | } |
---|
2482 | else { |
---|
2483 | retval=true; |
---|
2484 | } |
---|
2485 | liberaMemoria(whard); |
---|
2486 | liberaMemoria(idhardwares); |
---|
2487 | return (retval); |
---|
2488 | } |
---|
2489 | // ________________________________________________________________________________________________________ |
---|
2490 | // Función: cuestionPerfilHardware |
---|
2491 | // |
---|
2492 | // Descripción: |
---|
2493 | // Comprueba existencia de perfil hardware y actualización de éste para el ordenador |
---|
2494 | // Parámetros: |
---|
2495 | // - db: Objeto base de datos (ya operativo) |
---|
2496 | // - tbl: Objeto tabla |
---|
2497 | // - idc: Identificador de la Unidad organizativa donde se encuentra el cliente |
---|
2498 | // - ido: Identificador del ordenador |
---|
2499 | // - tbidhardware: Identificador del tipo de hardware |
---|
2500 | // - con: Número de componentes detectados para configurar un el perfil hardware |
---|
2501 | // - npc: Nombre del cliente |
---|
2502 | // ________________________________________________________________________________________________________ |
---|
2503 | bool cuestionPerfilHardware(Database db, Table tbl, char *idc, char *ido, |
---|
2504 | int idperfilhardware, char *idhardwares, char *npc, int *tbidhardware, |
---|
2505 | int lon) |
---|
2506 | { |
---|
2507 | char msglog[LONSTD], *sqlstr; |
---|
2508 | int i; |
---|
2509 | int nwidperfilhard; |
---|
2510 | |
---|
2511 | sqlstr = reservaMemoria(strlen(idhardwares)+LONSQL); // Reserva para escribir sentencia SQL |
---|
2512 | if (sqlstr == NULL) { |
---|
2513 | syslog(LOG_ERR, "%s:%d OOM\n", __FILE__, __LINE__); |
---|
2514 | return false; |
---|
2515 | } |
---|
2516 | // Busca perfil hard del ordenador que contenga todos los componentes hardware encontrados |
---|
2517 | sprintf(sqlstr, "SELECT idperfilhard FROM" |
---|
2518 | " (SELECT perfileshard_hardwares.idperfilhard as idperfilhard," |
---|
2519 | " group_concat(cast(perfileshard_hardwares.idhardware AS char( 11) )" |
---|
2520 | " ORDER BY perfileshard_hardwares.idhardware SEPARATOR ',' ) AS idhardwares" |
---|
2521 | " FROM perfileshard_hardwares" |
---|
2522 | " GROUP BY perfileshard_hardwares.idperfilhard) AS temp" |
---|
2523 | " WHERE idhardwares LIKE '%s'", idhardwares); |
---|
2524 | |
---|
2525 | if (!db.Execute(sqlstr, tbl)) { |
---|
2526 | db.GetErrorErrStr(msglog); |
---|
2527 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2528 | __func__, __LINE__, msglog); |
---|
2529 | liberaMemoria(sqlstr); |
---|
2530 | return false; |
---|
2531 | } |
---|
2532 | if (tbl.ISEOF()) { // No existe un perfil hardware con esos componentes de componentes hardware, lo crea |
---|
2533 | sprintf(sqlstr, "INSERT perfileshard (descripcion,idcentro,grupoid)" |
---|
2534 | " VALUES('Perfil hardware (%s) ',%s,0)", npc, idc); |
---|
2535 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2536 | db.GetErrorErrStr(msglog); |
---|
2537 | og_info(msglog); |
---|
2538 | liberaMemoria(sqlstr); |
---|
2539 | return false; |
---|
2540 | } |
---|
2541 | // Recupera el identificador del nuevo perfil hardware |
---|
2542 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
2543 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
2544 | db.GetErrorErrStr(msglog); |
---|
2545 | og_info(msglog); |
---|
2546 | liberaMemoria(sqlstr); |
---|
2547 | return false; |
---|
2548 | } |
---|
2549 | if (!tbl.ISEOF()) { // Si existe registro |
---|
2550 | if (!tbl.Get("identificador", nwidperfilhard)) { |
---|
2551 | tbl.GetErrorErrStr(msglog); |
---|
2552 | og_info(msglog); |
---|
2553 | liberaMemoria(sqlstr); |
---|
2554 | return false; |
---|
2555 | } |
---|
2556 | } |
---|
2557 | // Crea la relación entre perfiles y componenetes hardware |
---|
2558 | for (i = 0; i < lon; i++) { |
---|
2559 | sprintf(sqlstr, "INSERT perfileshard_hardwares (idperfilhard,idhardware)" |
---|
2560 | " VALUES(%d,%d)", nwidperfilhard, tbidhardware[i]); |
---|
2561 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2562 | db.GetErrorErrStr(msglog); |
---|
2563 | og_info(msglog); |
---|
2564 | liberaMemoria(sqlstr); |
---|
2565 | return false; |
---|
2566 | } |
---|
2567 | } |
---|
2568 | } else { // Existe un perfil con todos esos componentes |
---|
2569 | if (!tbl.Get("idperfilhard", nwidperfilhard)) { |
---|
2570 | tbl.GetErrorErrStr(msglog); |
---|
2571 | og_info(msglog); |
---|
2572 | liberaMemoria(sqlstr); |
---|
2573 | return false; |
---|
2574 | } |
---|
2575 | } |
---|
2576 | if (idperfilhardware != nwidperfilhard) { // No coinciden los perfiles |
---|
2577 | // Actualiza el identificador del perfil hardware del ordenador |
---|
2578 | sprintf(sqlstr, "UPDATE ordenadores SET idperfilhard=%d" |
---|
2579 | " WHERE idordenador=%s", nwidperfilhard, ido); |
---|
2580 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2581 | db.GetErrorErrStr(msglog); |
---|
2582 | og_info(msglog); |
---|
2583 | liberaMemoria(sqlstr); |
---|
2584 | return false; |
---|
2585 | } |
---|
2586 | } |
---|
2587 | /* Eliminar Relación de hardwares con Perfiles hardware que quedan húerfanos */ |
---|
2588 | sprintf(sqlstr, "DELETE FROM perfileshard_hardwares WHERE idperfilhard IN " |
---|
2589 | " (SELECT idperfilhard FROM perfileshard WHERE idperfilhard NOT IN" |
---|
2590 | " (SELECT DISTINCT idperfilhard from ordenadores))"); |
---|
2591 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2592 | db.GetErrorErrStr(msglog); |
---|
2593 | og_info(msglog); |
---|
2594 | liberaMemoria(sqlstr); |
---|
2595 | return false; |
---|
2596 | } |
---|
2597 | |
---|
2598 | /* Eliminar Perfiles hardware que quedan húerfanos */ |
---|
2599 | sprintf(sqlstr, "DELETE FROM perfileshard WHERE idperfilhard NOT IN" |
---|
2600 | " (SELECT DISTINCT idperfilhard FROM ordenadores)"); |
---|
2601 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2602 | db.GetErrorErrStr(msglog); |
---|
2603 | og_info(msglog); |
---|
2604 | liberaMemoria(sqlstr); |
---|
2605 | return false; |
---|
2606 | } |
---|
2607 | /* Eliminar Relación de hardwares con Perfiles hardware que quedan húerfanos */ |
---|
2608 | sprintf(sqlstr, "DELETE FROM perfileshard_hardwares WHERE idperfilhard NOT IN" |
---|
2609 | " (SELECT idperfilhard FROM perfileshard)"); |
---|
2610 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2611 | db.GetErrorErrStr(msglog); |
---|
2612 | og_info(msglog); |
---|
2613 | liberaMemoria(sqlstr); |
---|
2614 | return false; |
---|
2615 | } |
---|
2616 | liberaMemoria(sqlstr); |
---|
2617 | return true; |
---|
2618 | } |
---|
2619 | // ________________________________________________________________________________________________________ |
---|
2620 | // Función: InventarioSoftware |
---|
2621 | // |
---|
2622 | // Descripción: |
---|
2623 | // Solicita al cliente un inventario de su software |
---|
2624 | // Parámetros: |
---|
2625 | // - socket_c: Socket de la consola al envió el mensaje |
---|
2626 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
2627 | // Devuelve: |
---|
2628 | // true: Si el proceso es correcto |
---|
2629 | // false: En caso de ocurrir algún error |
---|
2630 | // ________________________________________________________________________________________________________ |
---|
2631 | static bool InventarioSoftware(TRAMA* ptrTrama, struct og_client *cli) |
---|
2632 | { |
---|
2633 | if (!enviaComando(ptrTrama, CLIENTE_OCUPADO)) { |
---|
2634 | respuestaConsola(og_client_socket(cli), ptrTrama, false); |
---|
2635 | return false; |
---|
2636 | } |
---|
2637 | respuestaConsola(og_client_socket(cli), ptrTrama, true); |
---|
2638 | return true; |
---|
2639 | } |
---|
2640 | // ________________________________________________________________________________________________________ |
---|
2641 | // Función: RESPUESTA_InventarioSoftware |
---|
2642 | // |
---|
2643 | // Descripción: |
---|
2644 | // Respuesta del cliente al comando InventarioSoftware |
---|
2645 | // Parámetros: |
---|
2646 | // - socket_c: Socket del cliente que envió el mensaje |
---|
2647 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
2648 | // Devuelve: |
---|
2649 | // true: Si el proceso es correcto |
---|
2650 | // false: En caso de ocurrir algún error |
---|
2651 | // ________________________________________________________________________________________________________ |
---|
2652 | static bool RESPUESTA_InventarioSoftware(TRAMA* ptrTrama, struct og_client *cli) |
---|
2653 | { |
---|
2654 | char msglog[LONSTD]; |
---|
2655 | Database db; |
---|
2656 | Table tbl; |
---|
2657 | bool res; |
---|
2658 | char *iph, *ido, *npc, *idc, *par, *sft, *buffer; |
---|
2659 | |
---|
2660 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
2661 | db.GetErrorErrStr(msglog); |
---|
2662 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
2663 | __func__, __LINE__, msglog); |
---|
2664 | return false; |
---|
2665 | } |
---|
2666 | |
---|
2667 | iph = copiaParametro("iph",ptrTrama); // Toma dirección ip |
---|
2668 | ido = copiaParametro("ido",ptrTrama); // Toma identificador del ordenador |
---|
2669 | |
---|
2670 | if (!respuestaEstandar(ptrTrama, iph, ido, db, tbl)) { |
---|
2671 | liberaMemoria(iph); |
---|
2672 | liberaMemoria(ido); |
---|
2673 | syslog(LOG_ERR, "failed to register notification\n"); |
---|
2674 | return false; |
---|
2675 | } |
---|
2676 | |
---|
2677 | npc = copiaParametro("npc",ptrTrama); |
---|
2678 | idc = copiaParametro("idc",ptrTrama); // Toma identificador del Centro |
---|
2679 | par = copiaParametro("par",ptrTrama); |
---|
2680 | sft = copiaParametro("sft",ptrTrama); |
---|
2681 | |
---|
2682 | buffer = rTrim(leeArchivo(sft)); |
---|
2683 | if (buffer) |
---|
2684 | res=actualizaSoftware(db, tbl, buffer, par, ido, npc, idc); |
---|
2685 | |
---|
2686 | liberaMemoria(iph); |
---|
2687 | liberaMemoria(ido); |
---|
2688 | liberaMemoria(npc); |
---|
2689 | liberaMemoria(idc); |
---|
2690 | liberaMemoria(par); |
---|
2691 | liberaMemoria(sft); |
---|
2692 | |
---|
2693 | if(!res){ |
---|
2694 | syslog(LOG_ERR, "cannot update software\n"); |
---|
2695 | return false; |
---|
2696 | } |
---|
2697 | |
---|
2698 | db.Close(); // Cierra conexión |
---|
2699 | return true; |
---|
2700 | } |
---|
2701 | // ________________________________________________________________________________________________________ |
---|
2702 | // Función: actualizaSoftware |
---|
2703 | // |
---|
2704 | // Descripción: |
---|
2705 | // Actualiza la base de datos con la configuración software del cliente |
---|
2706 | // Parámetros: |
---|
2707 | // - db: Objeto base de datos (ya operativo) |
---|
2708 | // - tbl: Objeto tabla |
---|
2709 | // - sft: cadena con el inventario software |
---|
2710 | // - par: Número de la partición |
---|
2711 | // - ido: Identificador del ordenador del cliente en la tabla |
---|
2712 | // - npc: Nombre del ordenador |
---|
2713 | // - idc: Identificador del centro o Unidad organizativa |
---|
2714 | // Devuelve: |
---|
2715 | // true: Si el proceso es correcto |
---|
2716 | // false: En caso de ocurrir algún error |
---|
2717 | // |
---|
2718 | // Versión 1.1.0: Se incluye el sistema operativo. Autora: Irina Gómez - ETSII Universidad Sevilla |
---|
2719 | // ________________________________________________________________________________________________________ |
---|
2720 | bool actualizaSoftware(Database db, Table tbl, char *sft, char *par,char *ido, |
---|
2721 | char *npc, char *idc) |
---|
2722 | { |
---|
2723 | int i, j, lon, aux, idperfilsoft, idnombreso; |
---|
2724 | bool retval; |
---|
2725 | char *wsft; |
---|
2726 | int tbidsoftware[MAXSOFTWARE]; |
---|
2727 | char *tbSoftware[MAXSOFTWARE],msglog[LONSTD], sqlstr[LONSQL], strInt[LONINT], *idsoftwares; |
---|
2728 | |
---|
2729 | /* Toma Centro (Unidad Organizativa) y perfil software */ |
---|
2730 | sprintf(sqlstr, "SELECT idperfilsoft,numpar" |
---|
2731 | " FROM ordenadores_particiones" |
---|
2732 | " WHERE idordenador=%s", ido); |
---|
2733 | |
---|
2734 | if (!db.Execute(sqlstr, tbl)) { |
---|
2735 | db.GetErrorErrStr(msglog); |
---|
2736 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2737 | __func__, __LINE__, msglog); |
---|
2738 | return false; |
---|
2739 | } |
---|
2740 | idperfilsoft = 0; // Por defecto se supone que el ordenador no tiene aún detectado el perfil software |
---|
2741 | while (!tbl.ISEOF()) { // Recorre particiones |
---|
2742 | if (!tbl.Get("numpar", aux)) { |
---|
2743 | tbl.GetErrorErrStr(msglog); |
---|
2744 | og_info(msglog); |
---|
2745 | return false; |
---|
2746 | } |
---|
2747 | if (aux == atoi(par)) { // Se encuentra la partición |
---|
2748 | if (!tbl.Get("idperfilsoft", idperfilsoft)) { |
---|
2749 | tbl.GetErrorErrStr(msglog); |
---|
2750 | og_info(msglog); |
---|
2751 | return false; |
---|
2752 | } |
---|
2753 | break; |
---|
2754 | } |
---|
2755 | tbl.MoveNext(); |
---|
2756 | } |
---|
2757 | wsft=escaparCadena(sft); // Codificar comillas simples |
---|
2758 | if(!wsft) |
---|
2759 | return false; |
---|
2760 | |
---|
2761 | /* Recorre componentes software*/ |
---|
2762 | lon = splitCadena(tbSoftware, wsft, '\n'); |
---|
2763 | |
---|
2764 | if (lon == 0) |
---|
2765 | return true; // No hay lineas que procesar |
---|
2766 | if (lon > MAXSOFTWARE) |
---|
2767 | lon = MAXSOFTWARE; // Limita el número de componentes software |
---|
2768 | |
---|
2769 | for (i = 0; i < lon; i++) { |
---|
2770 | // Primera línea es el sistema operativo: se obtiene identificador |
---|
2771 | if (i == 0) { |
---|
2772 | idnombreso = checkDato(db, tbl, rTrim(tbSoftware[i]), "nombresos", "nombreso", "idnombreso"); |
---|
2773 | continue; |
---|
2774 | } |
---|
2775 | |
---|
2776 | sprintf(sqlstr, |
---|
2777 | "SELECT idsoftware FROM softwares WHERE descripcion ='%s'", |
---|
2778 | rTrim(tbSoftware[i])); |
---|
2779 | |
---|
2780 | if (!db.Execute(sqlstr, tbl)) { |
---|
2781 | db.GetErrorErrStr(msglog); |
---|
2782 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2783 | __func__, __LINE__, msglog); |
---|
2784 | return false; |
---|
2785 | } |
---|
2786 | |
---|
2787 | if (tbl.ISEOF()) { // Software NO existente |
---|
2788 | sprintf(sqlstr, "INSERT INTO softwares (idtiposoftware,descripcion,idcentro,grupoid)" |
---|
2789 | " VALUES(2,'%s',%s,0)", tbSoftware[i], idc); |
---|
2790 | |
---|
2791 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2792 | db.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
2793 | og_info(msglog); |
---|
2794 | return false; |
---|
2795 | } |
---|
2796 | // Recupera el identificador del software |
---|
2797 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
2798 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
2799 | db.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
2800 | og_info(msglog); |
---|
2801 | return false; |
---|
2802 | } |
---|
2803 | if (!tbl.ISEOF()) { // Si existe registro |
---|
2804 | if (!tbl.Get("identificador", tbidsoftware[i])) { |
---|
2805 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
2806 | og_info(msglog); |
---|
2807 | return false; |
---|
2808 | } |
---|
2809 | } |
---|
2810 | } else { |
---|
2811 | if (!tbl.Get("idsoftware", tbidsoftware[i])) { // Toma dato |
---|
2812 | tbl.GetErrorErrStr(msglog); // Error al acceder al registro |
---|
2813 | og_info(msglog); |
---|
2814 | return false; |
---|
2815 | } |
---|
2816 | } |
---|
2817 | } |
---|
2818 | |
---|
2819 | // Ordena tabla de identificadores para cosultar si existe un pefil con esas especificaciones |
---|
2820 | |
---|
2821 | for (i = 0; i < lon - 1; i++) { |
---|
2822 | for (j = i + 1; j < lon; j++) { |
---|
2823 | if (tbidsoftware[i] > tbidsoftware[j]) { |
---|
2824 | aux = tbidsoftware[i]; |
---|
2825 | tbidsoftware[i] = tbidsoftware[j]; |
---|
2826 | tbidsoftware[j] = aux; |
---|
2827 | } |
---|
2828 | } |
---|
2829 | } |
---|
2830 | /* Crea cadena de identificadores de componentes software separados por coma */ |
---|
2831 | sprintf(strInt, "%d", tbidsoftware[lon - 1]); // Pasa a cadena el último identificador que es de mayor longitud |
---|
2832 | aux = strlen(strInt); // Calcula longitud de cadena para reservar espacio a todos los perfiles |
---|
2833 | idsoftwares = reservaMemoria((sizeof(aux)+1) * lon + lon); |
---|
2834 | if (idsoftwares == NULL) { |
---|
2835 | syslog(LOG_ERR, "%s:%d OOM\n", __FILE__, __LINE__); |
---|
2836 | return false; |
---|
2837 | } |
---|
2838 | aux = sprintf(idsoftwares, "%d", tbidsoftware[0]); |
---|
2839 | for (i = 1; i < lon; i++) |
---|
2840 | aux += sprintf(idsoftwares + aux, ",%d", tbidsoftware[i]); |
---|
2841 | |
---|
2842 | // Comprueba existencia de perfil software y actualización de éste para el ordenador |
---|
2843 | if (!cuestionPerfilSoftware(db, tbl, idc, ido, idperfilsoft, idnombreso, idsoftwares, |
---|
2844 | npc, par, tbidsoftware, lon)) { |
---|
2845 | syslog(LOG_ERR, "cannot update software\n"); |
---|
2846 | og_info(msglog); |
---|
2847 | retval=false; |
---|
2848 | } |
---|
2849 | else { |
---|
2850 | retval=true; |
---|
2851 | } |
---|
2852 | liberaMemoria(wsft); |
---|
2853 | liberaMemoria(idsoftwares); |
---|
2854 | return (retval); |
---|
2855 | } |
---|
2856 | // ________________________________________________________________________________________________________ |
---|
2857 | // Función: CuestionPerfilSoftware |
---|
2858 | // |
---|
2859 | // Parámetros: |
---|
2860 | // - db: Objeto base de datos (ya operativo) |
---|
2861 | // - tbl: Objeto tabla |
---|
2862 | // - idcentro: Identificador del centro en la tabla |
---|
2863 | // - ido: Identificador del ordenador del cliente en la tabla |
---|
2864 | // - idnombreso: Identificador del sistema operativo |
---|
2865 | // - idsoftwares: Cadena con los identificadores de componentes software separados por comas |
---|
2866 | // - npc: Nombre del ordenador del cliente |
---|
2867 | // - particion: Número de la partición |
---|
2868 | // - tbidsoftware: Array con los identificadores de componentes software |
---|
2869 | // - lon: Número de componentes |
---|
2870 | // Devuelve: |
---|
2871 | // true: Si el proceso es correcto |
---|
2872 | // false: En caso de ocurrir algún error |
---|
2873 | // |
---|
2874 | // Versión 1.1.0: Se incluye el sistema operativo. Autora: Irina Gómez - ETSII Universidad Sevilla |
---|
2875 | //_________________________________________________________________________________________________________ |
---|
2876 | bool cuestionPerfilSoftware(Database db, Table tbl, char *idc, char *ido, |
---|
2877 | int idperfilsoftware, int idnombreso, |
---|
2878 | char *idsoftwares, char *npc, char *par, |
---|
2879 | int *tbidsoftware, int lon) |
---|
2880 | { |
---|
2881 | char *sqlstr, msglog[LONSTD]; |
---|
2882 | int i, nwidperfilsoft; |
---|
2883 | |
---|
2884 | sqlstr = reservaMemoria(strlen(idsoftwares)+LONSQL); // Reserva para escribir sentencia SQL |
---|
2885 | if (sqlstr == NULL) { |
---|
2886 | syslog(LOG_ERR, "%s:%d OOM\n", __FILE__, __LINE__); |
---|
2887 | return false; |
---|
2888 | } |
---|
2889 | // Busca perfil soft del ordenador que contenga todos los componentes software encontrados |
---|
2890 | sprintf(sqlstr, "SELECT idperfilsoft FROM" |
---|
2891 | " (SELECT perfilessoft_softwares.idperfilsoft as idperfilsoft," |
---|
2892 | " group_concat(cast(perfilessoft_softwares.idsoftware AS char( 11) )" |
---|
2893 | " ORDER BY perfilessoft_softwares.idsoftware SEPARATOR ',' ) AS idsoftwares" |
---|
2894 | " FROM perfilessoft_softwares" |
---|
2895 | " GROUP BY perfilessoft_softwares.idperfilsoft) AS temp" |
---|
2896 | " WHERE idsoftwares LIKE '%s'", idsoftwares); |
---|
2897 | |
---|
2898 | if (!db.Execute(sqlstr, tbl)) { |
---|
2899 | db.GetErrorErrStr(msglog); |
---|
2900 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2901 | __func__, __LINE__, msglog); |
---|
2902 | liberaMemoria(sqlstr); |
---|
2903 | return false; |
---|
2904 | } |
---|
2905 | if (tbl.ISEOF()) { // No existe un perfil software con esos componentes de componentes software, lo crea |
---|
2906 | sprintf(sqlstr, "INSERT perfilessoft (descripcion, idcentro, grupoid, idnombreso)" |
---|
2907 | " VALUES('Perfil Software (%s, Part:%s) ',%s,0,%i)", npc, par, idc,idnombreso); |
---|
2908 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2909 | db.GetErrorErrStr(msglog); |
---|
2910 | og_info(msglog); |
---|
2911 | return false; |
---|
2912 | } |
---|
2913 | // Recupera el identificador del nuevo perfil software |
---|
2914 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
2915 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
2916 | tbl.GetErrorErrStr(msglog); |
---|
2917 | og_info(msglog); |
---|
2918 | liberaMemoria(sqlstr); |
---|
2919 | return false; |
---|
2920 | } |
---|
2921 | if (!tbl.ISEOF()) { // Si existe registro |
---|
2922 | if (!tbl.Get("identificador", nwidperfilsoft)) { |
---|
2923 | tbl.GetErrorErrStr(msglog); |
---|
2924 | og_info(msglog); |
---|
2925 | liberaMemoria(sqlstr); |
---|
2926 | return false; |
---|
2927 | } |
---|
2928 | } |
---|
2929 | // Crea la relación entre perfiles y componenetes software |
---|
2930 | for (i = 0; i < lon; i++) { |
---|
2931 | sprintf(sqlstr, "INSERT perfilessoft_softwares (idperfilsoft,idsoftware)" |
---|
2932 | " VALUES(%d,%d)", nwidperfilsoft, tbidsoftware[i]); |
---|
2933 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2934 | db.GetErrorErrStr(msglog); |
---|
2935 | og_info(msglog); |
---|
2936 | liberaMemoria(sqlstr); |
---|
2937 | return false; |
---|
2938 | } |
---|
2939 | } |
---|
2940 | } else { // Existe un perfil con todos esos componentes |
---|
2941 | if (!tbl.Get("idperfilsoft", nwidperfilsoft)) { |
---|
2942 | tbl.GetErrorErrStr(msglog); |
---|
2943 | og_info(msglog); |
---|
2944 | liberaMemoria(sqlstr); |
---|
2945 | return false; |
---|
2946 | } |
---|
2947 | } |
---|
2948 | |
---|
2949 | if (idperfilsoftware != nwidperfilsoft) { // No coinciden los perfiles |
---|
2950 | // Actualiza el identificador del perfil software del ordenador |
---|
2951 | sprintf(sqlstr, "UPDATE ordenadores_particiones SET idperfilsoft=%d,idimagen=0" |
---|
2952 | " WHERE idordenador=%s AND numpar=%s", nwidperfilsoft, ido, par); |
---|
2953 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2954 | db.GetErrorErrStr(msglog); |
---|
2955 | og_info(msglog); |
---|
2956 | liberaMemoria(sqlstr); |
---|
2957 | return false; |
---|
2958 | } |
---|
2959 | } |
---|
2960 | |
---|
2961 | /* DEPURACIÓN DE PERFILES SOFTWARE */ |
---|
2962 | |
---|
2963 | /* Eliminar Relación de softwares con Perfiles software que quedan húerfanos */ |
---|
2964 | sprintf(sqlstr, "DELETE FROM perfilessoft_softwares WHERE idperfilsoft IN "\ |
---|
2965 | " (SELECT idperfilsoft FROM perfilessoft WHERE idperfilsoft NOT IN"\ |
---|
2966 | " (SELECT DISTINCT idperfilsoft from ordenadores_particiones) AND idperfilsoft NOT IN"\ |
---|
2967 | " (SELECT DISTINCT idperfilsoft from imagenes))"); |
---|
2968 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2969 | db.GetErrorErrStr(msglog); |
---|
2970 | og_info(msglog); |
---|
2971 | liberaMemoria(sqlstr); |
---|
2972 | return false; |
---|
2973 | } |
---|
2974 | /* Eliminar Perfiles software que quedan húerfanos */ |
---|
2975 | sprintf(sqlstr, "DELETE FROM perfilessoft WHERE idperfilsoft NOT IN" |
---|
2976 | " (SELECT DISTINCT idperfilsoft from ordenadores_particiones)"\ |
---|
2977 | " AND idperfilsoft NOT IN"\ |
---|
2978 | " (SELECT DISTINCT idperfilsoft from imagenes)"); |
---|
2979 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2980 | db.GetErrorErrStr(msglog); |
---|
2981 | og_info(msglog); |
---|
2982 | liberaMemoria(sqlstr); |
---|
2983 | return false; |
---|
2984 | } |
---|
2985 | /* Eliminar Relación de softwares con Perfiles software que quedan húerfanos */ |
---|
2986 | sprintf(sqlstr, "DELETE FROM perfilessoft_softwares WHERE idperfilsoft NOT IN" |
---|
2987 | " (SELECT idperfilsoft from perfilessoft)"); |
---|
2988 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
2989 | db.GetErrorErrStr(msglog); |
---|
2990 | og_info(msglog); |
---|
2991 | liberaMemoria(sqlstr); |
---|
2992 | return false; |
---|
2993 | } |
---|
2994 | liberaMemoria(sqlstr); |
---|
2995 | return true; |
---|
2996 | } |
---|
2997 | // ________________________________________________________________________________________________________ |
---|
2998 | // Función: enviaArchivo |
---|
2999 | // |
---|
3000 | // Descripción: |
---|
3001 | // Envia un archivo por la red, por bloques |
---|
3002 | // Parámetros: |
---|
3003 | // - socket_c: Socket del cliente que envió el mensaje |
---|
3004 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
3005 | // Devuelve: |
---|
3006 | // true: Si el proceso es correcto |
---|
3007 | // false: En caso de ocurrir algún error |
---|
3008 | // ________________________________________________________________________________________________________ |
---|
3009 | static bool enviaArchivo(TRAMA *ptrTrama, struct og_client *cli) |
---|
3010 | { |
---|
3011 | int socket_c = og_client_socket(cli); |
---|
3012 | char *nfl; |
---|
3013 | |
---|
3014 | // Toma parámetros |
---|
3015 | nfl = copiaParametro("nfl",ptrTrama); // Toma nombre completo del archivo |
---|
3016 | if (!sendArchivo(&socket_c, nfl)) { |
---|
3017 | liberaMemoria(nfl); |
---|
3018 | syslog(LOG_ERR, "Problem sending file\n"); |
---|
3019 | return false; |
---|
3020 | } |
---|
3021 | liberaMemoria(nfl); |
---|
3022 | return true; |
---|
3023 | } |
---|
3024 | // ________________________________________________________________________________________________________ |
---|
3025 | // Función: enviaArchivo |
---|
3026 | // |
---|
3027 | // Descripción: |
---|
3028 | // Envia un archivo por la red, por bloques |
---|
3029 | // Parámetros: |
---|
3030 | // - socket_c: Socket del cliente que envió el mensaje |
---|
3031 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
3032 | // Devuelve: |
---|
3033 | // true: Si el proceso es correcto |
---|
3034 | // false: En caso de ocurrir algún error |
---|
3035 | // ________________________________________________________________________________________________________ |
---|
3036 | static bool recibeArchivo(TRAMA *ptrTrama, struct og_client *cli) |
---|
3037 | { |
---|
3038 | int socket_c = og_client_socket(cli); |
---|
3039 | char *nfl; |
---|
3040 | |
---|
3041 | // Toma parámetros |
---|
3042 | nfl = copiaParametro("nfl",ptrTrama); // Toma nombre completo del archivo |
---|
3043 | ptrTrama->tipo = MSG_NOTIFICACION; |
---|
3044 | enviaFlag(&socket_c, ptrTrama); |
---|
3045 | if (!recArchivo(&socket_c, nfl)) { |
---|
3046 | liberaMemoria(nfl); |
---|
3047 | syslog(LOG_ERR, "Problem receiving file\n"); |
---|
3048 | return false; |
---|
3049 | } |
---|
3050 | liberaMemoria(nfl); |
---|
3051 | return true; |
---|
3052 | } |
---|
3053 | // ________________________________________________________________________________________________________ |
---|
3054 | // Función: envioProgramacion |
---|
3055 | // |
---|
3056 | // Descripción: |
---|
3057 | // Envia un comando de actualización a todos los ordenadores que han sido programados con |
---|
3058 | // alguna acción para que entren en el bucle de comandos pendientes y las ejecuten |
---|
3059 | // Parámetros: |
---|
3060 | // - socket_c: Socket del cliente que envió el mensaje |
---|
3061 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros |
---|
3062 | // Devuelve: |
---|
3063 | // true: Si el proceso es correcto |
---|
3064 | // false: En caso de ocurrir algún error |
---|
3065 | // ________________________________________________________________________________________________________ |
---|
3066 | static bool envioProgramacion(TRAMA *ptrTrama, struct og_client *cli) |
---|
3067 | { |
---|
3068 | char *ptrIP[MAXIMOS_CLIENTES],*ptrMacs[MAXIMOS_CLIENTES]; |
---|
3069 | char sqlstr[LONSQL], msglog[LONSTD]; |
---|
3070 | char *idp,iph[LONIP],mac[LONMAC]; |
---|
3071 | Database db; |
---|
3072 | Table tbl; |
---|
3073 | int idx,idcomando,lon; |
---|
3074 | |
---|
3075 | if (!db.Open(usuario, pasguor, datasource, catalog)) { |
---|
3076 | db.GetErrorErrStr(msglog); |
---|
3077 | syslog(LOG_ERR, "cannot open connection database (%s:%d) %s\n", |
---|
3078 | __func__, __LINE__, msglog); |
---|
3079 | return false; |
---|
3080 | } |
---|
3081 | |
---|
3082 | idp = copiaParametro("idp",ptrTrama); // Toma identificador de la programación de la tabla acciones |
---|
3083 | |
---|
3084 | sprintf(sqlstr, "SELECT ordenadores.ip,ordenadores.mac,acciones.idcomando FROM acciones "\ |
---|
3085 | " INNER JOIN ordenadores ON ordenadores.ip=acciones.ip"\ |
---|
3086 | " WHERE acciones.idprogramacion=%s",idp); |
---|
3087 | |
---|
3088 | liberaMemoria(idp); |
---|
3089 | |
---|
3090 | if (!db.Execute(sqlstr, tbl)) { |
---|
3091 | db.GetErrorErrStr(msglog); |
---|
3092 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3093 | __func__, __LINE__, msglog); |
---|
3094 | return false; |
---|
3095 | } |
---|
3096 | db.Close(); |
---|
3097 | if(tbl.ISEOF()) |
---|
3098 | return true; // No existen registros |
---|
3099 | |
---|
3100 | /* Prepara la trama de actualizacion */ |
---|
3101 | |
---|
3102 | initParametros(ptrTrama,0); |
---|
3103 | ptrTrama->tipo=MSG_COMANDO; |
---|
3104 | sprintf(ptrTrama->parametros, "nfn=Actualizar\r"); |
---|
3105 | |
---|
3106 | while (!tbl.ISEOF()) { // Recorre particiones |
---|
3107 | if (!tbl.Get("ip", iph)) { |
---|
3108 | tbl.GetErrorErrStr(msglog); |
---|
3109 | syslog(LOG_ERR, "cannot find ip column in table: %s\n", |
---|
3110 | msglog); |
---|
3111 | return false; |
---|
3112 | } |
---|
3113 | if (!tbl.Get("idcomando", idcomando)) { |
---|
3114 | tbl.GetErrorErrStr(msglog); |
---|
3115 | syslog(LOG_ERR, "cannot find idcomando column in table: %s\n", |
---|
3116 | msglog); |
---|
3117 | return false; |
---|
3118 | } |
---|
3119 | if(idcomando==1){ // Arrancar |
---|
3120 | if (!tbl.Get("mac", mac)) { |
---|
3121 | tbl.GetErrorErrStr(msglog); |
---|
3122 | syslog(LOG_ERR, "cannot find mac column in table: %s\n", |
---|
3123 | msglog); |
---|
3124 | return false; |
---|
3125 | } |
---|
3126 | |
---|
3127 | lon = splitCadena(ptrIP, iph, ';'); |
---|
3128 | lon = splitCadena(ptrMacs, mac, ';'); |
---|
3129 | |
---|
3130 | // Se manda por broadcast y por unicast |
---|
3131 | if (!Levanta(ptrIP, ptrMacs, lon, (char*)"1")) |
---|
3132 | return false; |
---|
3133 | |
---|
3134 | if (!Levanta(ptrIP, ptrMacs, lon, (char*)"2")) |
---|
3135 | return false; |
---|
3136 | |
---|
3137 | } |
---|
3138 | if (clienteDisponible(iph, &idx)) { // Si el cliente puede recibir comandos |
---|
3139 | int sock = tbsockets[idx].cli ? tbsockets[idx].cli->io.fd : -1; |
---|
3140 | |
---|
3141 | strcpy(tbsockets[idx].estado, CLIENTE_OCUPADO); // Actualiza el estado del cliente |
---|
3142 | if (!mandaTrama(&sock, ptrTrama)) { |
---|
3143 | syslog(LOG_ERR, "failed to send response: %s\n", |
---|
3144 | strerror(errno)); |
---|
3145 | return false; |
---|
3146 | } |
---|
3147 | //close(tbsockets[idx].sock); // Cierra el socket del cliente hasta nueva disponibilidad |
---|
3148 | } |
---|
3149 | tbl.MoveNext(); |
---|
3150 | } |
---|
3151 | return true; // No existen registros |
---|
3152 | } |
---|
3153 | |
---|
3154 | // This object stores function handler for messages |
---|
3155 | static struct { |
---|
3156 | const char *nf; // Nombre de la función |
---|
3157 | bool (*fcn)(TRAMA *, struct og_client *cli); |
---|
3158 | } tbfuncionesServer[] = { |
---|
3159 | { "InclusionCliente", InclusionCliente, }, |
---|
3160 | { "InclusionClienteWinLnx", InclusionClienteWinLnx, }, |
---|
3161 | { "AutoexecCliente", AutoexecCliente, }, |
---|
3162 | { "ComandosPendientes", ComandosPendientes, }, |
---|
3163 | { "DisponibilidadComandos", DisponibilidadComandos, }, |
---|
3164 | { "RESPUESTA_Arrancar", RESPUESTA_Arrancar, }, |
---|
3165 | { "Apagar", Apagar, }, |
---|
3166 | { "RESPUESTA_Apagar", RESPUESTA_Apagar, }, |
---|
3167 | { "RESPUESTA_Reiniciar", RESPUESTA_Reiniciar, }, |
---|
3168 | { "RESPUESTA_IniciarSesion", RESPUESTA_IniciarSesion, }, |
---|
3169 | { "CrearImagen", CrearImagen, }, |
---|
3170 | { "RESPUESTA_CrearImagen", RESPUESTA_CrearImagen, }, |
---|
3171 | { "CrearImagenBasica", CrearImagenBasica, }, |
---|
3172 | { "RESPUESTA_CrearImagenBasica", RESPUESTA_CrearImagenBasica, }, |
---|
3173 | { "CrearSoftIncremental", CrearSoftIncremental, }, |
---|
3174 | { "RESPUESTA_CrearSoftIncremental", RESPUESTA_CrearSoftIncremental, }, |
---|
3175 | { "RestaurarImagen", RestaurarImagen, }, |
---|
3176 | { "RESPUESTA_RestaurarImagen", RESPUESTA_RestaurarImagen }, |
---|
3177 | { "RestaurarImagenBasica", RestaurarImagenBasica, }, |
---|
3178 | { "RESPUESTA_RestaurarImagenBasica", RESPUESTA_RestaurarImagenBasica, }, |
---|
3179 | { "RestaurarSoftIncremental", RestaurarSoftIncremental, }, |
---|
3180 | { "RESPUESTA_RestaurarSoftIncremental", RESPUESTA_RestaurarSoftIncremental, }, |
---|
3181 | { "Configurar", Configurar, }, |
---|
3182 | { "RESPUESTA_Configurar", RESPUESTA_Configurar, }, |
---|
3183 | { "EjecutarScript", EjecutarScript, }, |
---|
3184 | { "RESPUESTA_EjecutarScript", RESPUESTA_EjecutarScript, }, |
---|
3185 | { "InventarioHardware", InventarioHardware, }, |
---|
3186 | { "RESPUESTA_InventarioHardware", RESPUESTA_InventarioHardware, }, |
---|
3187 | { "InventarioSoftware", InventarioSoftware }, |
---|
3188 | { "RESPUESTA_InventarioSoftware", RESPUESTA_InventarioSoftware, }, |
---|
3189 | { "enviaArchivo", enviaArchivo, }, |
---|
3190 | { "recibeArchivo", recibeArchivo, }, |
---|
3191 | { "envioProgramacion", envioProgramacion, }, |
---|
3192 | { NULL, NULL, }, |
---|
3193 | }; |
---|
3194 | |
---|
3195 | // ________________________________________________________________________________________________________ |
---|
3196 | // Función: gestionaTrama |
---|
3197 | // |
---|
3198 | // Descripción: |
---|
3199 | // Procesa las tramas recibidas . |
---|
3200 | // Parametros: |
---|
3201 | // - s : Socket usado para comunicaciones |
---|
3202 | // Devuelve: |
---|
3203 | // true: Si el proceso es correcto |
---|
3204 | // false: En caso de ocurrir algún error |
---|
3205 | // ________________________________________________________________________________________________________ |
---|
3206 | static void gestionaTrama(TRAMA *ptrTrama, struct og_client *cli) |
---|
3207 | { |
---|
3208 | int i, res; |
---|
3209 | char *nfn; |
---|
3210 | |
---|
3211 | if (ptrTrama){ |
---|
3212 | INTROaFINCAD(ptrTrama); |
---|
3213 | nfn = copiaParametro("nfn",ptrTrama); // Toma nombre de la función |
---|
3214 | |
---|
3215 | for (i = 0; tbfuncionesServer[i].fcn; i++) { |
---|
3216 | if (!strncmp(tbfuncionesServer[i].nf, nfn, |
---|
3217 | strlen(tbfuncionesServer[i].nf))) { |
---|
3218 | res = tbfuncionesServer[i].fcn(ptrTrama, cli); |
---|
3219 | if (!res) { |
---|
3220 | syslog(LOG_ERR, "Failed handling of %s for client %s:%hu\n", |
---|
3221 | tbfuncionesServer[i].nf, |
---|
3222 | inet_ntoa(cli->addr.sin_addr), |
---|
3223 | ntohs(cli->addr.sin_port)); |
---|
3224 | } else { |
---|
3225 | syslog(LOG_DEBUG, "Successful handling of %s for client %s:%hu\n", |
---|
3226 | tbfuncionesServer[i].nf, |
---|
3227 | inet_ntoa(cli->addr.sin_addr), |
---|
3228 | ntohs(cli->addr.sin_port)); |
---|
3229 | } |
---|
3230 | break; |
---|
3231 | } |
---|
3232 | } |
---|
3233 | if (!tbfuncionesServer[i].fcn) |
---|
3234 | syslog(LOG_ERR, "unknown request %s from client %s:%hu\n", |
---|
3235 | nfn, inet_ntoa(cli->addr.sin_addr), |
---|
3236 | ntohs(cli->addr.sin_port)); |
---|
3237 | |
---|
3238 | liberaMemoria(nfn); |
---|
3239 | } |
---|
3240 | } |
---|
3241 | |
---|
3242 | static void og_client_release(struct ev_loop *loop, struct og_client *cli) |
---|
3243 | { |
---|
3244 | if (cli->keepalive_idx >= 0) { |
---|
3245 | syslog(LOG_DEBUG, "closing keepalive connection for %s:%hu in slot %d\n", |
---|
3246 | inet_ntoa(cli->addr.sin_addr), |
---|
3247 | ntohs(cli->addr.sin_port), cli->keepalive_idx); |
---|
3248 | tbsockets[cli->keepalive_idx].cli = NULL; |
---|
3249 | } |
---|
3250 | |
---|
3251 | ev_io_stop(loop, &cli->io); |
---|
3252 | close(cli->io.fd); |
---|
3253 | free(cli); |
---|
3254 | } |
---|
3255 | |
---|
3256 | static void og_client_keepalive(struct ev_loop *loop, struct og_client *cli) |
---|
3257 | { |
---|
3258 | struct og_client *old_cli; |
---|
3259 | |
---|
3260 | old_cli = tbsockets[cli->keepalive_idx].cli; |
---|
3261 | if (old_cli && old_cli != cli) { |
---|
3262 | syslog(LOG_DEBUG, "closing old keepalive connection for %s:%hu\n", |
---|
3263 | inet_ntoa(old_cli->addr.sin_addr), |
---|
3264 | ntohs(old_cli->addr.sin_port)); |
---|
3265 | |
---|
3266 | og_client_release(loop, old_cli); |
---|
3267 | } |
---|
3268 | tbsockets[cli->keepalive_idx].cli = cli; |
---|
3269 | } |
---|
3270 | |
---|
3271 | static void og_client_reset_state(struct og_client *cli) |
---|
3272 | { |
---|
3273 | cli->state = OG_CLIENT_RECEIVING_HEADER; |
---|
3274 | cli->buf_len = 0; |
---|
3275 | } |
---|
3276 | |
---|
3277 | static int og_client_state_recv_hdr(struct og_client *cli) |
---|
3278 | { |
---|
3279 | char hdrlen[LONHEXPRM]; |
---|
3280 | |
---|
3281 | /* Still too short to validate protocol fingerprint and message |
---|
3282 | * length. |
---|
3283 | */ |
---|
3284 | if (cli->buf_len < 15 + LONHEXPRM) |
---|
3285 | return 0; |
---|
3286 | |
---|
3287 | if (strncmp(cli->buf, "@JMMLCAMDJ_MCDJ", 15)) { |
---|
3288 | syslog(LOG_ERR, "bad fingerprint from client %s:%hu, closing\n", |
---|
3289 | inet_ntoa(cli->addr.sin_addr), |
---|
3290 | ntohs(cli->addr.sin_port)); |
---|
3291 | return -1; |
---|
3292 | } |
---|
3293 | |
---|
3294 | memcpy(hdrlen, &cli->buf[LONGITUD_CABECERATRAMA], LONHEXPRM); |
---|
3295 | cli->msg_len = strtol(hdrlen, NULL, 16); |
---|
3296 | |
---|
3297 | /* Header announces more that we can fit into buffer. */ |
---|
3298 | if (cli->msg_len >= sizeof(cli->buf)) { |
---|
3299 | syslog(LOG_ERR, "too large message %u bytes from %s:%hu\n", |
---|
3300 | cli->msg_len, inet_ntoa(cli->addr.sin_addr), |
---|
3301 | ntohs(cli->addr.sin_port)); |
---|
3302 | return -1; |
---|
3303 | } |
---|
3304 | |
---|
3305 | return 1; |
---|
3306 | } |
---|
3307 | |
---|
3308 | static TRAMA *og_msg_alloc(char *data, unsigned int len) |
---|
3309 | { |
---|
3310 | TRAMA *ptrTrama; |
---|
3311 | |
---|
3312 | ptrTrama = (TRAMA *)reservaMemoria(sizeof(TRAMA)); |
---|
3313 | if (!ptrTrama) { |
---|
3314 | syslog(LOG_ERR, "OOM\n"); |
---|
3315 | return NULL; |
---|
3316 | } |
---|
3317 | |
---|
3318 | initParametros(ptrTrama, len); |
---|
3319 | memcpy(ptrTrama, "@JMMLCAMDJ_MCDJ", LONGITUD_CABECERATRAMA); |
---|
3320 | memcpy(ptrTrama->parametros, data, len); |
---|
3321 | ptrTrama->lonprm = len; |
---|
3322 | |
---|
3323 | return ptrTrama; |
---|
3324 | } |
---|
3325 | |
---|
3326 | static void og_msg_free(TRAMA *ptrTrama) |
---|
3327 | { |
---|
3328 | liberaMemoria(ptrTrama->parametros); |
---|
3329 | liberaMemoria(ptrTrama); |
---|
3330 | } |
---|
3331 | |
---|
3332 | static int og_client_state_process_payload(struct og_client *cli) |
---|
3333 | { |
---|
3334 | TRAMA *ptrTrama; |
---|
3335 | char *data; |
---|
3336 | int len; |
---|
3337 | |
---|
3338 | len = cli->msg_len - (LONGITUD_CABECERATRAMA + LONHEXPRM); |
---|
3339 | data = &cli->buf[LONGITUD_CABECERATRAMA + LONHEXPRM]; |
---|
3340 | |
---|
3341 | ptrTrama = og_msg_alloc(data, len); |
---|
3342 | if (!ptrTrama) |
---|
3343 | return -1; |
---|
3344 | |
---|
3345 | gestionaTrama(ptrTrama, cli); |
---|
3346 | |
---|
3347 | og_msg_free(ptrTrama); |
---|
3348 | |
---|
3349 | return 1; |
---|
3350 | } |
---|
3351 | |
---|
3352 | struct og_msg_params { |
---|
3353 | const char *ips_array[64]; |
---|
3354 | const char *mac_array[64]; |
---|
3355 | unsigned int ips_array_len; |
---|
3356 | const char *wol_type; |
---|
3357 | char run_cmd[4096]; |
---|
3358 | const char *disk; |
---|
3359 | const char *partition; |
---|
3360 | }; |
---|
3361 | |
---|
3362 | static int og_json_parse_clients(json_t *element, struct og_msg_params *params) |
---|
3363 | { |
---|
3364 | unsigned int i; |
---|
3365 | json_t *k; |
---|
3366 | |
---|
3367 | if (json_typeof(element) != JSON_ARRAY) |
---|
3368 | return -1; |
---|
3369 | |
---|
3370 | for (i = 0; i < json_array_size(element); i++) { |
---|
3371 | k = json_array_get(element, i); |
---|
3372 | if (json_typeof(k) != JSON_STRING) |
---|
3373 | return -1; |
---|
3374 | |
---|
3375 | params->ips_array[params->ips_array_len++] = |
---|
3376 | json_string_value(k); |
---|
3377 | } |
---|
3378 | return 0; |
---|
3379 | } |
---|
3380 | |
---|
3381 | static int og_cmd_legacy_send(struct og_msg_params *params, const char *cmd, |
---|
3382 | const char *state) |
---|
3383 | { |
---|
3384 | char buf[4096] = {}; |
---|
3385 | int len, err = 0; |
---|
3386 | TRAMA *msg; |
---|
3387 | |
---|
3388 | len = snprintf(buf, sizeof(buf), "nfn=%s\r", cmd); |
---|
3389 | |
---|
3390 | msg = og_msg_alloc(buf, len); |
---|
3391 | if (!msg) |
---|
3392 | return -1; |
---|
3393 | |
---|
3394 | if (!og_send_cmd((char **)params->ips_array, params->ips_array_len, |
---|
3395 | state, msg)) |
---|
3396 | err = -1; |
---|
3397 | |
---|
3398 | og_msg_free(msg); |
---|
3399 | |
---|
3400 | return err; |
---|
3401 | } |
---|
3402 | |
---|
3403 | static int og_cmd_post_clients(json_t *element, struct og_msg_params *params) |
---|
3404 | { |
---|
3405 | const char *key; |
---|
3406 | json_t *value; |
---|
3407 | int err = 0; |
---|
3408 | |
---|
3409 | if (json_typeof(element) != JSON_OBJECT) |
---|
3410 | return -1; |
---|
3411 | |
---|
3412 | json_object_foreach(element, key, value) { |
---|
3413 | if (!strcmp(key, "clients")) |
---|
3414 | err = og_json_parse_clients(value, params); |
---|
3415 | |
---|
3416 | if (err < 0) |
---|
3417 | break; |
---|
3418 | } |
---|
3419 | |
---|
3420 | return og_cmd_legacy_send(params, "Sondeo", CLIENTE_APAGADO); |
---|
3421 | } |
---|
3422 | |
---|
3423 | struct og_buffer { |
---|
3424 | char *data; |
---|
3425 | int len; |
---|
3426 | }; |
---|
3427 | |
---|
3428 | static int og_json_dump_clients(const char *buffer, size_t size, void *data) |
---|
3429 | { |
---|
3430 | struct og_buffer *og_buffer = (struct og_buffer *)data; |
---|
3431 | |
---|
3432 | memcpy(og_buffer->data + og_buffer->len, buffer, size); |
---|
3433 | og_buffer->len += size; |
---|
3434 | |
---|
3435 | return 0; |
---|
3436 | } |
---|
3437 | |
---|
3438 | static int og_cmd_get_clients(json_t *element, struct og_msg_params *params, |
---|
3439 | char *buffer_reply) |
---|
3440 | { |
---|
3441 | json_t *root, *array, *addr, *state, *object; |
---|
3442 | struct og_buffer og_buffer = { |
---|
3443 | .data = buffer_reply, |
---|
3444 | }; |
---|
3445 | int i; |
---|
3446 | |
---|
3447 | array = json_array(); |
---|
3448 | if (!array) |
---|
3449 | return -1; |
---|
3450 | |
---|
3451 | for (i = 0; i < MAXIMOS_CLIENTES; i++) { |
---|
3452 | if (tbsockets[i].ip[0] == '\0') |
---|
3453 | continue; |
---|
3454 | |
---|
3455 | object = json_object(); |
---|
3456 | if (!object) { |
---|
3457 | json_decref(array); |
---|
3458 | return -1; |
---|
3459 | } |
---|
3460 | addr = json_string(tbsockets[i].ip); |
---|
3461 | if (!addr) { |
---|
3462 | json_decref(object); |
---|
3463 | json_decref(array); |
---|
3464 | return -1; |
---|
3465 | } |
---|
3466 | json_object_set_new(object, "addr", addr); |
---|
3467 | |
---|
3468 | state = json_string(tbsockets[i].estado); |
---|
3469 | if (!state) { |
---|
3470 | json_decref(object); |
---|
3471 | json_decref(array); |
---|
3472 | return -1; |
---|
3473 | } |
---|
3474 | json_object_set_new(object, "state", state); |
---|
3475 | |
---|
3476 | json_array_append_new(array, object); |
---|
3477 | } |
---|
3478 | root = json_pack("{s:o}", "clients", array); |
---|
3479 | if (!root) { |
---|
3480 | json_decref(array); |
---|
3481 | return -1; |
---|
3482 | } |
---|
3483 | |
---|
3484 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 4096); |
---|
3485 | json_decref(root); |
---|
3486 | |
---|
3487 | return 0; |
---|
3488 | } |
---|
3489 | |
---|
3490 | static int og_json_parse_target(json_t *element, struct og_msg_params *params) |
---|
3491 | { |
---|
3492 | const char *key; |
---|
3493 | json_t *value; |
---|
3494 | |
---|
3495 | if (json_typeof(element) != JSON_OBJECT) { |
---|
3496 | return -1; |
---|
3497 | } |
---|
3498 | |
---|
3499 | json_object_foreach(element, key, value) { |
---|
3500 | if (!strcmp(key, "addr")) { |
---|
3501 | if (json_typeof(value) != JSON_STRING) |
---|
3502 | return -1; |
---|
3503 | |
---|
3504 | params->ips_array[params->ips_array_len] = |
---|
3505 | json_string_value(value); |
---|
3506 | } else if (!strcmp(key, "mac")) { |
---|
3507 | if (json_typeof(value) != JSON_STRING) |
---|
3508 | return -1; |
---|
3509 | |
---|
3510 | params->mac_array[params->ips_array_len] = |
---|
3511 | json_string_value(value); |
---|
3512 | } |
---|
3513 | } |
---|
3514 | |
---|
3515 | return 0; |
---|
3516 | } |
---|
3517 | |
---|
3518 | static int og_json_parse_targets(json_t *element, struct og_msg_params *params) |
---|
3519 | { |
---|
3520 | unsigned int i; |
---|
3521 | json_t *k; |
---|
3522 | int err; |
---|
3523 | |
---|
3524 | if (json_typeof(element) != JSON_ARRAY) |
---|
3525 | return -1; |
---|
3526 | |
---|
3527 | for (i = 0; i < json_array_size(element); i++) { |
---|
3528 | k = json_array_get(element, i); |
---|
3529 | |
---|
3530 | if (json_typeof(k) != JSON_OBJECT) |
---|
3531 | return -1; |
---|
3532 | |
---|
3533 | err = og_json_parse_target(k, params); |
---|
3534 | if (err < 0) |
---|
3535 | return err; |
---|
3536 | |
---|
3537 | params->ips_array_len++; |
---|
3538 | } |
---|
3539 | return 0; |
---|
3540 | } |
---|
3541 | |
---|
3542 | static int og_json_parse_type(json_t *element, struct og_msg_params *params) |
---|
3543 | { |
---|
3544 | const char *type; |
---|
3545 | |
---|
3546 | if (json_typeof(element) != JSON_STRING) |
---|
3547 | return -1; |
---|
3548 | |
---|
3549 | params->wol_type = json_string_value(element); |
---|
3550 | |
---|
3551 | type = json_string_value(element); |
---|
3552 | if (!strcmp(type, "unicast")) |
---|
3553 | params->wol_type = "2"; |
---|
3554 | else if (!strcmp(type, "broadcast")) |
---|
3555 | params->wol_type = "1"; |
---|
3556 | |
---|
3557 | return 0; |
---|
3558 | } |
---|
3559 | |
---|
3560 | static int og_cmd_wol(json_t *element, struct og_msg_params *params) |
---|
3561 | { |
---|
3562 | const char *key; |
---|
3563 | json_t *value; |
---|
3564 | int err = 0; |
---|
3565 | |
---|
3566 | if (json_typeof(element) != JSON_OBJECT) |
---|
3567 | return -1; |
---|
3568 | |
---|
3569 | json_object_foreach(element, key, value) { |
---|
3570 | if (!strcmp(key, "clients")) { |
---|
3571 | err = og_json_parse_targets(value, params); |
---|
3572 | } else if (!strcmp(key, "type")) { |
---|
3573 | err = og_json_parse_type(value, params); |
---|
3574 | } |
---|
3575 | |
---|
3576 | if (err < 0) |
---|
3577 | break; |
---|
3578 | } |
---|
3579 | |
---|
3580 | if (!Levanta((char **)params->ips_array, (char **)params->mac_array, |
---|
3581 | params->ips_array_len, (char *)params->wol_type)) |
---|
3582 | return -1; |
---|
3583 | |
---|
3584 | return 0; |
---|
3585 | } |
---|
3586 | |
---|
3587 | static int og_json_parse_run(json_t *element, struct og_msg_params *params) |
---|
3588 | { |
---|
3589 | if (json_typeof(element) != JSON_STRING) |
---|
3590 | return -1; |
---|
3591 | |
---|
3592 | snprintf(params->run_cmd, sizeof(params->run_cmd), "%s", |
---|
3593 | json_string_value(element)); |
---|
3594 | |
---|
3595 | return 0; |
---|
3596 | } |
---|
3597 | |
---|
3598 | static int og_cmd_run_post(json_t *element, struct og_msg_params *params) |
---|
3599 | { |
---|
3600 | char buf[4096] = {}, iph[4096] = {}; |
---|
3601 | int err = 0, len; |
---|
3602 | const char *key; |
---|
3603 | unsigned int i; |
---|
3604 | json_t *value; |
---|
3605 | TRAMA *msg; |
---|
3606 | |
---|
3607 | if (json_typeof(element) != JSON_OBJECT) |
---|
3608 | return -1; |
---|
3609 | |
---|
3610 | json_object_foreach(element, key, value) { |
---|
3611 | if (!strcmp(key, "clients")) |
---|
3612 | err = og_json_parse_clients(value, params); |
---|
3613 | if (!strcmp(key, "run")) |
---|
3614 | err = og_json_parse_run(value, params); |
---|
3615 | |
---|
3616 | if (err < 0) |
---|
3617 | break; |
---|
3618 | } |
---|
3619 | |
---|
3620 | for (i = 0; i < params->ips_array_len; i++) { |
---|
3621 | len = snprintf(iph + strlen(iph), sizeof(iph), "%s;", |
---|
3622 | params->ips_array[i]); |
---|
3623 | } |
---|
3624 | len = snprintf(buf, sizeof(buf), "nfn=ConsolaRemota\riph=%s\rscp=%s\r", |
---|
3625 | iph, params->run_cmd); |
---|
3626 | |
---|
3627 | msg = og_msg_alloc(buf, len); |
---|
3628 | if (!msg) |
---|
3629 | return -1; |
---|
3630 | |
---|
3631 | if (!og_send_cmd((char **)params->ips_array, params->ips_array_len, |
---|
3632 | CLIENTE_OCUPADO, msg)) |
---|
3633 | err = -1; |
---|
3634 | |
---|
3635 | og_msg_free(msg); |
---|
3636 | |
---|
3637 | if (err < 0) |
---|
3638 | return err; |
---|
3639 | |
---|
3640 | for (i = 0; i < params->ips_array_len; i++) { |
---|
3641 | char filename[4096]; |
---|
3642 | FILE *f; |
---|
3643 | |
---|
3644 | sprintf(filename, "/tmp/_Seconsola_%s", params->ips_array[i]); |
---|
3645 | f = fopen(filename, "wt"); |
---|
3646 | fclose(f); |
---|
3647 | } |
---|
3648 | |
---|
3649 | return 0; |
---|
3650 | } |
---|
3651 | |
---|
3652 | static int og_cmd_run_get(json_t *element, struct og_msg_params *params, |
---|
3653 | char *buffer_reply) |
---|
3654 | { |
---|
3655 | struct og_buffer og_buffer = { |
---|
3656 | .data = buffer_reply, |
---|
3657 | }; |
---|
3658 | json_t *root, *value, *array; |
---|
3659 | const char *key; |
---|
3660 | unsigned int i; |
---|
3661 | int err = 0; |
---|
3662 | |
---|
3663 | if (json_typeof(element) != JSON_OBJECT) |
---|
3664 | return -1; |
---|
3665 | |
---|
3666 | json_object_foreach(element, key, value) { |
---|
3667 | if (!strcmp(key, "clients")) |
---|
3668 | err = og_json_parse_clients(value, params); |
---|
3669 | |
---|
3670 | if (err < 0) |
---|
3671 | return err; |
---|
3672 | } |
---|
3673 | |
---|
3674 | array = json_array(); |
---|
3675 | if (!array) |
---|
3676 | return -1; |
---|
3677 | |
---|
3678 | for (i = 0; i < params->ips_array_len; i++) { |
---|
3679 | json_t *object, *output, *addr; |
---|
3680 | char data[4096] = {}; |
---|
3681 | char filename[4096]; |
---|
3682 | int fd, numbytes; |
---|
3683 | |
---|
3684 | sprintf(filename, "/tmp/_Seconsola_%s", params->ips_array[i]); |
---|
3685 | |
---|
3686 | fd = open(filename, O_RDONLY); |
---|
3687 | if (!fd) |
---|
3688 | return -1; |
---|
3689 | |
---|
3690 | numbytes = read(fd, data, sizeof(data)); |
---|
3691 | if (numbytes < 0) { |
---|
3692 | close(fd); |
---|
3693 | return -1; |
---|
3694 | } |
---|
3695 | data[sizeof(data) - 1] = '\0'; |
---|
3696 | close(fd); |
---|
3697 | |
---|
3698 | object = json_object(); |
---|
3699 | if (!object) { |
---|
3700 | json_decref(array); |
---|
3701 | return -1; |
---|
3702 | } |
---|
3703 | addr = json_string(params->ips_array[i]); |
---|
3704 | if (!addr) { |
---|
3705 | json_decref(object); |
---|
3706 | json_decref(array); |
---|
3707 | return -1; |
---|
3708 | } |
---|
3709 | json_object_set_new(object, "addr", addr); |
---|
3710 | |
---|
3711 | output = json_string(data); |
---|
3712 | if (!output) { |
---|
3713 | json_decref(object); |
---|
3714 | json_decref(array); |
---|
3715 | return -1; |
---|
3716 | } |
---|
3717 | json_object_set_new(object, "output", output); |
---|
3718 | |
---|
3719 | json_array_append_new(array, object); |
---|
3720 | } |
---|
3721 | |
---|
3722 | root = json_pack("{s:o}", "clients", array); |
---|
3723 | if (!root) |
---|
3724 | return -1; |
---|
3725 | |
---|
3726 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 4096); |
---|
3727 | json_decref(root); |
---|
3728 | |
---|
3729 | return 0; |
---|
3730 | } |
---|
3731 | |
---|
3732 | static int og_json_parse_disk(json_t *element, struct og_msg_params *params) |
---|
3733 | { |
---|
3734 | if (json_typeof(element) != JSON_STRING) |
---|
3735 | return -1; |
---|
3736 | |
---|
3737 | params->disk = json_string_value(element); |
---|
3738 | |
---|
3739 | return 0; |
---|
3740 | } |
---|
3741 | |
---|
3742 | static int og_json_parse_partition(json_t *element, |
---|
3743 | struct og_msg_params *params) |
---|
3744 | { |
---|
3745 | if (json_typeof(element) != JSON_STRING) |
---|
3746 | return -1; |
---|
3747 | |
---|
3748 | params->partition = json_string_value(element); |
---|
3749 | |
---|
3750 | return 0; |
---|
3751 | } |
---|
3752 | |
---|
3753 | static int og_cmd_session(json_t *element, struct og_msg_params *params) |
---|
3754 | { |
---|
3755 | char buf[4096], iph[4096]; |
---|
3756 | int err = 0, len; |
---|
3757 | const char *key; |
---|
3758 | unsigned int i; |
---|
3759 | json_t *value; |
---|
3760 | TRAMA *msg; |
---|
3761 | |
---|
3762 | if (json_typeof(element) != JSON_OBJECT) |
---|
3763 | return -1; |
---|
3764 | |
---|
3765 | json_object_foreach(element, key, value) { |
---|
3766 | if (!strcmp(key, "clients")) { |
---|
3767 | err = og_json_parse_clients(value, params); |
---|
3768 | } else if (!strcmp(key, "disk")) { |
---|
3769 | err = og_json_parse_disk(value, params); |
---|
3770 | } else if (!strcmp(key, "partition")) { |
---|
3771 | err = og_json_parse_partition(value, params); |
---|
3772 | } |
---|
3773 | |
---|
3774 | if (err < 0) |
---|
3775 | return err; |
---|
3776 | } |
---|
3777 | |
---|
3778 | for (i = 0; i < params->ips_array_len; i++) { |
---|
3779 | snprintf(iph + strlen(iph), sizeof(iph), "%s;", |
---|
3780 | params->ips_array[i]); |
---|
3781 | } |
---|
3782 | len = snprintf(buf, sizeof(buf), |
---|
3783 | "nfn=IniciarSesion\riph=%s\rdsk=%s\rpar=%s\r", |
---|
3784 | iph, params->disk, params->partition); |
---|
3785 | |
---|
3786 | msg = og_msg_alloc(buf, len); |
---|
3787 | if (!msg) |
---|
3788 | return -1; |
---|
3789 | |
---|
3790 | if (!og_send_cmd((char **)params->ips_array, params->ips_array_len, |
---|
3791 | CLIENTE_APAGADO, msg)) |
---|
3792 | err = -1; |
---|
3793 | |
---|
3794 | og_msg_free(msg); |
---|
3795 | |
---|
3796 | return 0; |
---|
3797 | } |
---|
3798 | |
---|
3799 | static int og_cmd_poweroff(json_t *element, struct og_msg_params *params) |
---|
3800 | { |
---|
3801 | const char *key; |
---|
3802 | json_t *value; |
---|
3803 | int err = 0; |
---|
3804 | |
---|
3805 | if (json_typeof(element) != JSON_OBJECT) |
---|
3806 | return -1; |
---|
3807 | |
---|
3808 | json_object_foreach(element, key, value) { |
---|
3809 | if (!strcmp(key, "clients")) |
---|
3810 | err = og_json_parse_clients(value, params); |
---|
3811 | |
---|
3812 | if (err < 0) |
---|
3813 | break; |
---|
3814 | } |
---|
3815 | |
---|
3816 | return og_cmd_legacy_send(params, "Apagar", CLIENTE_OCUPADO); |
---|
3817 | } |
---|
3818 | |
---|
3819 | static int og_cmd_refresh(json_t *element, struct og_msg_params *params) |
---|
3820 | { |
---|
3821 | const char *key; |
---|
3822 | json_t *value; |
---|
3823 | int err = 0; |
---|
3824 | |
---|
3825 | if (json_typeof(element) != JSON_OBJECT) |
---|
3826 | return -1; |
---|
3827 | |
---|
3828 | json_object_foreach(element, key, value) { |
---|
3829 | if (!strcmp(key, "clients")) |
---|
3830 | err = og_json_parse_clients(value, params); |
---|
3831 | |
---|
3832 | if (err < 0) |
---|
3833 | break; |
---|
3834 | } |
---|
3835 | |
---|
3836 | return og_cmd_legacy_send(params, "Actualizar", CLIENTE_APAGADO); |
---|
3837 | } |
---|
3838 | |
---|
3839 | static int og_cmd_reboot(json_t *element, struct og_msg_params *params) |
---|
3840 | { |
---|
3841 | const char *key; |
---|
3842 | json_t *value; |
---|
3843 | int err = 0; |
---|
3844 | |
---|
3845 | if (json_typeof(element) != JSON_OBJECT) |
---|
3846 | return -1; |
---|
3847 | |
---|
3848 | json_object_foreach(element, key, value) { |
---|
3849 | if (!strcmp(key, "clients")) |
---|
3850 | err = og_json_parse_clients(value, params); |
---|
3851 | |
---|
3852 | if (err < 0) |
---|
3853 | break; |
---|
3854 | } |
---|
3855 | |
---|
3856 | return og_cmd_legacy_send(params, "Reiniciar", CLIENTE_OCUPADO); |
---|
3857 | } |
---|
3858 | |
---|
3859 | static int og_cmd_stop(json_t *element, struct og_msg_params *params) |
---|
3860 | { |
---|
3861 | const char *key; |
---|
3862 | json_t *value; |
---|
3863 | int err = 0; |
---|
3864 | |
---|
3865 | if (json_typeof(element) != JSON_OBJECT) |
---|
3866 | return -1; |
---|
3867 | |
---|
3868 | json_object_foreach(element, key, value) { |
---|
3869 | if (!strcmp(key, "clients")) |
---|
3870 | err = og_json_parse_clients(value, params); |
---|
3871 | |
---|
3872 | if (err < 0) |
---|
3873 | break; |
---|
3874 | } |
---|
3875 | |
---|
3876 | return og_cmd_legacy_send(params, "Purgar", CLIENTE_APAGADO); |
---|
3877 | } |
---|
3878 | |
---|
3879 | static int og_cmd_hardware(json_t *element, struct og_msg_params *params) |
---|
3880 | { |
---|
3881 | const char *key; |
---|
3882 | json_t *value; |
---|
3883 | int err = 0; |
---|
3884 | |
---|
3885 | if (json_typeof(element) != JSON_OBJECT) |
---|
3886 | return -1; |
---|
3887 | |
---|
3888 | json_object_foreach(element, key, value) { |
---|
3889 | if (!strcmp(key, "clients")) |
---|
3890 | err = og_json_parse_clients(value, params); |
---|
3891 | |
---|
3892 | if (err < 0) |
---|
3893 | break; |
---|
3894 | } |
---|
3895 | |
---|
3896 | return og_cmd_legacy_send(params, "InventarioHardware", |
---|
3897 | CLIENTE_OCUPADO); |
---|
3898 | } |
---|
3899 | |
---|
3900 | static int og_cmd_software(json_t *element, struct og_msg_params *params) |
---|
3901 | { |
---|
3902 | const char *key; |
---|
3903 | json_t *value; |
---|
3904 | int err = 0; |
---|
3905 | |
---|
3906 | if (json_typeof(element) != JSON_OBJECT) |
---|
3907 | return -1; |
---|
3908 | |
---|
3909 | json_object_foreach(element, key, value) { |
---|
3910 | if (!strcmp(key, "clients")) |
---|
3911 | err = og_json_parse_clients(value, params); |
---|
3912 | |
---|
3913 | if (err < 0) |
---|
3914 | break; |
---|
3915 | } |
---|
3916 | |
---|
3917 | return og_cmd_legacy_send(params, "InventarioSoftware", |
---|
3918 | CLIENTE_OCUPADO); |
---|
3919 | } |
---|
3920 | |
---|
3921 | static int og_client_not_found(struct og_client *cli) |
---|
3922 | { |
---|
3923 | char buf[] = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; |
---|
3924 | |
---|
3925 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
3926 | |
---|
3927 | return -1; |
---|
3928 | } |
---|
3929 | |
---|
3930 | static int og_client_not_authorized(struct og_client *cli) |
---|
3931 | { |
---|
3932 | char buf[] = "HTTP/1.1 401 Unauthorized\r\n" |
---|
3933 | "WWW-Authenticate: Basic\r\n" |
---|
3934 | "Content-Length: 0\r\n\r\n"; |
---|
3935 | |
---|
3936 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
3937 | |
---|
3938 | return -1; |
---|
3939 | } |
---|
3940 | |
---|
3941 | static int og_client_ok(struct og_client *cli, char *buf_reply) |
---|
3942 | { |
---|
3943 | char buf[4096] = {}; |
---|
3944 | |
---|
3945 | sprintf(buf, "HTTP/1.1 200 OK\r\nContent-Length: %ld\r\n\r\n%s", |
---|
3946 | strlen(buf_reply), buf_reply); |
---|
3947 | |
---|
3948 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
3949 | |
---|
3950 | return 0; |
---|
3951 | } |
---|
3952 | |
---|
3953 | enum og_rest_method { |
---|
3954 | OG_METHOD_GET = 0, |
---|
3955 | OG_METHOD_POST, |
---|
3956 | }; |
---|
3957 | |
---|
3958 | static int og_client_state_process_payload_rest(struct og_client *cli) |
---|
3959 | { |
---|
3960 | struct og_msg_params params = {}; |
---|
3961 | enum og_rest_method method; |
---|
3962 | char buf_reply[4096] = {}; |
---|
3963 | const char *cmd, *body; |
---|
3964 | json_error_t json_err; |
---|
3965 | json_t *root = NULL; |
---|
3966 | int err = 0; |
---|
3967 | |
---|
3968 | if (!strncmp(cli->buf, "GET", strlen("GET"))) { |
---|
3969 | method = OG_METHOD_GET; |
---|
3970 | cmd = cli->buf + strlen("GET") + 2; |
---|
3971 | } else if (!strncmp(cli->buf, "POST", strlen("POST"))) { |
---|
3972 | method = OG_METHOD_POST; |
---|
3973 | cmd = cli->buf + strlen("POST") + 2; |
---|
3974 | } else |
---|
3975 | return -1; |
---|
3976 | |
---|
3977 | body = strstr(cli->buf, "\r\n\r\n") + 4; |
---|
3978 | |
---|
3979 | if (strcmp(cli->auth_token, auth_token)) { |
---|
3980 | syslog(LOG_ERR, "wrong Authentication key\n"); |
---|
3981 | return og_client_not_authorized(cli); |
---|
3982 | } |
---|
3983 | |
---|
3984 | if (cli->content_length) { |
---|
3985 | root = json_loads(body, 0, &json_err); |
---|
3986 | if (!root) { |
---|
3987 | syslog(LOG_ERR, "malformed json line %d: %s\n", |
---|
3988 | json_err.line, json_err.text); |
---|
3989 | return og_client_not_found(cli); |
---|
3990 | } |
---|
3991 | } |
---|
3992 | |
---|
3993 | if (!strncmp(cmd, "clients", strlen("clients"))) { |
---|
3994 | if (method != OG_METHOD_POST && |
---|
3995 | method != OG_METHOD_GET) |
---|
3996 | return -1; |
---|
3997 | |
---|
3998 | if (method == OG_METHOD_POST && !root) { |
---|
3999 | syslog(LOG_ERR, "command clients with no payload\n"); |
---|
4000 | return og_client_not_found(cli); |
---|
4001 | } |
---|
4002 | switch (method) { |
---|
4003 | case OG_METHOD_POST: |
---|
4004 | err = og_cmd_post_clients(root, ¶ms); |
---|
4005 | break; |
---|
4006 | case OG_METHOD_GET: |
---|
4007 | err = og_cmd_get_clients(root, ¶ms, buf_reply); |
---|
4008 | break; |
---|
4009 | } |
---|
4010 | } else if (!strncmp(cmd, "wol", strlen("wol"))) { |
---|
4011 | if (method != OG_METHOD_POST) |
---|
4012 | return -1; |
---|
4013 | |
---|
4014 | if (!root) { |
---|
4015 | syslog(LOG_ERR, "command wol with no payload\n"); |
---|
4016 | return og_client_not_found(cli); |
---|
4017 | } |
---|
4018 | err = og_cmd_wol(root, ¶ms); |
---|
4019 | } else if (!strncmp(cmd, "shell/run", strlen("shell/run"))) { |
---|
4020 | if (method != OG_METHOD_POST) |
---|
4021 | return -1; |
---|
4022 | |
---|
4023 | if (!root) { |
---|
4024 | syslog(LOG_ERR, "command run with no payload\n"); |
---|
4025 | return og_client_not_found(cli); |
---|
4026 | } |
---|
4027 | err = og_cmd_run_post(root, ¶ms); |
---|
4028 | } else if (!strncmp(cmd, "shell/output", strlen("shell/output"))) { |
---|
4029 | if (method != OG_METHOD_POST) |
---|
4030 | return -1; |
---|
4031 | |
---|
4032 | if (!root) { |
---|
4033 | syslog(LOG_ERR, "command output with no payload\n"); |
---|
4034 | return og_client_not_found(cli); |
---|
4035 | } |
---|
4036 | |
---|
4037 | err = og_cmd_run_get(root, ¶ms, buf_reply); |
---|
4038 | } else if (!strncmp(cmd, "session", strlen("session"))) { |
---|
4039 | if (method != OG_METHOD_POST) |
---|
4040 | return -1; |
---|
4041 | |
---|
4042 | if (!root) { |
---|
4043 | syslog(LOG_ERR, "command session with no payload\n"); |
---|
4044 | return og_client_not_found(cli); |
---|
4045 | } |
---|
4046 | err = og_cmd_session(root, ¶ms); |
---|
4047 | } else if (!strncmp(cmd, "poweroff", strlen("poweroff"))) { |
---|
4048 | if (method != OG_METHOD_POST) |
---|
4049 | return -1; |
---|
4050 | |
---|
4051 | if (!root) { |
---|
4052 | syslog(LOG_ERR, "command poweroff with no payload\n"); |
---|
4053 | return og_client_not_found(cli); |
---|
4054 | } |
---|
4055 | err = og_cmd_poweroff(root, ¶ms); |
---|
4056 | } else if (!strncmp(cmd, "reboot", strlen("reboot"))) { |
---|
4057 | if (method != OG_METHOD_POST) |
---|
4058 | return -1; |
---|
4059 | |
---|
4060 | if (!root) { |
---|
4061 | syslog(LOG_ERR, "command reboot with no payload\n"); |
---|
4062 | return og_client_not_found(cli); |
---|
4063 | } |
---|
4064 | err = og_cmd_reboot(root, ¶ms); |
---|
4065 | } else if (!strncmp(cmd, "stop", strlen("stop"))) { |
---|
4066 | if (method != OG_METHOD_POST) |
---|
4067 | return -1; |
---|
4068 | |
---|
4069 | if (!root) { |
---|
4070 | syslog(LOG_ERR, "command stop with no payload\n"); |
---|
4071 | return og_client_not_found(cli); |
---|
4072 | } |
---|
4073 | err = og_cmd_stop(root, ¶ms); |
---|
4074 | } else if (!strncmp(cmd, "refresh", strlen("refresh"))) { |
---|
4075 | if (method != OG_METHOD_POST) |
---|
4076 | return -1; |
---|
4077 | |
---|
4078 | if (!root) { |
---|
4079 | syslog(LOG_ERR, "command refresh with no payload\n"); |
---|
4080 | return og_client_not_found(cli); |
---|
4081 | } |
---|
4082 | err = og_cmd_refresh(root, ¶ms); |
---|
4083 | } else if (!strncmp(cmd, "hardware", strlen("hardware"))) { |
---|
4084 | if (method != OG_METHOD_POST) |
---|
4085 | return -1; |
---|
4086 | |
---|
4087 | if (!root) { |
---|
4088 | syslog(LOG_ERR, "command hardware with no payload\n"); |
---|
4089 | return og_client_not_found(cli); |
---|
4090 | } |
---|
4091 | err = og_cmd_hardware(root, ¶ms); |
---|
4092 | } else if (!strncmp(cmd, "software", strlen("software"))) { |
---|
4093 | if (method != OG_METHOD_POST) |
---|
4094 | return -1; |
---|
4095 | |
---|
4096 | if (!root) { |
---|
4097 | syslog(LOG_ERR, "command software with no payload\n"); |
---|
4098 | return og_client_not_found(cli); |
---|
4099 | } |
---|
4100 | err = og_cmd_software(root, ¶ms); |
---|
4101 | } else { |
---|
4102 | syslog(LOG_ERR, "unknown command %s\n", cmd); |
---|
4103 | err = og_client_not_found(cli); |
---|
4104 | } |
---|
4105 | |
---|
4106 | if (root) |
---|
4107 | json_decref(root); |
---|
4108 | |
---|
4109 | if (!err) |
---|
4110 | og_client_ok(cli, buf_reply); |
---|
4111 | |
---|
4112 | return err; |
---|
4113 | } |
---|
4114 | |
---|
4115 | static int og_client_state_recv_hdr_rest(struct og_client *cli) |
---|
4116 | { |
---|
4117 | char *ptr; |
---|
4118 | |
---|
4119 | ptr = strstr(cli->buf, "\r\n\r\n"); |
---|
4120 | if (!ptr) |
---|
4121 | return 0; |
---|
4122 | |
---|
4123 | cli->msg_len = ptr - cli->buf + 4; |
---|
4124 | |
---|
4125 | ptr = strstr(cli->buf, "Content-Length: "); |
---|
4126 | if (ptr) { |
---|
4127 | sscanf(ptr, "Content-Length: %i[^\r\n]", &cli->content_length); |
---|
4128 | cli->msg_len += cli->content_length; |
---|
4129 | } |
---|
4130 | |
---|
4131 | ptr = strstr(cli->buf, "Authorization: "); |
---|
4132 | if (ptr) |
---|
4133 | sscanf(ptr, "Authorization: %64[^\r\n]", cli->auth_token); |
---|
4134 | |
---|
4135 | return 1; |
---|
4136 | } |
---|
4137 | |
---|
4138 | static void og_client_read_cb(struct ev_loop *loop, struct ev_io *io, int events) |
---|
4139 | { |
---|
4140 | struct og_client *cli; |
---|
4141 | int ret; |
---|
4142 | |
---|
4143 | cli = container_of(io, struct og_client, io); |
---|
4144 | |
---|
4145 | if (events & EV_ERROR) { |
---|
4146 | syslog(LOG_ERR, "unexpected error event from client %s:%hu\n", |
---|
4147 | inet_ntoa(cli->addr.sin_addr), |
---|
4148 | ntohs(cli->addr.sin_port)); |
---|
4149 | goto close; |
---|
4150 | } |
---|
4151 | |
---|
4152 | ret = recv(io->fd, cli->buf + cli->buf_len, |
---|
4153 | sizeof(cli->buf) - cli->buf_len, 0); |
---|
4154 | if (ret <= 0) { |
---|
4155 | if (ret < 0) { |
---|
4156 | syslog(LOG_ERR, "error reading from client %s:%hu (%s)\n", |
---|
4157 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port), |
---|
4158 | strerror(errno)); |
---|
4159 | } else { |
---|
4160 | syslog(LOG_DEBUG, "closed connection by %s:%hu\n", |
---|
4161 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
4162 | } |
---|
4163 | goto close; |
---|
4164 | } |
---|
4165 | |
---|
4166 | if (cli->keepalive_idx >= 0) |
---|
4167 | return; |
---|
4168 | |
---|
4169 | ev_timer_again(loop, &cli->timer); |
---|
4170 | |
---|
4171 | cli->buf_len += ret; |
---|
4172 | |
---|
4173 | switch (cli->state) { |
---|
4174 | case OG_CLIENT_RECEIVING_HEADER: |
---|
4175 | if (cli->rest) |
---|
4176 | ret = og_client_state_recv_hdr_rest(cli); |
---|
4177 | else |
---|
4178 | ret = og_client_state_recv_hdr(cli); |
---|
4179 | |
---|
4180 | if (ret < 0) |
---|
4181 | goto close; |
---|
4182 | if (!ret) |
---|
4183 | return; |
---|
4184 | |
---|
4185 | cli->state = OG_CLIENT_RECEIVING_PAYLOAD; |
---|
4186 | /* Fall through. */ |
---|
4187 | case OG_CLIENT_RECEIVING_PAYLOAD: |
---|
4188 | /* Still not enough data to process request. */ |
---|
4189 | if (cli->buf_len < cli->msg_len) |
---|
4190 | return; |
---|
4191 | |
---|
4192 | cli->state = OG_CLIENT_PROCESSING_REQUEST; |
---|
4193 | /* fall through. */ |
---|
4194 | case OG_CLIENT_PROCESSING_REQUEST: |
---|
4195 | syslog(LOG_DEBUG, "processing request from %s:%hu\n", |
---|
4196 | inet_ntoa(cli->addr.sin_addr), |
---|
4197 | ntohs(cli->addr.sin_port)); |
---|
4198 | |
---|
4199 | if (cli->rest) |
---|
4200 | ret = og_client_state_process_payload_rest(cli); |
---|
4201 | else |
---|
4202 | ret = og_client_state_process_payload(cli); |
---|
4203 | if (ret < 0) |
---|
4204 | goto close; |
---|
4205 | |
---|
4206 | if (cli->keepalive_idx < 0) { |
---|
4207 | syslog(LOG_DEBUG, "server closing connection to %s:%hu\n", |
---|
4208 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
4209 | goto close; |
---|
4210 | } else { |
---|
4211 | syslog(LOG_DEBUG, "leaving client %s:%hu in keepalive mode\n", |
---|
4212 | inet_ntoa(cli->addr.sin_addr), |
---|
4213 | ntohs(cli->addr.sin_port)); |
---|
4214 | og_client_keepalive(loop, cli); |
---|
4215 | og_client_reset_state(cli); |
---|
4216 | } |
---|
4217 | break; |
---|
4218 | default: |
---|
4219 | syslog(LOG_ERR, "unknown state, critical internal error\n"); |
---|
4220 | goto close; |
---|
4221 | } |
---|
4222 | return; |
---|
4223 | close: |
---|
4224 | ev_timer_stop(loop, &cli->timer); |
---|
4225 | og_client_release(loop, cli); |
---|
4226 | } |
---|
4227 | |
---|
4228 | static void og_client_timer_cb(struct ev_loop *loop, ev_timer *timer, int events) |
---|
4229 | { |
---|
4230 | struct og_client *cli; |
---|
4231 | |
---|
4232 | cli = container_of(timer, struct og_client, timer); |
---|
4233 | if (cli->keepalive_idx >= 0) { |
---|
4234 | ev_timer_again(loop, &cli->timer); |
---|
4235 | return; |
---|
4236 | } |
---|
4237 | syslog(LOG_ERR, "timeout request for client %s:%hu\n", |
---|
4238 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
4239 | |
---|
4240 | og_client_release(loop, cli); |
---|
4241 | } |
---|
4242 | |
---|
4243 | static int socket_s, socket_rest; |
---|
4244 | |
---|
4245 | static void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, |
---|
4246 | int events) |
---|
4247 | { |
---|
4248 | struct sockaddr_in client_addr; |
---|
4249 | socklen_t addrlen = sizeof(client_addr); |
---|
4250 | struct og_client *cli; |
---|
4251 | int client_sd; |
---|
4252 | |
---|
4253 | if (events & EV_ERROR) |
---|
4254 | return; |
---|
4255 | |
---|
4256 | client_sd = accept(io->fd, (struct sockaddr *)&client_addr, &addrlen); |
---|
4257 | if (client_sd < 0) { |
---|
4258 | syslog(LOG_ERR, "cannot accept client connection\n"); |
---|
4259 | return; |
---|
4260 | } |
---|
4261 | |
---|
4262 | cli = (struct og_client *)calloc(1, sizeof(struct og_client)); |
---|
4263 | if (!cli) { |
---|
4264 | close(client_sd); |
---|
4265 | return; |
---|
4266 | } |
---|
4267 | memcpy(&cli->addr, &client_addr, sizeof(client_addr)); |
---|
4268 | cli->keepalive_idx = -1; |
---|
4269 | |
---|
4270 | if (io->fd == socket_rest) |
---|
4271 | cli->rest = true; |
---|
4272 | |
---|
4273 | syslog(LOG_DEBUG, "connection from client %s:%hu\n", |
---|
4274 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
4275 | |
---|
4276 | ev_io_init(&cli->io, og_client_read_cb, client_sd, EV_READ); |
---|
4277 | ev_io_start(loop, &cli->io); |
---|
4278 | ev_timer_init(&cli->timer, og_client_timer_cb, OG_CLIENT_TIMEOUT, 0.); |
---|
4279 | ev_timer_start(loop, &cli->timer); |
---|
4280 | } |
---|
4281 | |
---|
4282 | static int og_socket_server_init(const char *port) |
---|
4283 | { |
---|
4284 | struct sockaddr_in local; |
---|
4285 | int sd, on = 1; |
---|
4286 | |
---|
4287 | sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
---|
4288 | if (sd < 0) { |
---|
4289 | syslog(LOG_ERR, "cannot create main socket\n"); |
---|
4290 | return -1; |
---|
4291 | } |
---|
4292 | setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(int)); |
---|
4293 | |
---|
4294 | local.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
4295 | local.sin_family = AF_INET; |
---|
4296 | local.sin_port = htons(atoi(port)); |
---|
4297 | |
---|
4298 | if (bind(sd, (struct sockaddr *) &local, sizeof(local)) < 0) { |
---|
4299 | syslog(LOG_ERR, "cannot bind socket\n"); |
---|
4300 | return -1; |
---|
4301 | } |
---|
4302 | |
---|
4303 | listen(sd, 250); |
---|
4304 | |
---|
4305 | return sd; |
---|
4306 | } |
---|
4307 | |
---|
4308 | int main(int argc, char *argv[]) |
---|
4309 | { |
---|
4310 | struct ev_io ev_io_server, ev_io_server_rest; |
---|
4311 | struct ev_loop *loop = ev_default_loop(0); |
---|
4312 | int i; |
---|
4313 | |
---|
4314 | if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) |
---|
4315 | exit(EXIT_FAILURE); |
---|
4316 | |
---|
4317 | openlog("ogAdmServer", LOG_PID, LOG_DAEMON); |
---|
4318 | |
---|
4319 | /*-------------------------------------------------------------------------------------------------------- |
---|
4320 | Validación de parámetros de ejecución y lectura del fichero de configuración del servicio |
---|
4321 | ---------------------------------------------------------------------------------------------------------*/ |
---|
4322 | if (!validacionParametros(argc, argv, 1)) // Valida parámetros de ejecución |
---|
4323 | exit(EXIT_FAILURE); |
---|
4324 | |
---|
4325 | if (!tomaConfiguracion(szPathFileCfg)) { // Toma parametros de configuracion |
---|
4326 | exit(EXIT_FAILURE); |
---|
4327 | } |
---|
4328 | |
---|
4329 | /*-------------------------------------------------------------------------------------------------------- |
---|
4330 | // Inicializa array de información de los clientes |
---|
4331 | ---------------------------------------------------------------------------------------------------------*/ |
---|
4332 | for (i = 0; i < MAXIMOS_CLIENTES; i++) { |
---|
4333 | tbsockets[i].ip[0] = '\0'; |
---|
4334 | tbsockets[i].cli = NULL; |
---|
4335 | } |
---|
4336 | /*-------------------------------------------------------------------------------------------------------- |
---|
4337 | Creación y configuración del socket del servicio |
---|
4338 | ---------------------------------------------------------------------------------------------------------*/ |
---|
4339 | socket_s = og_socket_server_init(puerto); |
---|
4340 | if (socket_s < 0) |
---|
4341 | exit(EXIT_FAILURE); |
---|
4342 | |
---|
4343 | ev_io_init(&ev_io_server, og_server_accept_cb, socket_s, EV_READ); |
---|
4344 | ev_io_start(loop, &ev_io_server); |
---|
4345 | |
---|
4346 | socket_rest = og_socket_server_init("8888"); |
---|
4347 | if (socket_rest < 0) |
---|
4348 | exit(EXIT_FAILURE); |
---|
4349 | |
---|
4350 | ev_io_init(&ev_io_server_rest, og_server_accept_cb, socket_rest, EV_READ); |
---|
4351 | ev_io_start(loop, &ev_io_server_rest); |
---|
4352 | |
---|
4353 | infoLog(1); // Inicio de sesión |
---|
4354 | |
---|
4355 | /* old log file has been deprecated. */ |
---|
4356 | og_log(97, false); |
---|
4357 | |
---|
4358 | syslog(LOG_INFO, "Waiting for connections\n"); |
---|
4359 | |
---|
4360 | while (1) |
---|
4361 | ev_loop(loop, 0); |
---|
4362 | |
---|
4363 | exit(EXIT_SUCCESS); |
---|
4364 | } |
---|