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 "dbi.h" |
---|
12 | #include "list.h" |
---|
13 | #include "schedule.h" |
---|
14 | #include <ev.h> |
---|
15 | #include <syslog.h> |
---|
16 | #include <sys/ioctl.h> |
---|
17 | #include <ifaddrs.h> |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <fcntl.h> |
---|
21 | #include <jansson.h> |
---|
22 | #include <time.h> |
---|
23 | |
---|
24 | static char usuario[LONPRM]; // Usuario de acceso a la base de datos |
---|
25 | static char pasguor[LONPRM]; // Password del usuario |
---|
26 | static char datasource[LONPRM]; // Dirección IP del gestor de base de datos |
---|
27 | static char catalog[LONPRM]; // Nombre de la base de datos |
---|
28 | static char interface[LONPRM]; // Interface name |
---|
29 | static char auth_token[LONPRM]; // API token |
---|
30 | |
---|
31 | static struct og_dbi_config dbi_config = { |
---|
32 | .user = usuario, |
---|
33 | .passwd = pasguor, |
---|
34 | .host = datasource, |
---|
35 | .database = catalog, |
---|
36 | }; |
---|
37 | |
---|
38 | //________________________________________________________________________________________________________ |
---|
39 | // Función: tomaConfiguracion |
---|
40 | // |
---|
41 | // Descripción: |
---|
42 | // Lee el fichero de configuración del servicio |
---|
43 | // Parámetros: |
---|
44 | // filecfg : Ruta completa al fichero de configuración |
---|
45 | // Devuelve: |
---|
46 | // true: Si el proceso es correcto |
---|
47 | // false: En caso de ocurrir algún error |
---|
48 | //________________________________________________________________________________________________________ |
---|
49 | static bool tomaConfiguracion(const char *filecfg) |
---|
50 | { |
---|
51 | char buf[1024], *line; |
---|
52 | char *key, *value; |
---|
53 | FILE *fcfg; |
---|
54 | |
---|
55 | if (filecfg == NULL || strlen(filecfg) == 0) { |
---|
56 | syslog(LOG_ERR, "No configuration file has been specified\n"); |
---|
57 | return false; |
---|
58 | } |
---|
59 | |
---|
60 | fcfg = fopen(filecfg, "rt"); |
---|
61 | if (fcfg == NULL) { |
---|
62 | syslog(LOG_ERR, "Cannot open configuration file `%s'\n", |
---|
63 | filecfg); |
---|
64 | return false; |
---|
65 | } |
---|
66 | |
---|
67 | servidoradm[0] = '\0'; //inicializar variables globales |
---|
68 | |
---|
69 | line = fgets(buf, sizeof(buf), fcfg); |
---|
70 | while (line != NULL) { |
---|
71 | const char *delim = "="; |
---|
72 | |
---|
73 | line[strlen(line) - 1] = '\0'; |
---|
74 | |
---|
75 | key = strtok(line, delim); |
---|
76 | value = strtok(NULL, delim); |
---|
77 | |
---|
78 | if (!strcmp(StrToUpper(key), "SERVIDORADM")) |
---|
79 | snprintf(servidoradm, sizeof(servidoradm), "%s", value); |
---|
80 | else if (!strcmp(StrToUpper(key), "PUERTO")) |
---|
81 | snprintf(puerto, sizeof(puerto), "%s", value); |
---|
82 | else if (!strcmp(StrToUpper(key), "USUARIO")) |
---|
83 | snprintf(usuario, sizeof(usuario), "%s", value); |
---|
84 | else if (!strcmp(StrToUpper(key), "PASSWORD")) |
---|
85 | snprintf(pasguor, sizeof(pasguor), "%s", value); |
---|
86 | else if (!strcmp(StrToUpper(key), "DATASOURCE")) |
---|
87 | snprintf(datasource, sizeof(datasource), "%s", value); |
---|
88 | else if (!strcmp(StrToUpper(key), "CATALOG")) |
---|
89 | snprintf(catalog, sizeof(catalog), "%s", value); |
---|
90 | else if (!strcmp(StrToUpper(key), "INTERFACE")) |
---|
91 | snprintf(interface, sizeof(interface), "%s", value); |
---|
92 | else if (!strcmp(StrToUpper(key), "APITOKEN")) |
---|
93 | snprintf(auth_token, sizeof(auth_token), "%s", value); |
---|
94 | |
---|
95 | line = fgets(buf, sizeof(buf), fcfg); |
---|
96 | } |
---|
97 | |
---|
98 | fclose(fcfg); |
---|
99 | |
---|
100 | if (!servidoradm[0]) { |
---|
101 | syslog(LOG_ERR, "Missing SERVIDORADM in configuration file\n"); |
---|
102 | return false; |
---|
103 | } |
---|
104 | if (!puerto[0]) { |
---|
105 | syslog(LOG_ERR, "Missing PUERTO in configuration file\n"); |
---|
106 | return false; |
---|
107 | } |
---|
108 | if (!usuario[0]) { |
---|
109 | syslog(LOG_ERR, "Missing USUARIO in configuration file\n"); |
---|
110 | return false; |
---|
111 | } |
---|
112 | if (!pasguor[0]) { |
---|
113 | syslog(LOG_ERR, "Missing PASSWORD in configuration file\n"); |
---|
114 | return false; |
---|
115 | } |
---|
116 | if (!datasource[0]) { |
---|
117 | syslog(LOG_ERR, "Missing DATASOURCE in configuration file\n"); |
---|
118 | return false; |
---|
119 | } |
---|
120 | if (!catalog[0]) { |
---|
121 | syslog(LOG_ERR, "Missing CATALOG in configuration file\n"); |
---|
122 | return false; |
---|
123 | } |
---|
124 | if (!interface[0]) |
---|
125 | syslog(LOG_ERR, "Missing INTERFACE in configuration file\n"); |
---|
126 | |
---|
127 | return true; |
---|
128 | } |
---|
129 | |
---|
130 | enum og_client_state { |
---|
131 | OG_CLIENT_RECEIVING_HEADER = 0, |
---|
132 | OG_CLIENT_RECEIVING_PAYLOAD, |
---|
133 | OG_CLIENT_PROCESSING_REQUEST, |
---|
134 | }; |
---|
135 | |
---|
136 | #define OG_MSG_REQUEST_MAXLEN 65536 |
---|
137 | #define OG_CMD_MAXLEN 64 |
---|
138 | |
---|
139 | /* Shut down connection if there is no complete message after 10 seconds. */ |
---|
140 | #define OG_CLIENT_TIMEOUT 10 |
---|
141 | |
---|
142 | /* Agent client operation might take longer, shut down after 30 seconds. */ |
---|
143 | #define OG_AGENT_CLIENT_TIMEOUT 30 |
---|
144 | |
---|
145 | enum og_cmd_type { |
---|
146 | OG_CMD_UNSPEC, |
---|
147 | OG_CMD_WOL, |
---|
148 | OG_CMD_PROBE, |
---|
149 | OG_CMD_SHELL_RUN, |
---|
150 | OG_CMD_SESSION, |
---|
151 | OG_CMD_POWEROFF, |
---|
152 | OG_CMD_REFRESH, |
---|
153 | OG_CMD_REBOOT, |
---|
154 | OG_CMD_STOP, |
---|
155 | OG_CMD_HARDWARE, |
---|
156 | OG_CMD_SOFTWARE, |
---|
157 | OG_CMD_IMAGE_CREATE, |
---|
158 | OG_CMD_IMAGE_RESTORE, |
---|
159 | OG_CMD_SETUP, |
---|
160 | OG_CMD_RUN_SCHEDULE, |
---|
161 | OG_CMD_MAX |
---|
162 | }; |
---|
163 | |
---|
164 | static LIST_HEAD(client_list); |
---|
165 | |
---|
166 | enum og_client_status { |
---|
167 | OG_CLIENT_STATUS_OGLIVE, |
---|
168 | OG_CLIENT_STATUS_BUSY, |
---|
169 | }; |
---|
170 | |
---|
171 | struct og_client { |
---|
172 | struct list_head list; |
---|
173 | struct ev_io io; |
---|
174 | struct ev_timer timer; |
---|
175 | struct sockaddr_in addr; |
---|
176 | enum og_client_state state; |
---|
177 | char buf[OG_MSG_REQUEST_MAXLEN]; |
---|
178 | unsigned int buf_len; |
---|
179 | unsigned int msg_len; |
---|
180 | int keepalive_idx; |
---|
181 | bool rest; |
---|
182 | bool agent; |
---|
183 | int content_length; |
---|
184 | char auth_token[64]; |
---|
185 | enum og_client_status status; |
---|
186 | enum og_cmd_type last_cmd; |
---|
187 | unsigned int last_cmd_id; |
---|
188 | }; |
---|
189 | |
---|
190 | static inline int og_client_socket(const struct og_client *cli) |
---|
191 | { |
---|
192 | return cli->io.fd; |
---|
193 | } |
---|
194 | |
---|
195 | static inline const char *og_client_status(const struct og_client *cli) |
---|
196 | { |
---|
197 | if (cli->last_cmd != OG_CMD_UNSPEC) |
---|
198 | return "BSY"; |
---|
199 | |
---|
200 | switch (cli->status) { |
---|
201 | case OG_CLIENT_STATUS_BUSY: |
---|
202 | return "BSY"; |
---|
203 | case OG_CLIENT_STATUS_OGLIVE: |
---|
204 | return "OPG"; |
---|
205 | default: |
---|
206 | return "OFF"; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | // ________________________________________________________________________________________________________ |
---|
211 | // Función: clienteDisponible |
---|
212 | // |
---|
213 | // Descripción: |
---|
214 | // Comprueba la disponibilidad del cliente para recibir comandos interactivos |
---|
215 | // Parametros: |
---|
216 | // - ip : La ip del cliente a buscar |
---|
217 | // - idx: (Salida) Indice que ocupa el cliente, de estar ya registrado |
---|
218 | // Devuelve: |
---|
219 | // true: Si el cliente está disponible |
---|
220 | // false: En caso contrario |
---|
221 | // ________________________________________________________________________________________________________ |
---|
222 | bool clienteDisponible(char *ip, int* idx) |
---|
223 | { |
---|
224 | int estado; |
---|
225 | |
---|
226 | if (clienteExistente(ip, idx)) { |
---|
227 | estado = strcmp(tbsockets[*idx].estado, CLIENTE_OCUPADO); // Cliente ocupado |
---|
228 | if (estado == 0) |
---|
229 | return false; |
---|
230 | |
---|
231 | estado = strcmp(tbsockets[*idx].estado, CLIENTE_APAGADO); // Cliente apagado |
---|
232 | if (estado == 0) |
---|
233 | return false; |
---|
234 | |
---|
235 | estado = strcmp(tbsockets[*idx].estado, CLIENTE_INICIANDO); // Cliente en proceso de inclusión |
---|
236 | if (estado == 0) |
---|
237 | return false; |
---|
238 | |
---|
239 | return true; // En caso contrario el cliente está disponible |
---|
240 | } |
---|
241 | return false; // Cliente no está registrado en el sistema |
---|
242 | } |
---|
243 | // ________________________________________________________________________________________________________ |
---|
244 | // Función: clienteExistente |
---|
245 | // |
---|
246 | // Descripción: |
---|
247 | // Comprueba si el cliente está registrado en la tabla de socket del sistema |
---|
248 | // Parametros: |
---|
249 | // - ip : La ip del cliente a buscar |
---|
250 | // - idx:(Salida) Indice que ocupa el cliente, de estar ya registrado |
---|
251 | // Devuelve: |
---|
252 | // true: Si el cliente está registrado |
---|
253 | // false: En caso contrario |
---|
254 | // ________________________________________________________________________________________________________ |
---|
255 | bool clienteExistente(char *ip, int* idx) |
---|
256 | { |
---|
257 | int i; |
---|
258 | for (i = 0; i < MAXIMOS_CLIENTES; i++) { |
---|
259 | if (contieneIP(ip, tbsockets[i].ip)) { // Si existe la IP en la cadena |
---|
260 | *idx = i; |
---|
261 | return true; |
---|
262 | } |
---|
263 | } |
---|
264 | return false; |
---|
265 | } |
---|
266 | // ________________________________________________________________________________________________________ |
---|
267 | // Función: actualizaConfiguracion |
---|
268 | // |
---|
269 | // Descripción: |
---|
270 | // Esta función actualiza la base de datos con la configuracion de particiones de un cliente |
---|
271 | // Parámetros: |
---|
272 | // - db: Objeto base de datos (ya operativo) |
---|
273 | // - tbl: Objeto tabla |
---|
274 | // - cfg: cadena con una Configuración |
---|
275 | // - ido: Identificador del ordenador cliente |
---|
276 | // Devuelve: |
---|
277 | // true: Si el proceso es correcto |
---|
278 | // false: En caso de ocurrir algún error |
---|
279 | // Especificaciones: |
---|
280 | // Los parametros de la configuración son: |
---|
281 | // par= Número de partición |
---|
282 | // cpt= Codigo o tipo de partición |
---|
283 | // sfi= Sistema de ficheros que está implementado en la partición |
---|
284 | // soi= Nombre del sistema de ficheros instalado en la partición |
---|
285 | // tam= Tamaño de la partición |
---|
286 | // ________________________________________________________________________________________________________ |
---|
287 | bool actualizaConfiguracion(struct og_dbi *dbi, char *cfg, int ido) |
---|
288 | { |
---|
289 | int lon, p, c,i, dato, swu, idsoi, idsfi,k; |
---|
290 | char *ptrPar[MAXPAR], *ptrCfg[7], *ptrDual[2], tbPar[LONSTD]; |
---|
291 | char *ser, *disk, *par, *cpt, *sfi, *soi, *tam, *uso; // Parametros de configuración. |
---|
292 | dbi_result result, result_update; |
---|
293 | const char *msglog; |
---|
294 | |
---|
295 | lon = 0; |
---|
296 | p = splitCadena(ptrPar, cfg, '\n'); |
---|
297 | for (i = 0; i < p; i++) { |
---|
298 | c = splitCadena(ptrCfg, ptrPar[i], '\t'); |
---|
299 | |
---|
300 | // Si la 1ª línea solo incluye el número de serie del equipo; actualizar BD. |
---|
301 | if (i == 0 && c == 1) { |
---|
302 | splitCadena(ptrDual, ptrCfg[0], '='); |
---|
303 | ser = ptrDual[1]; |
---|
304 | if (strlen(ser) > 0) { |
---|
305 | // Solo actualizar si número de serie no existía. |
---|
306 | result = dbi_conn_queryf(dbi->conn, |
---|
307 | "UPDATE ordenadores SET numserie='%s'" |
---|
308 | " WHERE idordenador=%d AND numserie IS NULL", |
---|
309 | ser, ido); |
---|
310 | if (!result) { |
---|
311 | dbi_conn_error(dbi->conn, &msglog); |
---|
312 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
313 | __func__, __LINE__, msglog); |
---|
314 | return false; |
---|
315 | } |
---|
316 | dbi_result_free(result); |
---|
317 | } |
---|
318 | continue; |
---|
319 | } |
---|
320 | |
---|
321 | // Distribución de particionado. |
---|
322 | disk = par = cpt = sfi = soi = tam = uso = NULL; |
---|
323 | |
---|
324 | splitCadena(ptrDual, ptrCfg[0], '='); |
---|
325 | disk = ptrDual[1]; // Número de disco |
---|
326 | |
---|
327 | splitCadena(ptrDual, ptrCfg[1], '='); |
---|
328 | par = ptrDual[1]; // Número de partición |
---|
329 | |
---|
330 | k=splitCadena(ptrDual, ptrCfg[2], '='); |
---|
331 | if(k==2){ |
---|
332 | cpt = ptrDual[1]; // Código de partición |
---|
333 | }else{ |
---|
334 | cpt = (char*)"0"; |
---|
335 | } |
---|
336 | |
---|
337 | k=splitCadena(ptrDual, ptrCfg[3], '='); |
---|
338 | if(k==2){ |
---|
339 | sfi = ptrDual[1]; // Sistema de ficheros |
---|
340 | /* Comprueba existencia del s0xistema de ficheros instalado */ |
---|
341 | idsfi = checkDato(dbi, sfi, "sistemasficheros", "descripcion","idsistemafichero"); |
---|
342 | } |
---|
343 | else |
---|
344 | idsfi=0; |
---|
345 | |
---|
346 | k=splitCadena(ptrDual, ptrCfg[4], '='); |
---|
347 | if(k==2){ // Sistema operativo detecdtado |
---|
348 | soi = ptrDual[1]; // Nombre del S.O. instalado |
---|
349 | /* Comprueba existencia del sistema operativo instalado */ |
---|
350 | idsoi = checkDato(dbi, soi, "nombresos", "nombreso", "idnombreso"); |
---|
351 | } |
---|
352 | else |
---|
353 | idsoi=0; |
---|
354 | |
---|
355 | splitCadena(ptrDual, ptrCfg[5], '='); |
---|
356 | tam = ptrDual[1]; // Tamaño de la partición |
---|
357 | |
---|
358 | splitCadena(ptrDual, ptrCfg[6], '='); |
---|
359 | uso = ptrDual[1]; // Porcentaje de uso del S.F. |
---|
360 | |
---|
361 | lon += sprintf(tbPar + lon, "(%s, %s),", disk, par); |
---|
362 | |
---|
363 | result = dbi_conn_queryf(dbi->conn, |
---|
364 | "SELECT numdisk, numpar, tamano, uso, idsistemafichero, idnombreso" |
---|
365 | " FROM ordenadores_particiones" |
---|
366 | " WHERE idordenador=%d AND numdisk=%s AND numpar=%s", |
---|
367 | ido, disk, par); |
---|
368 | if (!result) { |
---|
369 | dbi_conn_error(dbi->conn, &msglog); |
---|
370 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
371 | __func__, __LINE__, msglog); |
---|
372 | return false; |
---|
373 | } |
---|
374 | if (!dbi_result_next_row(result)) { |
---|
375 | result_update = dbi_conn_queryf(dbi->conn, |
---|
376 | "INSERT INTO ordenadores_particiones(idordenador,numdisk,numpar,codpar,tamano,uso,idsistemafichero,idnombreso,idimagen)" |
---|
377 | " VALUES(%d,%s,%s,0x%s,%s,%s,%d,%d,0)", |
---|
378 | ido, disk, par, cpt, tam, uso, idsfi, idsoi); |
---|
379 | if (!result_update) { |
---|
380 | dbi_conn_error(dbi->conn, &msglog); |
---|
381 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
382 | __func__, __LINE__, msglog); |
---|
383 | return false; |
---|
384 | } |
---|
385 | dbi_result_free(result_update); |
---|
386 | |
---|
387 | } else { // Existe el registro |
---|
388 | swu = true; // Se supone que algún dato ha cambiado |
---|
389 | |
---|
390 | dato = dbi_result_get_uint(result, "tamano"); |
---|
391 | if (atoi(tam) == dato) {// Parámetro tamaño igual al almacenado |
---|
392 | dato = dbi_result_get_uint(result, "idsistemafichero"); |
---|
393 | if (idsfi == dato) {// Parámetro sistema de fichero igual al almacenado |
---|
394 | dato = dbi_result_get_uint(result, "idnombreso"); |
---|
395 | if (idsoi == dato) {// Parámetro sistema de fichero distinto al almacenado |
---|
396 | swu = false; // Todos los parámetros de la partición son iguales, no se actualiza |
---|
397 | } |
---|
398 | } |
---|
399 | } |
---|
400 | if (swu) { // Hay que actualizar los parámetros de la partición |
---|
401 | result_update = dbi_conn_queryf(dbi->conn, |
---|
402 | "UPDATE ordenadores_particiones SET " |
---|
403 | " codpar=0x%s," |
---|
404 | " tamano=%s," |
---|
405 | " uso=%s," |
---|
406 | " idsistemafichero=%d," |
---|
407 | " idnombreso=%d," |
---|
408 | " idimagen=0," |
---|
409 | " idperfilsoft=0," |
---|
410 | " fechadespliegue=NULL" |
---|
411 | " WHERE idordenador=%d AND numdisk=%s AND numpar=%s", |
---|
412 | cpt, tam, uso, idsfi, idsoi, ido, disk, par); |
---|
413 | } else { // Actualizar porcentaje de uso. |
---|
414 | result_update = dbi_conn_queryf(dbi->conn, |
---|
415 | "UPDATE ordenadores_particiones SET " |
---|
416 | " codpar=0x%s," |
---|
417 | " uso=%s" |
---|
418 | " WHERE idordenador=%d AND numdisk=%s AND numpar=%s", |
---|
419 | cpt, uso, ido, disk, par); |
---|
420 | } |
---|
421 | if (!result_update) { |
---|
422 | dbi_conn_error(dbi->conn, &msglog); |
---|
423 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
424 | __func__, __LINE__, msglog); |
---|
425 | return false; |
---|
426 | } |
---|
427 | |
---|
428 | dbi_result_free(result_update); |
---|
429 | } |
---|
430 | dbi_result_free(result); |
---|
431 | } |
---|
432 | lon += sprintf(tbPar + lon, "(0,0)"); |
---|
433 | // Eliminar particiones almacenadas que ya no existen |
---|
434 | result_update = dbi_conn_queryf(dbi->conn, |
---|
435 | "DELETE FROM ordenadores_particiones WHERE idordenador=%d AND (numdisk, numpar) NOT IN (%s)", |
---|
436 | ido, tbPar); |
---|
437 | if (!result_update) { |
---|
438 | dbi_conn_error(dbi->conn, &msglog); |
---|
439 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
440 | __func__, __LINE__, msglog); |
---|
441 | return false; |
---|
442 | } |
---|
443 | dbi_result_free(result_update); |
---|
444 | |
---|
445 | return true; |
---|
446 | } |
---|
447 | // ________________________________________________________________________________________________________ |
---|
448 | // Función: checkDato |
---|
449 | // |
---|
450 | // Descripción: |
---|
451 | // Esta función comprueba si existe un dato en una tabla y si no es así lo incluye. devuelve en |
---|
452 | // cualquier caso el identificador del registro existenet o del insertado |
---|
453 | // Parámetros: |
---|
454 | // - db: Objeto base de datos (ya operativo) |
---|
455 | // - tbl: Objeto tabla |
---|
456 | // - dato: Dato |
---|
457 | // - tabla: Nombre de la tabla |
---|
458 | // - nomdato: Nombre del dato en la tabla |
---|
459 | // - nomidentificador: Nombre del identificador en la tabla |
---|
460 | // Devuelve: |
---|
461 | // El identificador del registro existente o el del insertado |
---|
462 | // |
---|
463 | // Especificaciones: |
---|
464 | // En caso de producirse algún error se devuelve el valor 0 |
---|
465 | // ________________________________________________________________________________________________________ |
---|
466 | |
---|
467 | int checkDato(struct og_dbi *dbi, char *dato, const char *tabla, |
---|
468 | const char *nomdato, const char *nomidentificador) |
---|
469 | { |
---|
470 | const char *msglog; |
---|
471 | int identificador; |
---|
472 | dbi_result result; |
---|
473 | |
---|
474 | if (strlen(dato) == 0) |
---|
475 | return (0); // EL dato no tiene valor |
---|
476 | result = dbi_conn_queryf(dbi->conn, |
---|
477 | "SELECT %s FROM %s WHERE %s ='%s'", nomidentificador, |
---|
478 | tabla, nomdato, dato); |
---|
479 | |
---|
480 | // Ejecuta consulta |
---|
481 | if (!result) { |
---|
482 | dbi_conn_error(dbi->conn, &msglog); |
---|
483 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
484 | __func__, __LINE__, msglog); |
---|
485 | return (0); |
---|
486 | } |
---|
487 | if (!dbi_result_next_row(result)) { // Software NO existente |
---|
488 | dbi_result_free(result); |
---|
489 | |
---|
490 | result = dbi_conn_queryf(dbi->conn, |
---|
491 | "INSERT INTO %s (%s) VALUES('%s')", tabla, nomdato, dato); |
---|
492 | if (!result) { |
---|
493 | dbi_conn_error(dbi->conn, &msglog); |
---|
494 | og_info((char *)msglog); |
---|
495 | return (0); |
---|
496 | } |
---|
497 | // Recupera el identificador del software |
---|
498 | identificador = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
499 | } else { |
---|
500 | identificador = dbi_result_get_uint(result, nomidentificador); |
---|
501 | } |
---|
502 | dbi_result_free(result); |
---|
503 | |
---|
504 | return (identificador); |
---|
505 | } |
---|
506 | |
---|
507 | struct og_task { |
---|
508 | uint32_t task_id; |
---|
509 | uint32_t procedure_id; |
---|
510 | uint32_t command_id; |
---|
511 | uint32_t center_id; |
---|
512 | uint32_t schedule_id; |
---|
513 | uint32_t type_scope; |
---|
514 | uint32_t scope; |
---|
515 | const char *filtered_scope; |
---|
516 | const char *params; |
---|
517 | }; |
---|
518 | |
---|
519 | static TRAMA *og_msg_alloc(char *data, unsigned int len); |
---|
520 | static void og_msg_free(TRAMA *ptrTrama); |
---|
521 | |
---|
522 | static bool og_send_cmd(char *ips_array[], int ips_array_len, |
---|
523 | const char *state, TRAMA *ptrTrama) |
---|
524 | { |
---|
525 | int i, idx; |
---|
526 | |
---|
527 | for (i = 0; i < ips_array_len; i++) { |
---|
528 | if (clienteDisponible(ips_array[i], &idx)) { // Si el cliente puede recibir comandos |
---|
529 | int sock = tbsockets[idx].cli ? tbsockets[idx].cli->io.fd : -1; |
---|
530 | |
---|
531 | strcpy(tbsockets[idx].estado, state); // Actualiza el estado del cliente |
---|
532 | if (sock >= 0 && !mandaTrama(&sock, ptrTrama)) { |
---|
533 | syslog(LOG_ERR, "failed to send response to %s:%s\n", |
---|
534 | ips_array[i], strerror(errno)); |
---|
535 | } |
---|
536 | } |
---|
537 | } |
---|
538 | return true; |
---|
539 | } |
---|
540 | |
---|
541 | // ________________________________________________________________________________________________________ |
---|
542 | // Función: Levanta |
---|
543 | // |
---|
544 | // Descripción: |
---|
545 | // Enciende ordenadores a través de la red cuyas macs se pasan como parámetro |
---|
546 | // Parámetros: |
---|
547 | // - iph: Cadena de direcciones ip separadas por ";" |
---|
548 | // - mac: Cadena de direcciones mac separadas por ";" |
---|
549 | // - mar: Método de arranque (1=Broadcast, 2=Unicast) |
---|
550 | // Devuelve: |
---|
551 | // true: Si el proceso es correcto |
---|
552 | // false: En caso de ocurrir algún error |
---|
553 | // ________________________________________________________________________________________________________ |
---|
554 | |
---|
555 | bool Levanta(char *ptrIP[], char *ptrMacs[], int lon, char *mar) |
---|
556 | { |
---|
557 | unsigned int on = 1; |
---|
558 | struct sockaddr_in local; |
---|
559 | int i, res; |
---|
560 | int s; |
---|
561 | |
---|
562 | /* Creación de socket para envío de magig packet */ |
---|
563 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
---|
564 | if (s < 0) { |
---|
565 | syslog(LOG_ERR, "cannot create socket for magic packet\n"); |
---|
566 | return false; |
---|
567 | } |
---|
568 | res = setsockopt(s, SOL_SOCKET, SO_BROADCAST, (unsigned int *) &on, |
---|
569 | sizeof(on)); |
---|
570 | if (res < 0) { |
---|
571 | syslog(LOG_ERR, "cannot set broadcast socket\n"); |
---|
572 | return false; |
---|
573 | } |
---|
574 | memset(&local, 0, sizeof(local)); |
---|
575 | local.sin_family = AF_INET; |
---|
576 | local.sin_port = htons(PUERTO_WAKEUP); |
---|
577 | local.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
578 | |
---|
579 | for (i = 0; i < lon; i++) { |
---|
580 | if (!WakeUp(s, ptrIP[i], ptrMacs[i], mar)) { |
---|
581 | syslog(LOG_ERR, "problem sending magic packet\n"); |
---|
582 | close(s); |
---|
583 | return false; |
---|
584 | } |
---|
585 | } |
---|
586 | close(s); |
---|
587 | return true; |
---|
588 | } |
---|
589 | |
---|
590 | #define OG_WOL_SEQUENCE 6 |
---|
591 | #define OG_WOL_MACADDR_LEN 6 |
---|
592 | #define OG_WOL_REPEAT 16 |
---|
593 | |
---|
594 | struct wol_msg { |
---|
595 | char secuencia_FF[OG_WOL_SEQUENCE]; |
---|
596 | char macbin[OG_WOL_REPEAT][OG_WOL_MACADDR_LEN]; |
---|
597 | }; |
---|
598 | |
---|
599 | static bool wake_up_broadcast(int sd, struct sockaddr_in *client, |
---|
600 | const struct wol_msg *msg) |
---|
601 | { |
---|
602 | struct sockaddr_in *broadcast_addr; |
---|
603 | struct ifaddrs *ifaddr, *ifa; |
---|
604 | int ret; |
---|
605 | |
---|
606 | if (getifaddrs(&ifaddr) < 0) { |
---|
607 | syslog(LOG_ERR, "cannot get list of addresses\n"); |
---|
608 | return false; |
---|
609 | } |
---|
610 | |
---|
611 | client->sin_addr.s_addr = htonl(INADDR_BROADCAST); |
---|
612 | |
---|
613 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
---|
614 | if (ifa->ifa_addr == NULL || |
---|
615 | ifa->ifa_addr->sa_family != AF_INET || |
---|
616 | strcmp(ifa->ifa_name, interface) != 0) |
---|
617 | continue; |
---|
618 | |
---|
619 | broadcast_addr = |
---|
620 | (struct sockaddr_in *)ifa->ifa_ifu.ifu_broadaddr; |
---|
621 | client->sin_addr.s_addr = broadcast_addr->sin_addr.s_addr; |
---|
622 | break; |
---|
623 | } |
---|
624 | freeifaddrs(ifaddr); |
---|
625 | |
---|
626 | ret = sendto(sd, msg, sizeof(*msg), 0, |
---|
627 | (struct sockaddr *)client, sizeof(*client)); |
---|
628 | if (ret < 0) { |
---|
629 | syslog(LOG_ERR, "failed to send broadcast wol\n"); |
---|
630 | return false; |
---|
631 | } |
---|
632 | |
---|
633 | return true; |
---|
634 | } |
---|
635 | |
---|
636 | static bool wake_up_unicast(int sd, struct sockaddr_in *client, |
---|
637 | const struct wol_msg *msg, |
---|
638 | const struct in_addr *addr) |
---|
639 | { |
---|
640 | int ret; |
---|
641 | |
---|
642 | client->sin_addr.s_addr = addr->s_addr; |
---|
643 | |
---|
644 | ret = sendto(sd, msg, sizeof(*msg), 0, |
---|
645 | (struct sockaddr *)client, sizeof(*client)); |
---|
646 | if (ret < 0) { |
---|
647 | syslog(LOG_ERR, "failed to send unicast wol\n"); |
---|
648 | return false; |
---|
649 | } |
---|
650 | |
---|
651 | return true; |
---|
652 | } |
---|
653 | |
---|
654 | enum wol_delivery_type { |
---|
655 | OG_WOL_BROADCAST = 1, |
---|
656 | OG_WOL_UNICAST = 2 |
---|
657 | }; |
---|
658 | |
---|
659 | //_____________________________________________________________________________________________________________ |
---|
660 | // Función: WakeUp |
---|
661 | // |
---|
662 | // Descripción: |
---|
663 | // Enciende el ordenador cuya MAC se pasa como parámetro |
---|
664 | // Parámetros: |
---|
665 | // - s : Socket para enviar trama magic packet |
---|
666 | // - iph : Cadena con la dirección ip |
---|
667 | // - mac : Cadena con la dirección mac en formato XXXXXXXXXXXX |
---|
668 | // - mar: Método de arranque (1=Broadcast, 2=Unicast) |
---|
669 | // Devuelve: |
---|
670 | // true: Si el proceso es correcto |
---|
671 | // false: En caso de ocurrir algún error |
---|
672 | //_____________________________________________________________________________________________________________ |
---|
673 | // |
---|
674 | bool WakeUp(int s, char* iph, char *mac, char *mar) |
---|
675 | { |
---|
676 | unsigned int macaddr[OG_WOL_MACADDR_LEN]; |
---|
677 | char HDaddress_bin[OG_WOL_MACADDR_LEN]; |
---|
678 | struct sockaddr_in WakeUpCliente; |
---|
679 | struct wol_msg Trama_WakeUp; |
---|
680 | struct in_addr addr; |
---|
681 | bool ret; |
---|
682 | int i; |
---|
683 | |
---|
684 | for (i = 0; i < 6; i++) // Primera secuencia de la trama Wake Up (0xFFFFFFFFFFFF) |
---|
685 | Trama_WakeUp.secuencia_FF[i] = 0xFF; |
---|
686 | |
---|
687 | sscanf(mac, "%02x%02x%02x%02x%02x%02x", |
---|
688 | &macaddr[0], &macaddr[1], &macaddr[2], |
---|
689 | &macaddr[3], &macaddr[4], &macaddr[5]); |
---|
690 | |
---|
691 | for (i = 0; i < 6; i++) |
---|
692 | HDaddress_bin[i] = (uint8_t)macaddr[i]; |
---|
693 | |
---|
694 | for (i = 0; i < 16; i++) // Segunda secuencia de la trama Wake Up , repetir 16 veces su la MAC |
---|
695 | memcpy(&Trama_WakeUp.macbin[i][0], &HDaddress_bin, 6); |
---|
696 | |
---|
697 | /* Creación de socket del cliente que recibe la trama magic packet */ |
---|
698 | WakeUpCliente.sin_family = AF_INET; |
---|
699 | WakeUpCliente.sin_port = htons((short) PUERTO_WAKEUP); |
---|
700 | |
---|
701 | switch (atoi(mar)) { |
---|
702 | case OG_WOL_BROADCAST: |
---|
703 | ret = wake_up_broadcast(s, &WakeUpCliente, &Trama_WakeUp); |
---|
704 | break; |
---|
705 | case OG_WOL_UNICAST: |
---|
706 | if (inet_aton(iph, &addr) < 0) { |
---|
707 | syslog(LOG_ERR, "bad IP address for unicast wol\n"); |
---|
708 | ret = false; |
---|
709 | break; |
---|
710 | } |
---|
711 | ret = wake_up_unicast(s, &WakeUpCliente, &Trama_WakeUp, &addr); |
---|
712 | break; |
---|
713 | default: |
---|
714 | syslog(LOG_ERR, "unknown wol type\n"); |
---|
715 | ret = false; |
---|
716 | break; |
---|
717 | } |
---|
718 | return ret; |
---|
719 | } |
---|
720 | |
---|
721 | // ________________________________________________________________________________________________________ |
---|
722 | // Función: actualizaCreacionImagen |
---|
723 | // |
---|
724 | // Descripción: |
---|
725 | // Esta función actualiza la base de datos con el resultado de la creación de una imagen |
---|
726 | // Parámetros: |
---|
727 | // - db: Objeto base de datos (ya operativo) |
---|
728 | // - tbl: Objeto tabla |
---|
729 | // - idi: Identificador de la imagen |
---|
730 | // - dsk: Disco de donde se creó |
---|
731 | // - par: Partición de donde se creó |
---|
732 | // - cpt: Código de partición |
---|
733 | // - ipr: Ip del repositorio |
---|
734 | // - ido: Identificador del ordenador modelo |
---|
735 | // Devuelve: |
---|
736 | // true: Si el proceso es correcto |
---|
737 | // false: En caso de ocurrir algún error |
---|
738 | // ________________________________________________________________________________________________________ |
---|
739 | bool actualizaCreacionImagen(struct og_dbi *dbi, char *idi, char *dsk, |
---|
740 | char *par, char *cpt, char *ipr, char *ido) |
---|
741 | { |
---|
742 | const char *msglog; |
---|
743 | dbi_result result; |
---|
744 | int idr,ifs; |
---|
745 | |
---|
746 | /* Toma identificador del repositorio correspondiente al ordenador modelo */ |
---|
747 | result = dbi_conn_queryf(dbi->conn, |
---|
748 | "SELECT repositorios.idrepositorio" |
---|
749 | " FROM repositorios" |
---|
750 | " LEFT JOIN ordenadores USING (idrepositorio)" |
---|
751 | " WHERE repositorios.ip='%s' AND ordenadores.idordenador=%s", ipr, ido); |
---|
752 | |
---|
753 | if (!result) { |
---|
754 | dbi_conn_error(dbi->conn, &msglog); |
---|
755 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
756 | __func__, __LINE__, msglog); |
---|
757 | return false; |
---|
758 | } |
---|
759 | if (!dbi_result_next_row(result)) { |
---|
760 | syslog(LOG_ERR, |
---|
761 | "repository does not exist in database (%s:%d)\n", |
---|
762 | __func__, __LINE__); |
---|
763 | dbi_result_free(result); |
---|
764 | return false; |
---|
765 | } |
---|
766 | idr = dbi_result_get_uint(result, "idrepositorio"); |
---|
767 | dbi_result_free(result); |
---|
768 | |
---|
769 | /* Toma identificador del perfilsoftware */ |
---|
770 | result = dbi_conn_queryf(dbi->conn, |
---|
771 | "SELECT idperfilsoft" |
---|
772 | " FROM ordenadores_particiones" |
---|
773 | " WHERE idordenador=%s AND numdisk=%s AND numpar=%s", ido, dsk, par); |
---|
774 | |
---|
775 | if (!result) { |
---|
776 | dbi_conn_error(dbi->conn, &msglog); |
---|
777 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
778 | __func__, __LINE__, msglog); |
---|
779 | return false; |
---|
780 | } |
---|
781 | if (!dbi_result_next_row(result)) { |
---|
782 | syslog(LOG_ERR, |
---|
783 | "software profile does not exist in database (%s:%d)\n", |
---|
784 | __func__, __LINE__); |
---|
785 | dbi_result_free(result); |
---|
786 | return false; |
---|
787 | } |
---|
788 | ifs = dbi_result_get_uint(result, "idperfilsoft"); |
---|
789 | dbi_result_free(result); |
---|
790 | |
---|
791 | /* Actualizar los datos de la imagen */ |
---|
792 | result = dbi_conn_queryf(dbi->conn, |
---|
793 | "UPDATE imagenes" |
---|
794 | " SET idordenador=%s, numdisk=%s, numpar=%s, codpar=%s," |
---|
795 | " idperfilsoft=%d, idrepositorio=%d," |
---|
796 | " fechacreacion=NOW(), revision=revision+1" |
---|
797 | " WHERE idimagen=%s", ido, dsk, par, cpt, ifs, idr, idi); |
---|
798 | |
---|
799 | if (!result) { |
---|
800 | dbi_conn_error(dbi->conn, &msglog); |
---|
801 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
802 | __func__, __LINE__, msglog); |
---|
803 | return false; |
---|
804 | } |
---|
805 | dbi_result_free(result); |
---|
806 | |
---|
807 | /* Actualizar los datos en el cliente */ |
---|
808 | result = dbi_conn_queryf(dbi->conn, |
---|
809 | "UPDATE ordenadores_particiones" |
---|
810 | " SET idimagen=%s, revision=(SELECT revision FROM imagenes WHERE idimagen=%s)," |
---|
811 | " fechadespliegue=NOW()" |
---|
812 | " WHERE idordenador=%s AND numdisk=%s AND numpar=%s", |
---|
813 | idi, idi, ido, dsk, par); |
---|
814 | if (!result) { |
---|
815 | dbi_conn_error(dbi->conn, &msglog); |
---|
816 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
817 | __func__, __LINE__, msglog); |
---|
818 | return false; |
---|
819 | } |
---|
820 | dbi_result_free(result); |
---|
821 | |
---|
822 | return true; |
---|
823 | } |
---|
824 | |
---|
825 | // ________________________________________________________________________________________________________ |
---|
826 | // Función: actualizaRestauracionImagen |
---|
827 | // |
---|
828 | // Descripción: |
---|
829 | // Esta función actualiza la base de datos con el resultado de la restauración de una imagen |
---|
830 | // Parámetros: |
---|
831 | // - db: Objeto base de datos (ya operativo) |
---|
832 | // - tbl: Objeto tabla |
---|
833 | // - idi: Identificador de la imagen |
---|
834 | // - dsk: Disco de donde se restauró |
---|
835 | // - par: Partición de donde se restauró |
---|
836 | // - ido: Identificador del cliente donde se restauró |
---|
837 | // - ifs: Identificador del perfil software contenido en la imagen |
---|
838 | // Devuelve: |
---|
839 | // true: Si el proceso es correcto |
---|
840 | // false: En caso de ocurrir algún error |
---|
841 | // ________________________________________________________________________________________________________ |
---|
842 | bool actualizaRestauracionImagen(struct og_dbi *dbi, char *idi, |
---|
843 | char *dsk, char *par, char *ido, char *ifs) |
---|
844 | { |
---|
845 | const char *msglog; |
---|
846 | dbi_result result; |
---|
847 | |
---|
848 | /* Actualizar los datos de la imagen */ |
---|
849 | result = dbi_conn_queryf(dbi->conn, |
---|
850 | "UPDATE ordenadores_particiones" |
---|
851 | " SET idimagen=%s, idperfilsoft=%s, fechadespliegue=NOW()," |
---|
852 | " revision=(SELECT revision FROM imagenes WHERE idimagen=%s)," |
---|
853 | " idnombreso=IFNULL((SELECT idnombreso FROM perfilessoft WHERE idperfilsoft=%s),0)" |
---|
854 | " WHERE idordenador=%s AND numdisk=%s AND numpar=%s", idi, ifs, idi, ifs, ido, dsk, par); |
---|
855 | |
---|
856 | if (!result) { |
---|
857 | dbi_conn_error(dbi->conn, &msglog); |
---|
858 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
859 | __func__, __LINE__, msglog); |
---|
860 | return false; |
---|
861 | } |
---|
862 | dbi_result_free(result); |
---|
863 | |
---|
864 | return true; |
---|
865 | } |
---|
866 | // ________________________________________________________________________________________________________ |
---|
867 | // Función: actualizaHardware |
---|
868 | // |
---|
869 | // Descripción: |
---|
870 | // Actualiza la base de datos con la configuracion hardware del cliente |
---|
871 | // Parámetros: |
---|
872 | // - db: Objeto base de datos (ya operativo) |
---|
873 | // - tbl: Objeto tabla |
---|
874 | // - hrd: cadena con el inventario hardware |
---|
875 | // - ido: Identificador del ordenador |
---|
876 | // - npc: Nombre del ordenador |
---|
877 | // - idc: Identificador del centro o Unidad organizativa |
---|
878 | // ________________________________________________________________________________________________________ |
---|
879 | // |
---|
880 | bool actualizaHardware(struct og_dbi *dbi, char *hrd, char *ido, char *npc, |
---|
881 | char *idc) |
---|
882 | { |
---|
883 | const char *msglog; |
---|
884 | int idtipohardware, idperfilhard; |
---|
885 | int lon, i, j, aux; |
---|
886 | bool retval; |
---|
887 | char *whard; |
---|
888 | int tbidhardware[MAXHARDWARE]; |
---|
889 | char *tbHardware[MAXHARDWARE],*dualHardware[2], strInt[LONINT], *idhardwares; |
---|
890 | dbi_result result; |
---|
891 | |
---|
892 | /* Toma Centro (Unidad Organizativa) */ |
---|
893 | result = dbi_conn_queryf(dbi->conn, |
---|
894 | "SELECT idperfilhard FROM ordenadores WHERE idordenador=%s", |
---|
895 | ido); |
---|
896 | if (!result) { |
---|
897 | dbi_conn_error(dbi->conn, &msglog); |
---|
898 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
899 | __func__, __LINE__, msglog); |
---|
900 | return false; |
---|
901 | } |
---|
902 | if (!dbi_result_next_row(result)) { |
---|
903 | syslog(LOG_ERR, "client does not exist in database (%s:%d)\n", |
---|
904 | __func__, __LINE__); |
---|
905 | dbi_result_free(result); |
---|
906 | return false; |
---|
907 | } |
---|
908 | idperfilhard = dbi_result_get_uint(result, "idperfilhard"); |
---|
909 | dbi_result_free(result); |
---|
910 | |
---|
911 | whard=escaparCadena(hrd); // Codificar comillas simples |
---|
912 | if(!whard) |
---|
913 | return false; |
---|
914 | /* Recorre componentes hardware*/ |
---|
915 | lon = splitCadena(tbHardware, whard, '\n'); |
---|
916 | if (lon > MAXHARDWARE) |
---|
917 | lon = MAXHARDWARE; // Limita el número de componentes hardware |
---|
918 | /* |
---|
919 | for (i=0;i<lon;i++){ |
---|
920 | sprintf(msglog,"Linea de inventario: %s",tbHardware[i]); |
---|
921 | RegistraLog(msglog,false); |
---|
922 | } |
---|
923 | */ |
---|
924 | for (i = 0; i < lon; i++) { |
---|
925 | splitCadena(dualHardware, rTrim(tbHardware[i]), '='); |
---|
926 | //sprintf(msglog,"nemonico: %s",dualHardware[0]); |
---|
927 | //RegistraLog(msglog,false); |
---|
928 | //sprintf(msglog,"valor: %s",dualHardware[1]); |
---|
929 | //RegistraLog(msglog,false); |
---|
930 | result = dbi_conn_queryf(dbi->conn, |
---|
931 | "SELECT idtipohardware,descripcion FROM tipohardwares WHERE nemonico='%s'", |
---|
932 | dualHardware[0]); |
---|
933 | if (!result) { |
---|
934 | dbi_conn_error(dbi->conn, &msglog); |
---|
935 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
936 | __func__, __LINE__, msglog); |
---|
937 | return false; |
---|
938 | } |
---|
939 | if (!dbi_result_next_row(result)) { // Tipo de Hardware NO existente |
---|
940 | dbi_result_free(result); |
---|
941 | return false; |
---|
942 | } else { // Tipo de Hardware Existe |
---|
943 | idtipohardware = dbi_result_get_uint(result, "idtipohardware"); |
---|
944 | dbi_result_free(result); |
---|
945 | |
---|
946 | result = dbi_conn_queryf(dbi->conn, |
---|
947 | "SELECT idhardware FROM hardwares WHERE idtipohardware=%d AND descripcion='%s'", |
---|
948 | idtipohardware, dualHardware[1]); |
---|
949 | |
---|
950 | if (!result) { |
---|
951 | dbi_conn_error(dbi->conn, &msglog); |
---|
952 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
953 | __func__, __LINE__, msglog); |
---|
954 | return false; |
---|
955 | } |
---|
956 | |
---|
957 | if (!dbi_result_next_row(result)) { // Hardware NO existente |
---|
958 | dbi_result_free(result); |
---|
959 | result = dbi_conn_queryf(dbi->conn, |
---|
960 | "INSERT hardwares (idtipohardware,descripcion,idcentro,grupoid) " |
---|
961 | " VALUES(%d,'%s',%s,0)", idtipohardware, |
---|
962 | dualHardware[1], idc); |
---|
963 | if (!result) { |
---|
964 | dbi_conn_error(dbi->conn, &msglog); |
---|
965 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
966 | __func__, __LINE__, msglog); |
---|
967 | return false; |
---|
968 | } |
---|
969 | |
---|
970 | // Recupera el identificador del hardware |
---|
971 | tbidhardware[i] = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
972 | } else { |
---|
973 | tbidhardware[i] = dbi_result_get_uint(result, "idhardware"); |
---|
974 | } |
---|
975 | dbi_result_free(result); |
---|
976 | } |
---|
977 | } |
---|
978 | // Ordena tabla de identificadores para cosultar si existe un pefil con esas especificaciones |
---|
979 | |
---|
980 | for (i = 0; i < lon - 1; i++) { |
---|
981 | for (j = i + 1; j < lon; j++) { |
---|
982 | if (tbidhardware[i] > tbidhardware[j]) { |
---|
983 | aux = tbidhardware[i]; |
---|
984 | tbidhardware[i] = tbidhardware[j]; |
---|
985 | tbidhardware[j] = aux; |
---|
986 | } |
---|
987 | } |
---|
988 | } |
---|
989 | /* Crea cadena de identificadores de componentes hardware separados por coma */ |
---|
990 | sprintf(strInt, "%d", tbidhardware[lon - 1]); // Pasa a cadena el último identificador que es de mayor longitud |
---|
991 | aux = strlen(strInt); // Calcula longitud de cadena para reservar espacio a todos los perfiles |
---|
992 | idhardwares = reservaMemoria(sizeof(aux) * lon + lon); |
---|
993 | if (idhardwares == NULL) { |
---|
994 | syslog(LOG_ERR, "%s:%d OOM\n", __FILE__, __LINE__); |
---|
995 | return false; |
---|
996 | } |
---|
997 | aux = sprintf(idhardwares, "%d", tbidhardware[0]); |
---|
998 | for (i = 1; i < lon; i++) |
---|
999 | aux += sprintf(idhardwares + aux, ",%d", tbidhardware[i]); |
---|
1000 | |
---|
1001 | if (!cuestionPerfilHardware(dbi, idc, ido, idperfilhard, idhardwares, |
---|
1002 | npc, tbidhardware, lon)) { |
---|
1003 | syslog(LOG_ERR, "Problem updating client hardware\n"); |
---|
1004 | retval=false; |
---|
1005 | } |
---|
1006 | else { |
---|
1007 | retval=true; |
---|
1008 | } |
---|
1009 | liberaMemoria(whard); |
---|
1010 | liberaMemoria(idhardwares); |
---|
1011 | return (retval); |
---|
1012 | } |
---|
1013 | // ________________________________________________________________________________________________________ |
---|
1014 | // Función: cuestionPerfilHardware |
---|
1015 | // |
---|
1016 | // Descripción: |
---|
1017 | // Comprueba existencia de perfil hardware y actualización de éste para el ordenador |
---|
1018 | // Parámetros: |
---|
1019 | // - db: Objeto base de datos (ya operativo) |
---|
1020 | // - tbl: Objeto tabla |
---|
1021 | // - idc: Identificador de la Unidad organizativa donde se encuentra el cliente |
---|
1022 | // - ido: Identificador del ordenador |
---|
1023 | // - tbidhardware: Identificador del tipo de hardware |
---|
1024 | // - con: Número de componentes detectados para configurar un el perfil hardware |
---|
1025 | // - npc: Nombre del cliente |
---|
1026 | // ________________________________________________________________________________________________________ |
---|
1027 | bool cuestionPerfilHardware(struct og_dbi *dbi, char *idc, char *ido, |
---|
1028 | int idperfilhardware, char *idhardwares, char *npc, int *tbidhardware, |
---|
1029 | int lon) |
---|
1030 | { |
---|
1031 | const char *msglog; |
---|
1032 | dbi_result result; |
---|
1033 | int i; |
---|
1034 | int nwidperfilhard; |
---|
1035 | |
---|
1036 | // Busca perfil hard del ordenador que contenga todos los componentes hardware encontrados |
---|
1037 | result = dbi_conn_queryf(dbi->conn, |
---|
1038 | "SELECT idperfilhard FROM" |
---|
1039 | " (SELECT perfileshard_hardwares.idperfilhard as idperfilhard," |
---|
1040 | " group_concat(cast(perfileshard_hardwares.idhardware AS char( 11) )" |
---|
1041 | " ORDER BY perfileshard_hardwares.idhardware SEPARATOR ',' ) AS idhardwares" |
---|
1042 | " FROM perfileshard_hardwares" |
---|
1043 | " GROUP BY perfileshard_hardwares.idperfilhard) AS temp" |
---|
1044 | " WHERE idhardwares LIKE '%s'", idhardwares); |
---|
1045 | |
---|
1046 | if (!result) { |
---|
1047 | dbi_conn_error(dbi->conn, &msglog); |
---|
1048 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1049 | __func__, __LINE__, msglog); |
---|
1050 | return false; |
---|
1051 | } |
---|
1052 | if (!dbi_result_next_row(result)) { |
---|
1053 | // No existe un perfil hardware con esos componentes de componentes hardware, lo crea |
---|
1054 | dbi_result_free(result); |
---|
1055 | result = dbi_conn_queryf(dbi->conn, |
---|
1056 | "INSERT perfileshard (descripcion,idcentro,grupoid)" |
---|
1057 | " VALUES('Perfil hardware (%s) ',%s,0)", npc, idc); |
---|
1058 | if (!result) { |
---|
1059 | dbi_conn_error(dbi->conn, &msglog); |
---|
1060 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1061 | __func__, __LINE__, msglog); |
---|
1062 | return false; |
---|
1063 | } |
---|
1064 | dbi_result_free(result); |
---|
1065 | |
---|
1066 | // Recupera el identificador del nuevo perfil hardware |
---|
1067 | nwidperfilhard = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
1068 | |
---|
1069 | // Crea la relación entre perfiles y componenetes hardware |
---|
1070 | for (i = 0; i < lon; i++) { |
---|
1071 | result = dbi_conn_queryf(dbi->conn, |
---|
1072 | "INSERT perfileshard_hardwares (idperfilhard,idhardware)" |
---|
1073 | " VALUES(%d,%d)", nwidperfilhard, tbidhardware[i]); |
---|
1074 | if (!result) { |
---|
1075 | dbi_conn_error(dbi->conn, &msglog); |
---|
1076 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1077 | __func__, __LINE__, msglog); |
---|
1078 | return false; |
---|
1079 | } |
---|
1080 | dbi_result_free(result); |
---|
1081 | } |
---|
1082 | } else { // Existe un perfil con todos esos componentes |
---|
1083 | nwidperfilhard = dbi_result_get_uint(result, "idperfilhard"); |
---|
1084 | dbi_result_free(result); |
---|
1085 | } |
---|
1086 | if (idperfilhardware != nwidperfilhard) { // No coinciden los perfiles |
---|
1087 | // Actualiza el identificador del perfil hardware del ordenador |
---|
1088 | result = dbi_conn_queryf(dbi->conn, |
---|
1089 | "UPDATE ordenadores SET idperfilhard=%d" |
---|
1090 | " WHERE idordenador=%s", nwidperfilhard, ido); |
---|
1091 | if (!result) { |
---|
1092 | dbi_conn_error(dbi->conn, &msglog); |
---|
1093 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1094 | __func__, __LINE__, msglog); |
---|
1095 | return false; |
---|
1096 | } |
---|
1097 | dbi_result_free(result); |
---|
1098 | } |
---|
1099 | /* Eliminar Relación de hardwares con Perfiles hardware que quedan húerfanos */ |
---|
1100 | result = dbi_conn_queryf(dbi->conn, |
---|
1101 | "DELETE FROM perfileshard_hardwares WHERE idperfilhard IN " |
---|
1102 | " (SELECT idperfilhard FROM perfileshard WHERE idperfilhard NOT IN" |
---|
1103 | " (SELECT DISTINCT idperfilhard from ordenadores))"); |
---|
1104 | if (!result) { |
---|
1105 | dbi_conn_error(dbi->conn, &msglog); |
---|
1106 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1107 | __func__, __LINE__, msglog); |
---|
1108 | return false; |
---|
1109 | } |
---|
1110 | dbi_result_free(result); |
---|
1111 | |
---|
1112 | /* Eliminar Perfiles hardware que quedan húerfanos */ |
---|
1113 | result = dbi_conn_queryf(dbi->conn, |
---|
1114 | "DELETE FROM perfileshard WHERE idperfilhard NOT IN" |
---|
1115 | " (SELECT DISTINCT idperfilhard FROM ordenadores)"); |
---|
1116 | if (!result) { |
---|
1117 | dbi_conn_error(dbi->conn, &msglog); |
---|
1118 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1119 | __func__, __LINE__, msglog); |
---|
1120 | return false; |
---|
1121 | } |
---|
1122 | dbi_result_free(result); |
---|
1123 | |
---|
1124 | /* Eliminar Relación de hardwares con Perfiles hardware que quedan húerfanos */ |
---|
1125 | result = dbi_conn_queryf(dbi->conn, |
---|
1126 | "DELETE FROM perfileshard_hardwares WHERE idperfilhard NOT IN" |
---|
1127 | " (SELECT idperfilhard FROM perfileshard)"); |
---|
1128 | if (!result) { |
---|
1129 | dbi_conn_error(dbi->conn, &msglog); |
---|
1130 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1131 | __func__, __LINE__, msglog); |
---|
1132 | return false; |
---|
1133 | } |
---|
1134 | dbi_result_free(result); |
---|
1135 | |
---|
1136 | return true; |
---|
1137 | } |
---|
1138 | // ________________________________________________________________________________________________________ |
---|
1139 | // Función: actualizaSoftware |
---|
1140 | // |
---|
1141 | // Descripción: |
---|
1142 | // Actualiza la base de datos con la configuración software del cliente |
---|
1143 | // Parámetros: |
---|
1144 | // - db: Objeto base de datos (ya operativo) |
---|
1145 | // - tbl: Objeto tabla |
---|
1146 | // - sft: cadena con el inventario software |
---|
1147 | // - par: Número de la partición |
---|
1148 | // - ido: Identificador del ordenador del cliente en la tabla |
---|
1149 | // - npc: Nombre del ordenador |
---|
1150 | // - idc: Identificador del centro o Unidad organizativa |
---|
1151 | // Devuelve: |
---|
1152 | // true: Si el proceso es correcto |
---|
1153 | // false: En caso de ocurrir algún error |
---|
1154 | // |
---|
1155 | // Versión 1.1.0: Se incluye el sistema operativo. Autora: Irina Gómez - ETSII Universidad Sevilla |
---|
1156 | // ________________________________________________________________________________________________________ |
---|
1157 | bool actualizaSoftware(struct og_dbi *dbi, char *sft, char *par,char *ido, |
---|
1158 | char *npc, char *idc) |
---|
1159 | { |
---|
1160 | int i, j, lon, aux, idperfilsoft, idnombreso; |
---|
1161 | bool retval; |
---|
1162 | char *wsft; |
---|
1163 | int tbidsoftware[MAXSOFTWARE]; |
---|
1164 | char *tbSoftware[MAXSOFTWARE], strInt[LONINT], *idsoftwares; |
---|
1165 | const char *msglog; |
---|
1166 | dbi_result result; |
---|
1167 | |
---|
1168 | /* Toma Centro (Unidad Organizativa) y perfil software */ |
---|
1169 | result = dbi_conn_queryf(dbi->conn, |
---|
1170 | "SELECT idperfilsoft,numpar" |
---|
1171 | " FROM ordenadores_particiones" |
---|
1172 | " WHERE idordenador=%s", ido); |
---|
1173 | if (!result) { |
---|
1174 | dbi_conn_error(dbi->conn, &msglog); |
---|
1175 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1176 | __func__, __LINE__, msglog); |
---|
1177 | return false; |
---|
1178 | } |
---|
1179 | idperfilsoft = 0; // Por defecto se supone que el ordenador no tiene aún detectado el perfil software |
---|
1180 | while (dbi_result_next_row(result)) { |
---|
1181 | aux = dbi_result_get_uint(result, "numpar"); |
---|
1182 | if (aux == atoi(par)) { // Se encuentra la partición |
---|
1183 | idperfilsoft = dbi_result_get_uint(result, "idperfilsoft"); |
---|
1184 | break; |
---|
1185 | } |
---|
1186 | } |
---|
1187 | dbi_result_free(result); |
---|
1188 | wsft=escaparCadena(sft); // Codificar comillas simples |
---|
1189 | if(!wsft) |
---|
1190 | return false; |
---|
1191 | |
---|
1192 | /* Recorre componentes software*/ |
---|
1193 | lon = splitCadena(tbSoftware, wsft, '\n'); |
---|
1194 | |
---|
1195 | if (lon == 0) |
---|
1196 | return true; // No hay lineas que procesar |
---|
1197 | if (lon > MAXSOFTWARE) |
---|
1198 | lon = MAXSOFTWARE; // Limita el número de componentes software |
---|
1199 | |
---|
1200 | idnombreso = 0; |
---|
1201 | for (i = 0; i < lon; i++) { |
---|
1202 | // Primera línea es el sistema operativo: se obtiene identificador |
---|
1203 | if (i == 0) { |
---|
1204 | idnombreso = checkDato(dbi, rTrim(tbSoftware[i]), "nombresos", "nombreso", "idnombreso"); |
---|
1205 | continue; |
---|
1206 | } |
---|
1207 | |
---|
1208 | result = dbi_conn_queryf(dbi->conn, |
---|
1209 | "SELECT idsoftware FROM softwares WHERE descripcion ='%s'", |
---|
1210 | rTrim(tbSoftware[i])); |
---|
1211 | if (!result) { |
---|
1212 | dbi_conn_error(dbi->conn, &msglog); |
---|
1213 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1214 | __func__, __LINE__, msglog); |
---|
1215 | return false; |
---|
1216 | } |
---|
1217 | |
---|
1218 | if (!dbi_result_next_row(result)) { |
---|
1219 | dbi_result_free(result); |
---|
1220 | result = dbi_conn_queryf(dbi->conn, |
---|
1221 | "INSERT INTO softwares (idtiposoftware,descripcion,idcentro,grupoid)" |
---|
1222 | " VALUES(2,'%s',%s,0)", tbSoftware[i], idc); |
---|
1223 | if (!result) { // Error al insertar |
---|
1224 | dbi_conn_error(dbi->conn, &msglog); |
---|
1225 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1226 | __func__, __LINE__, msglog); |
---|
1227 | return false; |
---|
1228 | } |
---|
1229 | |
---|
1230 | // Recupera el identificador del software |
---|
1231 | tbidsoftware[i] = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
1232 | } else { |
---|
1233 | tbidsoftware[i] = dbi_result_get_uint(result, "idsoftware"); |
---|
1234 | } |
---|
1235 | dbi_result_free(result); |
---|
1236 | } |
---|
1237 | |
---|
1238 | // Ordena tabla de identificadores para cosultar si existe un pefil con esas especificaciones |
---|
1239 | |
---|
1240 | for (i = 0; i < lon - 1; i++) { |
---|
1241 | for (j = i + 1; j < lon; j++) { |
---|
1242 | if (tbidsoftware[i] > tbidsoftware[j]) { |
---|
1243 | aux = tbidsoftware[i]; |
---|
1244 | tbidsoftware[i] = tbidsoftware[j]; |
---|
1245 | tbidsoftware[j] = aux; |
---|
1246 | } |
---|
1247 | } |
---|
1248 | } |
---|
1249 | /* Crea cadena de identificadores de componentes software separados por coma */ |
---|
1250 | sprintf(strInt, "%d", tbidsoftware[lon - 1]); // Pasa a cadena el último identificador que es de mayor longitud |
---|
1251 | aux = strlen(strInt); // Calcula longitud de cadena para reservar espacio a todos los perfiles |
---|
1252 | idsoftwares = reservaMemoria((sizeof(aux)+1) * lon + lon); |
---|
1253 | if (idsoftwares == NULL) { |
---|
1254 | syslog(LOG_ERR, "%s:%d OOM\n", __FILE__, __LINE__); |
---|
1255 | return false; |
---|
1256 | } |
---|
1257 | aux = sprintf(idsoftwares, "%d", tbidsoftware[0]); |
---|
1258 | for (i = 1; i < lon; i++) |
---|
1259 | aux += sprintf(idsoftwares + aux, ",%d", tbidsoftware[i]); |
---|
1260 | |
---|
1261 | // Comprueba existencia de perfil software y actualización de éste para el ordenador |
---|
1262 | if (!cuestionPerfilSoftware(dbi, idc, ido, idperfilsoft, idnombreso, idsoftwares, |
---|
1263 | npc, par, tbidsoftware, lon)) { |
---|
1264 | syslog(LOG_ERR, "cannot update software\n"); |
---|
1265 | og_info((char *)msglog); |
---|
1266 | retval=false; |
---|
1267 | } |
---|
1268 | else { |
---|
1269 | retval=true; |
---|
1270 | } |
---|
1271 | liberaMemoria(wsft); |
---|
1272 | liberaMemoria(idsoftwares); |
---|
1273 | return (retval); |
---|
1274 | } |
---|
1275 | // ________________________________________________________________________________________________________ |
---|
1276 | // Función: CuestionPerfilSoftware |
---|
1277 | // |
---|
1278 | // Parámetros: |
---|
1279 | // - db: Objeto base de datos (ya operativo) |
---|
1280 | // - tbl: Objeto tabla |
---|
1281 | // - idcentro: Identificador del centro en la tabla |
---|
1282 | // - ido: Identificador del ordenador del cliente en la tabla |
---|
1283 | // - idnombreso: Identificador del sistema operativo |
---|
1284 | // - idsoftwares: Cadena con los identificadores de componentes software separados por comas |
---|
1285 | // - npc: Nombre del ordenador del cliente |
---|
1286 | // - particion: Número de la partición |
---|
1287 | // - tbidsoftware: Array con los identificadores de componentes software |
---|
1288 | // - lon: Número de componentes |
---|
1289 | // Devuelve: |
---|
1290 | // true: Si el proceso es correcto |
---|
1291 | // false: En caso de ocurrir algún error |
---|
1292 | // |
---|
1293 | // Versión 1.1.0: Se incluye el sistema operativo. Autora: Irina Gómez - ETSII Universidad Sevilla |
---|
1294 | //_________________________________________________________________________________________________________ |
---|
1295 | bool cuestionPerfilSoftware(struct og_dbi *dbi, char *idc, char *ido, |
---|
1296 | int idperfilsoftware, int idnombreso, |
---|
1297 | char *idsoftwares, char *npc, char *par, |
---|
1298 | int *tbidsoftware, int lon) |
---|
1299 | { |
---|
1300 | int i, nwidperfilsoft; |
---|
1301 | const char *msglog; |
---|
1302 | dbi_result result; |
---|
1303 | |
---|
1304 | // Busca perfil soft del ordenador que contenga todos los componentes software encontrados |
---|
1305 | result = dbi_conn_queryf(dbi->conn, |
---|
1306 | "SELECT idperfilsoft FROM" |
---|
1307 | " (SELECT perfilessoft_softwares.idperfilsoft as idperfilsoft," |
---|
1308 | " group_concat(cast(perfilessoft_softwares.idsoftware AS char( 11) )" |
---|
1309 | " ORDER BY perfilessoft_softwares.idsoftware SEPARATOR ',' ) AS idsoftwares" |
---|
1310 | " FROM perfilessoft_softwares" |
---|
1311 | " GROUP BY perfilessoft_softwares.idperfilsoft) AS temp" |
---|
1312 | " WHERE idsoftwares LIKE '%s'", idsoftwares); |
---|
1313 | |
---|
1314 | if (!result) { |
---|
1315 | dbi_conn_error(dbi->conn, &msglog); |
---|
1316 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1317 | __func__, __LINE__, msglog); |
---|
1318 | return false; |
---|
1319 | } |
---|
1320 | if (!dbi_result_next_row(result)) { // No existe un perfil software con esos componentes de componentes software, lo crea |
---|
1321 | dbi_result_free(result); |
---|
1322 | result = dbi_conn_queryf(dbi->conn, |
---|
1323 | "INSERT perfilessoft (descripcion, idcentro, grupoid, idnombreso)" |
---|
1324 | " VALUES('Perfil Software (%s, Part:%s) ',%s,0,%i)", npc, par, idc,idnombreso); |
---|
1325 | if (!result) { |
---|
1326 | dbi_conn_error(dbi->conn, &msglog); |
---|
1327 | og_info((char *)msglog); |
---|
1328 | return false; |
---|
1329 | } |
---|
1330 | |
---|
1331 | dbi_result_free(result); |
---|
1332 | // Recupera el identificador del nuevo perfil software |
---|
1333 | nwidperfilsoft = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
1334 | |
---|
1335 | // Crea la relación entre perfiles y componenetes software |
---|
1336 | for (i = 0; i < lon; i++) { |
---|
1337 | result = dbi_conn_queryf(dbi->conn, |
---|
1338 | "INSERT perfilessoft_softwares (idperfilsoft,idsoftware)" |
---|
1339 | " VALUES(%d,%d)", nwidperfilsoft, tbidsoftware[i]); |
---|
1340 | if (!result) { |
---|
1341 | dbi_conn_error(dbi->conn, &msglog); |
---|
1342 | og_info((char *)msglog); |
---|
1343 | return false; |
---|
1344 | } |
---|
1345 | dbi_result_free(result); |
---|
1346 | } |
---|
1347 | } else { // Existe un perfil con todos esos componentes |
---|
1348 | nwidperfilsoft = dbi_result_get_uint(result, "idperfilsoft"); |
---|
1349 | dbi_result_free(result); |
---|
1350 | } |
---|
1351 | |
---|
1352 | if (idperfilsoftware != nwidperfilsoft) { // No coinciden los perfiles |
---|
1353 | // Actualiza el identificador del perfil software del ordenador |
---|
1354 | result = dbi_conn_queryf(dbi->conn, |
---|
1355 | "UPDATE ordenadores_particiones SET idperfilsoft=%d,idimagen=0" |
---|
1356 | " WHERE idordenador=%s AND numpar=%s", nwidperfilsoft, ido, par); |
---|
1357 | if (!result) { // Error al insertar |
---|
1358 | dbi_conn_error(dbi->conn, &msglog); |
---|
1359 | og_info((char *)msglog); |
---|
1360 | return false; |
---|
1361 | } |
---|
1362 | dbi_result_free(result); |
---|
1363 | } |
---|
1364 | |
---|
1365 | /* DEPURACIÓN DE PERFILES SOFTWARE */ |
---|
1366 | |
---|
1367 | /* Eliminar Relación de softwares con Perfiles software que quedan húerfanos */ |
---|
1368 | result = dbi_conn_queryf(dbi->conn, |
---|
1369 | "DELETE FROM perfilessoft_softwares WHERE idperfilsoft IN "\ |
---|
1370 | " (SELECT idperfilsoft FROM perfilessoft WHERE idperfilsoft NOT IN"\ |
---|
1371 | " (SELECT DISTINCT idperfilsoft from ordenadores_particiones) AND idperfilsoft NOT IN"\ |
---|
1372 | " (SELECT DISTINCT idperfilsoft from imagenes))"); |
---|
1373 | if (!result) { |
---|
1374 | dbi_conn_error(dbi->conn, &msglog); |
---|
1375 | og_info((char *)msglog); |
---|
1376 | return false; |
---|
1377 | } |
---|
1378 | dbi_result_free(result), |
---|
1379 | /* Eliminar Perfiles software que quedan húerfanos */ |
---|
1380 | result = dbi_conn_queryf(dbi->conn, |
---|
1381 | "DELETE FROM perfilessoft WHERE idperfilsoft NOT IN" |
---|
1382 | " (SELECT DISTINCT idperfilsoft from ordenadores_particiones)"\ |
---|
1383 | " AND idperfilsoft NOT IN"\ |
---|
1384 | " (SELECT DISTINCT idperfilsoft from imagenes)"); |
---|
1385 | if (!result) { |
---|
1386 | dbi_conn_error(dbi->conn, &msglog); |
---|
1387 | og_info((char *)msglog); |
---|
1388 | return false; |
---|
1389 | } |
---|
1390 | dbi_result_free(result), |
---|
1391 | |
---|
1392 | /* Eliminar Relación de softwares con Perfiles software que quedan húerfanos */ |
---|
1393 | result = dbi_conn_queryf(dbi->conn, |
---|
1394 | "DELETE FROM perfilessoft_softwares WHERE idperfilsoft NOT IN" |
---|
1395 | " (SELECT idperfilsoft from perfilessoft)"); |
---|
1396 | if (!result) { |
---|
1397 | dbi_conn_error(dbi->conn, &msglog); |
---|
1398 | og_info((char *)msglog); |
---|
1399 | return false; |
---|
1400 | } |
---|
1401 | dbi_result_free(result); |
---|
1402 | |
---|
1403 | return true; |
---|
1404 | } |
---|
1405 | |
---|
1406 | static void og_client_release(struct ev_loop *loop, struct og_client *cli) |
---|
1407 | { |
---|
1408 | if (cli->keepalive_idx >= 0) { |
---|
1409 | syslog(LOG_DEBUG, "closing keepalive connection for %s:%hu in slot %d\n", |
---|
1410 | inet_ntoa(cli->addr.sin_addr), |
---|
1411 | ntohs(cli->addr.sin_port), cli->keepalive_idx); |
---|
1412 | tbsockets[cli->keepalive_idx].cli = NULL; |
---|
1413 | } |
---|
1414 | |
---|
1415 | list_del(&cli->list); |
---|
1416 | ev_io_stop(loop, &cli->io); |
---|
1417 | close(cli->io.fd); |
---|
1418 | free(cli); |
---|
1419 | } |
---|
1420 | |
---|
1421 | static void og_client_keepalive(struct ev_loop *loop, struct og_client *cli) |
---|
1422 | { |
---|
1423 | struct og_client *old_cli; |
---|
1424 | |
---|
1425 | old_cli = tbsockets[cli->keepalive_idx].cli; |
---|
1426 | if (old_cli && old_cli != cli) { |
---|
1427 | syslog(LOG_DEBUG, "closing old keepalive connection for %s:%hu\n", |
---|
1428 | inet_ntoa(old_cli->addr.sin_addr), |
---|
1429 | ntohs(old_cli->addr.sin_port)); |
---|
1430 | |
---|
1431 | og_client_release(loop, old_cli); |
---|
1432 | } |
---|
1433 | tbsockets[cli->keepalive_idx].cli = cli; |
---|
1434 | } |
---|
1435 | |
---|
1436 | static void og_client_reset_state(struct og_client *cli) |
---|
1437 | { |
---|
1438 | cli->state = OG_CLIENT_RECEIVING_HEADER; |
---|
1439 | cli->buf_len = 0; |
---|
1440 | } |
---|
1441 | |
---|
1442 | static TRAMA *og_msg_alloc(char *data, unsigned int len) |
---|
1443 | { |
---|
1444 | TRAMA *ptrTrama; |
---|
1445 | |
---|
1446 | ptrTrama = (TRAMA *)reservaMemoria(sizeof(TRAMA)); |
---|
1447 | if (!ptrTrama) { |
---|
1448 | syslog(LOG_ERR, "OOM\n"); |
---|
1449 | return NULL; |
---|
1450 | } |
---|
1451 | |
---|
1452 | initParametros(ptrTrama, len); |
---|
1453 | memcpy(ptrTrama, "@JMMLCAMDJ_MCDJ", LONGITUD_CABECERATRAMA); |
---|
1454 | memcpy(ptrTrama->parametros, data, len); |
---|
1455 | ptrTrama->lonprm = len; |
---|
1456 | |
---|
1457 | return ptrTrama; |
---|
1458 | } |
---|
1459 | |
---|
1460 | static void og_msg_free(TRAMA *ptrTrama) |
---|
1461 | { |
---|
1462 | liberaMemoria(ptrTrama->parametros); |
---|
1463 | liberaMemoria(ptrTrama); |
---|
1464 | } |
---|
1465 | |
---|
1466 | #define OG_CLIENTS_MAX 4096 |
---|
1467 | #define OG_PARTITION_MAX 4 |
---|
1468 | |
---|
1469 | struct og_partition { |
---|
1470 | const char *disk; |
---|
1471 | const char *number; |
---|
1472 | const char *code; |
---|
1473 | const char *size; |
---|
1474 | const char *filesystem; |
---|
1475 | const char *format; |
---|
1476 | const char *os; |
---|
1477 | const char *used_size; |
---|
1478 | }; |
---|
1479 | |
---|
1480 | struct og_sync_params { |
---|
1481 | const char *sync; |
---|
1482 | const char *diff; |
---|
1483 | const char *remove; |
---|
1484 | const char *compress; |
---|
1485 | const char *cleanup; |
---|
1486 | const char *cache; |
---|
1487 | const char *cleanup_cache; |
---|
1488 | const char *remove_dst; |
---|
1489 | const char *diff_id; |
---|
1490 | const char *diff_name; |
---|
1491 | const char *path; |
---|
1492 | const char *method; |
---|
1493 | }; |
---|
1494 | |
---|
1495 | struct og_msg_params { |
---|
1496 | const char *ips_array[OG_CLIENTS_MAX]; |
---|
1497 | const char *mac_array[OG_CLIENTS_MAX]; |
---|
1498 | unsigned int ips_array_len; |
---|
1499 | const char *wol_type; |
---|
1500 | char run_cmd[4096]; |
---|
1501 | const char *disk; |
---|
1502 | const char *partition; |
---|
1503 | const char *repository; |
---|
1504 | const char *name; |
---|
1505 | const char *id; |
---|
1506 | const char *code; |
---|
1507 | const char *type; |
---|
1508 | const char *profile; |
---|
1509 | const char *cache; |
---|
1510 | const char *cache_size; |
---|
1511 | bool echo; |
---|
1512 | struct og_partition partition_setup[OG_PARTITION_MAX]; |
---|
1513 | struct og_sync_params sync_setup; |
---|
1514 | struct og_schedule_time time; |
---|
1515 | const char *task_id; |
---|
1516 | uint64_t flags; |
---|
1517 | }; |
---|
1518 | |
---|
1519 | #define OG_COMPUTER_NAME_MAXLEN 100 |
---|
1520 | |
---|
1521 | struct og_computer { |
---|
1522 | unsigned int id; |
---|
1523 | unsigned int center; |
---|
1524 | unsigned int room; |
---|
1525 | char name[OG_COMPUTER_NAME_MAXLEN + 1]; |
---|
1526 | }; |
---|
1527 | |
---|
1528 | #define OG_REST_PARAM_ADDR (1UL << 0) |
---|
1529 | #define OG_REST_PARAM_MAC (1UL << 1) |
---|
1530 | #define OG_REST_PARAM_WOL_TYPE (1UL << 2) |
---|
1531 | #define OG_REST_PARAM_RUN_CMD (1UL << 3) |
---|
1532 | #define OG_REST_PARAM_DISK (1UL << 4) |
---|
1533 | #define OG_REST_PARAM_PARTITION (1UL << 5) |
---|
1534 | #define OG_REST_PARAM_REPO (1UL << 6) |
---|
1535 | #define OG_REST_PARAM_NAME (1UL << 7) |
---|
1536 | #define OG_REST_PARAM_ID (1UL << 8) |
---|
1537 | #define OG_REST_PARAM_CODE (1UL << 9) |
---|
1538 | #define OG_REST_PARAM_TYPE (1UL << 10) |
---|
1539 | #define OG_REST_PARAM_PROFILE (1UL << 11) |
---|
1540 | #define OG_REST_PARAM_CACHE (1UL << 12) |
---|
1541 | #define OG_REST_PARAM_CACHE_SIZE (1UL << 13) |
---|
1542 | #define OG_REST_PARAM_PART_0 (1UL << 14) |
---|
1543 | #define OG_REST_PARAM_PART_1 (1UL << 15) |
---|
1544 | #define OG_REST_PARAM_PART_2 (1UL << 16) |
---|
1545 | #define OG_REST_PARAM_PART_3 (1UL << 17) |
---|
1546 | #define OG_REST_PARAM_SYNC_SYNC (1UL << 18) |
---|
1547 | #define OG_REST_PARAM_SYNC_DIFF (1UL << 19) |
---|
1548 | #define OG_REST_PARAM_SYNC_REMOVE (1UL << 20) |
---|
1549 | #define OG_REST_PARAM_SYNC_COMPRESS (1UL << 21) |
---|
1550 | #define OG_REST_PARAM_SYNC_CLEANUP (1UL << 22) |
---|
1551 | #define OG_REST_PARAM_SYNC_CACHE (1UL << 23) |
---|
1552 | #define OG_REST_PARAM_SYNC_CLEANUP_CACHE (1UL << 24) |
---|
1553 | #define OG_REST_PARAM_SYNC_REMOVE_DST (1UL << 25) |
---|
1554 | #define OG_REST_PARAM_SYNC_DIFF_ID (1UL << 26) |
---|
1555 | #define OG_REST_PARAM_SYNC_DIFF_NAME (1UL << 27) |
---|
1556 | #define OG_REST_PARAM_SYNC_PATH (1UL << 28) |
---|
1557 | #define OG_REST_PARAM_SYNC_METHOD (1UL << 29) |
---|
1558 | #define OG_REST_PARAM_ECHO (1UL << 30) |
---|
1559 | #define OG_REST_PARAM_TASK (1UL << 31) |
---|
1560 | #define OG_REST_PARAM_TIME_YEARS (1UL << 32) |
---|
1561 | #define OG_REST_PARAM_TIME_MONTHS (1UL << 33) |
---|
1562 | #define OG_REST_PARAM_TIME_WEEKS (1UL << 34) |
---|
1563 | #define OG_REST_PARAM_TIME_WEEK_DAYS (1UL << 35) |
---|
1564 | #define OG_REST_PARAM_TIME_DAYS (1UL << 36) |
---|
1565 | #define OG_REST_PARAM_TIME_HOURS (1UL << 37) |
---|
1566 | #define OG_REST_PARAM_TIME_AM_PM (1UL << 38) |
---|
1567 | #define OG_REST_PARAM_TIME_MINUTES (1UL << 39) |
---|
1568 | |
---|
1569 | enum og_rest_method { |
---|
1570 | OG_METHOD_GET = 0, |
---|
1571 | OG_METHOD_POST, |
---|
1572 | OG_METHOD_NO_HTTP |
---|
1573 | }; |
---|
1574 | |
---|
1575 | static struct og_client *og_client_find(const char *ip) |
---|
1576 | { |
---|
1577 | struct og_client *client; |
---|
1578 | struct in_addr addr; |
---|
1579 | int res; |
---|
1580 | |
---|
1581 | res = inet_aton(ip, &addr); |
---|
1582 | if (!res) { |
---|
1583 | syslog(LOG_ERR, "Invalid IP string: %s\n", ip); |
---|
1584 | return NULL; |
---|
1585 | } |
---|
1586 | |
---|
1587 | list_for_each_entry(client, &client_list, list) { |
---|
1588 | if (client->addr.sin_addr.s_addr == addr.s_addr && client->agent) { |
---|
1589 | return client; |
---|
1590 | } |
---|
1591 | } |
---|
1592 | |
---|
1593 | return NULL; |
---|
1594 | } |
---|
1595 | |
---|
1596 | static bool og_msg_params_validate(const struct og_msg_params *params, |
---|
1597 | const uint64_t flags) |
---|
1598 | { |
---|
1599 | return (params->flags & flags) == flags; |
---|
1600 | } |
---|
1601 | |
---|
1602 | static int og_json_parse_clients(json_t *element, struct og_msg_params *params) |
---|
1603 | { |
---|
1604 | unsigned int i; |
---|
1605 | json_t *k; |
---|
1606 | |
---|
1607 | if (json_typeof(element) != JSON_ARRAY) |
---|
1608 | return -1; |
---|
1609 | |
---|
1610 | for (i = 0; i < json_array_size(element); i++) { |
---|
1611 | k = json_array_get(element, i); |
---|
1612 | if (json_typeof(k) != JSON_STRING) |
---|
1613 | return -1; |
---|
1614 | |
---|
1615 | params->ips_array[params->ips_array_len++] = |
---|
1616 | json_string_value(k); |
---|
1617 | |
---|
1618 | params->flags |= OG_REST_PARAM_ADDR; |
---|
1619 | } |
---|
1620 | |
---|
1621 | return 0; |
---|
1622 | } |
---|
1623 | |
---|
1624 | static int og_json_parse_string(json_t *element, const char **str) |
---|
1625 | { |
---|
1626 | if (json_typeof(element) != JSON_STRING) |
---|
1627 | return -1; |
---|
1628 | |
---|
1629 | *str = json_string_value(element); |
---|
1630 | return 0; |
---|
1631 | } |
---|
1632 | |
---|
1633 | static int og_json_parse_uint(json_t *element, uint32_t *integer) |
---|
1634 | { |
---|
1635 | if (json_typeof(element) != JSON_INTEGER) |
---|
1636 | return -1; |
---|
1637 | |
---|
1638 | *integer = json_integer_value(element); |
---|
1639 | return 0; |
---|
1640 | } |
---|
1641 | |
---|
1642 | static int og_json_parse_bool(json_t *element, bool *value) |
---|
1643 | { |
---|
1644 | if (json_typeof(element) == JSON_TRUE) |
---|
1645 | *value = true; |
---|
1646 | else if (json_typeof(element) == JSON_FALSE) |
---|
1647 | *value = false; |
---|
1648 | else |
---|
1649 | return -1; |
---|
1650 | |
---|
1651 | return 0; |
---|
1652 | } |
---|
1653 | |
---|
1654 | static int og_json_parse_sync_params(json_t *element, |
---|
1655 | struct og_msg_params *params) |
---|
1656 | { |
---|
1657 | const char *key; |
---|
1658 | json_t *value; |
---|
1659 | int err = 0; |
---|
1660 | |
---|
1661 | json_object_foreach(element, key, value) { |
---|
1662 | if (!strcmp(key, "sync")) { |
---|
1663 | err = og_json_parse_string(value, ¶ms->sync_setup.sync); |
---|
1664 | params->flags |= OG_REST_PARAM_SYNC_SYNC; |
---|
1665 | } else if (!strcmp(key, "diff")) { |
---|
1666 | err = og_json_parse_string(value, ¶ms->sync_setup.diff); |
---|
1667 | params->flags |= OG_REST_PARAM_SYNC_DIFF; |
---|
1668 | } else if (!strcmp(key, "remove")) { |
---|
1669 | err = og_json_parse_string(value, ¶ms->sync_setup.remove); |
---|
1670 | params->flags |= OG_REST_PARAM_SYNC_REMOVE; |
---|
1671 | } else if (!strcmp(key, "compress")) { |
---|
1672 | err = og_json_parse_string(value, ¶ms->sync_setup.compress); |
---|
1673 | params->flags |= OG_REST_PARAM_SYNC_COMPRESS; |
---|
1674 | } else if (!strcmp(key, "cleanup")) { |
---|
1675 | err = og_json_parse_string(value, ¶ms->sync_setup.cleanup); |
---|
1676 | params->flags |= OG_REST_PARAM_SYNC_CLEANUP; |
---|
1677 | } else if (!strcmp(key, "cache")) { |
---|
1678 | err = og_json_parse_string(value, ¶ms->sync_setup.cache); |
---|
1679 | params->flags |= OG_REST_PARAM_SYNC_CACHE; |
---|
1680 | } else if (!strcmp(key, "cleanup_cache")) { |
---|
1681 | err = og_json_parse_string(value, ¶ms->sync_setup.cleanup_cache); |
---|
1682 | params->flags |= OG_REST_PARAM_SYNC_CLEANUP_CACHE; |
---|
1683 | } else if (!strcmp(key, "remove_dst")) { |
---|
1684 | err = og_json_parse_string(value, ¶ms->sync_setup.remove_dst); |
---|
1685 | params->flags |= OG_REST_PARAM_SYNC_REMOVE_DST; |
---|
1686 | } else if (!strcmp(key, "diff_id")) { |
---|
1687 | err = og_json_parse_string(value, ¶ms->sync_setup.diff_id); |
---|
1688 | params->flags |= OG_REST_PARAM_SYNC_DIFF_ID; |
---|
1689 | } else if (!strcmp(key, "diff_name")) { |
---|
1690 | err = og_json_parse_string(value, ¶ms->sync_setup.diff_name); |
---|
1691 | params->flags |= OG_REST_PARAM_SYNC_DIFF_NAME; |
---|
1692 | } else if (!strcmp(key, "path")) { |
---|
1693 | err = og_json_parse_string(value, ¶ms->sync_setup.path); |
---|
1694 | params->flags |= OG_REST_PARAM_SYNC_PATH; |
---|
1695 | } else if (!strcmp(key, "method")) { |
---|
1696 | err = og_json_parse_string(value, ¶ms->sync_setup.method); |
---|
1697 | params->flags |= OG_REST_PARAM_SYNC_METHOD; |
---|
1698 | } |
---|
1699 | |
---|
1700 | if (err != 0) |
---|
1701 | return err; |
---|
1702 | } |
---|
1703 | return err; |
---|
1704 | } |
---|
1705 | |
---|
1706 | #define OG_PARAM_PART_NUMBER (1UL << 0) |
---|
1707 | #define OG_PARAM_PART_CODE (1UL << 1) |
---|
1708 | #define OG_PARAM_PART_FILESYSTEM (1UL << 2) |
---|
1709 | #define OG_PARAM_PART_SIZE (1UL << 3) |
---|
1710 | #define OG_PARAM_PART_FORMAT (1UL << 4) |
---|
1711 | #define OG_PARAM_PART_DISK (1UL << 5) |
---|
1712 | #define OG_PARAM_PART_OS (1UL << 6) |
---|
1713 | #define OG_PARAM_PART_USED_SIZE (1UL << 7) |
---|
1714 | |
---|
1715 | static int og_json_parse_partition(json_t *element, |
---|
1716 | struct og_partition *part, |
---|
1717 | uint64_t required_flags) |
---|
1718 | { |
---|
1719 | uint64_t flags = 0UL; |
---|
1720 | const char *key; |
---|
1721 | json_t *value; |
---|
1722 | int err = 0; |
---|
1723 | |
---|
1724 | json_object_foreach(element, key, value) { |
---|
1725 | if (!strcmp(key, "partition")) { |
---|
1726 | err = og_json_parse_string(value, &part->number); |
---|
1727 | flags |= OG_PARAM_PART_NUMBER; |
---|
1728 | } else if (!strcmp(key, "code")) { |
---|
1729 | err = og_json_parse_string(value, &part->code); |
---|
1730 | flags |= OG_PARAM_PART_CODE; |
---|
1731 | } else if (!strcmp(key, "filesystem")) { |
---|
1732 | err = og_json_parse_string(value, &part->filesystem); |
---|
1733 | flags |= OG_PARAM_PART_FILESYSTEM; |
---|
1734 | } else if (!strcmp(key, "size")) { |
---|
1735 | err = og_json_parse_string(value, &part->size); |
---|
1736 | flags |= OG_PARAM_PART_SIZE; |
---|
1737 | } else if (!strcmp(key, "format")) { |
---|
1738 | err = og_json_parse_string(value, &part->format); |
---|
1739 | flags |= OG_PARAM_PART_FORMAT; |
---|
1740 | } else if (!strcmp(key, "disk")) { |
---|
1741 | err = og_json_parse_string(value, &part->disk); |
---|
1742 | flags |= OG_PARAM_PART_DISK; |
---|
1743 | } else if (!strcmp(key, "os")) { |
---|
1744 | err = og_json_parse_string(value, &part->os); |
---|
1745 | flags |= OG_PARAM_PART_OS; |
---|
1746 | } else if (!strcmp(key, "used_size")) { |
---|
1747 | err = og_json_parse_string(value, &part->used_size); |
---|
1748 | flags |= OG_PARAM_PART_USED_SIZE; |
---|
1749 | } |
---|
1750 | |
---|
1751 | if (err < 0) |
---|
1752 | return err; |
---|
1753 | } |
---|
1754 | |
---|
1755 | if (flags != required_flags) |
---|
1756 | return -1; |
---|
1757 | |
---|
1758 | return err; |
---|
1759 | } |
---|
1760 | |
---|
1761 | static int og_json_parse_partition_setup(json_t *element, |
---|
1762 | struct og_msg_params *params) |
---|
1763 | { |
---|
1764 | unsigned int i; |
---|
1765 | json_t *k; |
---|
1766 | |
---|
1767 | if (json_typeof(element) != JSON_ARRAY) |
---|
1768 | return -1; |
---|
1769 | |
---|
1770 | for (i = 0; i < json_array_size(element) && i < OG_PARTITION_MAX; ++i) { |
---|
1771 | k = json_array_get(element, i); |
---|
1772 | |
---|
1773 | if (json_typeof(k) != JSON_OBJECT) |
---|
1774 | return -1; |
---|
1775 | |
---|
1776 | if (og_json_parse_partition(k, ¶ms->partition_setup[i], |
---|
1777 | OG_PARAM_PART_NUMBER | |
---|
1778 | OG_PARAM_PART_CODE | |
---|
1779 | OG_PARAM_PART_FILESYSTEM | |
---|
1780 | OG_PARAM_PART_SIZE | |
---|
1781 | OG_PARAM_PART_FORMAT) < 0) |
---|
1782 | return -1; |
---|
1783 | |
---|
1784 | params->flags |= (OG_REST_PARAM_PART_0 << i); |
---|
1785 | } |
---|
1786 | return 0; |
---|
1787 | } |
---|
1788 | |
---|
1789 | static int og_json_parse_time_params(json_t *element, |
---|
1790 | struct og_msg_params *params) |
---|
1791 | { |
---|
1792 | const char *key; |
---|
1793 | json_t *value; |
---|
1794 | int err = 0; |
---|
1795 | |
---|
1796 | json_object_foreach(element, key, value) { |
---|
1797 | if (!strcmp(key, "years")) { |
---|
1798 | err = og_json_parse_uint(value, ¶ms->time.years); |
---|
1799 | params->flags |= OG_REST_PARAM_TIME_YEARS; |
---|
1800 | } else if (!strcmp(key, "months")) { |
---|
1801 | err = og_json_parse_uint(value, ¶ms->time.months); |
---|
1802 | params->flags |= OG_REST_PARAM_TIME_MONTHS; |
---|
1803 | } else if (!strcmp(key, "weeks")) { |
---|
1804 | err = og_json_parse_uint(value, ¶ms->time.weeks); |
---|
1805 | params->flags |= OG_REST_PARAM_TIME_WEEKS; |
---|
1806 | } else if (!strcmp(key, "week_days")) { |
---|
1807 | err = og_json_parse_uint(value, ¶ms->time.week_days); |
---|
1808 | params->flags |= OG_REST_PARAM_TIME_WEEK_DAYS; |
---|
1809 | } else if (!strcmp(key, "days")) { |
---|
1810 | err = og_json_parse_uint(value, ¶ms->time.days); |
---|
1811 | params->flags |= OG_REST_PARAM_TIME_DAYS; |
---|
1812 | } else if (!strcmp(key, "hours")) { |
---|
1813 | err = og_json_parse_uint(value, ¶ms->time.hours); |
---|
1814 | params->flags |= OG_REST_PARAM_TIME_HOURS; |
---|
1815 | } else if (!strcmp(key, "am_pm")) { |
---|
1816 | err = og_json_parse_uint(value, ¶ms->time.am_pm); |
---|
1817 | params->flags |= OG_REST_PARAM_TIME_AM_PM; |
---|
1818 | } else if (!strcmp(key, "minutes")) { |
---|
1819 | err = og_json_parse_uint(value, ¶ms->time.minutes); |
---|
1820 | params->flags |= OG_REST_PARAM_TIME_MINUTES; |
---|
1821 | } |
---|
1822 | if (err != 0) |
---|
1823 | return err; |
---|
1824 | } |
---|
1825 | |
---|
1826 | return err; |
---|
1827 | } |
---|
1828 | |
---|
1829 | static const char *og_cmd_to_uri[OG_CMD_MAX] = { |
---|
1830 | [OG_CMD_WOL] = "wol", |
---|
1831 | [OG_CMD_PROBE] = "probe", |
---|
1832 | [OG_CMD_SHELL_RUN] = "shell/run", |
---|
1833 | [OG_CMD_SESSION] = "session", |
---|
1834 | [OG_CMD_POWEROFF] = "poweroff", |
---|
1835 | [OG_CMD_REFRESH] = "refresh", |
---|
1836 | [OG_CMD_REBOOT] = "reboot", |
---|
1837 | [OG_CMD_STOP] = "stop", |
---|
1838 | [OG_CMD_HARDWARE] = "hardware", |
---|
1839 | [OG_CMD_SOFTWARE] = "software", |
---|
1840 | [OG_CMD_IMAGE_CREATE] = "image/create", |
---|
1841 | [OG_CMD_IMAGE_RESTORE] = "image/restore", |
---|
1842 | [OG_CMD_SETUP] = "setup", |
---|
1843 | [OG_CMD_RUN_SCHEDULE] = "run/schedule", |
---|
1844 | }; |
---|
1845 | |
---|
1846 | static bool og_client_is_busy(const struct og_client *cli, |
---|
1847 | enum og_cmd_type type) |
---|
1848 | { |
---|
1849 | switch (type) { |
---|
1850 | case OG_CMD_REBOOT: |
---|
1851 | case OG_CMD_POWEROFF: |
---|
1852 | case OG_CMD_STOP: |
---|
1853 | break; |
---|
1854 | default: |
---|
1855 | if (cli->last_cmd != OG_CMD_UNSPEC) |
---|
1856 | return true; |
---|
1857 | break; |
---|
1858 | } |
---|
1859 | |
---|
1860 | return false; |
---|
1861 | } |
---|
1862 | |
---|
1863 | static int og_send_request(enum og_rest_method method, enum og_cmd_type type, |
---|
1864 | const struct og_msg_params *params, |
---|
1865 | const json_t *data) |
---|
1866 | { |
---|
1867 | const char *content_type = "Content-Type: application/json"; |
---|
1868 | char content [OG_MSG_REQUEST_MAXLEN - 700] = {}; |
---|
1869 | char buf[OG_MSG_REQUEST_MAXLEN] = {}; |
---|
1870 | unsigned int content_length; |
---|
1871 | char method_str[5] = {}; |
---|
1872 | struct og_client *cli; |
---|
1873 | const char *uri; |
---|
1874 | unsigned int i; |
---|
1875 | int client_sd; |
---|
1876 | |
---|
1877 | if (method == OG_METHOD_GET) |
---|
1878 | snprintf(method_str, 5, "GET"); |
---|
1879 | else if (method == OG_METHOD_POST) |
---|
1880 | snprintf(method_str, 5, "POST"); |
---|
1881 | else |
---|
1882 | return -1; |
---|
1883 | |
---|
1884 | if (!data) |
---|
1885 | content_length = 0; |
---|
1886 | else |
---|
1887 | content_length = json_dumpb(data, content, |
---|
1888 | OG_MSG_REQUEST_MAXLEN - 700, |
---|
1889 | JSON_COMPACT); |
---|
1890 | |
---|
1891 | uri = og_cmd_to_uri[type]; |
---|
1892 | snprintf(buf, OG_MSG_REQUEST_MAXLEN, |
---|
1893 | "%s /%s HTTP/1.1\r\nContent-Length: %d\r\n%s\r\n\r\n%s", |
---|
1894 | method_str, uri, content_length, content_type, content); |
---|
1895 | |
---|
1896 | for (i = 0; i < params->ips_array_len; i++) { |
---|
1897 | cli = og_client_find(params->ips_array[i]); |
---|
1898 | if (!cli) |
---|
1899 | continue; |
---|
1900 | |
---|
1901 | if (og_client_is_busy(cli, type)) |
---|
1902 | continue; |
---|
1903 | |
---|
1904 | client_sd = cli->io.fd; |
---|
1905 | if (client_sd < 0) { |
---|
1906 | syslog(LOG_INFO, "Client %s not conected\n", |
---|
1907 | params->ips_array[i]); |
---|
1908 | continue; |
---|
1909 | } |
---|
1910 | |
---|
1911 | if (send(client_sd, buf, strlen(buf), 0) < 0) |
---|
1912 | continue; |
---|
1913 | |
---|
1914 | cli->last_cmd = type; |
---|
1915 | } |
---|
1916 | |
---|
1917 | return 0; |
---|
1918 | } |
---|
1919 | |
---|
1920 | static int og_cmd_post_clients(json_t *element, struct og_msg_params *params) |
---|
1921 | { |
---|
1922 | const char *key; |
---|
1923 | json_t *value; |
---|
1924 | int err = 0; |
---|
1925 | |
---|
1926 | if (json_typeof(element) != JSON_OBJECT) |
---|
1927 | return -1; |
---|
1928 | |
---|
1929 | json_object_foreach(element, key, value) { |
---|
1930 | if (!strcmp(key, "clients")) |
---|
1931 | err = og_json_parse_clients(value, params); |
---|
1932 | |
---|
1933 | if (err < 0) |
---|
1934 | break; |
---|
1935 | } |
---|
1936 | |
---|
1937 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
1938 | return -1; |
---|
1939 | |
---|
1940 | return og_send_request(OG_METHOD_POST, OG_CMD_PROBE, params, NULL); |
---|
1941 | } |
---|
1942 | |
---|
1943 | struct og_buffer { |
---|
1944 | char *data; |
---|
1945 | int len; |
---|
1946 | }; |
---|
1947 | |
---|
1948 | static int og_json_dump_clients(const char *buffer, size_t size, void *data) |
---|
1949 | { |
---|
1950 | struct og_buffer *og_buffer = (struct og_buffer *)data; |
---|
1951 | |
---|
1952 | memcpy(og_buffer->data + og_buffer->len, buffer, size); |
---|
1953 | og_buffer->len += size; |
---|
1954 | |
---|
1955 | return 0; |
---|
1956 | } |
---|
1957 | |
---|
1958 | static int og_cmd_get_clients(json_t *element, struct og_msg_params *params, |
---|
1959 | char *buffer_reply) |
---|
1960 | { |
---|
1961 | json_t *root, *array, *addr, *state, *object; |
---|
1962 | struct og_client *client; |
---|
1963 | struct og_buffer og_buffer = { |
---|
1964 | .data = buffer_reply, |
---|
1965 | }; |
---|
1966 | |
---|
1967 | array = json_array(); |
---|
1968 | if (!array) |
---|
1969 | return -1; |
---|
1970 | |
---|
1971 | list_for_each_entry(client, &client_list, list) { |
---|
1972 | if (!client->agent) |
---|
1973 | continue; |
---|
1974 | |
---|
1975 | object = json_object(); |
---|
1976 | if (!object) { |
---|
1977 | json_decref(array); |
---|
1978 | return -1; |
---|
1979 | } |
---|
1980 | addr = json_string(inet_ntoa(client->addr.sin_addr)); |
---|
1981 | if (!addr) { |
---|
1982 | json_decref(object); |
---|
1983 | json_decref(array); |
---|
1984 | return -1; |
---|
1985 | } |
---|
1986 | json_object_set_new(object, "addr", addr); |
---|
1987 | state = json_string(og_client_status(client)); |
---|
1988 | if (!state) { |
---|
1989 | json_decref(object); |
---|
1990 | json_decref(array); |
---|
1991 | return -1; |
---|
1992 | } |
---|
1993 | json_object_set_new(object, "state", state); |
---|
1994 | json_array_append_new(array, object); |
---|
1995 | } |
---|
1996 | root = json_pack("{s:o}", "clients", array); |
---|
1997 | if (!root) { |
---|
1998 | json_decref(array); |
---|
1999 | return -1; |
---|
2000 | } |
---|
2001 | |
---|
2002 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
2003 | json_decref(root); |
---|
2004 | |
---|
2005 | return 0; |
---|
2006 | } |
---|
2007 | |
---|
2008 | static int og_json_parse_target(json_t *element, struct og_msg_params *params) |
---|
2009 | { |
---|
2010 | const char *key; |
---|
2011 | json_t *value; |
---|
2012 | |
---|
2013 | if (json_typeof(element) != JSON_OBJECT) { |
---|
2014 | return -1; |
---|
2015 | } |
---|
2016 | |
---|
2017 | json_object_foreach(element, key, value) { |
---|
2018 | if (!strcmp(key, "addr")) { |
---|
2019 | if (json_typeof(value) != JSON_STRING) |
---|
2020 | return -1; |
---|
2021 | |
---|
2022 | params->ips_array[params->ips_array_len] = |
---|
2023 | json_string_value(value); |
---|
2024 | |
---|
2025 | params->flags |= OG_REST_PARAM_ADDR; |
---|
2026 | } else if (!strcmp(key, "mac")) { |
---|
2027 | if (json_typeof(value) != JSON_STRING) |
---|
2028 | return -1; |
---|
2029 | |
---|
2030 | params->mac_array[params->ips_array_len] = |
---|
2031 | json_string_value(value); |
---|
2032 | |
---|
2033 | params->flags |= OG_REST_PARAM_MAC; |
---|
2034 | } |
---|
2035 | } |
---|
2036 | |
---|
2037 | return 0; |
---|
2038 | } |
---|
2039 | |
---|
2040 | static int og_json_parse_targets(json_t *element, struct og_msg_params *params) |
---|
2041 | { |
---|
2042 | unsigned int i; |
---|
2043 | json_t *k; |
---|
2044 | int err; |
---|
2045 | |
---|
2046 | if (json_typeof(element) != JSON_ARRAY) |
---|
2047 | return -1; |
---|
2048 | |
---|
2049 | for (i = 0; i < json_array_size(element); i++) { |
---|
2050 | k = json_array_get(element, i); |
---|
2051 | |
---|
2052 | if (json_typeof(k) != JSON_OBJECT) |
---|
2053 | return -1; |
---|
2054 | |
---|
2055 | err = og_json_parse_target(k, params); |
---|
2056 | if (err < 0) |
---|
2057 | return err; |
---|
2058 | |
---|
2059 | params->ips_array_len++; |
---|
2060 | } |
---|
2061 | return 0; |
---|
2062 | } |
---|
2063 | |
---|
2064 | static int og_json_parse_type(json_t *element, struct og_msg_params *params) |
---|
2065 | { |
---|
2066 | const char *type; |
---|
2067 | |
---|
2068 | if (json_typeof(element) != JSON_STRING) |
---|
2069 | return -1; |
---|
2070 | |
---|
2071 | params->wol_type = json_string_value(element); |
---|
2072 | |
---|
2073 | type = json_string_value(element); |
---|
2074 | if (!strcmp(type, "unicast")) |
---|
2075 | params->wol_type = "2"; |
---|
2076 | else if (!strcmp(type, "broadcast")) |
---|
2077 | params->wol_type = "1"; |
---|
2078 | |
---|
2079 | params->flags |= OG_REST_PARAM_WOL_TYPE; |
---|
2080 | |
---|
2081 | return 0; |
---|
2082 | } |
---|
2083 | |
---|
2084 | static int og_cmd_wol(json_t *element, struct og_msg_params *params) |
---|
2085 | { |
---|
2086 | const char *key; |
---|
2087 | json_t *value; |
---|
2088 | int err = 0; |
---|
2089 | |
---|
2090 | if (json_typeof(element) != JSON_OBJECT) |
---|
2091 | return -1; |
---|
2092 | |
---|
2093 | json_object_foreach(element, key, value) { |
---|
2094 | if (!strcmp(key, "clients")) { |
---|
2095 | err = og_json_parse_targets(value, params); |
---|
2096 | } else if (!strcmp(key, "type")) { |
---|
2097 | err = og_json_parse_type(value, params); |
---|
2098 | } |
---|
2099 | |
---|
2100 | if (err < 0) |
---|
2101 | break; |
---|
2102 | } |
---|
2103 | |
---|
2104 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2105 | OG_REST_PARAM_MAC | |
---|
2106 | OG_REST_PARAM_WOL_TYPE)) |
---|
2107 | return -1; |
---|
2108 | |
---|
2109 | if (!Levanta((char **)params->ips_array, (char **)params->mac_array, |
---|
2110 | params->ips_array_len, (char *)params->wol_type)) |
---|
2111 | return -1; |
---|
2112 | |
---|
2113 | return 0; |
---|
2114 | } |
---|
2115 | |
---|
2116 | static int og_json_parse_run(json_t *element, struct og_msg_params *params) |
---|
2117 | { |
---|
2118 | if (json_typeof(element) != JSON_STRING) |
---|
2119 | return -1; |
---|
2120 | |
---|
2121 | snprintf(params->run_cmd, sizeof(params->run_cmd), "%s", |
---|
2122 | json_string_value(element)); |
---|
2123 | |
---|
2124 | params->flags |= OG_REST_PARAM_RUN_CMD; |
---|
2125 | |
---|
2126 | return 0; |
---|
2127 | } |
---|
2128 | |
---|
2129 | static int og_cmd_run_post(json_t *element, struct og_msg_params *params) |
---|
2130 | { |
---|
2131 | json_t *value, *clients; |
---|
2132 | const char *key; |
---|
2133 | unsigned int i; |
---|
2134 | int err = 0; |
---|
2135 | |
---|
2136 | if (json_typeof(element) != JSON_OBJECT) |
---|
2137 | return -1; |
---|
2138 | |
---|
2139 | json_object_foreach(element, key, value) { |
---|
2140 | if (!strcmp(key, "clients")) |
---|
2141 | err = og_json_parse_clients(value, params); |
---|
2142 | else if (!strcmp(key, "run")) |
---|
2143 | err = og_json_parse_run(value, params); |
---|
2144 | else if (!strcmp(key, "echo")) { |
---|
2145 | err = og_json_parse_bool(value, ¶ms->echo); |
---|
2146 | params->flags |= OG_REST_PARAM_ECHO; |
---|
2147 | } |
---|
2148 | |
---|
2149 | if (err < 0) |
---|
2150 | break; |
---|
2151 | } |
---|
2152 | |
---|
2153 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2154 | OG_REST_PARAM_RUN_CMD | |
---|
2155 | OG_REST_PARAM_ECHO)) |
---|
2156 | return -1; |
---|
2157 | |
---|
2158 | clients = json_copy(element); |
---|
2159 | json_object_del(clients, "clients"); |
---|
2160 | |
---|
2161 | err = og_send_request(OG_METHOD_POST, OG_CMD_SHELL_RUN, params, clients); |
---|
2162 | if (err < 0) |
---|
2163 | return err; |
---|
2164 | |
---|
2165 | for (i = 0; i < params->ips_array_len; i++) { |
---|
2166 | char filename[4096]; |
---|
2167 | FILE *f; |
---|
2168 | |
---|
2169 | sprintf(filename, "/tmp/_Seconsola_%s", params->ips_array[i]); |
---|
2170 | f = fopen(filename, "wt"); |
---|
2171 | fclose(f); |
---|
2172 | } |
---|
2173 | |
---|
2174 | return 0; |
---|
2175 | } |
---|
2176 | |
---|
2177 | static int og_cmd_run_get(json_t *element, struct og_msg_params *params, |
---|
2178 | char *buffer_reply) |
---|
2179 | { |
---|
2180 | struct og_buffer og_buffer = { |
---|
2181 | .data = buffer_reply, |
---|
2182 | }; |
---|
2183 | json_t *root, *value, *array; |
---|
2184 | const char *key; |
---|
2185 | unsigned int i; |
---|
2186 | int err = 0; |
---|
2187 | |
---|
2188 | if (json_typeof(element) != JSON_OBJECT) |
---|
2189 | return -1; |
---|
2190 | |
---|
2191 | json_object_foreach(element, key, value) { |
---|
2192 | if (!strcmp(key, "clients")) |
---|
2193 | err = og_json_parse_clients(value, params); |
---|
2194 | |
---|
2195 | if (err < 0) |
---|
2196 | return err; |
---|
2197 | } |
---|
2198 | |
---|
2199 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
2200 | return -1; |
---|
2201 | |
---|
2202 | array = json_array(); |
---|
2203 | if (!array) |
---|
2204 | return -1; |
---|
2205 | |
---|
2206 | for (i = 0; i < params->ips_array_len; i++) { |
---|
2207 | json_t *object, *output, *addr; |
---|
2208 | char data[4096] = {}; |
---|
2209 | char filename[4096]; |
---|
2210 | int fd, numbytes; |
---|
2211 | |
---|
2212 | sprintf(filename, "/tmp/_Seconsola_%s", params->ips_array[i]); |
---|
2213 | |
---|
2214 | fd = open(filename, O_RDONLY); |
---|
2215 | if (!fd) |
---|
2216 | return -1; |
---|
2217 | |
---|
2218 | numbytes = read(fd, data, sizeof(data)); |
---|
2219 | if (numbytes < 0) { |
---|
2220 | close(fd); |
---|
2221 | return -1; |
---|
2222 | } |
---|
2223 | data[sizeof(data) - 1] = '\0'; |
---|
2224 | close(fd); |
---|
2225 | |
---|
2226 | object = json_object(); |
---|
2227 | if (!object) { |
---|
2228 | json_decref(array); |
---|
2229 | return -1; |
---|
2230 | } |
---|
2231 | addr = json_string(params->ips_array[i]); |
---|
2232 | if (!addr) { |
---|
2233 | json_decref(object); |
---|
2234 | json_decref(array); |
---|
2235 | return -1; |
---|
2236 | } |
---|
2237 | json_object_set_new(object, "addr", addr); |
---|
2238 | |
---|
2239 | output = json_string(data); |
---|
2240 | if (!output) { |
---|
2241 | json_decref(object); |
---|
2242 | json_decref(array); |
---|
2243 | return -1; |
---|
2244 | } |
---|
2245 | json_object_set_new(object, "output", output); |
---|
2246 | |
---|
2247 | json_array_append_new(array, object); |
---|
2248 | } |
---|
2249 | |
---|
2250 | root = json_pack("{s:o}", "clients", array); |
---|
2251 | if (!root) |
---|
2252 | return -1; |
---|
2253 | |
---|
2254 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
2255 | json_decref(root); |
---|
2256 | |
---|
2257 | return 0; |
---|
2258 | } |
---|
2259 | |
---|
2260 | static int og_cmd_session(json_t *element, struct og_msg_params *params) |
---|
2261 | { |
---|
2262 | json_t *clients, *value; |
---|
2263 | const char *key; |
---|
2264 | int err = 0; |
---|
2265 | |
---|
2266 | if (json_typeof(element) != JSON_OBJECT) |
---|
2267 | return -1; |
---|
2268 | |
---|
2269 | json_object_foreach(element, key, value) { |
---|
2270 | if (!strcmp(key, "clients")) { |
---|
2271 | err = og_json_parse_clients(value, params); |
---|
2272 | } else if (!strcmp(key, "disk")) { |
---|
2273 | err = og_json_parse_string(value, ¶ms->disk); |
---|
2274 | params->flags |= OG_REST_PARAM_DISK; |
---|
2275 | } else if (!strcmp(key, "partition")) { |
---|
2276 | err = og_json_parse_string(value, ¶ms->partition); |
---|
2277 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
2278 | } |
---|
2279 | |
---|
2280 | if (err < 0) |
---|
2281 | return err; |
---|
2282 | } |
---|
2283 | |
---|
2284 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2285 | OG_REST_PARAM_DISK | |
---|
2286 | OG_REST_PARAM_PARTITION)) |
---|
2287 | return -1; |
---|
2288 | |
---|
2289 | clients = json_copy(element); |
---|
2290 | json_object_del(clients, "clients"); |
---|
2291 | |
---|
2292 | return og_send_request(OG_METHOD_POST, OG_CMD_SESSION, params, clients); |
---|
2293 | } |
---|
2294 | |
---|
2295 | static int og_cmd_poweroff(json_t *element, struct og_msg_params *params) |
---|
2296 | { |
---|
2297 | const char *key; |
---|
2298 | json_t *value; |
---|
2299 | int err = 0; |
---|
2300 | |
---|
2301 | if (json_typeof(element) != JSON_OBJECT) |
---|
2302 | return -1; |
---|
2303 | |
---|
2304 | json_object_foreach(element, key, value) { |
---|
2305 | if (!strcmp(key, "clients")) |
---|
2306 | err = og_json_parse_clients(value, params); |
---|
2307 | |
---|
2308 | if (err < 0) |
---|
2309 | break; |
---|
2310 | } |
---|
2311 | |
---|
2312 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
2313 | return -1; |
---|
2314 | |
---|
2315 | return og_send_request(OG_METHOD_POST, OG_CMD_POWEROFF, params, NULL); |
---|
2316 | } |
---|
2317 | |
---|
2318 | static int og_cmd_refresh(json_t *element, struct og_msg_params *params) |
---|
2319 | { |
---|
2320 | const char *key; |
---|
2321 | json_t *value; |
---|
2322 | int err = 0; |
---|
2323 | |
---|
2324 | if (json_typeof(element) != JSON_OBJECT) |
---|
2325 | return -1; |
---|
2326 | |
---|
2327 | json_object_foreach(element, key, value) { |
---|
2328 | if (!strcmp(key, "clients")) |
---|
2329 | err = og_json_parse_clients(value, params); |
---|
2330 | |
---|
2331 | if (err < 0) |
---|
2332 | break; |
---|
2333 | } |
---|
2334 | |
---|
2335 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
2336 | return -1; |
---|
2337 | |
---|
2338 | return og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, params, NULL); |
---|
2339 | } |
---|
2340 | |
---|
2341 | static int og_cmd_reboot(json_t *element, struct og_msg_params *params) |
---|
2342 | { |
---|
2343 | const char *key; |
---|
2344 | json_t *value; |
---|
2345 | int err = 0; |
---|
2346 | |
---|
2347 | if (json_typeof(element) != JSON_OBJECT) |
---|
2348 | return -1; |
---|
2349 | |
---|
2350 | json_object_foreach(element, key, value) { |
---|
2351 | if (!strcmp(key, "clients")) |
---|
2352 | err = og_json_parse_clients(value, params); |
---|
2353 | |
---|
2354 | if (err < 0) |
---|
2355 | break; |
---|
2356 | } |
---|
2357 | |
---|
2358 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
2359 | return -1; |
---|
2360 | |
---|
2361 | return og_send_request(OG_METHOD_POST, OG_CMD_REBOOT, params, NULL); |
---|
2362 | } |
---|
2363 | |
---|
2364 | static int og_cmd_stop(json_t *element, struct og_msg_params *params) |
---|
2365 | { |
---|
2366 | const char *key; |
---|
2367 | json_t *value; |
---|
2368 | int err = 0; |
---|
2369 | |
---|
2370 | if (json_typeof(element) != JSON_OBJECT) |
---|
2371 | return -1; |
---|
2372 | |
---|
2373 | json_object_foreach(element, key, value) { |
---|
2374 | if (!strcmp(key, "clients")) |
---|
2375 | err = og_json_parse_clients(value, params); |
---|
2376 | |
---|
2377 | if (err < 0) |
---|
2378 | break; |
---|
2379 | } |
---|
2380 | |
---|
2381 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
2382 | return -1; |
---|
2383 | |
---|
2384 | return og_send_request(OG_METHOD_POST, OG_CMD_STOP, params, NULL); |
---|
2385 | } |
---|
2386 | |
---|
2387 | static int og_cmd_hardware(json_t *element, struct og_msg_params *params) |
---|
2388 | { |
---|
2389 | const char *key; |
---|
2390 | json_t *value; |
---|
2391 | int err = 0; |
---|
2392 | |
---|
2393 | if (json_typeof(element) != JSON_OBJECT) |
---|
2394 | return -1; |
---|
2395 | |
---|
2396 | json_object_foreach(element, key, value) { |
---|
2397 | if (!strcmp(key, "clients")) |
---|
2398 | err = og_json_parse_clients(value, params); |
---|
2399 | |
---|
2400 | if (err < 0) |
---|
2401 | break; |
---|
2402 | } |
---|
2403 | |
---|
2404 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
2405 | return -1; |
---|
2406 | |
---|
2407 | return og_send_request(OG_METHOD_GET, OG_CMD_HARDWARE, params, NULL); |
---|
2408 | } |
---|
2409 | |
---|
2410 | static int og_cmd_software(json_t *element, struct og_msg_params *params) |
---|
2411 | { |
---|
2412 | json_t *clients, *value; |
---|
2413 | const char *key; |
---|
2414 | int err = 0; |
---|
2415 | |
---|
2416 | if (json_typeof(element) != JSON_OBJECT) |
---|
2417 | return -1; |
---|
2418 | |
---|
2419 | json_object_foreach(element, key, value) { |
---|
2420 | if (!strcmp(key, "clients")) |
---|
2421 | err = og_json_parse_clients(value, params); |
---|
2422 | else if (!strcmp(key, "disk")) { |
---|
2423 | err = og_json_parse_string(value, ¶ms->disk); |
---|
2424 | params->flags |= OG_REST_PARAM_DISK; |
---|
2425 | } |
---|
2426 | else if (!strcmp(key, "partition")) { |
---|
2427 | err = og_json_parse_string(value, ¶ms->partition); |
---|
2428 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
2429 | } |
---|
2430 | |
---|
2431 | if (err < 0) |
---|
2432 | break; |
---|
2433 | } |
---|
2434 | |
---|
2435 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2436 | OG_REST_PARAM_DISK | |
---|
2437 | OG_REST_PARAM_PARTITION)) |
---|
2438 | return -1; |
---|
2439 | |
---|
2440 | clients = json_copy(element); |
---|
2441 | json_object_del(clients, "clients"); |
---|
2442 | |
---|
2443 | return og_send_request(OG_METHOD_POST, OG_CMD_SOFTWARE, params, clients); |
---|
2444 | } |
---|
2445 | |
---|
2446 | static int og_cmd_create_image(json_t *element, struct og_msg_params *params) |
---|
2447 | { |
---|
2448 | json_t *value, *clients; |
---|
2449 | const char *key; |
---|
2450 | int err = 0; |
---|
2451 | |
---|
2452 | if (json_typeof(element) != JSON_OBJECT) |
---|
2453 | return -1; |
---|
2454 | |
---|
2455 | json_object_foreach(element, key, value) { |
---|
2456 | if (!strcmp(key, "disk")) { |
---|
2457 | err = og_json_parse_string(value, ¶ms->disk); |
---|
2458 | params->flags |= OG_REST_PARAM_DISK; |
---|
2459 | } else if (!strcmp(key, "partition")) { |
---|
2460 | err = og_json_parse_string(value, ¶ms->partition); |
---|
2461 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
2462 | } else if (!strcmp(key, "name")) { |
---|
2463 | err = og_json_parse_string(value, ¶ms->name); |
---|
2464 | params->flags |= OG_REST_PARAM_NAME; |
---|
2465 | } else if (!strcmp(key, "repository")) { |
---|
2466 | err = og_json_parse_string(value, ¶ms->repository); |
---|
2467 | params->flags |= OG_REST_PARAM_REPO; |
---|
2468 | } else if (!strcmp(key, "clients")) { |
---|
2469 | err = og_json_parse_clients(value, params); |
---|
2470 | } else if (!strcmp(key, "id")) { |
---|
2471 | err = og_json_parse_string(value, ¶ms->id); |
---|
2472 | params->flags |= OG_REST_PARAM_ID; |
---|
2473 | } else if (!strcmp(key, "code")) { |
---|
2474 | err = og_json_parse_string(value, ¶ms->code); |
---|
2475 | params->flags |= OG_REST_PARAM_CODE; |
---|
2476 | } |
---|
2477 | |
---|
2478 | if (err < 0) |
---|
2479 | break; |
---|
2480 | } |
---|
2481 | |
---|
2482 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2483 | OG_REST_PARAM_DISK | |
---|
2484 | OG_REST_PARAM_PARTITION | |
---|
2485 | OG_REST_PARAM_CODE | |
---|
2486 | OG_REST_PARAM_ID | |
---|
2487 | OG_REST_PARAM_NAME | |
---|
2488 | OG_REST_PARAM_REPO)) |
---|
2489 | return -1; |
---|
2490 | |
---|
2491 | clients = json_copy(element); |
---|
2492 | json_object_del(clients, "clients"); |
---|
2493 | |
---|
2494 | return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_CREATE, params, |
---|
2495 | clients); |
---|
2496 | } |
---|
2497 | |
---|
2498 | static int og_cmd_restore_image(json_t *element, struct og_msg_params *params) |
---|
2499 | { |
---|
2500 | json_t *clients, *value; |
---|
2501 | const char *key; |
---|
2502 | int err = 0; |
---|
2503 | |
---|
2504 | if (json_typeof(element) != JSON_OBJECT) |
---|
2505 | return -1; |
---|
2506 | |
---|
2507 | json_object_foreach(element, key, value) { |
---|
2508 | if (!strcmp(key, "disk")) { |
---|
2509 | err = og_json_parse_string(value, ¶ms->disk); |
---|
2510 | params->flags |= OG_REST_PARAM_DISK; |
---|
2511 | } else if (!strcmp(key, "partition")) { |
---|
2512 | err = og_json_parse_string(value, ¶ms->partition); |
---|
2513 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
2514 | } else if (!strcmp(key, "name")) { |
---|
2515 | err = og_json_parse_string(value, ¶ms->name); |
---|
2516 | params->flags |= OG_REST_PARAM_NAME; |
---|
2517 | } else if (!strcmp(key, "repository")) { |
---|
2518 | err = og_json_parse_string(value, ¶ms->repository); |
---|
2519 | params->flags |= OG_REST_PARAM_REPO; |
---|
2520 | } else if (!strcmp(key, "clients")) { |
---|
2521 | err = og_json_parse_clients(value, params); |
---|
2522 | } else if (!strcmp(key, "type")) { |
---|
2523 | err = og_json_parse_string(value, ¶ms->type); |
---|
2524 | params->flags |= OG_REST_PARAM_TYPE; |
---|
2525 | } else if (!strcmp(key, "profile")) { |
---|
2526 | err = og_json_parse_string(value, ¶ms->profile); |
---|
2527 | params->flags |= OG_REST_PARAM_PROFILE; |
---|
2528 | } else if (!strcmp(key, "id")) { |
---|
2529 | err = og_json_parse_string(value, ¶ms->id); |
---|
2530 | params->flags |= OG_REST_PARAM_ID; |
---|
2531 | } |
---|
2532 | |
---|
2533 | if (err < 0) |
---|
2534 | break; |
---|
2535 | } |
---|
2536 | |
---|
2537 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2538 | OG_REST_PARAM_DISK | |
---|
2539 | OG_REST_PARAM_PARTITION | |
---|
2540 | OG_REST_PARAM_NAME | |
---|
2541 | OG_REST_PARAM_REPO | |
---|
2542 | OG_REST_PARAM_TYPE | |
---|
2543 | OG_REST_PARAM_PROFILE | |
---|
2544 | OG_REST_PARAM_ID)) |
---|
2545 | return -1; |
---|
2546 | |
---|
2547 | clients = json_copy(element); |
---|
2548 | json_object_del(clients, "clients"); |
---|
2549 | |
---|
2550 | return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_RESTORE, params, |
---|
2551 | clients); |
---|
2552 | } |
---|
2553 | |
---|
2554 | static int og_cmd_setup(json_t *element, struct og_msg_params *params) |
---|
2555 | { |
---|
2556 | json_t *value, *clients; |
---|
2557 | const char *key; |
---|
2558 | int err = 0; |
---|
2559 | |
---|
2560 | if (json_typeof(element) != JSON_OBJECT) |
---|
2561 | return -1; |
---|
2562 | |
---|
2563 | json_object_foreach(element, key, value) { |
---|
2564 | if (!strcmp(key, "clients")) { |
---|
2565 | err = og_json_parse_clients(value, params); |
---|
2566 | } else if (!strcmp(key, "disk")) { |
---|
2567 | err = og_json_parse_string(value, ¶ms->disk); |
---|
2568 | params->flags |= OG_REST_PARAM_DISK; |
---|
2569 | } else if (!strcmp(key, "cache")) { |
---|
2570 | err = og_json_parse_string(value, ¶ms->cache); |
---|
2571 | params->flags |= OG_REST_PARAM_CACHE; |
---|
2572 | } else if (!strcmp(key, "cache_size")) { |
---|
2573 | err = og_json_parse_string(value, ¶ms->cache_size); |
---|
2574 | params->flags |= OG_REST_PARAM_CACHE_SIZE; |
---|
2575 | } else if (!strcmp(key, "partition_setup")) { |
---|
2576 | err = og_json_parse_partition_setup(value, params); |
---|
2577 | } |
---|
2578 | |
---|
2579 | if (err < 0) |
---|
2580 | break; |
---|
2581 | } |
---|
2582 | |
---|
2583 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2584 | OG_REST_PARAM_DISK | |
---|
2585 | OG_REST_PARAM_CACHE | |
---|
2586 | OG_REST_PARAM_CACHE_SIZE | |
---|
2587 | OG_REST_PARAM_PART_0 | |
---|
2588 | OG_REST_PARAM_PART_1 | |
---|
2589 | OG_REST_PARAM_PART_2 | |
---|
2590 | OG_REST_PARAM_PART_3)) |
---|
2591 | return -1; |
---|
2592 | |
---|
2593 | clients = json_copy(element); |
---|
2594 | json_object_del(clients, "clients"); |
---|
2595 | |
---|
2596 | return og_send_request(OG_METHOD_POST, OG_CMD_SETUP, params, clients); |
---|
2597 | } |
---|
2598 | |
---|
2599 | static int og_cmd_run_schedule(json_t *element, struct og_msg_params *params) |
---|
2600 | { |
---|
2601 | const char *key; |
---|
2602 | json_t *value; |
---|
2603 | int err = 0; |
---|
2604 | |
---|
2605 | json_object_foreach(element, key, value) { |
---|
2606 | if (!strcmp(key, "clients")) |
---|
2607 | err = og_json_parse_clients(value, params); |
---|
2608 | |
---|
2609 | if (err < 0) |
---|
2610 | break; |
---|
2611 | } |
---|
2612 | |
---|
2613 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
2614 | return -1; |
---|
2615 | |
---|
2616 | return og_send_request(OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, params, |
---|
2617 | NULL); |
---|
2618 | } |
---|
2619 | |
---|
2620 | static int og_cmd_create_basic_image(json_t *element, struct og_msg_params *params) |
---|
2621 | { |
---|
2622 | char buf[4096] = {}; |
---|
2623 | int err = 0, len; |
---|
2624 | const char *key; |
---|
2625 | json_t *value; |
---|
2626 | TRAMA *msg; |
---|
2627 | |
---|
2628 | if (json_typeof(element) != JSON_OBJECT) |
---|
2629 | return -1; |
---|
2630 | |
---|
2631 | json_object_foreach(element, key, value) { |
---|
2632 | if (!strcmp(key, "clients")) { |
---|
2633 | err = og_json_parse_clients(value, params); |
---|
2634 | } else if (!strcmp(key, "disk")) { |
---|
2635 | err = og_json_parse_string(value, ¶ms->disk); |
---|
2636 | params->flags |= OG_REST_PARAM_DISK; |
---|
2637 | } else if (!strcmp(key, "partition")) { |
---|
2638 | err = og_json_parse_string(value, ¶ms->partition); |
---|
2639 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
2640 | } else if (!strcmp(key, "code")) { |
---|
2641 | err = og_json_parse_string(value, ¶ms->code); |
---|
2642 | params->flags |= OG_REST_PARAM_CODE; |
---|
2643 | } else if (!strcmp(key, "id")) { |
---|
2644 | err = og_json_parse_string(value, ¶ms->id); |
---|
2645 | params->flags |= OG_REST_PARAM_ID; |
---|
2646 | } else if (!strcmp(key, "name")) { |
---|
2647 | err = og_json_parse_string(value, ¶ms->name); |
---|
2648 | params->flags |= OG_REST_PARAM_NAME; |
---|
2649 | } else if (!strcmp(key, "repository")) { |
---|
2650 | err = og_json_parse_string(value, ¶ms->repository); |
---|
2651 | params->flags |= OG_REST_PARAM_REPO; |
---|
2652 | } else if (!strcmp(key, "sync_params")) { |
---|
2653 | err = og_json_parse_sync_params(value, params); |
---|
2654 | } |
---|
2655 | |
---|
2656 | if (err < 0) |
---|
2657 | break; |
---|
2658 | } |
---|
2659 | |
---|
2660 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2661 | OG_REST_PARAM_DISK | |
---|
2662 | OG_REST_PARAM_PARTITION | |
---|
2663 | OG_REST_PARAM_CODE | |
---|
2664 | OG_REST_PARAM_ID | |
---|
2665 | OG_REST_PARAM_NAME | |
---|
2666 | OG_REST_PARAM_REPO | |
---|
2667 | OG_REST_PARAM_SYNC_SYNC | |
---|
2668 | OG_REST_PARAM_SYNC_DIFF | |
---|
2669 | OG_REST_PARAM_SYNC_REMOVE | |
---|
2670 | OG_REST_PARAM_SYNC_COMPRESS | |
---|
2671 | OG_REST_PARAM_SYNC_CLEANUP | |
---|
2672 | OG_REST_PARAM_SYNC_CACHE | |
---|
2673 | OG_REST_PARAM_SYNC_CLEANUP_CACHE | |
---|
2674 | OG_REST_PARAM_SYNC_REMOVE_DST)) |
---|
2675 | return -1; |
---|
2676 | |
---|
2677 | len = snprintf(buf, sizeof(buf), |
---|
2678 | "nfn=CrearImagenBasica\rdsk=%s\rpar=%s\rcpt=%s\ridi=%s\r" |
---|
2679 | "nci=%s\ripr=%s\rrti=\rmsy=%s\rwhl=%s\reli=%s\rcmp=%s\rbpi=%s\r" |
---|
2680 | "cpc=%s\rbpc=%s\rnba=%s\r", |
---|
2681 | params->disk, params->partition, params->code, params->id, |
---|
2682 | params->name, params->repository, params->sync_setup.sync, |
---|
2683 | params->sync_setup.diff, params->sync_setup.remove, |
---|
2684 | params->sync_setup.compress, params->sync_setup.cleanup, |
---|
2685 | params->sync_setup.cache, params->sync_setup.cleanup_cache, |
---|
2686 | params->sync_setup.remove_dst); |
---|
2687 | |
---|
2688 | msg = og_msg_alloc(buf, len); |
---|
2689 | if (!msg) |
---|
2690 | return -1; |
---|
2691 | |
---|
2692 | og_send_cmd((char **)params->ips_array, params->ips_array_len, |
---|
2693 | CLIENTE_OCUPADO, msg); |
---|
2694 | |
---|
2695 | og_msg_free(msg); |
---|
2696 | |
---|
2697 | return 0; |
---|
2698 | } |
---|
2699 | |
---|
2700 | static int og_cmd_create_incremental_image(json_t *element, struct og_msg_params *params) |
---|
2701 | { |
---|
2702 | char buf[4096] = {}; |
---|
2703 | int err = 0, len; |
---|
2704 | const char *key; |
---|
2705 | json_t *value; |
---|
2706 | TRAMA *msg; |
---|
2707 | |
---|
2708 | if (json_typeof(element) != JSON_OBJECT) |
---|
2709 | return -1; |
---|
2710 | |
---|
2711 | json_object_foreach(element, key, value) { |
---|
2712 | if (!strcmp(key, "clients")) |
---|
2713 | err = og_json_parse_clients(value, params); |
---|
2714 | else if (!strcmp(key, "disk")) { |
---|
2715 | err = og_json_parse_string(value, ¶ms->disk); |
---|
2716 | params->flags |= OG_REST_PARAM_DISK; |
---|
2717 | } else if (!strcmp(key, "partition")) { |
---|
2718 | err = og_json_parse_string(value, ¶ms->partition); |
---|
2719 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
2720 | } else if (!strcmp(key, "id")) { |
---|
2721 | err = og_json_parse_string(value, ¶ms->id); |
---|
2722 | params->flags |= OG_REST_PARAM_ID; |
---|
2723 | } else if (!strcmp(key, "name")) { |
---|
2724 | err = og_json_parse_string(value, ¶ms->name); |
---|
2725 | params->flags |= OG_REST_PARAM_NAME; |
---|
2726 | } else if (!strcmp(key, "repository")) { |
---|
2727 | err = og_json_parse_string(value, ¶ms->repository); |
---|
2728 | params->flags |= OG_REST_PARAM_REPO; |
---|
2729 | } else if (!strcmp(key, "sync_params")) { |
---|
2730 | err = og_json_parse_sync_params(value, params); |
---|
2731 | } |
---|
2732 | |
---|
2733 | if (err < 0) |
---|
2734 | break; |
---|
2735 | } |
---|
2736 | |
---|
2737 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2738 | OG_REST_PARAM_DISK | |
---|
2739 | OG_REST_PARAM_PARTITION | |
---|
2740 | OG_REST_PARAM_ID | |
---|
2741 | OG_REST_PARAM_NAME | |
---|
2742 | OG_REST_PARAM_REPO | |
---|
2743 | OG_REST_PARAM_SYNC_SYNC | |
---|
2744 | OG_REST_PARAM_SYNC_PATH | |
---|
2745 | OG_REST_PARAM_SYNC_DIFF | |
---|
2746 | OG_REST_PARAM_SYNC_DIFF_ID | |
---|
2747 | OG_REST_PARAM_SYNC_DIFF_NAME | |
---|
2748 | OG_REST_PARAM_SYNC_REMOVE | |
---|
2749 | OG_REST_PARAM_SYNC_COMPRESS | |
---|
2750 | OG_REST_PARAM_SYNC_CLEANUP | |
---|
2751 | OG_REST_PARAM_SYNC_CACHE | |
---|
2752 | OG_REST_PARAM_SYNC_CLEANUP_CACHE | |
---|
2753 | OG_REST_PARAM_SYNC_REMOVE_DST)) |
---|
2754 | return -1; |
---|
2755 | |
---|
2756 | len = snprintf(buf, sizeof(buf), |
---|
2757 | "nfn=CrearSoftIncremental\rdsk=%s\rpar=%s\ridi=%s\rnci=%s\r" |
---|
2758 | "rti=%s\ripr=%s\ridf=%s\rncf=%s\rmsy=%s\rwhl=%s\reli=%s\rcmp=%s\r" |
---|
2759 | "bpi=%s\rcpc=%s\rbpc=%s\rnba=%s\r", |
---|
2760 | params->disk, params->partition, params->id, params->name, |
---|
2761 | params->sync_setup.path, params->repository, params->sync_setup.diff_id, |
---|
2762 | params->sync_setup.diff_name, params->sync_setup.sync, |
---|
2763 | params->sync_setup.diff, params->sync_setup.remove_dst, |
---|
2764 | params->sync_setup.compress, params->sync_setup.cleanup, |
---|
2765 | params->sync_setup.cache, params->sync_setup.cleanup_cache, |
---|
2766 | params->sync_setup.remove_dst); |
---|
2767 | |
---|
2768 | msg = og_msg_alloc(buf, len); |
---|
2769 | if (!msg) |
---|
2770 | return -1; |
---|
2771 | |
---|
2772 | og_send_cmd((char **)params->ips_array, params->ips_array_len, |
---|
2773 | CLIENTE_OCUPADO, msg); |
---|
2774 | |
---|
2775 | og_msg_free(msg); |
---|
2776 | |
---|
2777 | return 0; |
---|
2778 | } |
---|
2779 | |
---|
2780 | static int og_cmd_restore_basic_image(json_t *element, struct og_msg_params *params) |
---|
2781 | { |
---|
2782 | char buf[4096] = {}; |
---|
2783 | int err = 0, len; |
---|
2784 | const char *key; |
---|
2785 | json_t *value; |
---|
2786 | TRAMA *msg; |
---|
2787 | |
---|
2788 | if (json_typeof(element) != JSON_OBJECT) |
---|
2789 | return -1; |
---|
2790 | |
---|
2791 | json_object_foreach(element, key, value) { |
---|
2792 | if (!strcmp(key, "clients")) { |
---|
2793 | err = og_json_parse_clients(value, params); |
---|
2794 | } else if (!strcmp(key, "disk")) { |
---|
2795 | err = og_json_parse_string(value, ¶ms->disk); |
---|
2796 | params->flags |= OG_REST_PARAM_DISK; |
---|
2797 | } else if (!strcmp(key, "partition")) { |
---|
2798 | err = og_json_parse_string(value, ¶ms->partition); |
---|
2799 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
2800 | } else if (!strcmp(key, "id")) { |
---|
2801 | err = og_json_parse_string(value, ¶ms->id); |
---|
2802 | params->flags |= OG_REST_PARAM_ID; |
---|
2803 | } else if (!strcmp(key, "name")) { |
---|
2804 | err = og_json_parse_string(value, ¶ms->name); |
---|
2805 | params->flags |= OG_REST_PARAM_NAME; |
---|
2806 | } else if (!strcmp(key, "repository")) { |
---|
2807 | err = og_json_parse_string(value, ¶ms->repository); |
---|
2808 | params->flags |= OG_REST_PARAM_REPO; |
---|
2809 | } else if (!strcmp(key, "profile")) { |
---|
2810 | err = og_json_parse_string(value, ¶ms->profile); |
---|
2811 | params->flags |= OG_REST_PARAM_PROFILE; |
---|
2812 | } else if (!strcmp(key, "type")) { |
---|
2813 | err = og_json_parse_string(value, ¶ms->type); |
---|
2814 | params->flags |= OG_REST_PARAM_TYPE; |
---|
2815 | } else if (!strcmp(key, "sync_params")) { |
---|
2816 | err = og_json_parse_sync_params(value, params); |
---|
2817 | } |
---|
2818 | |
---|
2819 | if (err < 0) |
---|
2820 | break; |
---|
2821 | } |
---|
2822 | |
---|
2823 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2824 | OG_REST_PARAM_DISK | |
---|
2825 | OG_REST_PARAM_PARTITION | |
---|
2826 | OG_REST_PARAM_ID | |
---|
2827 | OG_REST_PARAM_NAME | |
---|
2828 | OG_REST_PARAM_REPO | |
---|
2829 | OG_REST_PARAM_PROFILE | |
---|
2830 | OG_REST_PARAM_TYPE | |
---|
2831 | OG_REST_PARAM_SYNC_PATH | |
---|
2832 | OG_REST_PARAM_SYNC_METHOD | |
---|
2833 | OG_REST_PARAM_SYNC_SYNC | |
---|
2834 | OG_REST_PARAM_SYNC_DIFF | |
---|
2835 | OG_REST_PARAM_SYNC_REMOVE | |
---|
2836 | OG_REST_PARAM_SYNC_COMPRESS | |
---|
2837 | OG_REST_PARAM_SYNC_CLEANUP | |
---|
2838 | OG_REST_PARAM_SYNC_CACHE | |
---|
2839 | OG_REST_PARAM_SYNC_CLEANUP_CACHE | |
---|
2840 | OG_REST_PARAM_SYNC_REMOVE_DST)) |
---|
2841 | return -1; |
---|
2842 | |
---|
2843 | len = snprintf(buf, sizeof(buf), |
---|
2844 | "nfn=RestaurarImagenBasica\rdsk=%s\rpar=%s\ridi=%s\rnci=%s\r" |
---|
2845 | "ipr=%s\rifs=%s\rrti=%s\rmet=%s\rmsy=%s\rtpt=%s\rwhl=%s\r" |
---|
2846 | "eli=%s\rcmp=%s\rbpi=%s\rcpc=%s\rbpc=%s\rnba=%s\r", |
---|
2847 | params->disk, params->partition, params->id, params->name, |
---|
2848 | params->repository, params->profile, params->sync_setup.path, |
---|
2849 | params->sync_setup.method, params->sync_setup.sync, params->type, |
---|
2850 | params->sync_setup.diff, params->sync_setup.remove, |
---|
2851 | params->sync_setup.compress, params->sync_setup.cleanup, |
---|
2852 | params->sync_setup.cache, params->sync_setup.cleanup_cache, |
---|
2853 | params->sync_setup.remove_dst); |
---|
2854 | |
---|
2855 | msg = og_msg_alloc(buf, len); |
---|
2856 | if (!msg) |
---|
2857 | return -1; |
---|
2858 | |
---|
2859 | og_send_cmd((char **)params->ips_array, params->ips_array_len, |
---|
2860 | CLIENTE_OCUPADO, msg); |
---|
2861 | |
---|
2862 | og_msg_free(msg); |
---|
2863 | |
---|
2864 | return 0; |
---|
2865 | } |
---|
2866 | |
---|
2867 | static int og_cmd_restore_incremental_image(json_t *element, struct og_msg_params *params) |
---|
2868 | { |
---|
2869 | char buf[4096] = {}; |
---|
2870 | int err = 0, len; |
---|
2871 | const char *key; |
---|
2872 | json_t *value; |
---|
2873 | TRAMA *msg; |
---|
2874 | |
---|
2875 | if (json_typeof(element) != JSON_OBJECT) |
---|
2876 | return -1; |
---|
2877 | |
---|
2878 | json_object_foreach(element, key, value) { |
---|
2879 | if (!strcmp(key, "clients")) { |
---|
2880 | err = og_json_parse_clients(value, params); |
---|
2881 | } else if (!strcmp(key, "disk")) { |
---|
2882 | err = og_json_parse_string(value, ¶ms->disk); |
---|
2883 | params->flags |= OG_REST_PARAM_DISK; |
---|
2884 | } else if (!strcmp(key, "partition")) { |
---|
2885 | err = og_json_parse_string(value, ¶ms->partition); |
---|
2886 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
2887 | } else if (!strcmp(key, "id")) { |
---|
2888 | err = og_json_parse_string(value, ¶ms->id); |
---|
2889 | params->flags |= OG_REST_PARAM_ID; |
---|
2890 | } else if (!strcmp(key, "name")) { |
---|
2891 | err = og_json_parse_string(value, ¶ms->name); |
---|
2892 | params->flags |= OG_REST_PARAM_NAME; |
---|
2893 | } else if (!strcmp(key, "repository")) { |
---|
2894 | err = og_json_parse_string(value, ¶ms->repository); |
---|
2895 | params->flags |= OG_REST_PARAM_REPO; |
---|
2896 | } else if (!strcmp(key, "profile")) { |
---|
2897 | err = og_json_parse_string(value, ¶ms->profile); |
---|
2898 | params->flags |= OG_REST_PARAM_PROFILE; |
---|
2899 | } else if (!strcmp(key, "type")) { |
---|
2900 | err = og_json_parse_string(value, ¶ms->type); |
---|
2901 | params->flags |= OG_REST_PARAM_TYPE; |
---|
2902 | } else if (!strcmp(key, "sync_params")) { |
---|
2903 | err = og_json_parse_sync_params(value, params); |
---|
2904 | } |
---|
2905 | |
---|
2906 | if (err < 0) |
---|
2907 | break; |
---|
2908 | } |
---|
2909 | |
---|
2910 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2911 | OG_REST_PARAM_DISK | |
---|
2912 | OG_REST_PARAM_PARTITION | |
---|
2913 | OG_REST_PARAM_ID | |
---|
2914 | OG_REST_PARAM_NAME | |
---|
2915 | OG_REST_PARAM_REPO | |
---|
2916 | OG_REST_PARAM_PROFILE | |
---|
2917 | OG_REST_PARAM_TYPE | |
---|
2918 | OG_REST_PARAM_SYNC_DIFF_ID | |
---|
2919 | OG_REST_PARAM_SYNC_DIFF_NAME | |
---|
2920 | OG_REST_PARAM_SYNC_PATH | |
---|
2921 | OG_REST_PARAM_SYNC_METHOD | |
---|
2922 | OG_REST_PARAM_SYNC_SYNC | |
---|
2923 | OG_REST_PARAM_SYNC_DIFF | |
---|
2924 | OG_REST_PARAM_SYNC_REMOVE | |
---|
2925 | OG_REST_PARAM_SYNC_COMPRESS | |
---|
2926 | OG_REST_PARAM_SYNC_CLEANUP | |
---|
2927 | OG_REST_PARAM_SYNC_CACHE | |
---|
2928 | OG_REST_PARAM_SYNC_CLEANUP_CACHE | |
---|
2929 | OG_REST_PARAM_SYNC_REMOVE_DST)) |
---|
2930 | return -1; |
---|
2931 | |
---|
2932 | len = snprintf(buf, sizeof(buf), |
---|
2933 | "nfn=RestaurarSoftIncremental\rdsk=%s\rpar=%s\ridi=%s\rnci=%s\r" |
---|
2934 | "ipr=%s\rifs=%s\ridf=%s\rncf=%s\rrti=%s\rmet=%s\rmsy=%s\r" |
---|
2935 | "tpt=%s\rwhl=%s\reli=%s\rcmp=%s\rbpi=%s\rcpc=%s\rbpc=%s\r" |
---|
2936 | "nba=%s\r", |
---|
2937 | params->disk, params->partition, params->id, params->name, |
---|
2938 | params->repository, params->profile, params->sync_setup.diff_id, |
---|
2939 | params->sync_setup.diff_name, params->sync_setup.path, |
---|
2940 | params->sync_setup.method, params->sync_setup.sync, params->type, |
---|
2941 | params->sync_setup.diff, params->sync_setup.remove, |
---|
2942 | params->sync_setup.compress, params->sync_setup.cleanup, |
---|
2943 | params->sync_setup.cache, params->sync_setup.cleanup_cache, |
---|
2944 | params->sync_setup.remove_dst); |
---|
2945 | |
---|
2946 | msg = og_msg_alloc(buf, len); |
---|
2947 | if (!msg) |
---|
2948 | return -1; |
---|
2949 | |
---|
2950 | og_send_cmd((char **)params->ips_array, params->ips_array_len, |
---|
2951 | CLIENTE_OCUPADO, msg); |
---|
2952 | |
---|
2953 | og_msg_free(msg); |
---|
2954 | |
---|
2955 | return 0; |
---|
2956 | } |
---|
2957 | |
---|
2958 | struct og_cmd { |
---|
2959 | uint32_t id; |
---|
2960 | struct list_head list; |
---|
2961 | uint32_t client_id; |
---|
2962 | const char *ip; |
---|
2963 | const char *mac; |
---|
2964 | enum og_cmd_type type; |
---|
2965 | enum og_rest_method method; |
---|
2966 | struct og_msg_params params; |
---|
2967 | json_t *json; |
---|
2968 | }; |
---|
2969 | |
---|
2970 | static LIST_HEAD(cmd_list); |
---|
2971 | |
---|
2972 | static const struct og_cmd *og_cmd_find(const char *client_ip) |
---|
2973 | { |
---|
2974 | struct og_cmd *cmd, *next; |
---|
2975 | |
---|
2976 | list_for_each_entry_safe(cmd, next, &cmd_list, list) { |
---|
2977 | if (strcmp(cmd->ip, client_ip)) |
---|
2978 | continue; |
---|
2979 | |
---|
2980 | list_del(&cmd->list); |
---|
2981 | return cmd; |
---|
2982 | } |
---|
2983 | |
---|
2984 | return NULL; |
---|
2985 | } |
---|
2986 | |
---|
2987 | static void og_cmd_free(const struct og_cmd *cmd) |
---|
2988 | { |
---|
2989 | struct og_msg_params *params = (struct og_msg_params *)&cmd->params; |
---|
2990 | int i; |
---|
2991 | |
---|
2992 | for (i = 0; i < params->ips_array_len; i++) { |
---|
2993 | free((void *)params->ips_array[i]); |
---|
2994 | free((void *)params->mac_array[i]); |
---|
2995 | } |
---|
2996 | free((void *)params->wol_type); |
---|
2997 | |
---|
2998 | if (cmd->json) |
---|
2999 | json_decref(cmd->json); |
---|
3000 | |
---|
3001 | free((void *)cmd->ip); |
---|
3002 | free((void *)cmd->mac); |
---|
3003 | free((void *)cmd); |
---|
3004 | } |
---|
3005 | |
---|
3006 | static void og_cmd_init(struct og_cmd *cmd, enum og_rest_method method, |
---|
3007 | enum og_cmd_type type, json_t *root) |
---|
3008 | { |
---|
3009 | cmd->type = type; |
---|
3010 | cmd->method = method; |
---|
3011 | cmd->params.ips_array[0] = strdup(cmd->ip); |
---|
3012 | cmd->params.ips_array_len = 1; |
---|
3013 | cmd->json = root; |
---|
3014 | } |
---|
3015 | |
---|
3016 | static int og_cmd_legacy_wol(const char *input, struct og_cmd *cmd) |
---|
3017 | { |
---|
3018 | char wol_type[2] = {}; |
---|
3019 | |
---|
3020 | if (sscanf(input, "mar=%s", wol_type) != 1) { |
---|
3021 | syslog(LOG_ERR, "malformed database legacy input\n"); |
---|
3022 | return -1; |
---|
3023 | } |
---|
3024 | |
---|
3025 | og_cmd_init(cmd, OG_METHOD_NO_HTTP, OG_CMD_WOL, NULL); |
---|
3026 | cmd->params.mac_array[0] = strdup(cmd->mac); |
---|
3027 | cmd->params.wol_type = strdup(wol_type); |
---|
3028 | |
---|
3029 | return 0; |
---|
3030 | } |
---|
3031 | |
---|
3032 | static int og_cmd_legacy_shell_run(const char *input, struct og_cmd *cmd) |
---|
3033 | { |
---|
3034 | json_t *root, *script, *echo; |
---|
3035 | |
---|
3036 | script = json_string(input + 4); |
---|
3037 | echo = json_boolean(false); |
---|
3038 | |
---|
3039 | root = json_object(); |
---|
3040 | if (!root) |
---|
3041 | return -1; |
---|
3042 | json_object_set_new(root, "run", script); |
---|
3043 | json_object_set_new(root, "echo", echo); |
---|
3044 | |
---|
3045 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_SHELL_RUN, root); |
---|
3046 | |
---|
3047 | return 0; |
---|
3048 | } |
---|
3049 | |
---|
3050 | #define OG_DB_SMALLINT_MAXLEN 6 |
---|
3051 | |
---|
3052 | static int og_cmd_legacy_session(const char *input, struct og_cmd *cmd) |
---|
3053 | { |
---|
3054 | char part_str[OG_DB_SMALLINT_MAXLEN + 1]; |
---|
3055 | char disk_str[OG_DB_SMALLINT_MAXLEN + 1]; |
---|
3056 | json_t *root, *disk, *partition; |
---|
3057 | |
---|
3058 | if (sscanf(input, "dsk=%s\rpar=%s\r", disk_str, part_str) != 2) |
---|
3059 | return -1; |
---|
3060 | partition = json_string(part_str); |
---|
3061 | disk = json_string(disk_str); |
---|
3062 | |
---|
3063 | root = json_object(); |
---|
3064 | if (!root) |
---|
3065 | return -1; |
---|
3066 | json_object_set_new(root, "partition", partition); |
---|
3067 | json_object_set_new(root, "disk", disk); |
---|
3068 | |
---|
3069 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_SESSION, root); |
---|
3070 | |
---|
3071 | return 0; |
---|
3072 | } |
---|
3073 | |
---|
3074 | static int og_cmd_legacy_poweroff(const char *input, struct og_cmd *cmd) |
---|
3075 | { |
---|
3076 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_POWEROFF, NULL); |
---|
3077 | |
---|
3078 | return 0; |
---|
3079 | } |
---|
3080 | |
---|
3081 | static int og_cmd_legacy_refresh(const char *input, struct og_cmd *cmd) |
---|
3082 | { |
---|
3083 | og_cmd_init(cmd, OG_METHOD_GET, OG_CMD_REFRESH, NULL); |
---|
3084 | |
---|
3085 | return 0; |
---|
3086 | } |
---|
3087 | |
---|
3088 | static int og_cmd_legacy_reboot(const char *input, struct og_cmd *cmd) |
---|
3089 | { |
---|
3090 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_REBOOT, NULL); |
---|
3091 | |
---|
3092 | return 0; |
---|
3093 | } |
---|
3094 | |
---|
3095 | static int og_cmd_legacy_stop(const char *input, struct og_cmd *cmd) |
---|
3096 | { |
---|
3097 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_STOP, NULL); |
---|
3098 | |
---|
3099 | return 0; |
---|
3100 | } |
---|
3101 | |
---|
3102 | static int og_cmd_legacy_hardware(const char *input, struct og_cmd *cmd) |
---|
3103 | { |
---|
3104 | og_cmd_init(cmd, OG_METHOD_GET, OG_CMD_HARDWARE, NULL); |
---|
3105 | |
---|
3106 | return 0; |
---|
3107 | } |
---|
3108 | |
---|
3109 | static int og_cmd_legacy_software(const char *input, struct og_cmd *cmd) |
---|
3110 | { |
---|
3111 | og_cmd_init(cmd, OG_METHOD_GET, OG_CMD_SOFTWARE, NULL); |
---|
3112 | |
---|
3113 | return 0; |
---|
3114 | } |
---|
3115 | |
---|
3116 | #define OG_DB_IMAGE_NAME_MAXLEN 50 |
---|
3117 | #define OG_DB_FILESYSTEM_MAXLEN 16 |
---|
3118 | #define OG_DB_INT8_MAXLEN 8 |
---|
3119 | #define OG_DB_INT_MAXLEN 11 |
---|
3120 | #define OG_DB_IP_MAXLEN 15 |
---|
3121 | |
---|
3122 | struct og_image_legacy { |
---|
3123 | char software_id[OG_DB_INT_MAXLEN + 1]; |
---|
3124 | char image_id[OG_DB_INT_MAXLEN + 1]; |
---|
3125 | char name[OG_DB_IMAGE_NAME_MAXLEN + 1]; |
---|
3126 | char repo[OG_DB_IP_MAXLEN + 1]; |
---|
3127 | char part[OG_DB_SMALLINT_MAXLEN + 1]; |
---|
3128 | char disk[OG_DB_SMALLINT_MAXLEN + 1]; |
---|
3129 | char code[OG_DB_INT8_MAXLEN + 1]; |
---|
3130 | }; |
---|
3131 | |
---|
3132 | struct og_legacy_partition { |
---|
3133 | char partition[OG_DB_SMALLINT_MAXLEN + 1]; |
---|
3134 | char code[OG_DB_INT8_MAXLEN + 1]; |
---|
3135 | char size[OG_DB_INT_MAXLEN + 1]; |
---|
3136 | char filesystem[OG_DB_FILESYSTEM_MAXLEN + 1]; |
---|
3137 | char format[2]; /* Format is a boolean 0 or 1 => length is 2 */ |
---|
3138 | }; |
---|
3139 | |
---|
3140 | static int og_cmd_legacy_image_create(const char *input, struct og_cmd *cmd) |
---|
3141 | { |
---|
3142 | json_t *root, *disk, *partition, *code, *image_id, *name, *repo; |
---|
3143 | struct og_image_legacy img = {}; |
---|
3144 | |
---|
3145 | if (sscanf(input, "dsk=%s\rpar=%s\rcpt=%s\ridi=%s\rnci=%s\ripr=%s\r", |
---|
3146 | img.disk, img.part, img.code, img.image_id, img.name, |
---|
3147 | img.repo) != 6) |
---|
3148 | return -1; |
---|
3149 | image_id = json_string(img.image_id); |
---|
3150 | partition = json_string(img.part); |
---|
3151 | code = json_string(img.code); |
---|
3152 | name = json_string(img.name); |
---|
3153 | repo = json_string(img.repo); |
---|
3154 | disk = json_string(img.disk); |
---|
3155 | |
---|
3156 | root = json_object(); |
---|
3157 | if (!root) |
---|
3158 | return -1; |
---|
3159 | json_object_set_new(root, "partition", partition); |
---|
3160 | json_object_set_new(root, "repository", repo); |
---|
3161 | json_object_set_new(root, "id", image_id); |
---|
3162 | json_object_set_new(root, "code", code); |
---|
3163 | json_object_set_new(root, "name", name); |
---|
3164 | json_object_set_new(root, "disk", disk); |
---|
3165 | |
---|
3166 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_IMAGE_CREATE, root); |
---|
3167 | |
---|
3168 | return 0; |
---|
3169 | } |
---|
3170 | |
---|
3171 | #define OG_DB_RESTORE_TYPE_MAXLEN 64 |
---|
3172 | |
---|
3173 | static int og_cmd_legacy_image_restore(const char *input, struct og_cmd *cmd) |
---|
3174 | { |
---|
3175 | json_t *root, *disk, *partition, *image_id, *name, *repo; |
---|
3176 | char restore_type_str[OG_DB_RESTORE_TYPE_MAXLEN + 1] = {}; |
---|
3177 | char software_id_str[OG_DB_INT_MAXLEN + 1] = {}; |
---|
3178 | json_t *software_id, *restore_type; |
---|
3179 | struct og_image_legacy img = {}; |
---|
3180 | |
---|
3181 | if (sscanf(input, |
---|
3182 | "dsk=%s\rpar=%s\ridi=%s\rnci=%s\ripr=%s\rifs=%s\rptc=%s\r", |
---|
3183 | img.disk, img.part, img.image_id, img.name, img.repo, |
---|
3184 | software_id_str, restore_type_str) != 7) |
---|
3185 | return -1; |
---|
3186 | |
---|
3187 | restore_type = json_string(restore_type_str); |
---|
3188 | software_id = json_string(software_id_str); |
---|
3189 | image_id = json_string(img.image_id); |
---|
3190 | partition = json_string(img.part); |
---|
3191 | name = json_string(img.name); |
---|
3192 | repo = json_string(img.repo); |
---|
3193 | disk = json_string(img.disk); |
---|
3194 | |
---|
3195 | root = json_object(); |
---|
3196 | if (!root) |
---|
3197 | return -1; |
---|
3198 | json_object_set_new(root, "profile", software_id); |
---|
3199 | json_object_set_new(root, "partition", partition); |
---|
3200 | json_object_set_new(root, "type", restore_type); |
---|
3201 | json_object_set_new(root, "repository", repo); |
---|
3202 | json_object_set_new(root, "id", image_id); |
---|
3203 | json_object_set_new(root, "name", name); |
---|
3204 | json_object_set_new(root, "disk", disk); |
---|
3205 | |
---|
3206 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_IMAGE_RESTORE, root); |
---|
3207 | |
---|
3208 | return 0; |
---|
3209 | } |
---|
3210 | |
---|
3211 | static int og_cmd_legacy_setup(const char *input, struct og_cmd *cmd) |
---|
3212 | { |
---|
3213 | json_t *root, *disk, *cache, *cache_size, *partition_setup, *object; |
---|
3214 | struct og_legacy_partition part_cfg[OG_PARTITION_MAX] = {}; |
---|
3215 | char cache_size_str [OG_DB_INT_MAXLEN + 1]; |
---|
3216 | char disk_str [OG_DB_SMALLINT_MAXLEN + 1]; |
---|
3217 | json_t *part, *code, *fs, *size, *format; |
---|
3218 | unsigned int partition_len = 0; |
---|
3219 | const char *in_ptr; |
---|
3220 | char cache_str[2]; |
---|
3221 | |
---|
3222 | if (sscanf(input, "dsk=%s\rcfg=dis=%*[^*]*che=%[^*]*tch=%[^!]!", |
---|
3223 | disk_str, cache_str, cache_size_str) != 3) |
---|
3224 | return -1; |
---|
3225 | |
---|
3226 | in_ptr = strstr(input, "!") + 1; |
---|
3227 | while (strlen(in_ptr) > 0) { |
---|
3228 | if(sscanf(in_ptr, |
---|
3229 | "par=%[^*]*cpt=%[^*]*sfi=%[^*]*tam=%[^*]*ope=%[^%%]%%", |
---|
3230 | part_cfg[partition_len].partition, |
---|
3231 | part_cfg[partition_len].code, |
---|
3232 | part_cfg[partition_len].filesystem, |
---|
3233 | part_cfg[partition_len].size, |
---|
3234 | part_cfg[partition_len].format) != 5) |
---|
3235 | return -1; |
---|
3236 | in_ptr = strstr(in_ptr, "%") + 1; |
---|
3237 | partition_len++; |
---|
3238 | } |
---|
3239 | |
---|
3240 | root = json_object(); |
---|
3241 | if (!root) |
---|
3242 | return -1; |
---|
3243 | |
---|
3244 | cache_size = json_string(cache_size_str); |
---|
3245 | cache = json_string(cache_str); |
---|
3246 | partition_setup = json_array(); |
---|
3247 | disk = json_string(disk_str); |
---|
3248 | |
---|
3249 | for (unsigned int i = 0; i < partition_len; ++i) { |
---|
3250 | object = json_object(); |
---|
3251 | if (!object) { |
---|
3252 | json_decref(root); |
---|
3253 | return -1; |
---|
3254 | } |
---|
3255 | |
---|
3256 | part = json_string(part_cfg[i].partition); |
---|
3257 | fs = json_string(part_cfg[i].filesystem); |
---|
3258 | format = json_string(part_cfg[i].format); |
---|
3259 | code = json_string(part_cfg[i].code); |
---|
3260 | size = json_string(part_cfg[i].size); |
---|
3261 | |
---|
3262 | json_object_set_new(object, "partition", part); |
---|
3263 | json_object_set_new(object, "filesystem", fs); |
---|
3264 | json_object_set_new(object, "format", format); |
---|
3265 | json_object_set_new(object, "code", code); |
---|
3266 | json_object_set_new(object, "size", size); |
---|
3267 | |
---|
3268 | json_array_append_new(partition_setup, object); |
---|
3269 | } |
---|
3270 | |
---|
3271 | json_object_set_new(root, "partition_setup", partition_setup); |
---|
3272 | json_object_set_new(root, "cache_size", cache_size); |
---|
3273 | json_object_set_new(root, "cache", cache); |
---|
3274 | json_object_set_new(root, "disk", disk); |
---|
3275 | |
---|
3276 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_SETUP, root); |
---|
3277 | |
---|
3278 | return 0; |
---|
3279 | } |
---|
3280 | |
---|
3281 | static int og_cmd_legacy_run_schedule(const char *input, struct og_cmd *cmd) |
---|
3282 | { |
---|
3283 | og_cmd_init(cmd, OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, NULL); |
---|
3284 | |
---|
3285 | return 0; |
---|
3286 | } |
---|
3287 | |
---|
3288 | static int og_cmd_legacy(const char *input, struct og_cmd *cmd) |
---|
3289 | { |
---|
3290 | char legacy_cmd[32] = {}; |
---|
3291 | int err = -1; |
---|
3292 | |
---|
3293 | if (sscanf(input, "nfn=%31s\r", legacy_cmd) != 1) { |
---|
3294 | syslog(LOG_ERR, "malformed database legacy input\n"); |
---|
3295 | return -1; |
---|
3296 | } |
---|
3297 | input = strchr(input, '\r') + 1; |
---|
3298 | |
---|
3299 | if (!strcmp(legacy_cmd, "Arrancar")) { |
---|
3300 | err = og_cmd_legacy_wol(input, cmd); |
---|
3301 | } else if (!strcmp(legacy_cmd, "EjecutarScript")) { |
---|
3302 | err = og_cmd_legacy_shell_run(input, cmd); |
---|
3303 | } else if (!strcmp(legacy_cmd, "IniciarSesion")) { |
---|
3304 | err = og_cmd_legacy_session(input, cmd); |
---|
3305 | } else if (!strcmp(legacy_cmd, "Apagar")) { |
---|
3306 | err = og_cmd_legacy_poweroff(input, cmd); |
---|
3307 | } else if (!strcmp(legacy_cmd, "Actualizar")) { |
---|
3308 | err = og_cmd_legacy_refresh(input, cmd); |
---|
3309 | } else if (!strcmp(legacy_cmd, "Reiniciar")) { |
---|
3310 | err = og_cmd_legacy_reboot(input, cmd); |
---|
3311 | } else if (!strcmp(legacy_cmd, "Purgar")) { |
---|
3312 | err = og_cmd_legacy_stop(input, cmd); |
---|
3313 | } else if (!strcmp(legacy_cmd, "InventarioHardware")) { |
---|
3314 | err = og_cmd_legacy_hardware(input, cmd); |
---|
3315 | } else if (!strcmp(legacy_cmd, "InventarioSoftware")) { |
---|
3316 | err = og_cmd_legacy_software(input, cmd); |
---|
3317 | } else if (!strcmp(legacy_cmd, "CrearImagen")) { |
---|
3318 | err = og_cmd_legacy_image_create(input, cmd); |
---|
3319 | } else if (!strcmp(legacy_cmd, "RestaurarImagen")) { |
---|
3320 | err = og_cmd_legacy_image_restore(input, cmd); |
---|
3321 | } else if (!strcmp(legacy_cmd, "Configurar")) { |
---|
3322 | err = og_cmd_legacy_setup(input, cmd); |
---|
3323 | } else if (!strcmp(legacy_cmd, "EjecutaComandosPendientes") || |
---|
3324 | !strcmp(legacy_cmd, "Actualizar")) { |
---|
3325 | err = og_cmd_legacy_run_schedule(input, cmd); |
---|
3326 | } |
---|
3327 | |
---|
3328 | return err; |
---|
3329 | } |
---|
3330 | |
---|
3331 | static int og_dbi_add_action(const struct og_dbi *dbi, const struct og_task *task, |
---|
3332 | struct og_cmd *cmd) |
---|
3333 | { |
---|
3334 | char start_date_string[24]; |
---|
3335 | struct tm *start_date; |
---|
3336 | const char *msglog; |
---|
3337 | dbi_result result; |
---|
3338 | time_t now; |
---|
3339 | |
---|
3340 | time(&now); |
---|
3341 | start_date = localtime(&now); |
---|
3342 | |
---|
3343 | sprintf(start_date_string, "%hu/%hhu/%hhu %hhu:%hhu:%hhu", |
---|
3344 | start_date->tm_year + 1900, start_date->tm_mon + 1, |
---|
3345 | start_date->tm_mday, start_date->tm_hour, start_date->tm_min, |
---|
3346 | start_date->tm_sec); |
---|
3347 | result = dbi_conn_queryf(dbi->conn, |
---|
3348 | "INSERT INTO acciones (idordenador, " |
---|
3349 | "tipoaccion, idtipoaccion, descriaccion, ip, " |
---|
3350 | "sesion, idcomando, parametros, fechahorareg, " |
---|
3351 | "estado, resultado, ambito, idambito, " |
---|
3352 | "restrambito, idprocedimiento, idcentro, " |
---|
3353 | "idprogramacion) " |
---|
3354 | "VALUES (%d, %d, %d, '%s', '%s', %d, %d, '%s', " |
---|
3355 | "'%s', %d, %d, %d, %d, '%s', %d, %d, %d)", |
---|
3356 | cmd->client_id, EJECUCION_TAREA, task->task_id, |
---|
3357 | "", cmd->ip, 0, task->command_id, |
---|
3358 | task->params, start_date_string, |
---|
3359 | ACCION_INICIADA, ACCION_SINRESULTADO, |
---|
3360 | task->type_scope, task->scope, "", |
---|
3361 | task->procedure_id, task->center_id, |
---|
3362 | task->schedule_id); |
---|
3363 | if (!result) { |
---|
3364 | dbi_conn_error(dbi->conn, &msglog); |
---|
3365 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3366 | __func__, __LINE__, msglog); |
---|
3367 | return -1; |
---|
3368 | } |
---|
3369 | cmd->id = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
3370 | dbi_result_free(result); |
---|
3371 | |
---|
3372 | return 0; |
---|
3373 | } |
---|
3374 | |
---|
3375 | static int og_queue_task_command(struct og_dbi *dbi, const struct og_task *task, |
---|
3376 | char *query) |
---|
3377 | { |
---|
3378 | struct og_cmd *cmd; |
---|
3379 | const char *msglog; |
---|
3380 | dbi_result result; |
---|
3381 | |
---|
3382 | result = dbi_conn_queryf(dbi->conn, query); |
---|
3383 | if (!result) { |
---|
3384 | dbi_conn_error(dbi->conn, &msglog); |
---|
3385 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3386 | __func__, __LINE__, msglog); |
---|
3387 | return -1; |
---|
3388 | } |
---|
3389 | |
---|
3390 | while (dbi_result_next_row(result)) { |
---|
3391 | cmd = (struct og_cmd *)calloc(1, sizeof(struct og_cmd)); |
---|
3392 | if (!cmd) { |
---|
3393 | dbi_result_free(result); |
---|
3394 | return -1; |
---|
3395 | } |
---|
3396 | |
---|
3397 | cmd->client_id = dbi_result_get_uint(result, "idordenador"); |
---|
3398 | cmd->ip = strdup(dbi_result_get_string(result, "ip")); |
---|
3399 | cmd->mac = strdup(dbi_result_get_string(result, "mac")); |
---|
3400 | og_cmd_legacy(task->params, cmd); |
---|
3401 | |
---|
3402 | if (og_dbi_add_action(dbi, task, cmd)) { |
---|
3403 | dbi_result_free(result); |
---|
3404 | return -1; |
---|
3405 | } |
---|
3406 | |
---|
3407 | list_add_tail(&cmd->list, &cmd_list); |
---|
3408 | } |
---|
3409 | |
---|
3410 | dbi_result_free(result); |
---|
3411 | |
---|
3412 | return 0; |
---|
3413 | } |
---|
3414 | |
---|
3415 | static int og_queue_task_group_clients(struct og_dbi *dbi, struct og_task *task, |
---|
3416 | char *query) |
---|
3417 | { |
---|
3418 | |
---|
3419 | const char *msglog; |
---|
3420 | dbi_result result; |
---|
3421 | |
---|
3422 | result = dbi_conn_queryf(dbi->conn, query); |
---|
3423 | if (!result) { |
---|
3424 | dbi_conn_error(dbi->conn, &msglog); |
---|
3425 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3426 | __func__, __LINE__, msglog); |
---|
3427 | return -1; |
---|
3428 | } |
---|
3429 | |
---|
3430 | while (dbi_result_next_row(result)) { |
---|
3431 | uint32_t group_id = dbi_result_get_uint(result, "idgrupo"); |
---|
3432 | |
---|
3433 | sprintf(query, "SELECT idgrupo FROM gruposordenadores " |
---|
3434 | "WHERE grupoid=%d", group_id); |
---|
3435 | if (og_queue_task_group_clients(dbi, task, query)) { |
---|
3436 | dbi_result_free(result); |
---|
3437 | return -1; |
---|
3438 | } |
---|
3439 | |
---|
3440 | sprintf(query,"SELECT ip, mac, idordenador FROM ordenadores " |
---|
3441 | "WHERE grupoid=%d", group_id); |
---|
3442 | if (og_queue_task_command(dbi, task, query)) { |
---|
3443 | dbi_result_free(result); |
---|
3444 | return -1; |
---|
3445 | } |
---|
3446 | |
---|
3447 | } |
---|
3448 | |
---|
3449 | dbi_result_free(result); |
---|
3450 | |
---|
3451 | return 0; |
---|
3452 | } |
---|
3453 | |
---|
3454 | static int og_queue_task_group_classrooms(struct og_dbi *dbi, |
---|
3455 | struct og_task *task, char *query) |
---|
3456 | { |
---|
3457 | |
---|
3458 | const char *msglog; |
---|
3459 | dbi_result result; |
---|
3460 | |
---|
3461 | result = dbi_conn_queryf(dbi->conn, query); |
---|
3462 | if (!result) { |
---|
3463 | dbi_conn_error(dbi->conn, &msglog); |
---|
3464 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3465 | __func__, __LINE__, msglog); |
---|
3466 | return -1; |
---|
3467 | } |
---|
3468 | |
---|
3469 | while (dbi_result_next_row(result)) { |
---|
3470 | uint32_t group_id = dbi_result_get_uint(result, "idgrupo"); |
---|
3471 | |
---|
3472 | sprintf(query, "SELECT idgrupo FROM grupos " |
---|
3473 | "WHERE grupoid=%d AND tipo=%d", group_id, AMBITO_GRUPOSAULAS); |
---|
3474 | if (og_queue_task_group_classrooms(dbi, task, query)) { |
---|
3475 | dbi_result_free(result); |
---|
3476 | return -1; |
---|
3477 | } |
---|
3478 | |
---|
3479 | sprintf(query, |
---|
3480 | "SELECT ip,mac,idordenador " |
---|
3481 | "FROM ordenadores INNER JOIN aulas " |
---|
3482 | "WHERE ordenadores.idaula=aulas.idaula " |
---|
3483 | "AND aulas.grupoid=%d", |
---|
3484 | group_id); |
---|
3485 | if (og_queue_task_command(dbi, task, query)) { |
---|
3486 | dbi_result_free(result); |
---|
3487 | return -1; |
---|
3488 | } |
---|
3489 | |
---|
3490 | } |
---|
3491 | |
---|
3492 | dbi_result_free(result); |
---|
3493 | |
---|
3494 | return 0; |
---|
3495 | } |
---|
3496 | |
---|
3497 | static int og_queue_task_clients(struct og_dbi *dbi, struct og_task *task) |
---|
3498 | { |
---|
3499 | char query[4096]; |
---|
3500 | |
---|
3501 | switch (task->type_scope) { |
---|
3502 | case AMBITO_CENTROS: |
---|
3503 | sprintf(query, |
---|
3504 | "SELECT ip,mac,idordenador " |
---|
3505 | "FROM ordenadores INNER JOIN aulas " |
---|
3506 | "WHERE ordenadores.idaula=aulas.idaula " |
---|
3507 | "AND idcentro=%d", |
---|
3508 | task->scope); |
---|
3509 | return og_queue_task_command(dbi, task, query); |
---|
3510 | case AMBITO_GRUPOSAULAS: |
---|
3511 | sprintf(query, |
---|
3512 | "SELECT idgrupo FROM grupos " |
---|
3513 | "WHERE idgrupo=%i AND tipo=%d", |
---|
3514 | task->scope, AMBITO_GRUPOSAULAS); |
---|
3515 | return og_queue_task_group_classrooms(dbi, task, query); |
---|
3516 | case AMBITO_AULAS: |
---|
3517 | sprintf(query, |
---|
3518 | "SELECT ip,mac,idordenador FROM ordenadores " |
---|
3519 | "WHERE idaula=%d", |
---|
3520 | task->scope); |
---|
3521 | return og_queue_task_command(dbi, task, query); |
---|
3522 | case AMBITO_GRUPOSORDENADORES: |
---|
3523 | sprintf(query, |
---|
3524 | "SELECT idgrupo FROM gruposordenadores " |
---|
3525 | "WHERE idgrupo = %d", |
---|
3526 | task->scope); |
---|
3527 | return og_queue_task_group_clients(dbi, task, query); |
---|
3528 | case AMBITO_ORDENADORES: |
---|
3529 | sprintf(query, |
---|
3530 | "SELECT ip, mac, idordenador FROM ordenadores " |
---|
3531 | "WHERE idordenador = %d", |
---|
3532 | task->scope); |
---|
3533 | return og_queue_task_command(dbi, task, query); |
---|
3534 | } |
---|
3535 | return 0; |
---|
3536 | } |
---|
3537 | |
---|
3538 | static int og_dbi_queue_procedure(struct og_dbi *dbi, struct og_task *task) |
---|
3539 | { |
---|
3540 | uint32_t procedure_id; |
---|
3541 | const char *msglog; |
---|
3542 | dbi_result result; |
---|
3543 | |
---|
3544 | result = dbi_conn_queryf(dbi->conn, |
---|
3545 | "SELECT parametros, procedimientoid, idcomando " |
---|
3546 | "FROM procedimientos_acciones " |
---|
3547 | "WHERE idprocedimiento=%d ORDER BY orden", task->procedure_id); |
---|
3548 | if (!result) { |
---|
3549 | dbi_conn_error(dbi->conn, &msglog); |
---|
3550 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3551 | __func__, __LINE__, msglog); |
---|
3552 | return -1; |
---|
3553 | } |
---|
3554 | |
---|
3555 | while (dbi_result_next_row(result)) { |
---|
3556 | procedure_id = dbi_result_get_uint(result, "procedimientoid"); |
---|
3557 | if (procedure_id > 0) { |
---|
3558 | task->procedure_id = procedure_id; |
---|
3559 | if (og_dbi_queue_procedure(dbi, task)) |
---|
3560 | return -1; |
---|
3561 | continue; |
---|
3562 | } |
---|
3563 | |
---|
3564 | task->params = strdup(dbi_result_get_string(result, "parametros")); |
---|
3565 | task->command_id = dbi_result_get_uint(result, "idcomando"); |
---|
3566 | if (og_queue_task_clients(dbi, task)) |
---|
3567 | return -1; |
---|
3568 | } |
---|
3569 | |
---|
3570 | dbi_result_free(result); |
---|
3571 | |
---|
3572 | return 0; |
---|
3573 | } |
---|
3574 | |
---|
3575 | static int og_dbi_queue_task(struct og_dbi *dbi, uint32_t task_id, |
---|
3576 | uint32_t schedule_id) |
---|
3577 | { |
---|
3578 | struct og_task task = {}; |
---|
3579 | uint32_t task_id_next; |
---|
3580 | struct og_cmd *cmd; |
---|
3581 | const char *msglog; |
---|
3582 | dbi_result result; |
---|
3583 | |
---|
3584 | task.schedule_id = schedule_id; |
---|
3585 | |
---|
3586 | result = dbi_conn_queryf(dbi->conn, |
---|
3587 | "SELECT tareas_acciones.orden, " |
---|
3588 | "tareas_acciones.idprocedimiento, " |
---|
3589 | "tareas_acciones.tareaid, " |
---|
3590 | "tareas.idtarea, " |
---|
3591 | "tareas.idcentro, " |
---|
3592 | "tareas.ambito, " |
---|
3593 | "tareas.idambito, " |
---|
3594 | "tareas.restrambito " |
---|
3595 | " FROM tareas" |
---|
3596 | " INNER JOIN tareas_acciones ON tareas_acciones.idtarea=tareas.idtarea" |
---|
3597 | " WHERE tareas_acciones.idtarea=%u ORDER BY tareas_acciones.orden ASC", task_id); |
---|
3598 | if (!result) { |
---|
3599 | dbi_conn_error(dbi->conn, &msglog); |
---|
3600 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3601 | __func__, __LINE__, msglog); |
---|
3602 | return -1; |
---|
3603 | } |
---|
3604 | |
---|
3605 | while (dbi_result_next_row(result)) { |
---|
3606 | task_id_next = dbi_result_get_uint(result, "tareaid"); |
---|
3607 | |
---|
3608 | if (task_id_next > 0) { |
---|
3609 | if (og_dbi_queue_task(dbi, task_id_next, schedule_id)) |
---|
3610 | return -1; |
---|
3611 | |
---|
3612 | continue; |
---|
3613 | } |
---|
3614 | task.task_id = dbi_result_get_uint(result, "idtarea"); |
---|
3615 | task.center_id = dbi_result_get_uint(result, "idcentro"); |
---|
3616 | task.procedure_id = dbi_result_get_uint(result, "idprocedimiento"); |
---|
3617 | task.type_scope = dbi_result_get_uint(result, "ambito"); |
---|
3618 | task.scope = dbi_result_get_uint(result, "idambito"); |
---|
3619 | task.filtered_scope = dbi_result_get_string(result, "restrambito"); |
---|
3620 | |
---|
3621 | og_dbi_queue_procedure(dbi, &task); |
---|
3622 | } |
---|
3623 | |
---|
3624 | dbi_result_free(result); |
---|
3625 | |
---|
3626 | list_for_each_entry(cmd, &cmd_list, list) { |
---|
3627 | if (cmd->type != OG_CMD_WOL) |
---|
3628 | continue; |
---|
3629 | |
---|
3630 | if (!Levanta((char **)cmd->params.ips_array, |
---|
3631 | (char **)cmd->params.mac_array, |
---|
3632 | cmd->params.ips_array_len, |
---|
3633 | (char *)cmd->params.wol_type)) |
---|
3634 | return -1; |
---|
3635 | } |
---|
3636 | |
---|
3637 | return 0; |
---|
3638 | } |
---|
3639 | |
---|
3640 | void og_dbi_schedule_task(unsigned int task_id, unsigned int schedule_id) |
---|
3641 | { |
---|
3642 | struct og_msg_params params = {}; |
---|
3643 | bool duplicated = false; |
---|
3644 | struct og_cmd *cmd; |
---|
3645 | struct og_dbi *dbi; |
---|
3646 | unsigned int i; |
---|
3647 | |
---|
3648 | dbi = og_dbi_open(&dbi_config); |
---|
3649 | if (!dbi) { |
---|
3650 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
3651 | __func__, __LINE__); |
---|
3652 | return; |
---|
3653 | } |
---|
3654 | og_dbi_queue_task(dbi, task_id, schedule_id); |
---|
3655 | og_dbi_close(dbi); |
---|
3656 | |
---|
3657 | list_for_each_entry(cmd, &cmd_list, list) { |
---|
3658 | for (i = 0; i < params.ips_array_len; i++) { |
---|
3659 | if (!strncmp(cmd->ip, params.ips_array[i], |
---|
3660 | OG_DB_IP_MAXLEN)) { |
---|
3661 | duplicated = true; |
---|
3662 | break; |
---|
3663 | } |
---|
3664 | } |
---|
3665 | |
---|
3666 | if (!duplicated) |
---|
3667 | params.ips_array[params.ips_array_len++] = cmd->ip; |
---|
3668 | else |
---|
3669 | duplicated = false; |
---|
3670 | } |
---|
3671 | |
---|
3672 | og_send_request(OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, ¶ms, NULL); |
---|
3673 | } |
---|
3674 | |
---|
3675 | static int og_cmd_task_post(json_t *element, struct og_msg_params *params) |
---|
3676 | { |
---|
3677 | struct og_cmd *cmd; |
---|
3678 | struct og_dbi *dbi; |
---|
3679 | const char *key; |
---|
3680 | json_t *value; |
---|
3681 | int err; |
---|
3682 | |
---|
3683 | if (json_typeof(element) != JSON_OBJECT) |
---|
3684 | return -1; |
---|
3685 | |
---|
3686 | json_object_foreach(element, key, value) { |
---|
3687 | if (!strcmp(key, "task")) { |
---|
3688 | err = og_json_parse_string(value, ¶ms->task_id); |
---|
3689 | params->flags |= OG_REST_PARAM_TASK; |
---|
3690 | } |
---|
3691 | |
---|
3692 | if (err < 0) |
---|
3693 | break; |
---|
3694 | } |
---|
3695 | |
---|
3696 | if (!og_msg_params_validate(params, OG_REST_PARAM_TASK)) |
---|
3697 | return -1; |
---|
3698 | |
---|
3699 | dbi = og_dbi_open(&dbi_config); |
---|
3700 | if (!dbi) { |
---|
3701 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
3702 | __func__, __LINE__); |
---|
3703 | return -1; |
---|
3704 | } |
---|
3705 | |
---|
3706 | og_dbi_queue_task(dbi, atoi(params->task_id), 0); |
---|
3707 | og_dbi_close(dbi); |
---|
3708 | |
---|
3709 | list_for_each_entry(cmd, &cmd_list, list) |
---|
3710 | params->ips_array[params->ips_array_len++] = cmd->ip; |
---|
3711 | |
---|
3712 | return og_send_request(OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, params, |
---|
3713 | NULL); |
---|
3714 | } |
---|
3715 | |
---|
3716 | static int og_dbi_schedule_get(void) |
---|
3717 | { |
---|
3718 | uint32_t schedule_id, task_id; |
---|
3719 | struct og_schedule_time time; |
---|
3720 | struct og_dbi *dbi; |
---|
3721 | const char *msglog; |
---|
3722 | dbi_result result; |
---|
3723 | |
---|
3724 | dbi = og_dbi_open(&dbi_config); |
---|
3725 | if (!dbi) { |
---|
3726 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
3727 | __func__, __LINE__); |
---|
3728 | return -1; |
---|
3729 | } |
---|
3730 | |
---|
3731 | result = dbi_conn_queryf(dbi->conn, |
---|
3732 | "SELECT idprogramacion, tipoaccion, identificador, " |
---|
3733 | "sesion, annos, meses, diario, dias, semanas, horas, " |
---|
3734 | "ampm, minutos FROM programaciones " |
---|
3735 | "WHERE suspendida = 0"); |
---|
3736 | if (!result) { |
---|
3737 | dbi_conn_error(dbi->conn, &msglog); |
---|
3738 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3739 | __func__, __LINE__, msglog); |
---|
3740 | og_dbi_close(dbi); |
---|
3741 | return -1; |
---|
3742 | } |
---|
3743 | |
---|
3744 | while (dbi_result_next_row(result)) { |
---|
3745 | memset(&time, 0, sizeof(time)); |
---|
3746 | schedule_id = dbi_result_get_uint(result, "idprogramacion"); |
---|
3747 | task_id = dbi_result_get_uint(result, "identificador"); |
---|
3748 | time.years = dbi_result_get_uint(result, "annos"); |
---|
3749 | time.months = dbi_result_get_uint(result, "meses"); |
---|
3750 | time.weeks = dbi_result_get_uint(result, "semanas"); |
---|
3751 | time.week_days = dbi_result_get_uint(result, "dias"); |
---|
3752 | time.days = dbi_result_get_uint(result, "diario"); |
---|
3753 | time.hours = dbi_result_get_uint(result, "horas"); |
---|
3754 | time.am_pm = dbi_result_get_uint(result, "ampm"); |
---|
3755 | time.minutes = dbi_result_get_uint(result, "minutos"); |
---|
3756 | |
---|
3757 | og_schedule_create(schedule_id, task_id, &time); |
---|
3758 | } |
---|
3759 | |
---|
3760 | dbi_result_free(result); |
---|
3761 | og_dbi_close(dbi); |
---|
3762 | |
---|
3763 | return 0; |
---|
3764 | } |
---|
3765 | |
---|
3766 | static int og_dbi_schedule_create(struct og_dbi *dbi, |
---|
3767 | struct og_msg_params *params, |
---|
3768 | uint32_t *schedule_id) |
---|
3769 | { |
---|
3770 | const char *msglog; |
---|
3771 | dbi_result result; |
---|
3772 | uint8_t suspended = 0; |
---|
3773 | uint8_t type = 3; |
---|
3774 | |
---|
3775 | result = dbi_conn_queryf(dbi->conn, |
---|
3776 | "INSERT INTO programaciones (tipoaccion," |
---|
3777 | " identificador, nombrebloque, annos, meses," |
---|
3778 | " semanas, dias, diario, horas, ampm, minutos," |
---|
3779 | " suspendida) VALUES (%d, %s, '%s', %d, %d," |
---|
3780 | " %d, %d, %d, %d, %d, %d, %d)", type, |
---|
3781 | params->task_id, params->name, |
---|
3782 | params->time.years, params->time.months, |
---|
3783 | params->time.weeks, params->time.week_days, |
---|
3784 | params->time.days, params->time.hours, |
---|
3785 | params->time.am_pm, params->time.minutes, |
---|
3786 | suspended); |
---|
3787 | if (!result) { |
---|
3788 | dbi_conn_error(dbi->conn, &msglog); |
---|
3789 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3790 | __func__, __LINE__, msglog); |
---|
3791 | return -1; |
---|
3792 | } |
---|
3793 | dbi_result_free(result); |
---|
3794 | |
---|
3795 | *schedule_id = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
3796 | |
---|
3797 | return 0; |
---|
3798 | } |
---|
3799 | |
---|
3800 | static int og_dbi_schedule_update(struct og_dbi *dbi, |
---|
3801 | struct og_msg_params *params) |
---|
3802 | { |
---|
3803 | const char *msglog; |
---|
3804 | dbi_result result; |
---|
3805 | uint8_t type = 3; |
---|
3806 | |
---|
3807 | result = dbi_conn_queryf(dbi->conn, |
---|
3808 | "UPDATE programaciones SET tipoaccion=%d, " |
---|
3809 | "identificador='%s', nombrebloque='%s', " |
---|
3810 | "annos=%d, meses=%d, " |
---|
3811 | "diario=%d, horas=%d, ampm=%d, minutos=%d " |
---|
3812 | "WHERE idprogramacion='%s'", |
---|
3813 | type, params->task_id, params->name, |
---|
3814 | params->time.years, params->time.months, |
---|
3815 | params->time.days, params->time.hours, |
---|
3816 | params->time.am_pm, params->time.minutes, |
---|
3817 | params->id); |
---|
3818 | |
---|
3819 | if (!result) { |
---|
3820 | dbi_conn_error(dbi->conn, &msglog); |
---|
3821 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3822 | __func__, __LINE__, msglog); |
---|
3823 | return -1; |
---|
3824 | } |
---|
3825 | dbi_result_free(result); |
---|
3826 | |
---|
3827 | return 0; |
---|
3828 | } |
---|
3829 | |
---|
3830 | static int og_dbi_schedule_delete(struct og_dbi *dbi, uint32_t id) |
---|
3831 | { |
---|
3832 | const char *msglog; |
---|
3833 | dbi_result result; |
---|
3834 | |
---|
3835 | result = dbi_conn_queryf(dbi->conn, |
---|
3836 | "DELETE FROM programaciones WHERE idprogramacion=%d", |
---|
3837 | id); |
---|
3838 | if (!result) { |
---|
3839 | dbi_conn_error(dbi->conn, &msglog); |
---|
3840 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3841 | __func__, __LINE__, msglog); |
---|
3842 | return -1; |
---|
3843 | } |
---|
3844 | dbi_result_free(result); |
---|
3845 | |
---|
3846 | return 0; |
---|
3847 | } |
---|
3848 | |
---|
3849 | struct og_db_schedule { |
---|
3850 | uint32_t id; |
---|
3851 | uint32_t task_id; |
---|
3852 | const char *name; |
---|
3853 | struct og_schedule_time time; |
---|
3854 | uint32_t week_days; |
---|
3855 | uint32_t weeks; |
---|
3856 | uint32_t suspended; |
---|
3857 | uint32_t session; |
---|
3858 | }; |
---|
3859 | |
---|
3860 | static int og_dbi_schedule_get_json(struct og_dbi *dbi, json_t *root, |
---|
3861 | const char *task_id, const char *schedule_id) |
---|
3862 | { |
---|
3863 | struct og_db_schedule schedule; |
---|
3864 | json_t *obj, *array; |
---|
3865 | const char *msglog; |
---|
3866 | dbi_result result; |
---|
3867 | int err = 0; |
---|
3868 | |
---|
3869 | if (task_id) { |
---|
3870 | result = dbi_conn_queryf(dbi->conn, |
---|
3871 | "SELECT idprogramacion," |
---|
3872 | " identificador, nombrebloque," |
---|
3873 | " annos, meses, diario, dias," |
---|
3874 | " semanas, horas, ampm," |
---|
3875 | " minutos,suspendida, sesion " |
---|
3876 | "FROM programaciones " |
---|
3877 | "WHERE identificador=%d", |
---|
3878 | atoi(task_id)); |
---|
3879 | } else if (schedule_id) { |
---|
3880 | result = dbi_conn_queryf(dbi->conn, |
---|
3881 | "SELECT idprogramacion," |
---|
3882 | " identificador, nombrebloque," |
---|
3883 | " annos, meses, diario, dias," |
---|
3884 | " semanas, horas, ampm," |
---|
3885 | " minutos,suspendida, sesion " |
---|
3886 | "FROM programaciones " |
---|
3887 | "WHERE idprogramacion=%d", |
---|
3888 | atoi(schedule_id)); |
---|
3889 | } else { |
---|
3890 | result = dbi_conn_queryf(dbi->conn, |
---|
3891 | "SELECT idprogramacion," |
---|
3892 | " identificador, nombrebloque," |
---|
3893 | " annos, meses, diario, dias," |
---|
3894 | " semanas, horas, ampm," |
---|
3895 | " minutos,suspendida, sesion " |
---|
3896 | "FROM programaciones"); |
---|
3897 | } |
---|
3898 | |
---|
3899 | if (!result) { |
---|
3900 | dbi_conn_error(dbi->conn, &msglog); |
---|
3901 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3902 | __func__, __LINE__, msglog); |
---|
3903 | return -1; |
---|
3904 | } |
---|
3905 | |
---|
3906 | array = json_array(); |
---|
3907 | if (!array) |
---|
3908 | return -1; |
---|
3909 | |
---|
3910 | while (dbi_result_next_row(result)) { |
---|
3911 | schedule.id = dbi_result_get_uint(result, "idprogramacion"); |
---|
3912 | schedule.task_id = dbi_result_get_uint(result, "identificador"); |
---|
3913 | schedule.name = dbi_result_get_string(result, "nombrebloque"); |
---|
3914 | schedule.time.years = dbi_result_get_uint(result, "annos"); |
---|
3915 | schedule.time.months = dbi_result_get_uint(result, "meses"); |
---|
3916 | schedule.time.days = dbi_result_get_uint(result, "diario"); |
---|
3917 | schedule.time.hours = dbi_result_get_uint(result, "horas"); |
---|
3918 | schedule.time.am_pm = dbi_result_get_uint(result, "ampm"); |
---|
3919 | schedule.time.minutes = dbi_result_get_uint(result, "minutos"); |
---|
3920 | schedule.week_days = dbi_result_get_uint(result, "dias"); |
---|
3921 | schedule.weeks = dbi_result_get_uint(result, "semanas"); |
---|
3922 | schedule.suspended = dbi_result_get_uint(result, "suspendida"); |
---|
3923 | schedule.session = dbi_result_get_uint(result, "sesion"); |
---|
3924 | |
---|
3925 | obj = json_object(); |
---|
3926 | if (!obj) { |
---|
3927 | err = -1; |
---|
3928 | break; |
---|
3929 | } |
---|
3930 | json_object_set_new(obj, "id", json_integer(schedule.id)); |
---|
3931 | json_object_set_new(obj, "task", json_integer(schedule.task_id)); |
---|
3932 | json_object_set_new(obj, "name", json_string(schedule.name)); |
---|
3933 | json_object_set_new(obj, "years", json_integer(schedule.time.years)); |
---|
3934 | json_object_set_new(obj, "months", json_integer(schedule.time.months)); |
---|
3935 | json_object_set_new(obj, "days", json_integer(schedule.time.days)); |
---|
3936 | json_object_set_new(obj, "hours", json_integer(schedule.time.hours)); |
---|
3937 | json_object_set_new(obj, "am_pm", json_integer(schedule.time.am_pm)); |
---|
3938 | json_object_set_new(obj, "minutes", json_integer(schedule.time.minutes)); |
---|
3939 | json_object_set_new(obj, "week_days", json_integer(schedule.week_days)); |
---|
3940 | json_object_set_new(obj, "weeks", json_integer(schedule.weeks)); |
---|
3941 | json_object_set_new(obj, "suspended", json_integer(schedule.suspended)); |
---|
3942 | json_object_set_new(obj, "session", json_integer(schedule.session)); |
---|
3943 | |
---|
3944 | json_array_append_new(array, obj); |
---|
3945 | } |
---|
3946 | |
---|
3947 | json_object_set_new(root, "schedule", array); |
---|
3948 | |
---|
3949 | dbi_result_free(result); |
---|
3950 | |
---|
3951 | return err; |
---|
3952 | } |
---|
3953 | |
---|
3954 | static struct ev_loop *og_loop; |
---|
3955 | |
---|
3956 | static int og_cmd_schedule_create(json_t *element, struct og_msg_params *params) |
---|
3957 | { |
---|
3958 | uint32_t schedule_id; |
---|
3959 | struct og_dbi *dbi; |
---|
3960 | const char *key; |
---|
3961 | json_t *value; |
---|
3962 | int err; |
---|
3963 | |
---|
3964 | if (json_typeof(element) != JSON_OBJECT) |
---|
3965 | return -1; |
---|
3966 | |
---|
3967 | json_object_foreach(element, key, value) { |
---|
3968 | if (!strcmp(key, "task")) { |
---|
3969 | err = og_json_parse_string(value, ¶ms->task_id); |
---|
3970 | params->flags |= OG_REST_PARAM_TASK; |
---|
3971 | } else if (!strcmp(key, "name")) { |
---|
3972 | err = og_json_parse_string(value, ¶ms->name); |
---|
3973 | params->flags |= OG_REST_PARAM_NAME; |
---|
3974 | } else if (!strcmp(key, "when")) |
---|
3975 | err = og_json_parse_time_params(value, params); |
---|
3976 | |
---|
3977 | if (err < 0) |
---|
3978 | break; |
---|
3979 | } |
---|
3980 | |
---|
3981 | if (!og_msg_params_validate(params, OG_REST_PARAM_TASK | |
---|
3982 | OG_REST_PARAM_NAME | |
---|
3983 | OG_REST_PARAM_TIME_YEARS | |
---|
3984 | OG_REST_PARAM_TIME_MONTHS | |
---|
3985 | OG_REST_PARAM_TIME_WEEKS | |
---|
3986 | OG_REST_PARAM_TIME_WEEK_DAYS | |
---|
3987 | OG_REST_PARAM_TIME_DAYS | |
---|
3988 | OG_REST_PARAM_TIME_HOURS | |
---|
3989 | OG_REST_PARAM_TIME_MINUTES | |
---|
3990 | OG_REST_PARAM_TIME_AM_PM)) |
---|
3991 | return -1; |
---|
3992 | |
---|
3993 | dbi = og_dbi_open(&dbi_config); |
---|
3994 | if (!dbi) { |
---|
3995 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
3996 | __func__, __LINE__); |
---|
3997 | return -1; |
---|
3998 | } |
---|
3999 | |
---|
4000 | err = og_dbi_schedule_create(dbi, params, &schedule_id); |
---|
4001 | og_dbi_close(dbi); |
---|
4002 | |
---|
4003 | if (err < 0) |
---|
4004 | return -1; |
---|
4005 | |
---|
4006 | og_schedule_create(schedule_id, atoi(params->task_id), ¶ms->time); |
---|
4007 | og_schedule_refresh(og_loop); |
---|
4008 | |
---|
4009 | return err; |
---|
4010 | } |
---|
4011 | |
---|
4012 | static int og_cmd_schedule_update(json_t *element, struct og_msg_params *params) |
---|
4013 | { |
---|
4014 | struct og_dbi *dbi; |
---|
4015 | const char *key; |
---|
4016 | json_t *value; |
---|
4017 | int err; |
---|
4018 | |
---|
4019 | if (json_typeof(element) != JSON_OBJECT) |
---|
4020 | return -1; |
---|
4021 | |
---|
4022 | json_object_foreach(element, key, value) { |
---|
4023 | if (!strcmp(key, "id")) { |
---|
4024 | err = og_json_parse_string(value, ¶ms->id); |
---|
4025 | params->flags |= OG_REST_PARAM_ID; |
---|
4026 | } else if (!strcmp(key, "task")) { |
---|
4027 | err = og_json_parse_string(value, ¶ms->task_id); |
---|
4028 | params->flags |= OG_REST_PARAM_TASK; |
---|
4029 | } else if (!strcmp(key, "name")) { |
---|
4030 | err = og_json_parse_string(value, ¶ms->name); |
---|
4031 | params->flags |= OG_REST_PARAM_NAME; |
---|
4032 | } else if (!strcmp(key, "when")) |
---|
4033 | err = og_json_parse_time_params(value, params); |
---|
4034 | |
---|
4035 | if (err < 0) |
---|
4036 | break; |
---|
4037 | } |
---|
4038 | |
---|
4039 | if (!og_msg_params_validate(params, OG_REST_PARAM_ID | |
---|
4040 | OG_REST_PARAM_TASK | |
---|
4041 | OG_REST_PARAM_NAME | |
---|
4042 | OG_REST_PARAM_TIME_YEARS | |
---|
4043 | OG_REST_PARAM_TIME_MONTHS | |
---|
4044 | OG_REST_PARAM_TIME_DAYS | |
---|
4045 | OG_REST_PARAM_TIME_HOURS | |
---|
4046 | OG_REST_PARAM_TIME_MINUTES | |
---|
4047 | OG_REST_PARAM_TIME_AM_PM)) |
---|
4048 | return -1; |
---|
4049 | |
---|
4050 | dbi = og_dbi_open(&dbi_config); |
---|
4051 | if (!dbi) { |
---|
4052 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
4053 | __func__, __LINE__); |
---|
4054 | return -1; |
---|
4055 | } |
---|
4056 | |
---|
4057 | err = og_dbi_schedule_update(dbi, params); |
---|
4058 | og_dbi_close(dbi); |
---|
4059 | |
---|
4060 | if (err < 0) |
---|
4061 | return err; |
---|
4062 | |
---|
4063 | og_schedule_update(og_loop, atoi(params->id), atoi(params->task_id), |
---|
4064 | ¶ms->time); |
---|
4065 | og_schedule_refresh(og_loop); |
---|
4066 | |
---|
4067 | return err; |
---|
4068 | } |
---|
4069 | |
---|
4070 | static int og_cmd_schedule_delete(json_t *element, struct og_msg_params *params) |
---|
4071 | { |
---|
4072 | struct og_dbi *dbi; |
---|
4073 | const char *key; |
---|
4074 | json_t *value; |
---|
4075 | int err; |
---|
4076 | |
---|
4077 | if (json_typeof(element) != JSON_OBJECT) |
---|
4078 | return -1; |
---|
4079 | |
---|
4080 | json_object_foreach(element, key, value) { |
---|
4081 | if (!strcmp(key, "id")) { |
---|
4082 | err = og_json_parse_string(value, ¶ms->id); |
---|
4083 | params->flags |= OG_REST_PARAM_ID; |
---|
4084 | } else { |
---|
4085 | return -1; |
---|
4086 | } |
---|
4087 | |
---|
4088 | if (err < 0) |
---|
4089 | break; |
---|
4090 | } |
---|
4091 | |
---|
4092 | if (!og_msg_params_validate(params, OG_REST_PARAM_ID)) |
---|
4093 | return -1; |
---|
4094 | |
---|
4095 | dbi = og_dbi_open(&dbi_config); |
---|
4096 | if (!dbi) { |
---|
4097 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
4098 | __func__, __LINE__); |
---|
4099 | return -1; |
---|
4100 | } |
---|
4101 | |
---|
4102 | err = og_dbi_schedule_delete(dbi, atoi(params->id)); |
---|
4103 | og_dbi_close(dbi); |
---|
4104 | |
---|
4105 | og_schedule_delete(og_loop, atoi(params->id)); |
---|
4106 | |
---|
4107 | return err; |
---|
4108 | } |
---|
4109 | |
---|
4110 | static int og_cmd_schedule_get(json_t *element, struct og_msg_params *params, |
---|
4111 | char *buffer_reply) |
---|
4112 | { |
---|
4113 | struct og_buffer og_buffer = { |
---|
4114 | .data = buffer_reply, |
---|
4115 | }; |
---|
4116 | json_t *schedule_root; |
---|
4117 | struct og_dbi *dbi; |
---|
4118 | const char *key; |
---|
4119 | json_t *value; |
---|
4120 | int err; |
---|
4121 | |
---|
4122 | if (element) { |
---|
4123 | if (json_typeof(element) != JSON_OBJECT) |
---|
4124 | return -1; |
---|
4125 | |
---|
4126 | json_object_foreach(element, key, value) { |
---|
4127 | if (!strcmp(key, "task")) { |
---|
4128 | err = og_json_parse_string(value, |
---|
4129 | ¶ms->task_id); |
---|
4130 | } else if (!strcmp(key, "id")) { |
---|
4131 | err = og_json_parse_string(value, ¶ms->id); |
---|
4132 | } else { |
---|
4133 | return -1; |
---|
4134 | } |
---|
4135 | |
---|
4136 | if (err < 0) |
---|
4137 | break; |
---|
4138 | } |
---|
4139 | } |
---|
4140 | |
---|
4141 | dbi = og_dbi_open(&dbi_config); |
---|
4142 | if (!dbi) { |
---|
4143 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
4144 | __func__, __LINE__); |
---|
4145 | return -1; |
---|
4146 | } |
---|
4147 | |
---|
4148 | schedule_root = json_object(); |
---|
4149 | if (!schedule_root) { |
---|
4150 | og_dbi_close(dbi); |
---|
4151 | return -1; |
---|
4152 | } |
---|
4153 | |
---|
4154 | err = og_dbi_schedule_get_json(dbi, schedule_root, |
---|
4155 | params->task_id, params->id); |
---|
4156 | og_dbi_close(dbi); |
---|
4157 | |
---|
4158 | if (err >= 0) |
---|
4159 | json_dump_callback(schedule_root, og_json_dump_clients, &og_buffer, 0); |
---|
4160 | |
---|
4161 | json_decref(schedule_root); |
---|
4162 | |
---|
4163 | return err; |
---|
4164 | } |
---|
4165 | |
---|
4166 | static int og_client_method_not_found(struct og_client *cli) |
---|
4167 | { |
---|
4168 | /* To meet RFC 7231, this function MUST generate an Allow header field |
---|
4169 | * containing the correct methods. For example: "Allow: POST\r\n" |
---|
4170 | */ |
---|
4171 | char buf[] = "HTTP/1.1 405 Method Not Allowed\r\n" |
---|
4172 | "Content-Length: 0\r\n\r\n"; |
---|
4173 | |
---|
4174 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
4175 | |
---|
4176 | return -1; |
---|
4177 | } |
---|
4178 | |
---|
4179 | static int og_client_bad_request(struct og_client *cli) |
---|
4180 | { |
---|
4181 | char buf[] = "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n"; |
---|
4182 | |
---|
4183 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
4184 | |
---|
4185 | return -1; |
---|
4186 | } |
---|
4187 | |
---|
4188 | static int og_client_not_found(struct og_client *cli) |
---|
4189 | { |
---|
4190 | char buf[] = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; |
---|
4191 | |
---|
4192 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
4193 | |
---|
4194 | return -1; |
---|
4195 | } |
---|
4196 | |
---|
4197 | static int og_client_not_authorized(struct og_client *cli) |
---|
4198 | { |
---|
4199 | char buf[] = "HTTP/1.1 401 Unauthorized\r\n" |
---|
4200 | "WWW-Authenticate: Basic\r\n" |
---|
4201 | "Content-Length: 0\r\n\r\n"; |
---|
4202 | |
---|
4203 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
4204 | |
---|
4205 | return -1; |
---|
4206 | } |
---|
4207 | |
---|
4208 | static int og_server_internal_error(struct og_client *cli) |
---|
4209 | { |
---|
4210 | char buf[] = "HTTP/1.1 500 Internal Server Error\r\n" |
---|
4211 | "Content-Length: 0\r\n\r\n"; |
---|
4212 | |
---|
4213 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
4214 | |
---|
4215 | return -1; |
---|
4216 | } |
---|
4217 | |
---|
4218 | static int og_client_payload_too_large(struct og_client *cli) |
---|
4219 | { |
---|
4220 | char buf[] = "HTTP/1.1 413 Payload Too Large\r\n" |
---|
4221 | "Content-Length: 0\r\n\r\n"; |
---|
4222 | |
---|
4223 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
4224 | |
---|
4225 | return -1; |
---|
4226 | } |
---|
4227 | |
---|
4228 | #define OG_MSG_RESPONSE_MAXLEN 65536 |
---|
4229 | |
---|
4230 | static int og_client_ok(struct og_client *cli, char *buf_reply) |
---|
4231 | { |
---|
4232 | char buf[OG_MSG_RESPONSE_MAXLEN] = {}; |
---|
4233 | int err = 0, len; |
---|
4234 | |
---|
4235 | len = snprintf(buf, sizeof(buf), |
---|
4236 | "HTTP/1.1 200 OK\r\nContent-Length: %ld\r\n\r\n%s", |
---|
4237 | strlen(buf_reply), buf_reply); |
---|
4238 | if (len >= (int)sizeof(buf)) |
---|
4239 | err = og_server_internal_error(cli); |
---|
4240 | |
---|
4241 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
4242 | |
---|
4243 | return err; |
---|
4244 | } |
---|
4245 | |
---|
4246 | static int og_client_state_process_payload_rest(struct og_client *cli) |
---|
4247 | { |
---|
4248 | char buf_reply[OG_MSG_RESPONSE_MAXLEN] = {}; |
---|
4249 | struct og_msg_params params = {}; |
---|
4250 | enum og_rest_method method; |
---|
4251 | const char *cmd, *body; |
---|
4252 | json_error_t json_err; |
---|
4253 | json_t *root = NULL; |
---|
4254 | int err = 0; |
---|
4255 | |
---|
4256 | syslog(LOG_DEBUG, "%s:%hu %.32s ...\n", |
---|
4257 | inet_ntoa(cli->addr.sin_addr), |
---|
4258 | ntohs(cli->addr.sin_port), cli->buf); |
---|
4259 | |
---|
4260 | if (!strncmp(cli->buf, "GET", strlen("GET"))) { |
---|
4261 | method = OG_METHOD_GET; |
---|
4262 | cmd = cli->buf + strlen("GET") + 2; |
---|
4263 | } else if (!strncmp(cli->buf, "POST", strlen("POST"))) { |
---|
4264 | method = OG_METHOD_POST; |
---|
4265 | cmd = cli->buf + strlen("POST") + 2; |
---|
4266 | } else |
---|
4267 | return og_client_method_not_found(cli); |
---|
4268 | |
---|
4269 | body = strstr(cli->buf, "\r\n\r\n") + 4; |
---|
4270 | |
---|
4271 | if (strcmp(cli->auth_token, auth_token)) { |
---|
4272 | syslog(LOG_ERR, "wrong Authentication key\n"); |
---|
4273 | return og_client_not_authorized(cli); |
---|
4274 | } |
---|
4275 | |
---|
4276 | if (cli->content_length) { |
---|
4277 | root = json_loads(body, 0, &json_err); |
---|
4278 | if (!root) { |
---|
4279 | syslog(LOG_ERR, "malformed json line %d: %s\n", |
---|
4280 | json_err.line, json_err.text); |
---|
4281 | return og_client_not_found(cli); |
---|
4282 | } |
---|
4283 | } |
---|
4284 | |
---|
4285 | if (!strncmp(cmd, "clients", strlen("clients"))) { |
---|
4286 | if (method != OG_METHOD_POST && |
---|
4287 | method != OG_METHOD_GET) |
---|
4288 | return og_client_method_not_found(cli); |
---|
4289 | |
---|
4290 | if (method == OG_METHOD_POST && !root) { |
---|
4291 | syslog(LOG_ERR, "command clients with no payload\n"); |
---|
4292 | return og_client_bad_request(cli); |
---|
4293 | } |
---|
4294 | switch (method) { |
---|
4295 | case OG_METHOD_POST: |
---|
4296 | err = og_cmd_post_clients(root, ¶ms); |
---|
4297 | break; |
---|
4298 | case OG_METHOD_GET: |
---|
4299 | err = og_cmd_get_clients(root, ¶ms, buf_reply); |
---|
4300 | break; |
---|
4301 | default: |
---|
4302 | return og_client_bad_request(cli); |
---|
4303 | } |
---|
4304 | } else if (!strncmp(cmd, "wol", strlen("wol"))) { |
---|
4305 | if (method != OG_METHOD_POST) |
---|
4306 | return og_client_method_not_found(cli); |
---|
4307 | |
---|
4308 | if (!root) { |
---|
4309 | syslog(LOG_ERR, "command wol with no payload\n"); |
---|
4310 | return og_client_bad_request(cli); |
---|
4311 | } |
---|
4312 | err = og_cmd_wol(root, ¶ms); |
---|
4313 | } else if (!strncmp(cmd, "shell/run", strlen("shell/run"))) { |
---|
4314 | if (method != OG_METHOD_POST) |
---|
4315 | return og_client_method_not_found(cli); |
---|
4316 | |
---|
4317 | if (!root) { |
---|
4318 | syslog(LOG_ERR, "command run with no payload\n"); |
---|
4319 | return og_client_bad_request(cli); |
---|
4320 | } |
---|
4321 | err = og_cmd_run_post(root, ¶ms); |
---|
4322 | } else if (!strncmp(cmd, "shell/output", strlen("shell/output"))) { |
---|
4323 | if (method != OG_METHOD_POST) |
---|
4324 | return og_client_method_not_found(cli); |
---|
4325 | |
---|
4326 | if (!root) { |
---|
4327 | syslog(LOG_ERR, "command output with no payload\n"); |
---|
4328 | return og_client_bad_request(cli); |
---|
4329 | } |
---|
4330 | |
---|
4331 | err = og_cmd_run_get(root, ¶ms, buf_reply); |
---|
4332 | } else if (!strncmp(cmd, "session", strlen("session"))) { |
---|
4333 | if (method != OG_METHOD_POST) |
---|
4334 | return og_client_method_not_found(cli); |
---|
4335 | |
---|
4336 | if (!root) { |
---|
4337 | syslog(LOG_ERR, "command session with no payload\n"); |
---|
4338 | return og_client_bad_request(cli); |
---|
4339 | } |
---|
4340 | err = og_cmd_session(root, ¶ms); |
---|
4341 | } else if (!strncmp(cmd, "poweroff", strlen("poweroff"))) { |
---|
4342 | if (method != OG_METHOD_POST) |
---|
4343 | return og_client_method_not_found(cli); |
---|
4344 | |
---|
4345 | if (!root) { |
---|
4346 | syslog(LOG_ERR, "command poweroff with no payload\n"); |
---|
4347 | return og_client_bad_request(cli); |
---|
4348 | } |
---|
4349 | err = og_cmd_poweroff(root, ¶ms); |
---|
4350 | } else if (!strncmp(cmd, "reboot", strlen("reboot"))) { |
---|
4351 | if (method != OG_METHOD_POST) |
---|
4352 | return og_client_method_not_found(cli); |
---|
4353 | |
---|
4354 | if (!root) { |
---|
4355 | syslog(LOG_ERR, "command reboot with no payload\n"); |
---|
4356 | return og_client_bad_request(cli); |
---|
4357 | } |
---|
4358 | err = og_cmd_reboot(root, ¶ms); |
---|
4359 | } else if (!strncmp(cmd, "stop", strlen("stop"))) { |
---|
4360 | if (method != OG_METHOD_POST) |
---|
4361 | return og_client_method_not_found(cli); |
---|
4362 | |
---|
4363 | if (!root) { |
---|
4364 | syslog(LOG_ERR, "command stop with no payload\n"); |
---|
4365 | return og_client_bad_request(cli); |
---|
4366 | } |
---|
4367 | err = og_cmd_stop(root, ¶ms); |
---|
4368 | } else if (!strncmp(cmd, "refresh", strlen("refresh"))) { |
---|
4369 | if (method != OG_METHOD_POST) |
---|
4370 | return og_client_method_not_found(cli); |
---|
4371 | |
---|
4372 | if (!root) { |
---|
4373 | syslog(LOG_ERR, "command refresh with no payload\n"); |
---|
4374 | return og_client_bad_request(cli); |
---|
4375 | } |
---|
4376 | err = og_cmd_refresh(root, ¶ms); |
---|
4377 | } else if (!strncmp(cmd, "hardware", strlen("hardware"))) { |
---|
4378 | if (method != OG_METHOD_POST) |
---|
4379 | return og_client_method_not_found(cli); |
---|
4380 | |
---|
4381 | if (!root) { |
---|
4382 | syslog(LOG_ERR, "command hardware with no payload\n"); |
---|
4383 | return og_client_bad_request(cli); |
---|
4384 | } |
---|
4385 | err = og_cmd_hardware(root, ¶ms); |
---|
4386 | } else if (!strncmp(cmd, "software", strlen("software"))) { |
---|
4387 | if (method != OG_METHOD_POST) |
---|
4388 | return og_client_method_not_found(cli); |
---|
4389 | |
---|
4390 | if (!root) { |
---|
4391 | syslog(LOG_ERR, "command software with no payload\n"); |
---|
4392 | return og_client_bad_request(cli); |
---|
4393 | } |
---|
4394 | err = og_cmd_software(root, ¶ms); |
---|
4395 | } else if (!strncmp(cmd, "image/create/basic", |
---|
4396 | strlen("image/create/basic"))) { |
---|
4397 | if (method != OG_METHOD_POST) |
---|
4398 | return og_client_method_not_found(cli); |
---|
4399 | |
---|
4400 | if (!root) { |
---|
4401 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
4402 | return og_client_bad_request(cli); |
---|
4403 | } |
---|
4404 | err = og_cmd_create_basic_image(root, ¶ms); |
---|
4405 | } else if (!strncmp(cmd, "image/create/incremental", |
---|
4406 | strlen("image/create/incremental"))) { |
---|
4407 | if (method != OG_METHOD_POST) |
---|
4408 | return og_client_method_not_found(cli); |
---|
4409 | |
---|
4410 | if (!root) { |
---|
4411 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
4412 | return og_client_bad_request(cli); |
---|
4413 | } |
---|
4414 | err = og_cmd_create_incremental_image(root, ¶ms); |
---|
4415 | } else if (!strncmp(cmd, "image/create", strlen("image/create"))) { |
---|
4416 | if (method != OG_METHOD_POST) |
---|
4417 | return og_client_method_not_found(cli); |
---|
4418 | |
---|
4419 | if (!root) { |
---|
4420 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
4421 | return og_client_bad_request(cli); |
---|
4422 | } |
---|
4423 | err = og_cmd_create_image(root, ¶ms); |
---|
4424 | } else if (!strncmp(cmd, "image/restore/basic", |
---|
4425 | strlen("image/restore/basic"))) { |
---|
4426 | if (method != OG_METHOD_POST) |
---|
4427 | return og_client_method_not_found(cli); |
---|
4428 | |
---|
4429 | if (!root) { |
---|
4430 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
4431 | return og_client_bad_request(cli); |
---|
4432 | } |
---|
4433 | err = og_cmd_restore_basic_image(root, ¶ms); |
---|
4434 | } else if (!strncmp(cmd, "image/restore/incremental", |
---|
4435 | strlen("image/restore/incremental"))) { |
---|
4436 | if (method != OG_METHOD_POST) |
---|
4437 | return og_client_method_not_found(cli); |
---|
4438 | |
---|
4439 | if (!root) { |
---|
4440 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
4441 | return og_client_bad_request(cli); |
---|
4442 | } |
---|
4443 | err = og_cmd_restore_incremental_image(root, ¶ms); |
---|
4444 | } else if (!strncmp(cmd, "image/restore", strlen("image/restore"))) { |
---|
4445 | if (method != OG_METHOD_POST) |
---|
4446 | return og_client_method_not_found(cli); |
---|
4447 | |
---|
4448 | if (!root) { |
---|
4449 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
4450 | return og_client_bad_request(cli); |
---|
4451 | } |
---|
4452 | err = og_cmd_restore_image(root, ¶ms); |
---|
4453 | } else if (!strncmp(cmd, "setup", strlen("setup"))) { |
---|
4454 | if (method != OG_METHOD_POST) |
---|
4455 | return og_client_method_not_found(cli); |
---|
4456 | |
---|
4457 | if (!root) { |
---|
4458 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
4459 | return og_client_bad_request(cli); |
---|
4460 | } |
---|
4461 | err = og_cmd_setup(root, ¶ms); |
---|
4462 | } else if (!strncmp(cmd, "run/schedule", strlen("run/schedule"))) { |
---|
4463 | if (method != OG_METHOD_POST) |
---|
4464 | return og_client_method_not_found(cli); |
---|
4465 | |
---|
4466 | if (!root) { |
---|
4467 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
4468 | return og_client_bad_request(cli); |
---|
4469 | } |
---|
4470 | |
---|
4471 | err = og_cmd_run_schedule(root, ¶ms); |
---|
4472 | } else if (!strncmp(cmd, "task/run", strlen("task/run"))) { |
---|
4473 | if (method != OG_METHOD_POST) |
---|
4474 | return og_client_method_not_found(cli); |
---|
4475 | |
---|
4476 | if (!root) { |
---|
4477 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
4478 | return og_client_bad_request(cli); |
---|
4479 | } |
---|
4480 | err = og_cmd_task_post(root, ¶ms); |
---|
4481 | } else if (!strncmp(cmd, "schedule/create", |
---|
4482 | strlen("schedule/create"))) { |
---|
4483 | if (method != OG_METHOD_POST) |
---|
4484 | return og_client_method_not_found(cli); |
---|
4485 | |
---|
4486 | if (!root) { |
---|
4487 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
4488 | return og_client_bad_request(cli); |
---|
4489 | } |
---|
4490 | err = og_cmd_schedule_create(root, ¶ms); |
---|
4491 | } else if (!strncmp(cmd, "schedule/delete", |
---|
4492 | strlen("schedule/delete"))) { |
---|
4493 | if (method != OG_METHOD_POST) |
---|
4494 | return og_client_method_not_found(cli); |
---|
4495 | |
---|
4496 | if (!root) { |
---|
4497 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
4498 | return og_client_bad_request(cli); |
---|
4499 | } |
---|
4500 | err = og_cmd_schedule_delete(root, ¶ms); |
---|
4501 | } else if (!strncmp(cmd, "schedule/update", |
---|
4502 | strlen("schedule/update"))) { |
---|
4503 | if (method != OG_METHOD_POST) |
---|
4504 | return og_client_method_not_found(cli); |
---|
4505 | |
---|
4506 | if (!root) { |
---|
4507 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
4508 | return og_client_bad_request(cli); |
---|
4509 | } |
---|
4510 | err = og_cmd_schedule_update(root, ¶ms); |
---|
4511 | } else if (!strncmp(cmd, "schedule/get", |
---|
4512 | strlen("schedule/get"))) { |
---|
4513 | if (method != OG_METHOD_POST) |
---|
4514 | return og_client_method_not_found(cli); |
---|
4515 | |
---|
4516 | err = og_cmd_schedule_get(root, ¶ms, buf_reply); |
---|
4517 | } else { |
---|
4518 | syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd); |
---|
4519 | err = og_client_not_found(cli); |
---|
4520 | } |
---|
4521 | |
---|
4522 | if (root) |
---|
4523 | json_decref(root); |
---|
4524 | |
---|
4525 | if (err < 0) |
---|
4526 | return og_client_bad_request(cli); |
---|
4527 | |
---|
4528 | err = og_client_ok(cli, buf_reply); |
---|
4529 | if (err < 0) { |
---|
4530 | syslog(LOG_ERR, "HTTP response to %s:%hu is too large\n", |
---|
4531 | inet_ntoa(cli->addr.sin_addr), |
---|
4532 | ntohs(cli->addr.sin_port)); |
---|
4533 | } |
---|
4534 | |
---|
4535 | return err; |
---|
4536 | } |
---|
4537 | |
---|
4538 | static int og_client_state_recv_hdr_rest(struct og_client *cli) |
---|
4539 | { |
---|
4540 | char *ptr; |
---|
4541 | |
---|
4542 | ptr = strstr(cli->buf, "\r\n\r\n"); |
---|
4543 | if (!ptr) |
---|
4544 | return 0; |
---|
4545 | |
---|
4546 | cli->msg_len = ptr - cli->buf + 4; |
---|
4547 | |
---|
4548 | ptr = strstr(cli->buf, "Content-Length: "); |
---|
4549 | if (ptr) { |
---|
4550 | sscanf(ptr, "Content-Length: %i[^\r\n]", &cli->content_length); |
---|
4551 | if (cli->content_length < 0) |
---|
4552 | return -1; |
---|
4553 | cli->msg_len += cli->content_length; |
---|
4554 | } |
---|
4555 | |
---|
4556 | ptr = strstr(cli->buf, "Authorization: "); |
---|
4557 | if (ptr) |
---|
4558 | sscanf(ptr, "Authorization: %63[^\r\n]", cli->auth_token); |
---|
4559 | |
---|
4560 | return 1; |
---|
4561 | } |
---|
4562 | |
---|
4563 | static int og_client_recv(struct og_client *cli, int events) |
---|
4564 | { |
---|
4565 | struct ev_io *io = &cli->io; |
---|
4566 | int ret; |
---|
4567 | |
---|
4568 | if (events & EV_ERROR) { |
---|
4569 | syslog(LOG_ERR, "unexpected error event from client %s:%hu\n", |
---|
4570 | inet_ntoa(cli->addr.sin_addr), |
---|
4571 | ntohs(cli->addr.sin_port)); |
---|
4572 | return 0; |
---|
4573 | } |
---|
4574 | |
---|
4575 | ret = recv(io->fd, cli->buf + cli->buf_len, |
---|
4576 | sizeof(cli->buf) - cli->buf_len, 0); |
---|
4577 | if (ret <= 0) { |
---|
4578 | if (ret < 0) { |
---|
4579 | syslog(LOG_ERR, "error reading from client %s:%hu (%s)\n", |
---|
4580 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port), |
---|
4581 | strerror(errno)); |
---|
4582 | } else { |
---|
4583 | syslog(LOG_DEBUG, "closed connection by %s:%hu\n", |
---|
4584 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
4585 | } |
---|
4586 | return ret; |
---|
4587 | } |
---|
4588 | |
---|
4589 | return ret; |
---|
4590 | } |
---|
4591 | |
---|
4592 | static void og_client_read_cb(struct ev_loop *loop, struct ev_io *io, int events) |
---|
4593 | { |
---|
4594 | struct og_client *cli; |
---|
4595 | int ret; |
---|
4596 | |
---|
4597 | cli = container_of(io, struct og_client, io); |
---|
4598 | |
---|
4599 | ret = og_client_recv(cli, events); |
---|
4600 | if (ret <= 0) |
---|
4601 | goto close; |
---|
4602 | |
---|
4603 | if (cli->keepalive_idx >= 0) |
---|
4604 | return; |
---|
4605 | |
---|
4606 | ev_timer_again(loop, &cli->timer); |
---|
4607 | |
---|
4608 | cli->buf_len += ret; |
---|
4609 | if (cli->buf_len >= sizeof(cli->buf)) { |
---|
4610 | syslog(LOG_ERR, "client request from %s:%hu is too long\n", |
---|
4611 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
4612 | og_client_payload_too_large(cli); |
---|
4613 | goto close; |
---|
4614 | } |
---|
4615 | |
---|
4616 | switch (cli->state) { |
---|
4617 | case OG_CLIENT_RECEIVING_HEADER: |
---|
4618 | ret = og_client_state_recv_hdr_rest(cli); |
---|
4619 | if (ret < 0) |
---|
4620 | goto close; |
---|
4621 | if (!ret) |
---|
4622 | return; |
---|
4623 | |
---|
4624 | cli->state = OG_CLIENT_RECEIVING_PAYLOAD; |
---|
4625 | /* Fall through. */ |
---|
4626 | case OG_CLIENT_RECEIVING_PAYLOAD: |
---|
4627 | /* Still not enough data to process request. */ |
---|
4628 | if (cli->buf_len < cli->msg_len) |
---|
4629 | return; |
---|
4630 | |
---|
4631 | cli->state = OG_CLIENT_PROCESSING_REQUEST; |
---|
4632 | /* fall through. */ |
---|
4633 | case OG_CLIENT_PROCESSING_REQUEST: |
---|
4634 | ret = og_client_state_process_payload_rest(cli); |
---|
4635 | if (ret < 0) { |
---|
4636 | syslog(LOG_ERR, "Failed to process HTTP request from %s:%hu\n", |
---|
4637 | inet_ntoa(cli->addr.sin_addr), |
---|
4638 | ntohs(cli->addr.sin_port)); |
---|
4639 | } |
---|
4640 | if (ret < 0) |
---|
4641 | goto close; |
---|
4642 | |
---|
4643 | if (cli->keepalive_idx < 0) { |
---|
4644 | syslog(LOG_DEBUG, "server closing connection to %s:%hu\n", |
---|
4645 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
4646 | goto close; |
---|
4647 | } else { |
---|
4648 | syslog(LOG_DEBUG, "leaving client %s:%hu in keepalive mode\n", |
---|
4649 | inet_ntoa(cli->addr.sin_addr), |
---|
4650 | ntohs(cli->addr.sin_port)); |
---|
4651 | og_client_keepalive(loop, cli); |
---|
4652 | og_client_reset_state(cli); |
---|
4653 | } |
---|
4654 | break; |
---|
4655 | default: |
---|
4656 | syslog(LOG_ERR, "unknown state, critical internal error\n"); |
---|
4657 | goto close; |
---|
4658 | } |
---|
4659 | return; |
---|
4660 | close: |
---|
4661 | ev_timer_stop(loop, &cli->timer); |
---|
4662 | og_client_release(loop, cli); |
---|
4663 | } |
---|
4664 | |
---|
4665 | enum og_agent_state { |
---|
4666 | OG_AGENT_RECEIVING_HEADER = 0, |
---|
4667 | OG_AGENT_RECEIVING_PAYLOAD, |
---|
4668 | OG_AGENT_PROCESSING_RESPONSE, |
---|
4669 | }; |
---|
4670 | |
---|
4671 | static int og_agent_state_recv_hdr_rest(struct og_client *cli) |
---|
4672 | { |
---|
4673 | char *ptr; |
---|
4674 | |
---|
4675 | ptr = strstr(cli->buf, "\r\n\r\n"); |
---|
4676 | if (!ptr) |
---|
4677 | return 0; |
---|
4678 | |
---|
4679 | cli->msg_len = ptr - cli->buf + 4; |
---|
4680 | |
---|
4681 | ptr = strstr(cli->buf, "Content-Length: "); |
---|
4682 | if (ptr) { |
---|
4683 | sscanf(ptr, "Content-Length: %i[^\r\n]", &cli->content_length); |
---|
4684 | if (cli->content_length < 0) |
---|
4685 | return -1; |
---|
4686 | cli->msg_len += cli->content_length; |
---|
4687 | } |
---|
4688 | |
---|
4689 | return 1; |
---|
4690 | } |
---|
4691 | |
---|
4692 | static void og_agent_reset_state(struct og_client *cli) |
---|
4693 | { |
---|
4694 | cli->state = OG_AGENT_RECEIVING_HEADER; |
---|
4695 | cli->buf_len = 0; |
---|
4696 | cli->content_length = 0; |
---|
4697 | memset(cli->buf, 0, sizeof(cli->buf)); |
---|
4698 | } |
---|
4699 | |
---|
4700 | static int og_dbi_get_computer_info(struct og_computer *computer, |
---|
4701 | struct in_addr addr) |
---|
4702 | { |
---|
4703 | const char *msglog; |
---|
4704 | struct og_dbi *dbi; |
---|
4705 | dbi_result result; |
---|
4706 | |
---|
4707 | dbi = og_dbi_open(&dbi_config); |
---|
4708 | if (!dbi) { |
---|
4709 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
4710 | __func__, __LINE__); |
---|
4711 | return -1; |
---|
4712 | } |
---|
4713 | result = dbi_conn_queryf(dbi->conn, |
---|
4714 | "SELECT ordenadores.idordenador," |
---|
4715 | " ordenadores.nombreordenador," |
---|
4716 | " ordenadores.idaula," |
---|
4717 | " centros.idcentro FROM ordenadores " |
---|
4718 | "INNER JOIN aulas ON aulas.idaula=ordenadores.idaula " |
---|
4719 | "INNER JOIN centros ON centros.idcentro=aulas.idcentro " |
---|
4720 | "WHERE ordenadores.ip='%s'", inet_ntoa(addr)); |
---|
4721 | if (!result) { |
---|
4722 | dbi_conn_error(dbi->conn, &msglog); |
---|
4723 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
4724 | __func__, __LINE__, msglog); |
---|
4725 | og_dbi_close(dbi); |
---|
4726 | return -1; |
---|
4727 | } |
---|
4728 | if (!dbi_result_next_row(result)) { |
---|
4729 | syslog(LOG_ERR, "client does not exist in database (%s:%d)\n", |
---|
4730 | __func__, __LINE__); |
---|
4731 | dbi_result_free(result); |
---|
4732 | og_dbi_close(dbi); |
---|
4733 | return -1; |
---|
4734 | } |
---|
4735 | |
---|
4736 | computer->id = dbi_result_get_uint(result, "idordenador"); |
---|
4737 | computer->center = dbi_result_get_uint(result, "idcentro"); |
---|
4738 | computer->room = dbi_result_get_uint(result, "idaula"); |
---|
4739 | strncpy(computer->name, |
---|
4740 | dbi_result_get_string(result, "nombreordenador"), |
---|
4741 | OG_COMPUTER_NAME_MAXLEN); |
---|
4742 | |
---|
4743 | dbi_result_free(result); |
---|
4744 | og_dbi_close(dbi); |
---|
4745 | |
---|
4746 | return 0; |
---|
4747 | } |
---|
4748 | |
---|
4749 | static int og_resp_probe(struct og_client *cli, json_t *data) |
---|
4750 | { |
---|
4751 | const char *status = NULL; |
---|
4752 | const char *key; |
---|
4753 | json_t *value; |
---|
4754 | int err = 0; |
---|
4755 | |
---|
4756 | if (json_typeof(data) != JSON_OBJECT) |
---|
4757 | return -1; |
---|
4758 | |
---|
4759 | json_object_foreach(data, key, value) { |
---|
4760 | if (!strcmp(key, "status")) { |
---|
4761 | err = og_json_parse_string(value, &status); |
---|
4762 | if (err < 0) |
---|
4763 | return err; |
---|
4764 | } else { |
---|
4765 | return -1; |
---|
4766 | } |
---|
4767 | } |
---|
4768 | |
---|
4769 | if (!strcmp(status, "BSY")) |
---|
4770 | cli->status = OG_CLIENT_STATUS_BUSY; |
---|
4771 | else if (!strcmp(status, "OPG")) |
---|
4772 | cli->status = OG_CLIENT_STATUS_OGLIVE; |
---|
4773 | |
---|
4774 | return status ? 0 : -1; |
---|
4775 | } |
---|
4776 | |
---|
4777 | static int og_resp_shell_run(struct og_client *cli, json_t *data) |
---|
4778 | { |
---|
4779 | const char *output = NULL; |
---|
4780 | char filename[4096]; |
---|
4781 | const char *key; |
---|
4782 | json_t *value; |
---|
4783 | int err = -1; |
---|
4784 | FILE *file; |
---|
4785 | |
---|
4786 | if (json_typeof(data) != JSON_OBJECT) |
---|
4787 | return -1; |
---|
4788 | |
---|
4789 | json_object_foreach(data, key, value) { |
---|
4790 | if (!strcmp(key, "out")) { |
---|
4791 | err = og_json_parse_string(value, &output); |
---|
4792 | if (err < 0) |
---|
4793 | return err; |
---|
4794 | } else { |
---|
4795 | return -1; |
---|
4796 | } |
---|
4797 | } |
---|
4798 | |
---|
4799 | if (!output) { |
---|
4800 | syslog(LOG_ERR, "%s:%d: malformed json response\n", |
---|
4801 | __FILE__, __LINE__); |
---|
4802 | return -1; |
---|
4803 | } |
---|
4804 | |
---|
4805 | sprintf(filename, "/tmp/_Seconsola_%s", inet_ntoa(cli->addr.sin_addr)); |
---|
4806 | file = fopen(filename, "wt"); |
---|
4807 | if (!file) { |
---|
4808 | syslog(LOG_ERR, "cannot open file %s: %s\n", |
---|
4809 | filename, strerror(errno)); |
---|
4810 | return -1; |
---|
4811 | } |
---|
4812 | |
---|
4813 | fprintf(file, "%s", output); |
---|
4814 | fclose(file); |
---|
4815 | |
---|
4816 | return 0; |
---|
4817 | } |
---|
4818 | |
---|
4819 | struct og_computer_legacy { |
---|
4820 | char center[OG_DB_INT_MAXLEN + 1]; |
---|
4821 | char id[OG_DB_INT_MAXLEN + 1]; |
---|
4822 | char hardware[8192]; |
---|
4823 | }; |
---|
4824 | |
---|
4825 | static int og_resp_hardware(json_t *data, struct og_client *cli) |
---|
4826 | { |
---|
4827 | struct og_computer_legacy legacy = {}; |
---|
4828 | const char *hardware = NULL; |
---|
4829 | struct og_computer computer; |
---|
4830 | struct og_dbi *dbi; |
---|
4831 | const char *key; |
---|
4832 | json_t *value; |
---|
4833 | int err = 0; |
---|
4834 | bool res; |
---|
4835 | |
---|
4836 | if (json_typeof(data) != JSON_OBJECT) |
---|
4837 | return -1; |
---|
4838 | |
---|
4839 | json_object_foreach(data, key, value) { |
---|
4840 | if (!strcmp(key, "hardware")) { |
---|
4841 | err = og_json_parse_string(value, &hardware); |
---|
4842 | if (err < 0) |
---|
4843 | return -1; |
---|
4844 | } else { |
---|
4845 | return -1; |
---|
4846 | } |
---|
4847 | } |
---|
4848 | |
---|
4849 | if (!hardware) { |
---|
4850 | syslog(LOG_ERR, "malformed response json\n"); |
---|
4851 | return -1; |
---|
4852 | } |
---|
4853 | |
---|
4854 | err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr); |
---|
4855 | if (err < 0) |
---|
4856 | return -1; |
---|
4857 | |
---|
4858 | snprintf(legacy.center, sizeof(legacy.center), "%d", computer.center); |
---|
4859 | snprintf(legacy.id, sizeof(legacy.id), "%d", computer.id); |
---|
4860 | snprintf(legacy.hardware, sizeof(legacy.hardware), "%s", hardware); |
---|
4861 | |
---|
4862 | dbi = og_dbi_open(&dbi_config); |
---|
4863 | if (!dbi) { |
---|
4864 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
4865 | __func__, __LINE__); |
---|
4866 | return -1; |
---|
4867 | } |
---|
4868 | |
---|
4869 | res = actualizaHardware(dbi, legacy.hardware, legacy.id, computer.name, |
---|
4870 | legacy.center); |
---|
4871 | og_dbi_close(dbi); |
---|
4872 | |
---|
4873 | if (!res) { |
---|
4874 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
4875 | return -1; |
---|
4876 | } |
---|
4877 | |
---|
4878 | return 0; |
---|
4879 | } |
---|
4880 | |
---|
4881 | struct og_software_legacy { |
---|
4882 | char software[8192]; |
---|
4883 | char center[OG_DB_INT_MAXLEN + 1]; |
---|
4884 | char part[OG_DB_SMALLINT_MAXLEN + 1]; |
---|
4885 | char id[OG_DB_INT_MAXLEN + 1]; |
---|
4886 | }; |
---|
4887 | |
---|
4888 | static int og_resp_software(json_t *data, struct og_client *cli) |
---|
4889 | { |
---|
4890 | struct og_software_legacy legacy = {}; |
---|
4891 | const char *partition = NULL; |
---|
4892 | const char *software = NULL; |
---|
4893 | struct og_computer computer; |
---|
4894 | struct og_dbi *dbi; |
---|
4895 | const char *key; |
---|
4896 | json_t *value; |
---|
4897 | int err = 0; |
---|
4898 | bool res; |
---|
4899 | |
---|
4900 | if (json_typeof(data) != JSON_OBJECT) |
---|
4901 | return -1; |
---|
4902 | |
---|
4903 | json_object_foreach(data, key, value) { |
---|
4904 | if (!strcmp(key, "software")) |
---|
4905 | err = og_json_parse_string(value, &software); |
---|
4906 | else if (!strcmp(key, "partition")) |
---|
4907 | err = og_json_parse_string(value, &partition); |
---|
4908 | else |
---|
4909 | return -1; |
---|
4910 | |
---|
4911 | if (err < 0) |
---|
4912 | return -1; |
---|
4913 | } |
---|
4914 | |
---|
4915 | if (!software || !partition) { |
---|
4916 | syslog(LOG_ERR, "malformed response json\n"); |
---|
4917 | return -1; |
---|
4918 | } |
---|
4919 | |
---|
4920 | err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr); |
---|
4921 | if (err < 0) |
---|
4922 | return -1; |
---|
4923 | |
---|
4924 | snprintf(legacy.software, sizeof(legacy.software), "%s", software); |
---|
4925 | snprintf(legacy.part, sizeof(legacy.part), "%s", partition); |
---|
4926 | snprintf(legacy.id, sizeof(legacy.id), "%d", computer.id); |
---|
4927 | snprintf(legacy.center, sizeof(legacy.center), "%d", computer.center); |
---|
4928 | |
---|
4929 | dbi = og_dbi_open(&dbi_config); |
---|
4930 | if (!dbi) { |
---|
4931 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
4932 | __func__, __LINE__); |
---|
4933 | return -1; |
---|
4934 | } |
---|
4935 | |
---|
4936 | res = actualizaSoftware(dbi, legacy.software, legacy.part, legacy.id, |
---|
4937 | computer.name, legacy.center); |
---|
4938 | og_dbi_close(dbi); |
---|
4939 | |
---|
4940 | if (!res) { |
---|
4941 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
4942 | return -1; |
---|
4943 | } |
---|
4944 | |
---|
4945 | return 0; |
---|
4946 | } |
---|
4947 | |
---|
4948 | #define OG_PARAMS_RESP_REFRESH (OG_PARAM_PART_DISK | \ |
---|
4949 | OG_PARAM_PART_NUMBER | \ |
---|
4950 | OG_PARAM_PART_CODE | \ |
---|
4951 | OG_PARAM_PART_FILESYSTEM | \ |
---|
4952 | OG_PARAM_PART_OS | \ |
---|
4953 | OG_PARAM_PART_SIZE | \ |
---|
4954 | OG_PARAM_PART_USED_SIZE) |
---|
4955 | |
---|
4956 | static int og_json_parse_partition_array(json_t *value, |
---|
4957 | struct og_partition *partitions) |
---|
4958 | { |
---|
4959 | json_t *element; |
---|
4960 | int i, err; |
---|
4961 | |
---|
4962 | if (json_typeof(value) != JSON_ARRAY) |
---|
4963 | return -1; |
---|
4964 | |
---|
4965 | for (i = 0; i < json_array_size(value) && i < OG_PARTITION_MAX; i++) { |
---|
4966 | element = json_array_get(value, i); |
---|
4967 | |
---|
4968 | err = og_json_parse_partition(element, &partitions[i], |
---|
4969 | OG_PARAMS_RESP_REFRESH); |
---|
4970 | if (err < 0) |
---|
4971 | return err; |
---|
4972 | } |
---|
4973 | |
---|
4974 | return 0; |
---|
4975 | } |
---|
4976 | |
---|
4977 | static int og_resp_refresh(json_t *data, struct og_client *cli) |
---|
4978 | { |
---|
4979 | struct og_partition partitions[OG_PARTITION_MAX] = {}; |
---|
4980 | const char *serial_number = NULL; |
---|
4981 | struct og_partition disk_setup; |
---|
4982 | struct og_computer computer; |
---|
4983 | char cfg[1024] = {}; |
---|
4984 | struct og_dbi *dbi; |
---|
4985 | const char *key; |
---|
4986 | unsigned int i; |
---|
4987 | json_t *value; |
---|
4988 | int err = 0; |
---|
4989 | bool res; |
---|
4990 | |
---|
4991 | if (json_typeof(data) != JSON_OBJECT) |
---|
4992 | return -1; |
---|
4993 | |
---|
4994 | json_object_foreach(data, key, value) { |
---|
4995 | if (!strcmp(key, "disk_setup")) { |
---|
4996 | err = og_json_parse_partition(value, |
---|
4997 | &disk_setup, |
---|
4998 | OG_PARAMS_RESP_REFRESH); |
---|
4999 | } else if (!strcmp(key, "partition_setup")) { |
---|
5000 | err = og_json_parse_partition_array(value, partitions); |
---|
5001 | } else if (!strcmp(key, "serial_number")) { |
---|
5002 | err = og_json_parse_string(value, &serial_number); |
---|
5003 | } else { |
---|
5004 | return -1; |
---|
5005 | } |
---|
5006 | |
---|
5007 | if (err < 0) |
---|
5008 | return err; |
---|
5009 | } |
---|
5010 | |
---|
5011 | err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr); |
---|
5012 | if (err < 0) |
---|
5013 | return -1; |
---|
5014 | |
---|
5015 | if (strlen(serial_number) > 0) |
---|
5016 | snprintf(cfg, sizeof(cfg), "ser=%s\n", serial_number); |
---|
5017 | |
---|
5018 | if (!disk_setup.disk || !disk_setup.number || !disk_setup.code || |
---|
5019 | !disk_setup.filesystem || !disk_setup.os || !disk_setup.size || |
---|
5020 | !disk_setup.used_size) |
---|
5021 | return -1; |
---|
5022 | |
---|
5023 | snprintf(cfg + strlen(cfg), sizeof(cfg) - strlen(cfg), |
---|
5024 | "disk=%s\tpar=%s\tcpt=%s\tfsi=%s\tsoi=%s\ttam=%s\tuso=%s\n", |
---|
5025 | disk_setup.disk, disk_setup.number, disk_setup.code, |
---|
5026 | disk_setup.filesystem, disk_setup.os, disk_setup.size, |
---|
5027 | disk_setup.used_size); |
---|
5028 | |
---|
5029 | for (i = 0; i < OG_PARTITION_MAX; i++) { |
---|
5030 | if (!partitions[i].disk || !partitions[i].number || |
---|
5031 | !partitions[i].code || !partitions[i].filesystem || |
---|
5032 | !partitions[i].os || !partitions[i].size || |
---|
5033 | !partitions[i].used_size) |
---|
5034 | continue; |
---|
5035 | |
---|
5036 | snprintf(cfg + strlen(cfg), sizeof(cfg) - strlen(cfg), |
---|
5037 | "disk=%s\tpar=%s\tcpt=%s\tfsi=%s\tsoi=%s\ttam=%s\tuso=%s\n", |
---|
5038 | partitions[i].disk, partitions[i].number, |
---|
5039 | partitions[i].code, partitions[i].filesystem, |
---|
5040 | partitions[i].os, partitions[i].size, |
---|
5041 | partitions[i].used_size); |
---|
5042 | } |
---|
5043 | |
---|
5044 | dbi = og_dbi_open(&dbi_config); |
---|
5045 | if (!dbi) { |
---|
5046 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
5047 | __func__, __LINE__); |
---|
5048 | return -1; |
---|
5049 | } |
---|
5050 | res = actualizaConfiguracion(dbi, cfg, computer.id); |
---|
5051 | og_dbi_close(dbi); |
---|
5052 | |
---|
5053 | if (!res) { |
---|
5054 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
5055 | return -1; |
---|
5056 | } |
---|
5057 | |
---|
5058 | return 0; |
---|
5059 | } |
---|
5060 | |
---|
5061 | static int og_resp_image_create(json_t *data, struct og_client *cli) |
---|
5062 | { |
---|
5063 | struct og_software_legacy soft_legacy; |
---|
5064 | struct og_image_legacy img_legacy; |
---|
5065 | const char *partition = NULL; |
---|
5066 | const char *software = NULL; |
---|
5067 | const char *image_id = NULL; |
---|
5068 | struct og_computer computer; |
---|
5069 | const char *disk = NULL; |
---|
5070 | const char *code = NULL; |
---|
5071 | const char *name = NULL; |
---|
5072 | const char *repo = NULL; |
---|
5073 | struct og_dbi *dbi; |
---|
5074 | const char *key; |
---|
5075 | json_t *value; |
---|
5076 | int err = 0; |
---|
5077 | bool res; |
---|
5078 | |
---|
5079 | if (json_typeof(data) != JSON_OBJECT) |
---|
5080 | return -1; |
---|
5081 | |
---|
5082 | json_object_foreach(data, key, value) { |
---|
5083 | if (!strcmp(key, "software")) |
---|
5084 | err = og_json_parse_string(value, &software); |
---|
5085 | else if (!strcmp(key, "partition")) |
---|
5086 | err = og_json_parse_string(value, &partition); |
---|
5087 | else if (!strcmp(key, "disk")) |
---|
5088 | err = og_json_parse_string(value, &disk); |
---|
5089 | else if (!strcmp(key, "code")) |
---|
5090 | err = og_json_parse_string(value, &code); |
---|
5091 | else if (!strcmp(key, "id")) |
---|
5092 | err = og_json_parse_string(value, &image_id); |
---|
5093 | else if (!strcmp(key, "name")) |
---|
5094 | err = og_json_parse_string(value, &name); |
---|
5095 | else if (!strcmp(key, "repository")) |
---|
5096 | err = og_json_parse_string(value, &repo); |
---|
5097 | else |
---|
5098 | return -1; |
---|
5099 | |
---|
5100 | if (err < 0) |
---|
5101 | return err; |
---|
5102 | } |
---|
5103 | |
---|
5104 | if (!software || !partition || !disk || !code || !image_id || !name || |
---|
5105 | !repo) { |
---|
5106 | syslog(LOG_ERR, "malformed response json\n"); |
---|
5107 | return -1; |
---|
5108 | } |
---|
5109 | |
---|
5110 | err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr); |
---|
5111 | if (err < 0) |
---|
5112 | return -1; |
---|
5113 | |
---|
5114 | snprintf(soft_legacy.center, sizeof(soft_legacy.center), "%d", |
---|
5115 | computer.center); |
---|
5116 | snprintf(soft_legacy.software, sizeof(soft_legacy.software), "%s", |
---|
5117 | software); |
---|
5118 | snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%s", |
---|
5119 | image_id); |
---|
5120 | snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%d", computer.id); |
---|
5121 | snprintf(img_legacy.part, sizeof(img_legacy.part), "%s", partition); |
---|
5122 | snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%s", disk); |
---|
5123 | snprintf(img_legacy.code, sizeof(img_legacy.code), "%s", code); |
---|
5124 | snprintf(img_legacy.name, sizeof(img_legacy.name), "%s", name); |
---|
5125 | snprintf(img_legacy.repo, sizeof(img_legacy.repo), "%s", repo); |
---|
5126 | |
---|
5127 | dbi = og_dbi_open(&dbi_config); |
---|
5128 | if (!dbi) { |
---|
5129 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
5130 | __func__, __LINE__); |
---|
5131 | return -1; |
---|
5132 | } |
---|
5133 | |
---|
5134 | res = actualizaSoftware(dbi, |
---|
5135 | soft_legacy.software, |
---|
5136 | img_legacy.part, |
---|
5137 | soft_legacy.id, |
---|
5138 | computer.name, |
---|
5139 | soft_legacy.center); |
---|
5140 | if (!res) { |
---|
5141 | og_dbi_close(dbi); |
---|
5142 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
5143 | return -1; |
---|
5144 | } |
---|
5145 | |
---|
5146 | res = actualizaCreacionImagen(dbi, |
---|
5147 | img_legacy.image_id, |
---|
5148 | img_legacy.disk, |
---|
5149 | img_legacy.part, |
---|
5150 | img_legacy.code, |
---|
5151 | img_legacy.repo, |
---|
5152 | soft_legacy.id); |
---|
5153 | og_dbi_close(dbi); |
---|
5154 | |
---|
5155 | if (!res) { |
---|
5156 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
5157 | return -1; |
---|
5158 | } |
---|
5159 | |
---|
5160 | return 0; |
---|
5161 | } |
---|
5162 | |
---|
5163 | static int og_resp_image_restore(json_t *data, struct og_client *cli) |
---|
5164 | { |
---|
5165 | struct og_software_legacy soft_legacy; |
---|
5166 | struct og_image_legacy img_legacy; |
---|
5167 | const char *partition = NULL; |
---|
5168 | const char *image_id = NULL; |
---|
5169 | struct og_computer computer; |
---|
5170 | const char *disk = NULL; |
---|
5171 | dbi_result query_result; |
---|
5172 | struct og_dbi *dbi; |
---|
5173 | const char *key; |
---|
5174 | json_t *value; |
---|
5175 | int err = 0; |
---|
5176 | bool res; |
---|
5177 | |
---|
5178 | if (json_typeof(data) != JSON_OBJECT) |
---|
5179 | return -1; |
---|
5180 | |
---|
5181 | json_object_foreach(data, key, value) { |
---|
5182 | if (!strcmp(key, "partition")) |
---|
5183 | err = og_json_parse_string(value, &partition); |
---|
5184 | else if (!strcmp(key, "disk")) |
---|
5185 | err = og_json_parse_string(value, &disk); |
---|
5186 | else if (!strcmp(key, "image_id")) |
---|
5187 | err = og_json_parse_string(value, &image_id); |
---|
5188 | else |
---|
5189 | return -1; |
---|
5190 | |
---|
5191 | if (err < 0) |
---|
5192 | return err; |
---|
5193 | } |
---|
5194 | |
---|
5195 | if (!partition || !disk || !image_id) { |
---|
5196 | syslog(LOG_ERR, "malformed response json\n"); |
---|
5197 | return -1; |
---|
5198 | } |
---|
5199 | |
---|
5200 | err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr); |
---|
5201 | if (err < 0) |
---|
5202 | return -1; |
---|
5203 | |
---|
5204 | snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%s", |
---|
5205 | image_id); |
---|
5206 | snprintf(img_legacy.part, sizeof(img_legacy.part), "%s", partition); |
---|
5207 | snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%s", disk); |
---|
5208 | snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%d", computer.id); |
---|
5209 | |
---|
5210 | dbi = og_dbi_open(&dbi_config); |
---|
5211 | if (!dbi) { |
---|
5212 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
5213 | __func__, __LINE__); |
---|
5214 | return -1; |
---|
5215 | } |
---|
5216 | |
---|
5217 | query_result = dbi_conn_queryf(dbi->conn, |
---|
5218 | "SELECT idperfilsoft FROM imagenes " |
---|
5219 | " WHERE idimagen='%s'", |
---|
5220 | image_id); |
---|
5221 | if (!query_result) { |
---|
5222 | og_dbi_close(dbi); |
---|
5223 | syslog(LOG_ERR, "failed to query database\n"); |
---|
5224 | return -1; |
---|
5225 | } |
---|
5226 | if (!dbi_result_next_row(query_result)) { |
---|
5227 | dbi_result_free(query_result); |
---|
5228 | og_dbi_close(dbi); |
---|
5229 | syslog(LOG_ERR, "software profile does not exist in database\n"); |
---|
5230 | return -1; |
---|
5231 | } |
---|
5232 | snprintf(img_legacy.software_id, sizeof(img_legacy.software_id), |
---|
5233 | "%d", dbi_result_get_uint(query_result, "idperfilsoft")); |
---|
5234 | dbi_result_free(query_result); |
---|
5235 | |
---|
5236 | res = actualizaRestauracionImagen(dbi, |
---|
5237 | img_legacy.image_id, |
---|
5238 | img_legacy.disk, |
---|
5239 | img_legacy.part, |
---|
5240 | soft_legacy.id, |
---|
5241 | img_legacy.software_id); |
---|
5242 | og_dbi_close(dbi); |
---|
5243 | |
---|
5244 | if (!res) { |
---|
5245 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
5246 | return -1; |
---|
5247 | } |
---|
5248 | |
---|
5249 | return 0; |
---|
5250 | } |
---|
5251 | |
---|
5252 | static int og_dbi_update_action(struct og_client *cli, bool success) |
---|
5253 | { |
---|
5254 | char end_date_string[24]; |
---|
5255 | struct tm *end_date; |
---|
5256 | const char *msglog; |
---|
5257 | struct og_dbi *dbi; |
---|
5258 | uint8_t status = 2; |
---|
5259 | dbi_result result; |
---|
5260 | time_t now; |
---|
5261 | |
---|
5262 | if (!cli->last_cmd_id) |
---|
5263 | return 0; |
---|
5264 | |
---|
5265 | dbi = og_dbi_open(&dbi_config); |
---|
5266 | if (!dbi) { |
---|
5267 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
5268 | __func__, __LINE__); |
---|
5269 | return -1; |
---|
5270 | } |
---|
5271 | |
---|
5272 | time(&now); |
---|
5273 | end_date = localtime(&now); |
---|
5274 | |
---|
5275 | sprintf(end_date_string, "%hu/%hhu/%hhu %hhu:%hhu:%hhu", |
---|
5276 | end_date->tm_year + 1900, end_date->tm_mon + 1, |
---|
5277 | end_date->tm_mday, end_date->tm_hour, end_date->tm_min, |
---|
5278 | end_date->tm_sec); |
---|
5279 | result = dbi_conn_queryf(dbi->conn, |
---|
5280 | "UPDATE acciones SET fechahorafin='%s', " |
---|
5281 | "estado=%d, resultado=%d WHERE idaccion=%d", |
---|
5282 | end_date_string, ACCION_FINALIZADA, |
---|
5283 | status - success, cli->last_cmd_id); |
---|
5284 | |
---|
5285 | if (!result) { |
---|
5286 | dbi_conn_error(dbi->conn, &msglog); |
---|
5287 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
5288 | __func__, __LINE__, msglog); |
---|
5289 | og_dbi_close(dbi); |
---|
5290 | return -1; |
---|
5291 | } |
---|
5292 | cli->last_cmd_id = 0; |
---|
5293 | dbi_result_free(result); |
---|
5294 | og_dbi_close(dbi); |
---|
5295 | |
---|
5296 | return 0; |
---|
5297 | } |
---|
5298 | |
---|
5299 | static int og_agent_state_process_response(struct og_client *cli) |
---|
5300 | { |
---|
5301 | json_error_t json_err; |
---|
5302 | json_t *root; |
---|
5303 | int err = -1; |
---|
5304 | char *body; |
---|
5305 | |
---|
5306 | if (strncmp(cli->buf, "HTTP/1.0 200 OK", strlen("HTTP/1.0 200 OK"))) { |
---|
5307 | og_dbi_update_action(cli, false); |
---|
5308 | return -1; |
---|
5309 | } |
---|
5310 | og_dbi_update_action(cli, true); |
---|
5311 | |
---|
5312 | if (!cli->content_length) { |
---|
5313 | cli->last_cmd = OG_CMD_UNSPEC; |
---|
5314 | return 0; |
---|
5315 | } |
---|
5316 | |
---|
5317 | body = strstr(cli->buf, "\r\n\r\n") + 4; |
---|
5318 | |
---|
5319 | root = json_loads(body, 0, &json_err); |
---|
5320 | if (!root) { |
---|
5321 | syslog(LOG_ERR, "%s:%d: malformed json line %d: %s\n", |
---|
5322 | __FILE__, __LINE__, json_err.line, json_err.text); |
---|
5323 | return -1; |
---|
5324 | } |
---|
5325 | |
---|
5326 | switch (cli->last_cmd) { |
---|
5327 | case OG_CMD_PROBE: |
---|
5328 | err = og_resp_probe(cli, root); |
---|
5329 | break; |
---|
5330 | case OG_CMD_SHELL_RUN: |
---|
5331 | err = og_resp_shell_run(cli, root); |
---|
5332 | break; |
---|
5333 | case OG_CMD_HARDWARE: |
---|
5334 | err = og_resp_hardware(root, cli); |
---|
5335 | break; |
---|
5336 | case OG_CMD_SOFTWARE: |
---|
5337 | err = og_resp_software(root, cli); |
---|
5338 | break; |
---|
5339 | case OG_CMD_REFRESH: |
---|
5340 | err = og_resp_refresh(root, cli); |
---|
5341 | break; |
---|
5342 | case OG_CMD_SETUP: |
---|
5343 | err = og_resp_refresh(root, cli); |
---|
5344 | break; |
---|
5345 | case OG_CMD_IMAGE_CREATE: |
---|
5346 | err = og_resp_image_create(root, cli); |
---|
5347 | break; |
---|
5348 | case OG_CMD_IMAGE_RESTORE: |
---|
5349 | err = og_resp_image_restore(root, cli); |
---|
5350 | break; |
---|
5351 | default: |
---|
5352 | err = -1; |
---|
5353 | break; |
---|
5354 | } |
---|
5355 | |
---|
5356 | cli->last_cmd = OG_CMD_UNSPEC; |
---|
5357 | |
---|
5358 | return err; |
---|
5359 | } |
---|
5360 | |
---|
5361 | static void og_agent_deliver_pending_cmd(struct og_client *cli) |
---|
5362 | { |
---|
5363 | const struct og_cmd *cmd; |
---|
5364 | |
---|
5365 | cmd = og_cmd_find(inet_ntoa(cli->addr.sin_addr)); |
---|
5366 | if (!cmd) |
---|
5367 | return; |
---|
5368 | |
---|
5369 | og_send_request(cmd->method, cmd->type, &cmd->params, cmd->json); |
---|
5370 | cli->last_cmd_id = cmd->id; |
---|
5371 | |
---|
5372 | og_cmd_free(cmd); |
---|
5373 | } |
---|
5374 | |
---|
5375 | static void og_agent_read_cb(struct ev_loop *loop, struct ev_io *io, int events) |
---|
5376 | { |
---|
5377 | struct og_client *cli; |
---|
5378 | int ret; |
---|
5379 | |
---|
5380 | cli = container_of(io, struct og_client, io); |
---|
5381 | |
---|
5382 | ret = og_client_recv(cli, events); |
---|
5383 | if (ret <= 0) |
---|
5384 | goto close; |
---|
5385 | |
---|
5386 | ev_timer_again(loop, &cli->timer); |
---|
5387 | |
---|
5388 | cli->buf_len += ret; |
---|
5389 | if (cli->buf_len >= sizeof(cli->buf)) { |
---|
5390 | syslog(LOG_ERR, "client request from %s:%hu is too long\n", |
---|
5391 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
5392 | goto close; |
---|
5393 | } |
---|
5394 | |
---|
5395 | switch (cli->state) { |
---|
5396 | case OG_AGENT_RECEIVING_HEADER: |
---|
5397 | ret = og_agent_state_recv_hdr_rest(cli); |
---|
5398 | if (ret < 0) |
---|
5399 | goto close; |
---|
5400 | if (!ret) |
---|
5401 | return; |
---|
5402 | |
---|
5403 | cli->state = OG_AGENT_RECEIVING_PAYLOAD; |
---|
5404 | /* Fall through. */ |
---|
5405 | case OG_AGENT_RECEIVING_PAYLOAD: |
---|
5406 | /* Still not enough data to process request. */ |
---|
5407 | if (cli->buf_len < cli->msg_len) |
---|
5408 | return; |
---|
5409 | |
---|
5410 | cli->state = OG_AGENT_PROCESSING_RESPONSE; |
---|
5411 | /* fall through. */ |
---|
5412 | case OG_AGENT_PROCESSING_RESPONSE: |
---|
5413 | ret = og_agent_state_process_response(cli); |
---|
5414 | if (ret < 0) { |
---|
5415 | syslog(LOG_ERR, "Failed to process HTTP request from %s:%hu\n", |
---|
5416 | inet_ntoa(cli->addr.sin_addr), |
---|
5417 | ntohs(cli->addr.sin_port)); |
---|
5418 | goto close; |
---|
5419 | } |
---|
5420 | og_agent_deliver_pending_cmd(cli); |
---|
5421 | |
---|
5422 | syslog(LOG_DEBUG, "leaving client %s:%hu in keepalive mode\n", |
---|
5423 | inet_ntoa(cli->addr.sin_addr), |
---|
5424 | ntohs(cli->addr.sin_port)); |
---|
5425 | og_agent_reset_state(cli); |
---|
5426 | break; |
---|
5427 | default: |
---|
5428 | syslog(LOG_ERR, "unknown state, critical internal error\n"); |
---|
5429 | goto close; |
---|
5430 | } |
---|
5431 | return; |
---|
5432 | close: |
---|
5433 | ev_timer_stop(loop, &cli->timer); |
---|
5434 | og_client_release(loop, cli); |
---|
5435 | } |
---|
5436 | |
---|
5437 | static void og_client_timer_cb(struct ev_loop *loop, ev_timer *timer, int events) |
---|
5438 | { |
---|
5439 | struct og_client *cli; |
---|
5440 | |
---|
5441 | cli = container_of(timer, struct og_client, timer); |
---|
5442 | if (cli->keepalive_idx >= 0) { |
---|
5443 | ev_timer_again(loop, &cli->timer); |
---|
5444 | return; |
---|
5445 | } |
---|
5446 | syslog(LOG_ERR, "timeout request for client %s:%hu\n", |
---|
5447 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
5448 | |
---|
5449 | og_client_release(loop, cli); |
---|
5450 | } |
---|
5451 | |
---|
5452 | static void og_agent_send_refresh(struct og_client *cli) |
---|
5453 | { |
---|
5454 | struct og_msg_params params; |
---|
5455 | int err; |
---|
5456 | |
---|
5457 | params.ips_array[0] = inet_ntoa(cli->addr.sin_addr); |
---|
5458 | params.ips_array_len = 1; |
---|
5459 | |
---|
5460 | err = og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, ¶ms, NULL); |
---|
5461 | if (err < 0) { |
---|
5462 | syslog(LOG_ERR, "Can't send refresh to: %s\n", |
---|
5463 | params.ips_array[0]); |
---|
5464 | } else { |
---|
5465 | syslog(LOG_INFO, "Sent refresh to: %s\n", |
---|
5466 | params.ips_array[0]); |
---|
5467 | } |
---|
5468 | } |
---|
5469 | |
---|
5470 | static int socket_rest, socket_agent_rest; |
---|
5471 | |
---|
5472 | static void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, |
---|
5473 | int events) |
---|
5474 | { |
---|
5475 | struct sockaddr_in client_addr; |
---|
5476 | socklen_t addrlen = sizeof(client_addr); |
---|
5477 | struct og_client *cli; |
---|
5478 | int client_sd; |
---|
5479 | |
---|
5480 | if (events & EV_ERROR) |
---|
5481 | return; |
---|
5482 | |
---|
5483 | client_sd = accept(io->fd, (struct sockaddr *)&client_addr, &addrlen); |
---|
5484 | if (client_sd < 0) { |
---|
5485 | syslog(LOG_ERR, "cannot accept client connection\n"); |
---|
5486 | return; |
---|
5487 | } |
---|
5488 | |
---|
5489 | cli = (struct og_client *)calloc(1, sizeof(struct og_client)); |
---|
5490 | if (!cli) { |
---|
5491 | close(client_sd); |
---|
5492 | return; |
---|
5493 | } |
---|
5494 | memcpy(&cli->addr, &client_addr, sizeof(client_addr)); |
---|
5495 | if (io->fd == socket_agent_rest) |
---|
5496 | cli->keepalive_idx = 0; |
---|
5497 | else |
---|
5498 | cli->keepalive_idx = -1; |
---|
5499 | |
---|
5500 | if (io->fd == socket_rest) |
---|
5501 | cli->rest = true; |
---|
5502 | else if (io->fd == socket_agent_rest) |
---|
5503 | cli->agent = true; |
---|
5504 | |
---|
5505 | syslog(LOG_DEBUG, "connection from client %s:%hu\n", |
---|
5506 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
5507 | |
---|
5508 | if (io->fd == socket_agent_rest) |
---|
5509 | ev_io_init(&cli->io, og_agent_read_cb, client_sd, EV_READ); |
---|
5510 | else |
---|
5511 | ev_io_init(&cli->io, og_client_read_cb, client_sd, EV_READ); |
---|
5512 | |
---|
5513 | ev_io_start(loop, &cli->io); |
---|
5514 | if (io->fd == socket_agent_rest) { |
---|
5515 | ev_timer_init(&cli->timer, og_client_timer_cb, |
---|
5516 | OG_AGENT_CLIENT_TIMEOUT, 0.); |
---|
5517 | } else { |
---|
5518 | ev_timer_init(&cli->timer, og_client_timer_cb, |
---|
5519 | OG_CLIENT_TIMEOUT, 0.); |
---|
5520 | } |
---|
5521 | ev_timer_start(loop, &cli->timer); |
---|
5522 | list_add(&cli->list, &client_list); |
---|
5523 | |
---|
5524 | if (io->fd == socket_agent_rest) { |
---|
5525 | og_agent_send_refresh(cli); |
---|
5526 | } |
---|
5527 | } |
---|
5528 | |
---|
5529 | static int og_socket_server_init(const char *port) |
---|
5530 | { |
---|
5531 | struct sockaddr_in local; |
---|
5532 | int sd, on = 1; |
---|
5533 | |
---|
5534 | sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
---|
5535 | if (sd < 0) { |
---|
5536 | syslog(LOG_ERR, "cannot create main socket\n"); |
---|
5537 | return -1; |
---|
5538 | } |
---|
5539 | setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(int)); |
---|
5540 | |
---|
5541 | local.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
5542 | local.sin_family = AF_INET; |
---|
5543 | local.sin_port = htons(atoi(port)); |
---|
5544 | |
---|
5545 | if (bind(sd, (struct sockaddr *) &local, sizeof(local)) < 0) { |
---|
5546 | close(sd); |
---|
5547 | syslog(LOG_ERR, "cannot bind socket\n"); |
---|
5548 | return -1; |
---|
5549 | } |
---|
5550 | |
---|
5551 | listen(sd, 250); |
---|
5552 | |
---|
5553 | return sd; |
---|
5554 | } |
---|
5555 | |
---|
5556 | int main(int argc, char *argv[]) |
---|
5557 | { |
---|
5558 | struct ev_io ev_io_server_rest, ev_io_agent_rest; |
---|
5559 | int i; |
---|
5560 | |
---|
5561 | og_loop = ev_default_loop(0); |
---|
5562 | |
---|
5563 | if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) |
---|
5564 | exit(EXIT_FAILURE); |
---|
5565 | |
---|
5566 | openlog("ogAdmServer", LOG_PID, LOG_DAEMON); |
---|
5567 | |
---|
5568 | /*-------------------------------------------------------------------------------------------------------- |
---|
5569 | Validación de parámetros de ejecución y lectura del fichero de configuración del servicio |
---|
5570 | ---------------------------------------------------------------------------------------------------------*/ |
---|
5571 | if (!validacionParametros(argc, argv, 1)) // Valida parámetros de ejecución |
---|
5572 | exit(EXIT_FAILURE); |
---|
5573 | |
---|
5574 | if (!tomaConfiguracion(szPathFileCfg)) { // Toma parametros de configuracion |
---|
5575 | exit(EXIT_FAILURE); |
---|
5576 | } |
---|
5577 | |
---|
5578 | /*-------------------------------------------------------------------------------------------------------- |
---|
5579 | // Inicializa array de información de los clientes |
---|
5580 | ---------------------------------------------------------------------------------------------------------*/ |
---|
5581 | for (i = 0; i < MAXIMOS_CLIENTES; i++) { |
---|
5582 | tbsockets[i].ip[0] = '\0'; |
---|
5583 | tbsockets[i].cli = NULL; |
---|
5584 | } |
---|
5585 | /*-------------------------------------------------------------------------------------------------------- |
---|
5586 | Creación y configuración del socket del servicio |
---|
5587 | ---------------------------------------------------------------------------------------------------------*/ |
---|
5588 | |
---|
5589 | socket_rest = og_socket_server_init("8888"); |
---|
5590 | if (socket_rest < 0) |
---|
5591 | exit(EXIT_FAILURE); |
---|
5592 | |
---|
5593 | ev_io_init(&ev_io_server_rest, og_server_accept_cb, socket_rest, EV_READ); |
---|
5594 | ev_io_start(og_loop, &ev_io_server_rest); |
---|
5595 | |
---|
5596 | socket_agent_rest = og_socket_server_init("8889"); |
---|
5597 | if (socket_agent_rest < 0) |
---|
5598 | exit(EXIT_FAILURE); |
---|
5599 | |
---|
5600 | ev_io_init(&ev_io_agent_rest, og_server_accept_cb, socket_agent_rest, EV_READ); |
---|
5601 | ev_io_start(og_loop, &ev_io_agent_rest); |
---|
5602 | |
---|
5603 | if (og_dbi_schedule_get() < 0) |
---|
5604 | exit(EXIT_FAILURE); |
---|
5605 | |
---|
5606 | og_schedule_next(og_loop); |
---|
5607 | |
---|
5608 | infoLog(1); // Inicio de sesión |
---|
5609 | |
---|
5610 | /* old log file has been deprecated. */ |
---|
5611 | og_log(97, false); |
---|
5612 | |
---|
5613 | syslog(LOG_INFO, "Waiting for connections\n"); |
---|
5614 | |
---|
5615 | while (1) |
---|
5616 | ev_loop(og_loop, 0); |
---|
5617 | |
---|
5618 | exit(EXIT_SUCCESS); |
---|
5619 | } |
---|