[f679cf0] | 1 | // ******************************************************************************************************* |
---|
[3ec149c] | 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 |
---|
[f679cf0] | 8 | // ******************************************************************************************************* |
---|
[3ec149c] | 9 | #include "ogAdmServer.h" |
---|
| 10 | #include "ogAdmLib.c" |
---|
[43070d2] | 11 | #include "dbi.h" |
---|
[7242996] | 12 | #include "list.h" |
---|
[d327da6] | 13 | #include "schedule.h" |
---|
[9baecf8] | 14 | #include <ev.h> |
---|
[13e48b4] | 15 | #include <syslog.h> |
---|
[332487d] | 16 | #include <sys/ioctl.h> |
---|
| 17 | #include <ifaddrs.h> |
---|
[165cea3] | 18 | #include <sys/types.h> |
---|
| 19 | #include <sys/stat.h> |
---|
| 20 | #include <fcntl.h> |
---|
[1a08c06] | 21 | #include <jansson.h> |
---|
[d327da6] | 22 | #include <time.h> |
---|
[7cd0b13] | 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 |
---|
[332487d] | 28 | static char interface[LONPRM]; // Interface name |
---|
[2ccec278] | 29 | static char auth_token[LONPRM]; // API token |
---|
[7cd0b13] | 30 | |
---|
[43070d2] | 31 | static struct og_dbi_config dbi_config = { |
---|
| 32 | .user = usuario, |
---|
| 33 | .passwd = pasguor, |
---|
| 34 | .host = datasource, |
---|
| 35 | .database = catalog, |
---|
| 36 | }; |
---|
| 37 | |
---|
[3ec149c] | 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: |
---|
[5759db40] | 46 | // true: Si el proceso es correcto |
---|
| 47 | // false: En caso de ocurrir algún error |
---|
[3ec149c] | 48 | //________________________________________________________________________________________________________ |
---|
[d647d81] | 49 | static bool tomaConfiguracion(const char *filecfg) |
---|
| 50 | { |
---|
[353da2d] | 51 | char buf[1024], *line; |
---|
| 52 | char *key, *value; |
---|
| 53 | FILE *fcfg; |
---|
[3ec149c] | 54 | |
---|
| 55 | if (filecfg == NULL || strlen(filecfg) == 0) { |
---|
[8c04716] | 56 | syslog(LOG_ERR, "No configuration file has been specified\n"); |
---|
[5759db40] | 57 | return false; |
---|
[3ec149c] | 58 | } |
---|
| 59 | |
---|
| 60 | fcfg = fopen(filecfg, "rt"); |
---|
| 61 | if (fcfg == NULL) { |
---|
[8c04716] | 62 | syslog(LOG_ERR, "Cannot open configuration file `%s'\n", |
---|
| 63 | filecfg); |
---|
[5759db40] | 64 | return false; |
---|
[3ec149c] | 65 | } |
---|
| 66 | |
---|
[8aa0c08] | 67 | servidoradm[0] = '\0'; //inicializar variables globales |
---|
[3ec149c] | 68 | |
---|
[353da2d] | 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); |
---|
[332487d] | 90 | else if (!strcmp(StrToUpper(key), "INTERFACE")) |
---|
| 91 | snprintf(interface, sizeof(interface), "%s", value); |
---|
[2ccec278] | 92 | else if (!strcmp(StrToUpper(key), "APITOKEN")) |
---|
| 93 | snprintf(auth_token, sizeof(auth_token), "%s", value); |
---|
[353da2d] | 94 | |
---|
| 95 | line = fgets(buf, sizeof(buf), fcfg); |
---|
[3ec149c] | 96 | } |
---|
[353da2d] | 97 | |
---|
[bf17dde] | 98 | fclose(fcfg); |
---|
| 99 | |
---|
[f613fb2] | 100 | if (!servidoradm[0]) { |
---|
[8c04716] | 101 | syslog(LOG_ERR, "Missing SERVIDORADM in configuration file\n"); |
---|
[5759db40] | 102 | return false; |
---|
[3ec149c] | 103 | } |
---|
[f613fb2] | 104 | if (!puerto[0]) { |
---|
[8c04716] | 105 | syslog(LOG_ERR, "Missing PUERTO in configuration file\n"); |
---|
[5759db40] | 106 | return false; |
---|
[3ec149c] | 107 | } |
---|
[f613fb2] | 108 | if (!usuario[0]) { |
---|
[8c04716] | 109 | syslog(LOG_ERR, "Missing USUARIO in configuration file\n"); |
---|
[5759db40] | 110 | return false; |
---|
[3ec149c] | 111 | } |
---|
[f613fb2] | 112 | if (!pasguor[0]) { |
---|
[8c04716] | 113 | syslog(LOG_ERR, "Missing PASSWORD in configuration file\n"); |
---|
[5759db40] | 114 | return false; |
---|
[3ec149c] | 115 | } |
---|
[f613fb2] | 116 | if (!datasource[0]) { |
---|
[8c04716] | 117 | syslog(LOG_ERR, "Missing DATASOURCE in configuration file\n"); |
---|
[5759db40] | 118 | return false; |
---|
[3ec149c] | 119 | } |
---|
[f613fb2] | 120 | if (!catalog[0]) { |
---|
[8c04716] | 121 | syslog(LOG_ERR, "Missing CATALOG in configuration file\n"); |
---|
[5759db40] | 122 | return false; |
---|
[3ec149c] | 123 | } |
---|
[332487d] | 124 | if (!interface[0]) |
---|
| 125 | syslog(LOG_ERR, "Missing INTERFACE in configuration file\n"); |
---|
[0a73ecf7] | 126 | |
---|
[5759db40] | 127 | return true; |
---|
[3ec149c] | 128 | } |
---|
[0a73ecf7] | 129 | |
---|
[9baecf8] | 130 | enum og_client_state { |
---|
| 131 | OG_CLIENT_RECEIVING_HEADER = 0, |
---|
| 132 | OG_CLIENT_RECEIVING_PAYLOAD, |
---|
| 133 | OG_CLIENT_PROCESSING_REQUEST, |
---|
| 134 | }; |
---|
| 135 | |
---|
[c90cda4] | 136 | #define OG_MSG_REQUEST_MAXLEN 65536 |
---|
| 137 | #define OG_CMD_MAXLEN 64 |
---|
[d8a2d5f] | 138 | |
---|
[9baecf8] | 139 | /* Shut down connection if there is no complete message after 10 seconds. */ |
---|
| 140 | #define OG_CLIENT_TIMEOUT 10 |
---|
| 141 | |
---|
[c90cda4] | 142 | /* Agent client operation might take longer, shut down after 30 seconds. */ |
---|
| 143 | #define OG_AGENT_CLIENT_TIMEOUT 30 |
---|
| 144 | |
---|
[ecce978] | 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 | |
---|
[c90cda4] | 164 | static LIST_HEAD(client_list); |
---|
| 165 | |
---|
[ecce978] | 166 | enum og_client_status { |
---|
| 167 | OG_CLIENT_STATUS_OGLIVE, |
---|
| 168 | OG_CLIENT_STATUS_BUSY, |
---|
| 169 | }; |
---|
| 170 | |
---|
[9baecf8] | 171 | struct og_client { |
---|
[c90cda4] | 172 | struct list_head list; |
---|
[9baecf8] | 173 | struct ev_io io; |
---|
| 174 | struct ev_timer timer; |
---|
[13e48b4] | 175 | struct sockaddr_in addr; |
---|
[9baecf8] | 176 | enum og_client_state state; |
---|
[d8a2d5f] | 177 | char buf[OG_MSG_REQUEST_MAXLEN]; |
---|
[9baecf8] | 178 | unsigned int buf_len; |
---|
| 179 | unsigned int msg_len; |
---|
[2e0c063] | 180 | int keepalive_idx; |
---|
[1a08c06] | 181 | bool rest; |
---|
[c90cda4] | 182 | bool agent; |
---|
[8793b71] | 183 | int content_length; |
---|
[2ccec278] | 184 | char auth_token[64]; |
---|
[ecce978] | 185 | enum og_client_status status; |
---|
| 186 | enum og_cmd_type last_cmd; |
---|
[21434ba] | 187 | unsigned int last_cmd_id; |
---|
[9baecf8] | 188 | }; |
---|
| 189 | |
---|
| 190 | static inline int og_client_socket(const struct og_client *cli) |
---|
| 191 | { |
---|
| 192 | return cli->io.fd; |
---|
| 193 | } |
---|
| 194 | |
---|
[ecce978] | 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 | |
---|
[3ec149c] | 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: |
---|
[5759db40] | 219 | // true: Si el cliente está disponible |
---|
| 220 | // false: En caso contrario |
---|
[3ec149c] | 221 | // ________________________________________________________________________________________________________ |
---|
[d647d81] | 222 | bool clienteDisponible(char *ip, int* idx) |
---|
| 223 | { |
---|
[3ec149c] | 224 | int estado; |
---|
| 225 | |
---|
| 226 | if (clienteExistente(ip, idx)) { |
---|
| 227 | estado = strcmp(tbsockets[*idx].estado, CLIENTE_OCUPADO); // Cliente ocupado |
---|
| 228 | if (estado == 0) |
---|
[5759db40] | 229 | return false; |
---|
[3ec149c] | 230 | |
---|
| 231 | estado = strcmp(tbsockets[*idx].estado, CLIENTE_APAGADO); // Cliente apagado |
---|
| 232 | if (estado == 0) |
---|
[5759db40] | 233 | return false; |
---|
[3ec149c] | 234 | |
---|
| 235 | estado = strcmp(tbsockets[*idx].estado, CLIENTE_INICIANDO); // Cliente en proceso de inclusión |
---|
| 236 | if (estado == 0) |
---|
[5759db40] | 237 | return false; |
---|
[3ec149c] | 238 | |
---|
[5759db40] | 239 | return true; // En caso contrario el cliente está disponible |
---|
[3ec149c] | 240 | } |
---|
[5759db40] | 241 | return false; // Cliente no está registrado en el sistema |
---|
[3ec149c] | 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: |
---|
[5759db40] | 252 | // true: Si el cliente está registrado |
---|
| 253 | // false: En caso contrario |
---|
[3ec149c] | 254 | // ________________________________________________________________________________________________________ |
---|
[d647d81] | 255 | bool clienteExistente(char *ip, int* idx) |
---|
| 256 | { |
---|
[3ec149c] | 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; |
---|
[5759db40] | 261 | return true; |
---|
[3ec149c] | 262 | } |
---|
| 263 | } |
---|
[5759db40] | 264 | return false; |
---|
[3ec149c] | 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: |
---|
[5759db40] | 277 | // true: Si el proceso es correcto |
---|
| 278 | // false: En caso de ocurrir algún error |
---|
[3ec149c] | 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 | // ________________________________________________________________________________________________________ |
---|
[e052fdc] | 287 | bool actualizaConfiguracion(struct og_dbi *dbi, char *cfg, int ido) |
---|
[3ec149c] | 288 | { |
---|
[7224a0a] | 289 | int lon, p, c,i, dato, swu, idsoi, idsfi,k; |
---|
[0f1e5ad] | 290 | char *ptrPar[MAXPAR], *ptrCfg[7], *ptrDual[2], tbPar[LONSTD]; |
---|
[db4d467] | 291 | char *ser, *disk, *par, *cpt, *sfi, *soi, *tam, *uso; // Parametros de configuración. |
---|
[e052fdc] | 292 | dbi_result result, result_update; |
---|
| 293 | const char *msglog; |
---|
[3ec149c] | 294 | |
---|
[0d6ed7ac] | 295 | lon = 0; |
---|
[3ec149c] | 296 | p = splitCadena(ptrPar, cfg, '\n'); |
---|
| 297 | for (i = 0; i < p; i++) { |
---|
| 298 | c = splitCadena(ptrCfg, ptrPar[i], '\t'); |
---|
[db4d467] | 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. |
---|
[e052fdc] | 306 | result = dbi_conn_queryf(dbi->conn, |
---|
| 307 | "UPDATE ordenadores SET numserie='%s'" |
---|
[ac933ca] | 308 | " WHERE idordenador=%d AND numserie IS NULL", |
---|
[db4d467] | 309 | ser, ido); |
---|
[e052fdc] | 310 | if (!result) { |
---|
| 311 | dbi_conn_error(dbi->conn, &msglog); |
---|
[9bdd4aa] | 312 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 313 | __func__, __LINE__, msglog); |
---|
[5759db40] | 314 | return false; |
---|
[db4d467] | 315 | } |
---|
[9bdd4aa] | 316 | dbi_result_free(result); |
---|
[db4d467] | 317 | } |
---|
| 318 | continue; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | // Distribución de particionado. |
---|
[b0c9683] | 322 | disk = par = cpt = sfi = soi = tam = uso = NULL; |
---|
[db4d467] | 323 | |
---|
[3ec149c] | 324 | splitCadena(ptrDual, ptrCfg[0], '='); |
---|
[ba98026] | 325 | disk = ptrDual[1]; // Número de disco |
---|
[3ec149c] | 326 | |
---|
| 327 | splitCadena(ptrDual, ptrCfg[1], '='); |
---|
[ba98026] | 328 | par = ptrDual[1]; // Número de partición |
---|
| 329 | |
---|
[5f9eca0] | 330 | k=splitCadena(ptrDual, ptrCfg[2], '='); |
---|
| 331 | if(k==2){ |
---|
| 332 | cpt = ptrDual[1]; // Código de partición |
---|
| 333 | }else{ |
---|
[db4d467] | 334 | cpt = (char*)"0"; |
---|
[5f9eca0] | 335 | } |
---|
[3ec149c] | 336 | |
---|
[ba98026] | 337 | k=splitCadena(ptrDual, ptrCfg[3], '='); |
---|
[3ec149c] | 338 | if(k==2){ |
---|
| 339 | sfi = ptrDual[1]; // Sistema de ficheros |
---|
[0a73ecf7] | 340 | /* Comprueba existencia del s0xistema de ficheros instalado */ |
---|
[e052fdc] | 341 | idsfi = checkDato(dbi, sfi, "sistemasficheros", "descripcion","idsistemafichero"); |
---|
[3ec149c] | 342 | } |
---|
| 343 | else |
---|
| 344 | idsfi=0; |
---|
| 345 | |
---|
[ba98026] | 346 | k=splitCadena(ptrDual, ptrCfg[4], '='); |
---|
[3ec149c] | 347 | if(k==2){ // Sistema operativo detecdtado |
---|
| 348 | soi = ptrDual[1]; // Nombre del S.O. instalado |
---|
| 349 | /* Comprueba existencia del sistema operativo instalado */ |
---|
[e052fdc] | 350 | idsoi = checkDato(dbi, soi, "nombresos", "nombreso", "idnombreso"); |
---|
[3ec149c] | 351 | } |
---|
| 352 | else |
---|
| 353 | idsoi=0; |
---|
| 354 | |
---|
[ba98026] | 355 | splitCadena(ptrDual, ptrCfg[5], '='); |
---|
[3ec149c] | 356 | tam = ptrDual[1]; // Tamaño de la partición |
---|
| 357 | |
---|
[b0c9683] | 358 | splitCadena(ptrDual, ptrCfg[6], '='); |
---|
| 359 | uso = ptrDual[1]; // Porcentaje de uso del S.F. |
---|
| 360 | |
---|
[0d6ed7ac] | 361 | lon += sprintf(tbPar + lon, "(%s, %s),", disk, par); |
---|
[3ec149c] | 362 | |
---|
[e052fdc] | 363 | result = dbi_conn_queryf(dbi->conn, |
---|
| 364 | "SELECT numdisk, numpar, tamano, uso, idsistemafichero, idnombreso" |
---|
[b0c9683] | 365 | " FROM ordenadores_particiones" |
---|
| 366 | " WHERE idordenador=%d AND numdisk=%s AND numpar=%s", |
---|
[ba98026] | 367 | ido, disk, par); |
---|
[e052fdc] | 368 | if (!result) { |
---|
| 369 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 370 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 371 | __func__, __LINE__, msglog); |
---|
[5759db40] | 372 | return false; |
---|
[3ec149c] | 373 | } |
---|
[e052fdc] | 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)" |
---|
[b0c9683] | 377 | " VALUES(%d,%s,%s,0x%s,%s,%s,%d,%d,0)", |
---|
| 378 | ido, disk, par, cpt, tam, uso, idsfi, idsoi); |
---|
[e052fdc] | 379 | if (!result_update) { |
---|
| 380 | dbi_conn_error(dbi->conn, &msglog); |
---|
[9bdd4aa] | 381 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 382 | __func__, __LINE__, msglog); |
---|
[5759db40] | 383 | return false; |
---|
[3ec149c] | 384 | } |
---|
[e052fdc] | 385 | dbi_result_free(result_update); |
---|
| 386 | |
---|
[3ec149c] | 387 | } else { // Existe el registro |
---|
[5759db40] | 388 | swu = true; // Se supone que algún dato ha cambiado |
---|
[e052fdc] | 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 |
---|
[83f20d1] | 396 | swu = false; // Todos los parámetros de la partición son iguales, no se actualiza |
---|
[3ec149c] | 397 | } |
---|
| 398 | } |
---|
| 399 | } |
---|
| 400 | if (swu) { // Hay que actualizar los parámetros de la partición |
---|
[e052fdc] | 401 | result_update = dbi_conn_queryf(dbi->conn, |
---|
| 402 | "UPDATE ordenadores_particiones SET " |
---|
[3ec149c] | 403 | " codpar=0x%s," |
---|
| 404 | " tamano=%s," |
---|
[b0c9683] | 405 | " uso=%s," |
---|
[3ec149c] | 406 | " idsistemafichero=%d," |
---|
| 407 | " idnombreso=%d," |
---|
[c916af9] | 408 | " idimagen=0," |
---|
| 409 | " idperfilsoft=0," |
---|
| 410 | " fechadespliegue=NULL" |
---|
[ba98026] | 411 | " WHERE idordenador=%d AND numdisk=%s AND numpar=%s", |
---|
[b0c9683] | 412 | cpt, tam, uso, idsfi, idsoi, ido, disk, par); |
---|
[bbd1290] | 413 | } else { // Actualizar porcentaje de uso. |
---|
[e052fdc] | 414 | result_update = dbi_conn_queryf(dbi->conn, |
---|
| 415 | "UPDATE ordenadores_particiones SET " |
---|
[5118932] | 416 | " codpar=0x%s," |
---|
[bbd1290] | 417 | " uso=%s" |
---|
| 418 | " WHERE idordenador=%d AND numdisk=%s AND numpar=%s", |
---|
[5118932] | 419 | cpt, uso, ido, disk, par); |
---|
[bbd1290] | 420 | } |
---|
[e052fdc] | 421 | if (!result_update) { |
---|
| 422 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 423 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 424 | __func__, __LINE__, msglog); |
---|
[5759db40] | 425 | return false; |
---|
[3ec149c] | 426 | } |
---|
[e052fdc] | 427 | |
---|
| 428 | dbi_result_free(result_update); |
---|
[3ec149c] | 429 | } |
---|
[9bdd4aa] | 430 | dbi_result_free(result); |
---|
[3ec149c] | 431 | } |
---|
[0d6ed7ac] | 432 | lon += sprintf(tbPar + lon, "(0,0)"); |
---|
[3ec149c] | 433 | // Eliminar particiones almacenadas que ya no existen |
---|
[e052fdc] | 434 | result_update = dbi_conn_queryf(dbi->conn, |
---|
| 435 | "DELETE FROM ordenadores_particiones WHERE idordenador=%d AND (numdisk, numpar) NOT IN (%s)", |
---|
[0d6ed7ac] | 436 | ido, tbPar); |
---|
[e052fdc] | 437 | if (!result_update) { |
---|
| 438 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 439 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 440 | __func__, __LINE__, msglog); |
---|
[5759db40] | 441 | return false; |
---|
[3ec149c] | 442 | } |
---|
[e052fdc] | 443 | dbi_result_free(result_update); |
---|
| 444 | |
---|
[5759db40] | 445 | return true; |
---|
[3ec149c] | 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 | |
---|
[e052fdc] | 467 | int checkDato(struct og_dbi *dbi, char *dato, const char *tabla, |
---|
[d647d81] | 468 | const char *nomdato, const char *nomidentificador) |
---|
| 469 | { |
---|
[e052fdc] | 470 | const char *msglog; |
---|
[3ec149c] | 471 | int identificador; |
---|
[e052fdc] | 472 | dbi_result result; |
---|
[3ec149c] | 473 | |
---|
| 474 | if (strlen(dato) == 0) |
---|
| 475 | return (0); // EL dato no tiene valor |
---|
[e052fdc] | 476 | result = dbi_conn_queryf(dbi->conn, |
---|
| 477 | "SELECT %s FROM %s WHERE %s ='%s'", nomidentificador, |
---|
[3ec149c] | 478 | tabla, nomdato, dato); |
---|
| 479 | |
---|
| 480 | // Ejecuta consulta |
---|
[e052fdc] | 481 | if (!result) { |
---|
| 482 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 483 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 484 | __func__, __LINE__, msglog); |
---|
[3ec149c] | 485 | return (0); |
---|
| 486 | } |
---|
[e052fdc] | 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); |
---|
[3ec149c] | 495 | return (0); |
---|
| 496 | } |
---|
| 497 | // Recupera el identificador del software |
---|
[e052fdc] | 498 | identificador = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
[3ec149c] | 499 | } else { |
---|
[e052fdc] | 500 | identificador = dbi_result_get_uint(result, nomidentificador); |
---|
[3ec149c] | 501 | } |
---|
[e052fdc] | 502 | dbi_result_free(result); |
---|
| 503 | |
---|
[3ec149c] | 504 | return (identificador); |
---|
| 505 | } |
---|
[7242996] | 506 | |
---|
| 507 | struct og_task { |
---|
[251b388e] | 508 | uint32_t task_id; |
---|
[7242996] | 509 | uint32_t procedure_id; |
---|
[251b388e] | 510 | uint32_t command_id; |
---|
| 511 | uint32_t center_id; |
---|
| 512 | uint32_t schedule_id; |
---|
[7242996] | 513 | uint32_t type_scope; |
---|
| 514 | uint32_t scope; |
---|
| 515 | const char *filtered_scope; |
---|
| 516 | const char *params; |
---|
[71e678b] | 517 | }; |
---|
[8a2ae26] | 518 | |
---|
[71e678b] | 519 | static TRAMA *og_msg_alloc(char *data, unsigned int len); |
---|
| 520 | static void og_msg_free(TRAMA *ptrTrama); |
---|
| 521 | |
---|
[c8e415b] | 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 |
---|
[0deba63] | 532 | if (sock >= 0 && !mandaTrama(&sock, ptrTrama)) { |
---|
[c8e415b] | 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 | |
---|
[3ec149c] | 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: |
---|
[4329e85] | 547 | // - iph: Cadena de direcciones ip separadas por ";" |
---|
[3ec149c] | 548 | // - mac: Cadena de direcciones mac separadas por ";" |
---|
[4329e85] | 549 | // - mar: Método de arranque (1=Broadcast, 2=Unicast) |
---|
[3ec149c] | 550 | // Devuelve: |
---|
[5759db40] | 551 | // true: Si el proceso es correcto |
---|
| 552 | // false: En caso de ocurrir algún error |
---|
[3ec149c] | 553 | // ________________________________________________________________________________________________________ |
---|
[62c0560] | 554 | |
---|
| 555 | bool Levanta(char *ptrIP[], char *ptrMacs[], int lon, char *mar) |
---|
[4329e85] | 556 | { |
---|
[a52f983] | 557 | unsigned int on = 1; |
---|
[8aa0c08] | 558 | struct sockaddr_in local; |
---|
[62c0560] | 559 | int i, res; |
---|
[aaa2c57] | 560 | int s; |
---|
[3ec149c] | 561 | |
---|
| 562 | /* Creación de socket para envío de magig packet */ |
---|
| 563 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
---|
[70f6f10] | 564 | if (s < 0) { |
---|
[8c04716] | 565 | syslog(LOG_ERR, "cannot create socket for magic packet\n"); |
---|
[5759db40] | 566 | return false; |
---|
[3ec149c] | 567 | } |
---|
[a52f983] | 568 | res = setsockopt(s, SOL_SOCKET, SO_BROADCAST, (unsigned int *) &on, |
---|
| 569 | sizeof(on)); |
---|
[70f6f10] | 570 | if (res < 0) { |
---|
[8c04716] | 571 | syslog(LOG_ERR, "cannot set broadcast socket\n"); |
---|
[5759db40] | 572 | return false; |
---|
[3ec149c] | 573 | } |
---|
[a52f983] | 574 | memset(&local, 0, sizeof(local)); |
---|
[3ec149c] | 575 | local.sin_family = AF_INET; |
---|
[a52f983] | 576 | local.sin_port = htons(PUERTO_WAKEUP); |
---|
| 577 | local.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
| 578 | |
---|
[3ec149c] | 579 | for (i = 0; i < lon; i++) { |
---|
[aaa2c57] | 580 | if (!WakeUp(s, ptrIP[i], ptrMacs[i], mar)) { |
---|
[8c04716] | 581 | syslog(LOG_ERR, "problem sending magic packet\n"); |
---|
[3ec149c] | 582 | close(s); |
---|
[5759db40] | 583 | return false; |
---|
[3ec149c] | 584 | } |
---|
| 585 | } |
---|
| 586 | close(s); |
---|
[5759db40] | 587 | return true; |
---|
[3ec149c] | 588 | } |
---|
[332487d] | 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 | } |
---|
[d2a9dd8] | 624 | freeifaddrs(ifaddr); |
---|
[332487d] | 625 | |
---|
| 626 | ret = sendto(sd, msg, sizeof(*msg), 0, |
---|
[8aa0c08] | 627 | (struct sockaddr *)client, sizeof(*client)); |
---|
[332487d] | 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, |
---|
[8aa0c08] | 645 | (struct sockaddr *)client, sizeof(*client)); |
---|
[332487d] | 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 | |
---|
[3ec149c] | 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 |
---|
[4329e85] | 666 | // - iph : Cadena con la dirección ip |
---|
[3ec149c] | 667 | // - mac : Cadena con la dirección mac en formato XXXXXXXXXXXX |
---|
[4329e85] | 668 | // - mar: Método de arranque (1=Broadcast, 2=Unicast) |
---|
[3ec149c] | 669 | // Devuelve: |
---|
[5759db40] | 670 | // true: Si el proceso es correcto |
---|
| 671 | // false: En caso de ocurrir algún error |
---|
[3ec149c] | 672 | //_____________________________________________________________________________________________________________ |
---|
[4329e85] | 673 | // |
---|
[aaa2c57] | 674 | bool WakeUp(int s, char* iph, char *mac, char *mar) |
---|
[4329e85] | 675 | { |
---|
[43763e4] | 676 | unsigned int macaddr[OG_WOL_MACADDR_LEN]; |
---|
[332487d] | 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; |
---|
[3ec149c] | 683 | |
---|
| 684 | for (i = 0; i < 6; i++) // Primera secuencia de la trama Wake Up (0xFFFFFFFFFFFF) |
---|
| 685 | Trama_WakeUp.secuencia_FF[i] = 0xFF; |
---|
| 686 | |
---|
[a52f983] | 687 | sscanf(mac, "%02x%02x%02x%02x%02x%02x", |
---|
[43763e4] | 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]; |
---|
[3ec149c] | 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 | |
---|
[332487d] | 701 | switch (atoi(mar)) { |
---|
| 702 | case OG_WOL_BROADCAST: |
---|
[aaa2c57] | 703 | ret = wake_up_broadcast(s, &WakeUpCliente, &Trama_WakeUp); |
---|
[332487d] | 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 | } |
---|
[aaa2c57] | 711 | ret = wake_up_unicast(s, &WakeUpCliente, &Trama_WakeUp, &addr); |
---|
[332487d] | 712 | break; |
---|
| 713 | default: |
---|
| 714 | syslog(LOG_ERR, "unknown wol type\n"); |
---|
| 715 | ret = false; |
---|
| 716 | break; |
---|
| 717 | } |
---|
| 718 | return ret; |
---|
[3ec149c] | 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 |
---|
[c916af9] | 730 | // - dsk: Disco de donde se creó |
---|
[3ec149c] | 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: |
---|
[5759db40] | 736 | // true: Si el proceso es correcto |
---|
| 737 | // false: En caso de ocurrir algún error |
---|
[3ec149c] | 738 | // ________________________________________________________________________________________________________ |
---|
[7a8fae6] | 739 | bool actualizaCreacionImagen(struct og_dbi *dbi, char *idi, char *dsk, |
---|
[d647d81] | 740 | char *par, char *cpt, char *ipr, char *ido) |
---|
| 741 | { |
---|
[7a8fae6] | 742 | const char *msglog; |
---|
| 743 | dbi_result result; |
---|
[f029b3b] | 744 | int idr,ifs; |
---|
[3ec149c] | 745 | |
---|
[5a0e8ec] | 746 | /* Toma identificador del repositorio correspondiente al ordenador modelo */ |
---|
[7a8fae6] | 747 | result = dbi_conn_queryf(dbi->conn, |
---|
[c916af9] | 748 | "SELECT repositorios.idrepositorio" |
---|
[5a0e8ec] | 749 | " FROM repositorios" |
---|
| 750 | " LEFT JOIN ordenadores USING (idrepositorio)" |
---|
| 751 | " WHERE repositorios.ip='%s' AND ordenadores.idordenador=%s", ipr, ido); |
---|
[3ec149c] | 752 | |
---|
[7a8fae6] | 753 | if (!result) { |
---|
| 754 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 755 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 756 | __func__, __LINE__, msglog); |
---|
[5759db40] | 757 | return false; |
---|
[3ec149c] | 758 | } |
---|
[7a8fae6] | 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); |
---|
[5759db40] | 764 | return false; |
---|
[3ec149c] | 765 | } |
---|
[7a8fae6] | 766 | idr = dbi_result_get_uint(result, "idrepositorio"); |
---|
| 767 | dbi_result_free(result); |
---|
[3ec149c] | 768 | |
---|
| 769 | /* Toma identificador del perfilsoftware */ |
---|
[7a8fae6] | 770 | result = dbi_conn_queryf(dbi->conn, |
---|
[c916af9] | 771 | "SELECT idperfilsoft" |
---|
| 772 | " FROM ordenadores_particiones" |
---|
| 773 | " WHERE idordenador=%s AND numdisk=%s AND numpar=%s", ido, dsk, par); |
---|
[3ec149c] | 774 | |
---|
[7a8fae6] | 775 | if (!result) { |
---|
| 776 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 777 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 778 | __func__, __LINE__, msglog); |
---|
[5759db40] | 779 | return false; |
---|
[3ec149c] | 780 | } |
---|
[7a8fae6] | 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); |
---|
[5759db40] | 786 | return false; |
---|
[3ec149c] | 787 | } |
---|
[7a8fae6] | 788 | ifs = dbi_result_get_uint(result, "idperfilsoft"); |
---|
| 789 | dbi_result_free(result); |
---|
[3ec149c] | 790 | |
---|
| 791 | /* Actualizar los datos de la imagen */ |
---|
[7a8fae6] | 792 | result = dbi_conn_queryf(dbi->conn, |
---|
[ab4ab39] | 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); |
---|
[3ec149c] | 798 | |
---|
[7a8fae6] | 799 | if (!result) { |
---|
| 800 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 801 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 802 | __func__, __LINE__, msglog); |
---|
[5759db40] | 803 | return false; |
---|
[3ec149c] | 804 | } |
---|
[7a8fae6] | 805 | dbi_result_free(result); |
---|
| 806 | |
---|
[ab4ab39] | 807 | /* Actualizar los datos en el cliente */ |
---|
[7a8fae6] | 808 | result = dbi_conn_queryf(dbi->conn, |
---|
[ab4ab39] | 809 | "UPDATE ordenadores_particiones" |
---|
[f029b3b] | 810 | " SET idimagen=%s, revision=(SELECT revision FROM imagenes WHERE idimagen=%s)," |
---|
| 811 | " fechadespliegue=NOW()" |
---|
[ab4ab39] | 812 | " WHERE idordenador=%s AND numdisk=%s AND numpar=%s", |
---|
[f029b3b] | 813 | idi, idi, ido, dsk, par); |
---|
[7a8fae6] | 814 | if (!result) { |
---|
| 815 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 816 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 817 | __func__, __LINE__, msglog); |
---|
[5759db40] | 818 | return false; |
---|
[ab4ab39] | 819 | } |
---|
[71e678b] | 820 | dbi_result_free(result); |
---|
[3ec149c] | 821 | |
---|
[71e678b] | 822 | return true; |
---|
[0a73ecf7] | 823 | } |
---|
[71e678b] | 824 | |
---|
[0a73ecf7] | 825 | // ________________________________________________________________________________________________________ |
---|
[3ec149c] | 826 | // Función: actualizaRestauracionImagen |
---|
| 827 | // |
---|
| 828 | // Descripción: |
---|
[0a73ecf7] | 829 | // Esta función actualiza la base de datos con el resultado de la restauración de una imagen |
---|
[3ec149c] | 830 | // Parámetros: |
---|
| 831 | // - db: Objeto base de datos (ya operativo) |
---|
| 832 | // - tbl: Objeto tabla |
---|
| 833 | // - idi: Identificador de la imagen |
---|
[c916af9] | 834 | // - dsk: Disco de donde se restauró |
---|
[3ec149c] | 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: |
---|
[5759db40] | 839 | // true: Si el proceso es correcto |
---|
| 840 | // false: En caso de ocurrir algún error |
---|
[3ec149c] | 841 | // ________________________________________________________________________________________________________ |
---|
[e052fdc] | 842 | bool actualizaRestauracionImagen(struct og_dbi *dbi, char *idi, |
---|
[d647d81] | 843 | char *dsk, char *par, char *ido, char *ifs) |
---|
| 844 | { |
---|
[e052fdc] | 845 | const char *msglog; |
---|
| 846 | dbi_result result; |
---|
[3ec149c] | 847 | |
---|
| 848 | /* Actualizar los datos de la imagen */ |
---|
[e052fdc] | 849 | result = dbi_conn_queryf(dbi->conn, |
---|
[c916af9] | 850 | "UPDATE ordenadores_particiones" |
---|
[84fa8d6] | 851 | " SET idimagen=%s, idperfilsoft=%s, fechadespliegue=NOW()," |
---|
[c870c84] | 852 | " revision=(SELECT revision FROM imagenes WHERE idimagen=%s)," |
---|
| 853 | " idnombreso=IFNULL((SELECT idnombreso FROM perfilessoft WHERE idperfilsoft=%s),0)" |
---|
[dbbe689] | 854 | " WHERE idordenador=%s AND numdisk=%s AND numpar=%s", idi, ifs, idi, ifs, ido, dsk, par); |
---|
[3ec149c] | 855 | |
---|
[e052fdc] | 856 | if (!result) { |
---|
| 857 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 858 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 859 | __func__, __LINE__, msglog); |
---|
[5759db40] | 860 | return false; |
---|
[3ec149c] | 861 | } |
---|
[e052fdc] | 862 | dbi_result_free(result); |
---|
| 863 | |
---|
[5759db40] | 864 | return true; |
---|
[3ec149c] | 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 | // ________________________________________________________________________________________________________ |
---|
[0a73ecf7] | 879 | // |
---|
[54bd82d] | 880 | bool actualizaHardware(struct og_dbi *dbi, char *hrd, char *ido, char *npc, |
---|
[d647d81] | 881 | char *idc) |
---|
[0a73ecf7] | 882 | { |
---|
[54bd82d] | 883 | const char *msglog; |
---|
[3ec149c] | 884 | int idtipohardware, idperfilhard; |
---|
| 885 | int lon, i, j, aux; |
---|
[fc480f2] | 886 | bool retval; |
---|
[0a73ecf7] | 887 | char *whard; |
---|
[3ec149c] | 888 | int tbidhardware[MAXHARDWARE]; |
---|
[54bd82d] | 889 | char *tbHardware[MAXHARDWARE],*dualHardware[2], strInt[LONINT], *idhardwares; |
---|
| 890 | dbi_result result; |
---|
[3ec149c] | 891 | |
---|
| 892 | /* Toma Centro (Unidad Organizativa) */ |
---|
[54bd82d] | 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); |
---|
[8c04716] | 898 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 899 | __func__, __LINE__, msglog); |
---|
[5759db40] | 900 | return false; |
---|
[3ec149c] | 901 | } |
---|
[54bd82d] | 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); |
---|
[5759db40] | 906 | return false; |
---|
[3ec149c] | 907 | } |
---|
[54bd82d] | 908 | idperfilhard = dbi_result_get_uint(result, "idperfilhard"); |
---|
| 909 | dbi_result_free(result); |
---|
| 910 | |
---|
[0a73ecf7] | 911 | whard=escaparCadena(hrd); // Codificar comillas simples |
---|
| 912 | if(!whard) |
---|
[5759db40] | 913 | return false; |
---|
[3ec149c] | 914 | /* Recorre componentes hardware*/ |
---|
[0a73ecf7] | 915 | lon = splitCadena(tbHardware, whard, '\n'); |
---|
[3ec149c] | 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]); |
---|
[5759db40] | 921 | RegistraLog(msglog,false); |
---|
[3ec149c] | 922 | } |
---|
| 923 | */ |
---|
| 924 | for (i = 0; i < lon; i++) { |
---|
| 925 | splitCadena(dualHardware, rTrim(tbHardware[i]), '='); |
---|
| 926 | //sprintf(msglog,"nemonico: %s",dualHardware[0]); |
---|
[5759db40] | 927 | //RegistraLog(msglog,false); |
---|
[3ec149c] | 928 | //sprintf(msglog,"valor: %s",dualHardware[1]); |
---|
[5759db40] | 929 | //RegistraLog(msglog,false); |
---|
[54bd82d] | 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); |
---|
[8c04716] | 935 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 936 | __func__, __LINE__, msglog); |
---|
[5759db40] | 937 | return false; |
---|
[3ec149c] | 938 | } |
---|
[54bd82d] | 939 | if (!dbi_result_next_row(result)) { // Tipo de Hardware NO existente |
---|
| 940 | dbi_result_free(result); |
---|
[5759db40] | 941 | return false; |
---|
[3ec149c] | 942 | } else { // Tipo de Hardware Existe |
---|
[54bd82d] | 943 | idtipohardware = dbi_result_get_uint(result, "idtipohardware"); |
---|
| 944 | dbi_result_free(result); |
---|
[3ec149c] | 945 | |
---|
[54bd82d] | 946 | result = dbi_conn_queryf(dbi->conn, |
---|
| 947 | "SELECT idhardware FROM hardwares WHERE idtipohardware=%d AND descripcion='%s'", |
---|
| 948 | idtipohardware, dualHardware[1]); |
---|
[3ec149c] | 949 | |
---|
[54bd82d] | 950 | if (!result) { |
---|
| 951 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 952 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 953 | __func__, __LINE__, msglog); |
---|
[5759db40] | 954 | return false; |
---|
[3ec149c] | 955 | } |
---|
| 956 | |
---|
[54bd82d] | 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) " |
---|
[3ec149c] | 961 | " VALUES(%d,'%s',%s,0)", idtipohardware, |
---|
| 962 | dualHardware[1], idc); |
---|
[54bd82d] | 963 | if (!result) { |
---|
| 964 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 965 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 966 | __func__, __LINE__, msglog); |
---|
[5759db40] | 967 | return false; |
---|
[3ec149c] | 968 | } |
---|
[54bd82d] | 969 | |
---|
| 970 | // Recupera el identificador del hardware |
---|
| 971 | tbidhardware[i] = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
[3ec149c] | 972 | } else { |
---|
[54bd82d] | 973 | tbidhardware[i] = dbi_result_get_uint(result, "idhardware"); |
---|
[3ec149c] | 974 | } |
---|
[54bd82d] | 975 | dbi_result_free(result); |
---|
[3ec149c] | 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) { |
---|
[8c04716] | 994 | syslog(LOG_ERR, "%s:%d OOM\n", __FILE__, __LINE__); |
---|
[5759db40] | 995 | return false; |
---|
[3ec149c] | 996 | } |
---|
| 997 | aux = sprintf(idhardwares, "%d", tbidhardware[0]); |
---|
| 998 | for (i = 1; i < lon; i++) |
---|
| 999 | aux += sprintf(idhardwares + aux, ",%d", tbidhardware[i]); |
---|
| 1000 | |
---|
[54bd82d] | 1001 | if (!cuestionPerfilHardware(dbi, idc, ido, idperfilhard, idhardwares, |
---|
[3ec149c] | 1002 | npc, tbidhardware, lon)) { |
---|
[8c04716] | 1003 | syslog(LOG_ERR, "Problem updating client hardware\n"); |
---|
[5759db40] | 1004 | retval=false; |
---|
[3ec149c] | 1005 | } |
---|
[fc480f2] | 1006 | else { |
---|
[5759db40] | 1007 | retval=true; |
---|
[fc480f2] | 1008 | } |
---|
[0a73ecf7] | 1009 | liberaMemoria(whard); |
---|
[fc480f2] | 1010 | liberaMemoria(idhardwares); |
---|
| 1011 | return (retval); |
---|
[3ec149c] | 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 | // ________________________________________________________________________________________________________ |
---|
[54bd82d] | 1027 | bool cuestionPerfilHardware(struct og_dbi *dbi, char *idc, char *ido, |
---|
[d647d81] | 1028 | int idperfilhardware, char *idhardwares, char *npc, int *tbidhardware, |
---|
[3ec149c] | 1029 | int lon) |
---|
| 1030 | { |
---|
[54bd82d] | 1031 | const char *msglog; |
---|
| 1032 | dbi_result result; |
---|
[3ec149c] | 1033 | int i; |
---|
| 1034 | int nwidperfilhard; |
---|
| 1035 | |
---|
| 1036 | // Busca perfil hard del ordenador que contenga todos los componentes hardware encontrados |
---|
[54bd82d] | 1037 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1038 | "SELECT idperfilhard FROM" |
---|
[3ec149c] | 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); |
---|
[8c04716] | 1045 | |
---|
[54bd82d] | 1046 | if (!result) { |
---|
| 1047 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 1048 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 1049 | __func__, __LINE__, msglog); |
---|
[5759db40] | 1050 | return false; |
---|
[3ec149c] | 1051 | } |
---|
[54bd82d] | 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)" |
---|
[df052e1] | 1057 | " VALUES('Perfil hardware (%s) ',%s,0)", npc, idc); |
---|
[54bd82d] | 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); |
---|
[5759db40] | 1062 | return false; |
---|
[3ec149c] | 1063 | } |
---|
[54bd82d] | 1064 | dbi_result_free(result); |
---|
| 1065 | |
---|
[3ec149c] | 1066 | // Recupera el identificador del nuevo perfil hardware |
---|
[54bd82d] | 1067 | nwidperfilhard = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
| 1068 | |
---|
[3ec149c] | 1069 | // Crea la relación entre perfiles y componenetes hardware |
---|
| 1070 | for (i = 0; i < lon; i++) { |
---|
[54bd82d] | 1071 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1072 | "INSERT perfileshard_hardwares (idperfilhard,idhardware)" |
---|
[3ec149c] | 1073 | " VALUES(%d,%d)", nwidperfilhard, tbidhardware[i]); |
---|
[54bd82d] | 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); |
---|
[5759db40] | 1078 | return false; |
---|
[3ec149c] | 1079 | } |
---|
[54bd82d] | 1080 | dbi_result_free(result); |
---|
[3ec149c] | 1081 | } |
---|
| 1082 | } else { // Existe un perfil con todos esos componentes |
---|
[54bd82d] | 1083 | nwidperfilhard = dbi_result_get_uint(result, "idperfilhard"); |
---|
| 1084 | dbi_result_free(result); |
---|
[3ec149c] | 1085 | } |
---|
| 1086 | if (idperfilhardware != nwidperfilhard) { // No coinciden los perfiles |
---|
| 1087 | // Actualiza el identificador del perfil hardware del ordenador |
---|
[54bd82d] | 1088 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1089 | "UPDATE ordenadores SET idperfilhard=%d" |
---|
[3ec149c] | 1090 | " WHERE idordenador=%s", nwidperfilhard, ido); |
---|
[54bd82d] | 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); |
---|
[5759db40] | 1095 | return false; |
---|
[3ec149c] | 1096 | } |
---|
[54bd82d] | 1097 | dbi_result_free(result); |
---|
[3ec149c] | 1098 | } |
---|
| 1099 | /* Eliminar Relación de hardwares con Perfiles hardware que quedan húerfanos */ |
---|
[54bd82d] | 1100 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1101 | "DELETE FROM perfileshard_hardwares WHERE idperfilhard IN " |
---|
[3ec149c] | 1102 | " (SELECT idperfilhard FROM perfileshard WHERE idperfilhard NOT IN" |
---|
| 1103 | " (SELECT DISTINCT idperfilhard from ordenadores))"); |
---|
[54bd82d] | 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); |
---|
[5759db40] | 1108 | return false; |
---|
[3ec149c] | 1109 | } |
---|
[54bd82d] | 1110 | dbi_result_free(result); |
---|
[3ec149c] | 1111 | |
---|
| 1112 | /* Eliminar Perfiles hardware que quedan húerfanos */ |
---|
[54bd82d] | 1113 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1114 | "DELETE FROM perfileshard WHERE idperfilhard NOT IN" |
---|
[fc480f2] | 1115 | " (SELECT DISTINCT idperfilhard FROM ordenadores)"); |
---|
[54bd82d] | 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); |
---|
[5759db40] | 1120 | return false; |
---|
[3ec149c] | 1121 | } |
---|
[54bd82d] | 1122 | dbi_result_free(result); |
---|
| 1123 | |
---|
[3ec149c] | 1124 | /* Eliminar Relación de hardwares con Perfiles hardware que quedan húerfanos */ |
---|
[54bd82d] | 1125 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1126 | "DELETE FROM perfileshard_hardwares WHERE idperfilhard NOT IN" |
---|
[fc480f2] | 1127 | " (SELECT idperfilhard FROM perfileshard)"); |
---|
[54bd82d] | 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); |
---|
[5759db40] | 1132 | return false; |
---|
[3ec149c] | 1133 | } |
---|
[54bd82d] | 1134 | dbi_result_free(result); |
---|
| 1135 | |
---|
[5759db40] | 1136 | return true; |
---|
[3ec149c] | 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: |
---|
[5759db40] | 1152 | // true: Si el proceso es correcto |
---|
| 1153 | // false: En caso de ocurrir algún error |
---|
[38e2328] | 1154 | // |
---|
| 1155 | // Versión 1.1.0: Se incluye el sistema operativo. Autora: Irina Gómez - ETSII Universidad Sevilla |
---|
[3ec149c] | 1156 | // ________________________________________________________________________________________________________ |
---|
[e052fdc] | 1157 | bool actualizaSoftware(struct og_dbi *dbi, char *sft, char *par,char *ido, |
---|
[d647d81] | 1158 | char *npc, char *idc) |
---|
[0a73ecf7] | 1159 | { |
---|
[38e2328] | 1160 | int i, j, lon, aux, idperfilsoft, idnombreso; |
---|
[fc480f2] | 1161 | bool retval; |
---|
[0a73ecf7] | 1162 | char *wsft; |
---|
[3ec149c] | 1163 | int tbidsoftware[MAXSOFTWARE]; |
---|
[e052fdc] | 1164 | char *tbSoftware[MAXSOFTWARE], strInt[LONINT], *idsoftwares; |
---|
| 1165 | const char *msglog; |
---|
| 1166 | dbi_result result; |
---|
[3ec149c] | 1167 | |
---|
| 1168 | /* Toma Centro (Unidad Organizativa) y perfil software */ |
---|
[e052fdc] | 1169 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1170 | "SELECT idperfilsoft,numpar" |
---|
[3ec149c] | 1171 | " FROM ordenadores_particiones" |
---|
| 1172 | " WHERE idordenador=%s", ido); |
---|
[e052fdc] | 1173 | if (!result) { |
---|
| 1174 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 1175 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 1176 | __func__, __LINE__, msglog); |
---|
[5759db40] | 1177 | return false; |
---|
[3ec149c] | 1178 | } |
---|
| 1179 | idperfilsoft = 0; // Por defecto se supone que el ordenador no tiene aún detectado el perfil software |
---|
[e052fdc] | 1180 | while (dbi_result_next_row(result)) { |
---|
| 1181 | aux = dbi_result_get_uint(result, "numpar"); |
---|
[3ec149c] | 1182 | if (aux == atoi(par)) { // Se encuentra la partición |
---|
[e052fdc] | 1183 | idperfilsoft = dbi_result_get_uint(result, "idperfilsoft"); |
---|
[3ec149c] | 1184 | break; |
---|
| 1185 | } |
---|
| 1186 | } |
---|
[e052fdc] | 1187 | dbi_result_free(result); |
---|
[0a73ecf7] | 1188 | wsft=escaparCadena(sft); // Codificar comillas simples |
---|
| 1189 | if(!wsft) |
---|
[5759db40] | 1190 | return false; |
---|
[3ec149c] | 1191 | |
---|
| 1192 | /* Recorre componentes software*/ |
---|
[0a73ecf7] | 1193 | lon = splitCadena(tbSoftware, wsft, '\n'); |
---|
| 1194 | |
---|
[3ec149c] | 1195 | if (lon == 0) |
---|
[5759db40] | 1196 | return true; // No hay lineas que procesar |
---|
[3ec149c] | 1197 | if (lon > MAXSOFTWARE) |
---|
| 1198 | lon = MAXSOFTWARE; // Limita el número de componentes software |
---|
| 1199 | |
---|
[fe20a3c] | 1200 | idnombreso = 0; |
---|
[3ec149c] | 1201 | for (i = 0; i < lon; i++) { |
---|
[38e2328] | 1202 | // Primera línea es el sistema operativo: se obtiene identificador |
---|
| 1203 | if (i == 0) { |
---|
[e052fdc] | 1204 | idnombreso = checkDato(dbi, rTrim(tbSoftware[i]), "nombresos", "nombreso", "idnombreso"); |
---|
[38e2328] | 1205 | continue; |
---|
| 1206 | } |
---|
| 1207 | |
---|
[e052fdc] | 1208 | result = dbi_conn_queryf(dbi->conn, |
---|
[3ec149c] | 1209 | "SELECT idsoftware FROM softwares WHERE descripcion ='%s'", |
---|
| 1210 | rTrim(tbSoftware[i])); |
---|
[e052fdc] | 1211 | if (!result) { |
---|
| 1212 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 1213 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 1214 | __func__, __LINE__, msglog); |
---|
[5759db40] | 1215 | return false; |
---|
[3ec149c] | 1216 | } |
---|
| 1217 | |
---|
[e052fdc] | 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)" |
---|
[3ec149c] | 1222 | " VALUES(2,'%s',%s,0)", tbSoftware[i], idc); |
---|
[e052fdc] | 1223 | if (!result) { // Error al insertar |
---|
| 1224 | dbi_conn_error(dbi->conn, &msglog); |
---|
[9cf5bf7] | 1225 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 1226 | __func__, __LINE__, msglog); |
---|
[5759db40] | 1227 | return false; |
---|
[3ec149c] | 1228 | } |
---|
[e052fdc] | 1229 | |
---|
[3ec149c] | 1230 | // Recupera el identificador del software |
---|
[e052fdc] | 1231 | tbidsoftware[i] = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
[3ec149c] | 1232 | } else { |
---|
[e052fdc] | 1233 | tbidsoftware[i] = dbi_result_get_uint(result, "idsoftware"); |
---|
[3ec149c] | 1234 | } |
---|
[9cf5bf7] | 1235 | dbi_result_free(result); |
---|
[3ec149c] | 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) { |
---|
[8c04716] | 1254 | syslog(LOG_ERR, "%s:%d OOM\n", __FILE__, __LINE__); |
---|
[5759db40] | 1255 | return false; |
---|
[3ec149c] | 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 |
---|
[e052fdc] | 1262 | if (!cuestionPerfilSoftware(dbi, idc, ido, idperfilsoft, idnombreso, idsoftwares, |
---|
[3ec149c] | 1263 | npc, par, tbidsoftware, lon)) { |
---|
[8c04716] | 1264 | syslog(LOG_ERR, "cannot update software\n"); |
---|
[e052fdc] | 1265 | og_info((char *)msglog); |
---|
[5759db40] | 1266 | retval=false; |
---|
[3ec149c] | 1267 | } |
---|
[fc480f2] | 1268 | else { |
---|
[5759db40] | 1269 | retval=true; |
---|
[fc480f2] | 1270 | } |
---|
[0a73ecf7] | 1271 | liberaMemoria(wsft); |
---|
[fc480f2] | 1272 | liberaMemoria(idsoftwares); |
---|
| 1273 | return (retval); |
---|
[3ec149c] | 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 |
---|
[38e2328] | 1283 | // - idnombreso: Identificador del sistema operativo |
---|
[3ec149c] | 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: |
---|
[5759db40] | 1290 | // true: Si el proceso es correcto |
---|
| 1291 | // false: En caso de ocurrir algún error |
---|
[38e2328] | 1292 | // |
---|
| 1293 | // Versión 1.1.0: Se incluye el sistema operativo. Autora: Irina Gómez - ETSII Universidad Sevilla |
---|
| 1294 | //_________________________________________________________________________________________________________ |
---|
[e052fdc] | 1295 | bool cuestionPerfilSoftware(struct og_dbi *dbi, char *idc, char *ido, |
---|
[d647d81] | 1296 | int idperfilsoftware, int idnombreso, |
---|
| 1297 | char *idsoftwares, char *npc, char *par, |
---|
| 1298 | int *tbidsoftware, int lon) |
---|
| 1299 | { |
---|
[3ec149c] | 1300 | int i, nwidperfilsoft; |
---|
[e052fdc] | 1301 | const char *msglog; |
---|
| 1302 | dbi_result result; |
---|
[3ec149c] | 1303 | |
---|
| 1304 | // Busca perfil soft del ordenador que contenga todos los componentes software encontrados |
---|
[e052fdc] | 1305 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1306 | "SELECT idperfilsoft FROM" |
---|
[3ec149c] | 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); |
---|
[8c04716] | 1313 | |
---|
[e052fdc] | 1314 | if (!result) { |
---|
| 1315 | dbi_conn_error(dbi->conn, &msglog); |
---|
[8c04716] | 1316 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 1317 | __func__, __LINE__, msglog); |
---|
[5759db40] | 1318 | return false; |
---|
[3ec149c] | 1319 | } |
---|
[e052fdc] | 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)" |
---|
[38e2328] | 1324 | " VALUES('Perfil Software (%s, Part:%s) ',%s,0,%i)", npc, par, idc,idnombreso); |
---|
[e052fdc] | 1325 | if (!result) { |
---|
| 1326 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 1327 | og_info((char *)msglog); |
---|
[5759db40] | 1328 | return false; |
---|
[3ec149c] | 1329 | } |
---|
[e052fdc] | 1330 | |
---|
| 1331 | dbi_result_free(result); |
---|
[3ec149c] | 1332 | // Recupera el identificador del nuevo perfil software |
---|
[e052fdc] | 1333 | nwidperfilsoft = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
| 1334 | |
---|
[3ec149c] | 1335 | // Crea la relación entre perfiles y componenetes software |
---|
| 1336 | for (i = 0; i < lon; i++) { |
---|
[e052fdc] | 1337 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1338 | "INSERT perfilessoft_softwares (idperfilsoft,idsoftware)" |
---|
[3ec149c] | 1339 | " VALUES(%d,%d)", nwidperfilsoft, tbidsoftware[i]); |
---|
[e052fdc] | 1340 | if (!result) { |
---|
| 1341 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 1342 | og_info((char *)msglog); |
---|
[5759db40] | 1343 | return false; |
---|
[3ec149c] | 1344 | } |
---|
[e052fdc] | 1345 | dbi_result_free(result); |
---|
[3ec149c] | 1346 | } |
---|
| 1347 | } else { // Existe un perfil con todos esos componentes |
---|
[e052fdc] | 1348 | nwidperfilsoft = dbi_result_get_uint(result, "idperfilsoft"); |
---|
| 1349 | dbi_result_free(result); |
---|
[3ec149c] | 1350 | } |
---|
| 1351 | |
---|
| 1352 | if (idperfilsoftware != nwidperfilsoft) { // No coinciden los perfiles |
---|
| 1353 | // Actualiza el identificador del perfil software del ordenador |
---|
[e052fdc] | 1354 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1355 | "UPDATE ordenadores_particiones SET idperfilsoft=%d,idimagen=0" |
---|
[fc480f2] | 1356 | " WHERE idordenador=%s AND numpar=%s", nwidperfilsoft, ido, par); |
---|
[e052fdc] | 1357 | if (!result) { // Error al insertar |
---|
| 1358 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 1359 | og_info((char *)msglog); |
---|
[5759db40] | 1360 | return false; |
---|
[3ec149c] | 1361 | } |
---|
[e052fdc] | 1362 | dbi_result_free(result); |
---|
[3ec149c] | 1363 | } |
---|
| 1364 | |
---|
| 1365 | /* DEPURACIÓN DE PERFILES SOFTWARE */ |
---|
| 1366 | |
---|
| 1367 | /* Eliminar Relación de softwares con Perfiles software que quedan húerfanos */ |
---|
[e052fdc] | 1368 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1369 | "DELETE FROM perfilessoft_softwares WHERE idperfilsoft IN "\ |
---|
[3ec149c] | 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))"); |
---|
[e052fdc] | 1373 | if (!result) { |
---|
| 1374 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 1375 | og_info((char *)msglog); |
---|
[5759db40] | 1376 | return false; |
---|
[3ec149c] | 1377 | } |
---|
[e052fdc] | 1378 | dbi_result_free(result), |
---|
[3ec149c] | 1379 | /* Eliminar Perfiles software que quedan húerfanos */ |
---|
[e052fdc] | 1380 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1381 | "DELETE FROM perfilessoft WHERE idperfilsoft NOT IN" |
---|
[3ec149c] | 1382 | " (SELECT DISTINCT idperfilsoft from ordenadores_particiones)"\ |
---|
| 1383 | " AND idperfilsoft NOT IN"\ |
---|
| 1384 | " (SELECT DISTINCT idperfilsoft from imagenes)"); |
---|
[e052fdc] | 1385 | if (!result) { |
---|
| 1386 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 1387 | og_info((char *)msglog); |
---|
[5759db40] | 1388 | return false; |
---|
[3ec149c] | 1389 | } |
---|
[e052fdc] | 1390 | dbi_result_free(result), |
---|
| 1391 | |
---|
[3ec149c] | 1392 | /* Eliminar Relación de softwares con Perfiles software que quedan húerfanos */ |
---|
[e052fdc] | 1393 | result = dbi_conn_queryf(dbi->conn, |
---|
| 1394 | "DELETE FROM perfilessoft_softwares WHERE idperfilsoft NOT IN" |
---|
[fc480f2] | 1395 | " (SELECT idperfilsoft from perfilessoft)"); |
---|
[e052fdc] | 1396 | if (!result) { |
---|
| 1397 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 1398 | og_info((char *)msglog); |
---|
[5759db40] | 1399 | return false; |
---|
[3ec149c] | 1400 | } |
---|
[e052fdc] | 1401 | dbi_result_free(result); |
---|
| 1402 | |
---|
[5759db40] | 1403 | return true; |
---|
[3ec149c] | 1404 | } |
---|
[9baecf8] | 1405 | |
---|
[2e0c063] | 1406 | static void og_client_release(struct ev_loop *loop, struct og_client *cli) |
---|
| 1407 | { |
---|
| 1408 | if (cli->keepalive_idx >= 0) { |
---|
[0e16db2] | 1409 | syslog(LOG_DEBUG, "closing keepalive connection for %s:%hu in slot %d\n", |
---|
[2e0c063] | 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 | |
---|
[c90cda4] | 1415 | list_del(&cli->list); |
---|
[2e0c063] | 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) { |
---|
[0e16db2] | 1427 | syslog(LOG_DEBUG, "closing old keepalive connection for %s:%hu\n", |
---|
[2e0c063] | 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 | |
---|
[3973759] | 1442 | static TRAMA *og_msg_alloc(char *data, unsigned int len) |
---|
[39c3261] | 1443 | { |
---|
[9baecf8] | 1444 | TRAMA *ptrTrama; |
---|
[07c51e2] | 1445 | |
---|
| 1446 | ptrTrama = (TRAMA *)reservaMemoria(sizeof(TRAMA)); |
---|
| 1447 | if (!ptrTrama) { |
---|
| 1448 | syslog(LOG_ERR, "OOM\n"); |
---|
[3973759] | 1449 | return NULL; |
---|
[07c51e2] | 1450 | } |
---|
| 1451 | |
---|
| 1452 | initParametros(ptrTrama, len); |
---|
[3973759] | 1453 | memcpy(ptrTrama, "@JMMLCAMDJ_MCDJ", LONGITUD_CABECERATRAMA); |
---|
[07c51e2] | 1454 | memcpy(ptrTrama->parametros, data, len); |
---|
| 1455 | ptrTrama->lonprm = len; |
---|
| 1456 | |
---|
[3973759] | 1457 | return ptrTrama; |
---|
| 1458 | } |
---|
[07c51e2] | 1459 | |
---|
[3973759] | 1460 | static void og_msg_free(TRAMA *ptrTrama) |
---|
| 1461 | { |
---|
[07c51e2] | 1462 | liberaMemoria(ptrTrama->parametros); |
---|
| 1463 | liberaMemoria(ptrTrama); |
---|
[3973759] | 1464 | } |
---|
| 1465 | |
---|
[c935fc9] | 1466 | #define OG_CLIENTS_MAX 4096 |
---|
[2385710e] | 1467 | #define OG_PARTITION_MAX 4 |
---|
| 1468 | |
---|
| 1469 | struct og_partition { |
---|
[c90cda4] | 1470 | const char *disk; |
---|
[2385710e] | 1471 | const char *number; |
---|
| 1472 | const char *code; |
---|
| 1473 | const char *size; |
---|
| 1474 | const char *filesystem; |
---|
| 1475 | const char *format; |
---|
[c90cda4] | 1476 | const char *os; |
---|
| 1477 | const char *used_size; |
---|
[2385710e] | 1478 | }; |
---|
[c935fc9] | 1479 | |
---|
[7258dac] | 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; |
---|
[f72c984] | 1489 | const char *diff_id; |
---|
| 1490 | const char *diff_name; |
---|
[d521845] | 1491 | const char *path; |
---|
| 1492 | const char *method; |
---|
[7258dac] | 1493 | }; |
---|
| 1494 | |
---|
[1a08c06] | 1495 | struct og_msg_params { |
---|
[c935fc9] | 1496 | const char *ips_array[OG_CLIENTS_MAX]; |
---|
| 1497 | const char *mac_array[OG_CLIENTS_MAX]; |
---|
[1a08c06] | 1498 | unsigned int ips_array_len; |
---|
[73c4bdff] | 1499 | const char *wol_type; |
---|
[775f6f0] | 1500 | char run_cmd[4096]; |
---|
[22ab637] | 1501 | const char *disk; |
---|
| 1502 | const char *partition; |
---|
[7f2dd15] | 1503 | const char *repository; |
---|
| 1504 | const char *name; |
---|
| 1505 | const char *id; |
---|
| 1506 | const char *code; |
---|
[fc15dd1] | 1507 | const char *type; |
---|
| 1508 | const char *profile; |
---|
[2385710e] | 1509 | const char *cache; |
---|
| 1510 | const char *cache_size; |
---|
[454ab57] | 1511 | bool echo; |
---|
[2385710e] | 1512 | struct og_partition partition_setup[OG_PARTITION_MAX]; |
---|
[7258dac] | 1513 | struct og_sync_params sync_setup; |
---|
[d327da6] | 1514 | struct og_schedule_time time; |
---|
[7242996] | 1515 | const char *task_id; |
---|
[70a698a] | 1516 | uint64_t flags; |
---|
[1a08c06] | 1517 | }; |
---|
| 1518 | |
---|
[c90cda4] | 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 | |
---|
[70a698a] | 1528 | #define OG_REST_PARAM_ADDR (1UL << 0) |
---|
[becce94] | 1529 | #define OG_REST_PARAM_MAC (1UL << 1) |
---|
| 1530 | #define OG_REST_PARAM_WOL_TYPE (1UL << 2) |
---|
[3debec6] | 1531 | #define OG_REST_PARAM_RUN_CMD (1UL << 3) |
---|
[66ce511] | 1532 | #define OG_REST_PARAM_DISK (1UL << 4) |
---|
| 1533 | #define OG_REST_PARAM_PARTITION (1UL << 5) |
---|
[072f5c5] | 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) |
---|
[bc0ad31] | 1538 | #define OG_REST_PARAM_TYPE (1UL << 10) |
---|
| 1539 | #define OG_REST_PARAM_PROFILE (1UL << 11) |
---|
[12e25a9] | 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) |
---|
[6c1b3a9] | 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) |
---|
[454ab57] | 1558 | #define OG_REST_PARAM_ECHO (1UL << 30) |
---|
[7242996] | 1559 | #define OG_REST_PARAM_TASK (1UL << 31) |
---|
[d327da6] | 1560 | #define OG_REST_PARAM_TIME_YEARS (1UL << 32) |
---|
| 1561 | #define OG_REST_PARAM_TIME_MONTHS (1UL << 33) |
---|
[bb38557] | 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) |
---|
[70a698a] | 1568 | |
---|
[c90cda4] | 1569 | enum og_rest_method { |
---|
| 1570 | OG_METHOD_GET = 0, |
---|
| 1571 | OG_METHOD_POST, |
---|
[ecce978] | 1572 | OG_METHOD_NO_HTTP |
---|
[c90cda4] | 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 | |
---|
[70a698a] | 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 | |
---|
[1a08c06] | 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); |
---|
[70a698a] | 1617 | |
---|
[18d2376] | 1618 | params->flags |= OG_REST_PARAM_ADDR; |
---|
| 1619 | } |
---|
[70a698a] | 1620 | |
---|
[1a08c06] | 1621 | return 0; |
---|
| 1622 | } |
---|
| 1623 | |
---|
[70b1e58] | 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 | |
---|
[d327da6] | 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 | |
---|
[454ab57] | 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 | |
---|
[6c1b3a9] | 1654 | static int og_json_parse_sync_params(json_t *element, |
---|
| 1655 | struct og_msg_params *params) |
---|
[7258dac] | 1656 | { |
---|
| 1657 | const char *key; |
---|
| 1658 | json_t *value; |
---|
| 1659 | int err = 0; |
---|
| 1660 | |
---|
| 1661 | json_object_foreach(element, key, value) { |
---|
[6c1b3a9] | 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 | } |
---|
[7258dac] | 1699 | |
---|
| 1700 | if (err != 0) |
---|
| 1701 | return err; |
---|
| 1702 | } |
---|
| 1703 | return err; |
---|
| 1704 | } |
---|
| 1705 | |
---|
[12e25a9] | 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) |
---|
[c90cda4] | 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) |
---|
[12e25a9] | 1714 | |
---|
| 1715 | static int og_json_parse_partition(json_t *element, |
---|
[c90cda4] | 1716 | struct og_partition *part, |
---|
| 1717 | uint64_t required_flags) |
---|
[70aef0d] | 1718 | { |
---|
[12e25a9] | 1719 | uint64_t flags = 0UL; |
---|
[70aef0d] | 1720 | const char *key; |
---|
| 1721 | json_t *value; |
---|
| 1722 | int err = 0; |
---|
| 1723 | |
---|
| 1724 | json_object_foreach(element, key, value) { |
---|
[12e25a9] | 1725 | if (!strcmp(key, "partition")) { |
---|
[70aef0d] | 1726 | err = og_json_parse_string(value, &part->number); |
---|
[12e25a9] | 1727 | flags |= OG_PARAM_PART_NUMBER; |
---|
| 1728 | } else if (!strcmp(key, "code")) { |
---|
[70aef0d] | 1729 | err = og_json_parse_string(value, &part->code); |
---|
[12e25a9] | 1730 | flags |= OG_PARAM_PART_CODE; |
---|
| 1731 | } else if (!strcmp(key, "filesystem")) { |
---|
[70aef0d] | 1732 | err = og_json_parse_string(value, &part->filesystem); |
---|
[12e25a9] | 1733 | flags |= OG_PARAM_PART_FILESYSTEM; |
---|
| 1734 | } else if (!strcmp(key, "size")) { |
---|
[70aef0d] | 1735 | err = og_json_parse_string(value, &part->size); |
---|
[12e25a9] | 1736 | flags |= OG_PARAM_PART_SIZE; |
---|
| 1737 | } else if (!strcmp(key, "format")) { |
---|
[70aef0d] | 1738 | err = og_json_parse_string(value, &part->format); |
---|
[12e25a9] | 1739 | flags |= OG_PARAM_PART_FORMAT; |
---|
[c90cda4] | 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; |
---|
[12e25a9] | 1749 | } |
---|
[70aef0d] | 1750 | |
---|
| 1751 | if (err < 0) |
---|
| 1752 | return err; |
---|
| 1753 | } |
---|
[12e25a9] | 1754 | |
---|
[c90cda4] | 1755 | if (flags != required_flags) |
---|
[12e25a9] | 1756 | return -1; |
---|
| 1757 | |
---|
[70aef0d] | 1758 | return err; |
---|
[2385710e] | 1759 | } |
---|
| 1760 | |
---|
[12e25a9] | 1761 | static int og_json_parse_partition_setup(json_t *element, |
---|
| 1762 | struct og_msg_params *params) |
---|
[2385710e] | 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 | |
---|
[c90cda4] | 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) |
---|
[70aef0d] | 1782 | return -1; |
---|
[c90cda4] | 1783 | |
---|
| 1784 | params->flags |= (OG_REST_PARAM_PART_0 << i); |
---|
[2385710e] | 1785 | } |
---|
| 1786 | return 0; |
---|
| 1787 | } |
---|
| 1788 | |
---|
[d327da6] | 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; |
---|
[bb38557] | 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; |
---|
[d327da6] | 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 | |
---|
[ecce978] | 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 | |
---|
[1a08c06] | 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 | |
---|
[70a698a] | 1937 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
| 1938 | return -1; |
---|
| 1939 | |
---|
[ecce978] | 1940 | return og_send_request(OG_METHOD_POST, OG_CMD_PROBE, params, NULL); |
---|
[1a08c06] | 1941 | } |
---|
| 1942 | |
---|
[ea03692] | 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; |
---|
[c90cda4] | 1962 | struct og_client *client; |
---|
[ea03692] | 1963 | struct og_buffer og_buffer = { |
---|
| 1964 | .data = buffer_reply, |
---|
| 1965 | }; |
---|
| 1966 | |
---|
| 1967 | array = json_array(); |
---|
| 1968 | if (!array) |
---|
| 1969 | return -1; |
---|
| 1970 | |
---|
[c90cda4] | 1971 | list_for_each_entry(client, &client_list, list) { |
---|
| 1972 | if (!client->agent) |
---|
[ea03692] | 1973 | continue; |
---|
| 1974 | |
---|
| 1975 | object = json_object(); |
---|
| 1976 | if (!object) { |
---|
| 1977 | json_decref(array); |
---|
| 1978 | return -1; |
---|
| 1979 | } |
---|
[c90cda4] | 1980 | addr = json_string(inet_ntoa(client->addr.sin_addr)); |
---|
[ea03692] | 1981 | if (!addr) { |
---|
| 1982 | json_decref(object); |
---|
| 1983 | json_decref(array); |
---|
| 1984 | return -1; |
---|
| 1985 | } |
---|
| 1986 | json_object_set_new(object, "addr", addr); |
---|
[ecce978] | 1987 | state = json_string(og_client_status(client)); |
---|
[ea03692] | 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 | |
---|
[a36ed20] | 2002 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
[ea03692] | 2003 | json_decref(root); |
---|
| 2004 | |
---|
| 2005 | return 0; |
---|
| 2006 | } |
---|
| 2007 | |
---|
[73c4bdff] | 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); |
---|
[becce94] | 2024 | |
---|
| 2025 | params->flags |= OG_REST_PARAM_ADDR; |
---|
[73c4bdff] | 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); |
---|
[becce94] | 2032 | |
---|
| 2033 | params->flags |= OG_REST_PARAM_MAC; |
---|
[73c4bdff] | 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 | |
---|
[becce94] | 2079 | params->flags |= OG_REST_PARAM_WOL_TYPE; |
---|
| 2080 | |
---|
[73c4bdff] | 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 | |
---|
[becce94] | 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 | |
---|
[73c4bdff] | 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 | |
---|
[775f6f0] | 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 | |
---|
[3debec6] | 2124 | params->flags |= OG_REST_PARAM_RUN_CMD; |
---|
| 2125 | |
---|
[775f6f0] | 2126 | return 0; |
---|
| 2127 | } |
---|
| 2128 | |
---|
| 2129 | static int og_cmd_run_post(json_t *element, struct og_msg_params *params) |
---|
| 2130 | { |
---|
[c90cda4] | 2131 | json_t *value, *clients; |
---|
[775f6f0] | 2132 | const char *key; |
---|
| 2133 | unsigned int i; |
---|
[c90cda4] | 2134 | int err = 0; |
---|
[775f6f0] | 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); |
---|
[454ab57] | 2142 | else if (!strcmp(key, "run")) |
---|
[775f6f0] | 2143 | err = og_json_parse_run(value, params); |
---|
[454ab57] | 2144 | else if (!strcmp(key, "echo")) { |
---|
| 2145 | err = og_json_parse_bool(value, ¶ms->echo); |
---|
| 2146 | params->flags |= OG_REST_PARAM_ECHO; |
---|
| 2147 | } |
---|
[775f6f0] | 2148 | |
---|
| 2149 | if (err < 0) |
---|
| 2150 | break; |
---|
| 2151 | } |
---|
| 2152 | |
---|
[3debec6] | 2153 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
[454ab57] | 2154 | OG_REST_PARAM_RUN_CMD | |
---|
| 2155 | OG_REST_PARAM_ECHO)) |
---|
[3debec6] | 2156 | return -1; |
---|
| 2157 | |
---|
[c90cda4] | 2158 | clients = json_copy(element); |
---|
| 2159 | json_object_del(clients, "clients"); |
---|
[775f6f0] | 2160 | |
---|
[ecce978] | 2161 | err = og_send_request(OG_METHOD_POST, OG_CMD_SHELL_RUN, params, clients); |
---|
[775f6f0] | 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 | |
---|
[165cea3] | 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 | |
---|
[22469c6] | 2199 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
| 2200 | return -1; |
---|
| 2201 | |
---|
[165cea3] | 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 | |
---|
[a36ed20] | 2254 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
[165cea3] | 2255 | json_decref(root); |
---|
| 2256 | |
---|
| 2257 | return 0; |
---|
| 2258 | } |
---|
| 2259 | |
---|
[22ab637] | 2260 | static int og_cmd_session(json_t *element, struct og_msg_params *params) |
---|
| 2261 | { |
---|
[c90cda4] | 2262 | json_t *clients, *value; |
---|
[22ab637] | 2263 | const char *key; |
---|
[c90cda4] | 2264 | int err = 0; |
---|
[22ab637] | 2265 | |
---|
| 2266 | if (json_typeof(element) != JSON_OBJECT) |
---|
| 2267 | return -1; |
---|
| 2268 | |
---|
| 2269 | json_object_foreach(element, key, value) { |
---|
[66ce511] | 2270 | if (!strcmp(key, "clients")) { |
---|
[22ab637] | 2271 | err = og_json_parse_clients(value, params); |
---|
[66ce511] | 2272 | } else if (!strcmp(key, "disk")) { |
---|
[70b1e58] | 2273 | err = og_json_parse_string(value, ¶ms->disk); |
---|
[66ce511] | 2274 | params->flags |= OG_REST_PARAM_DISK; |
---|
| 2275 | } else if (!strcmp(key, "partition")) { |
---|
[70b1e58] | 2276 | err = og_json_parse_string(value, ¶ms->partition); |
---|
[66ce511] | 2277 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
| 2278 | } |
---|
[22ab637] | 2279 | |
---|
| 2280 | if (err < 0) |
---|
| 2281 | return err; |
---|
| 2282 | } |
---|
| 2283 | |
---|
[66ce511] | 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 | |
---|
[c90cda4] | 2289 | clients = json_copy(element); |
---|
| 2290 | json_object_del(clients, "clients"); |
---|
[22ab637] | 2291 | |
---|
[ecce978] | 2292 | return og_send_request(OG_METHOD_POST, OG_CMD_SESSION, params, clients); |
---|
[22ab637] | 2293 | } |
---|
| 2294 | |
---|
[b231a5a] | 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 | |
---|
[2aad3d9] | 2312 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
| 2313 | return -1; |
---|
| 2314 | |
---|
[ecce978] | 2315 | return og_send_request(OG_METHOD_POST, OG_CMD_POWEROFF, params, NULL); |
---|
[b231a5a] | 2316 | } |
---|
| 2317 | |
---|
[b317d24] | 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 | |
---|
[3402460] | 2335 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
| 2336 | return -1; |
---|
| 2337 | |
---|
[ecce978] | 2338 | return og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, params, NULL); |
---|
[b317d24] | 2339 | } |
---|
| 2340 | |
---|
[68270b3] | 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 | |
---|
[d86ddce] | 2358 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
| 2359 | return -1; |
---|
| 2360 | |
---|
[ecce978] | 2361 | return og_send_request(OG_METHOD_POST, OG_CMD_REBOOT, params, NULL); |
---|
[68270b3] | 2362 | } |
---|
| 2363 | |
---|
[5513435] | 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 | |
---|
[04a8c17] | 2381 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
| 2382 | return -1; |
---|
| 2383 | |
---|
[ecce978] | 2384 | return og_send_request(OG_METHOD_POST, OG_CMD_STOP, params, NULL); |
---|
[5513435] | 2385 | } |
---|
| 2386 | |
---|
[1316c877] | 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 | |
---|
[a0df5af7] | 2404 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
| 2405 | return -1; |
---|
| 2406 | |
---|
[ecce978] | 2407 | return og_send_request(OG_METHOD_GET, OG_CMD_HARDWARE, params, NULL); |
---|
[1316c877] | 2408 | } |
---|
| 2409 | |
---|
[a0f41fc] | 2410 | static int og_cmd_software(json_t *element, struct og_msg_params *params) |
---|
| 2411 | { |
---|
[c90cda4] | 2412 | json_t *clients, *value; |
---|
[a0f41fc] | 2413 | const char *key; |
---|
[c90cda4] | 2414 | int err = 0; |
---|
[a0f41fc] | 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); |
---|
[c80150e] | 2422 | else if (!strcmp(key, "disk")) { |
---|
[de36dbe] | 2423 | err = og_json_parse_string(value, ¶ms->disk); |
---|
[c80150e] | 2424 | params->flags |= OG_REST_PARAM_DISK; |
---|
| 2425 | } |
---|
| 2426 | else if (!strcmp(key, "partition")) { |
---|
[de36dbe] | 2427 | err = og_json_parse_string(value, ¶ms->partition); |
---|
[c80150e] | 2428 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
| 2429 | } |
---|
[a0f41fc] | 2430 | |
---|
| 2431 | if (err < 0) |
---|
| 2432 | break; |
---|
| 2433 | } |
---|
| 2434 | |
---|
[c80150e] | 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 | |
---|
[c90cda4] | 2440 | clients = json_copy(element); |
---|
| 2441 | json_object_del(clients, "clients"); |
---|
[de36dbe] | 2442 | |
---|
[ecce978] | 2443 | return og_send_request(OG_METHOD_POST, OG_CMD_SOFTWARE, params, clients); |
---|
[a0f41fc] | 2444 | } |
---|
| 2445 | |
---|
[7f2dd15] | 2446 | static int og_cmd_create_image(json_t *element, struct og_msg_params *params) |
---|
| 2447 | { |
---|
[c90cda4] | 2448 | json_t *value, *clients; |
---|
[7f2dd15] | 2449 | const char *key; |
---|
[c90cda4] | 2450 | int err = 0; |
---|
[7f2dd15] | 2451 | |
---|
| 2452 | if (json_typeof(element) != JSON_OBJECT) |
---|
| 2453 | return -1; |
---|
| 2454 | |
---|
| 2455 | json_object_foreach(element, key, value) { |
---|
[072f5c5] | 2456 | if (!strcmp(key, "disk")) { |
---|
[70b1e58] | 2457 | err = og_json_parse_string(value, ¶ms->disk); |
---|
[072f5c5] | 2458 | params->flags |= OG_REST_PARAM_DISK; |
---|
| 2459 | } else if (!strcmp(key, "partition")) { |
---|
[70b1e58] | 2460 | err = og_json_parse_string(value, ¶ms->partition); |
---|
[072f5c5] | 2461 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
| 2462 | } else if (!strcmp(key, "name")) { |
---|
[70b1e58] | 2463 | err = og_json_parse_string(value, ¶ms->name); |
---|
[072f5c5] | 2464 | params->flags |= OG_REST_PARAM_NAME; |
---|
| 2465 | } else if (!strcmp(key, "repository")) { |
---|
[70b1e58] | 2466 | err = og_json_parse_string(value, ¶ms->repository); |
---|
[072f5c5] | 2467 | params->flags |= OG_REST_PARAM_REPO; |
---|
| 2468 | } else if (!strcmp(key, "clients")) { |
---|
[7f2dd15] | 2469 | err = og_json_parse_clients(value, params); |
---|
[072f5c5] | 2470 | } else if (!strcmp(key, "id")) { |
---|
[70b1e58] | 2471 | err = og_json_parse_string(value, ¶ms->id); |
---|
[072f5c5] | 2472 | params->flags |= OG_REST_PARAM_ID; |
---|
| 2473 | } else if (!strcmp(key, "code")) { |
---|
[70b1e58] | 2474 | err = og_json_parse_string(value, ¶ms->code); |
---|
[072f5c5] | 2475 | params->flags |= OG_REST_PARAM_CODE; |
---|
| 2476 | } |
---|
[7f2dd15] | 2477 | |
---|
| 2478 | if (err < 0) |
---|
| 2479 | break; |
---|
| 2480 | } |
---|
| 2481 | |
---|
[072f5c5] | 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 | |
---|
[c90cda4] | 2491 | clients = json_copy(element); |
---|
| 2492 | json_object_del(clients, "clients"); |
---|
[7f2dd15] | 2493 | |
---|
[ecce978] | 2494 | return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_CREATE, params, |
---|
| 2495 | clients); |
---|
[7f2dd15] | 2496 | } |
---|
| 2497 | |
---|
[fc15dd1] | 2498 | static int og_cmd_restore_image(json_t *element, struct og_msg_params *params) |
---|
| 2499 | { |
---|
[c90cda4] | 2500 | json_t *clients, *value; |
---|
[fc15dd1] | 2501 | const char *key; |
---|
[c90cda4] | 2502 | int err = 0; |
---|
[fc15dd1] | 2503 | |
---|
| 2504 | if (json_typeof(element) != JSON_OBJECT) |
---|
| 2505 | return -1; |
---|
| 2506 | |
---|
| 2507 | json_object_foreach(element, key, value) { |
---|
[bc0ad31] | 2508 | if (!strcmp(key, "disk")) { |
---|
[fc15dd1] | 2509 | err = og_json_parse_string(value, ¶ms->disk); |
---|
[bc0ad31] | 2510 | params->flags |= OG_REST_PARAM_DISK; |
---|
| 2511 | } else if (!strcmp(key, "partition")) { |
---|
[fc15dd1] | 2512 | err = og_json_parse_string(value, ¶ms->partition); |
---|
[bc0ad31] | 2513 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
| 2514 | } else if (!strcmp(key, "name")) { |
---|
[fc15dd1] | 2515 | err = og_json_parse_string(value, ¶ms->name); |
---|
[bc0ad31] | 2516 | params->flags |= OG_REST_PARAM_NAME; |
---|
| 2517 | } else if (!strcmp(key, "repository")) { |
---|
[fc15dd1] | 2518 | err = og_json_parse_string(value, ¶ms->repository); |
---|
[bc0ad31] | 2519 | params->flags |= OG_REST_PARAM_REPO; |
---|
| 2520 | } else if (!strcmp(key, "clients")) { |
---|
[fc15dd1] | 2521 | err = og_json_parse_clients(value, params); |
---|
[bc0ad31] | 2522 | } else if (!strcmp(key, "type")) { |
---|
[fc15dd1] | 2523 | err = og_json_parse_string(value, ¶ms->type); |
---|
[bc0ad31] | 2524 | params->flags |= OG_REST_PARAM_TYPE; |
---|
| 2525 | } else if (!strcmp(key, "profile")) { |
---|
[fc15dd1] | 2526 | err = og_json_parse_string(value, ¶ms->profile); |
---|
[bc0ad31] | 2527 | params->flags |= OG_REST_PARAM_PROFILE; |
---|
| 2528 | } else if (!strcmp(key, "id")) { |
---|
[fc15dd1] | 2529 | err = og_json_parse_string(value, ¶ms->id); |
---|
[bc0ad31] | 2530 | params->flags |= OG_REST_PARAM_ID; |
---|
| 2531 | } |
---|
[fc15dd1] | 2532 | |
---|
| 2533 | if (err < 0) |
---|
| 2534 | break; |
---|
| 2535 | } |
---|
| 2536 | |
---|
[bc0ad31] | 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 | |
---|
[c90cda4] | 2547 | clients = json_copy(element); |
---|
| 2548 | json_object_del(clients, "clients"); |
---|
[fc15dd1] | 2549 | |
---|
[ecce978] | 2550 | return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_RESTORE, params, |
---|
| 2551 | clients); |
---|
[fc15dd1] | 2552 | } |
---|
| 2553 | |
---|
[9381fdf] | 2554 | static int og_cmd_setup(json_t *element, struct og_msg_params *params) |
---|
[2385710e] | 2555 | { |
---|
[c90cda4] | 2556 | json_t *value, *clients; |
---|
[2385710e] | 2557 | const char *key; |
---|
[c90cda4] | 2558 | int err = 0; |
---|
[2385710e] | 2559 | |
---|
| 2560 | if (json_typeof(element) != JSON_OBJECT) |
---|
| 2561 | return -1; |
---|
| 2562 | |
---|
| 2563 | json_object_foreach(element, key, value) { |
---|
[12e25a9] | 2564 | if (!strcmp(key, "clients")) { |
---|
[2385710e] | 2565 | err = og_json_parse_clients(value, params); |
---|
[12e25a9] | 2566 | } else if (!strcmp(key, "disk")) { |
---|
[2385710e] | 2567 | err = og_json_parse_string(value, ¶ms->disk); |
---|
[12e25a9] | 2568 | params->flags |= OG_REST_PARAM_DISK; |
---|
| 2569 | } else if (!strcmp(key, "cache")) { |
---|
[2385710e] | 2570 | err = og_json_parse_string(value, ¶ms->cache); |
---|
[12e25a9] | 2571 | params->flags |= OG_REST_PARAM_CACHE; |
---|
| 2572 | } else if (!strcmp(key, "cache_size")) { |
---|
[2385710e] | 2573 | err = og_json_parse_string(value, ¶ms->cache_size); |
---|
[12e25a9] | 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 | } |
---|
[2385710e] | 2578 | |
---|
| 2579 | if (err < 0) |
---|
| 2580 | break; |
---|
| 2581 | } |
---|
| 2582 | |
---|
[12e25a9] | 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 | |
---|
[c90cda4] | 2593 | clients = json_copy(element); |
---|
| 2594 | json_object_del(clients, "clients"); |
---|
[2385710e] | 2595 | |
---|
[ecce978] | 2596 | return og_send_request(OG_METHOD_POST, OG_CMD_SETUP, params, clients); |
---|
[2385710e] | 2597 | } |
---|
| 2598 | |
---|
[3ca392f] | 2599 | static int og_cmd_run_schedule(json_t *element, struct og_msg_params *params) |
---|
| 2600 | { |
---|
| 2601 | const char *key; |
---|
| 2602 | json_t *value; |
---|
[207a74b] | 2603 | int err = 0; |
---|
[3ca392f] | 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 | |
---|
[3aac64e] | 2613 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
| 2614 | return -1; |
---|
| 2615 | |
---|
[ecce978] | 2616 | return og_send_request(OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, params, |
---|
| 2617 | NULL); |
---|
[3ca392f] | 2618 | } |
---|
| 2619 | |
---|
[7258dac] | 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) { |
---|
[6c1b3a9] | 2632 | if (!strcmp(key, "clients")) { |
---|
[7258dac] | 2633 | err = og_json_parse_clients(value, params); |
---|
[6c1b3a9] | 2634 | } else if (!strcmp(key, "disk")) { |
---|
[7258dac] | 2635 | err = og_json_parse_string(value, ¶ms->disk); |
---|
[6c1b3a9] | 2636 | params->flags |= OG_REST_PARAM_DISK; |
---|
| 2637 | } else if (!strcmp(key, "partition")) { |
---|
[7258dac] | 2638 | err = og_json_parse_string(value, ¶ms->partition); |
---|
[6c1b3a9] | 2639 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
| 2640 | } else if (!strcmp(key, "code")) { |
---|
[7258dac] | 2641 | err = og_json_parse_string(value, ¶ms->code); |
---|
[6c1b3a9] | 2642 | params->flags |= OG_REST_PARAM_CODE; |
---|
| 2643 | } else if (!strcmp(key, "id")) { |
---|
[7258dac] | 2644 | err = og_json_parse_string(value, ¶ms->id); |
---|
[6c1b3a9] | 2645 | params->flags |= OG_REST_PARAM_ID; |
---|
| 2646 | } else if (!strcmp(key, "name")) { |
---|
[7258dac] | 2647 | err = og_json_parse_string(value, ¶ms->name); |
---|
[6c1b3a9] | 2648 | params->flags |= OG_REST_PARAM_NAME; |
---|
| 2649 | } else if (!strcmp(key, "repository")) { |
---|
[7258dac] | 2650 | err = og_json_parse_string(value, ¶ms->repository); |
---|
[6c1b3a9] | 2651 | params->flags |= OG_REST_PARAM_REPO; |
---|
| 2652 | } else if (!strcmp(key, "sync_params")) { |
---|
| 2653 | err = og_json_parse_sync_params(value, params); |
---|
| 2654 | } |
---|
[7258dac] | 2655 | |
---|
| 2656 | if (err < 0) |
---|
| 2657 | break; |
---|
| 2658 | } |
---|
| 2659 | |
---|
[6c1b3a9] | 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 | |
---|
[7258dac] | 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 | |
---|
[f72c984] | 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); |
---|
[3e6623d] | 2714 | else if (!strcmp(key, "disk")) { |
---|
[f72c984] | 2715 | err = og_json_parse_string(value, ¶ms->disk); |
---|
[3e6623d] | 2716 | params->flags |= OG_REST_PARAM_DISK; |
---|
| 2717 | } else if (!strcmp(key, "partition")) { |
---|
[f72c984] | 2718 | err = og_json_parse_string(value, ¶ms->partition); |
---|
[3e6623d] | 2719 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
| 2720 | } else if (!strcmp(key, "id")) { |
---|
[f72c984] | 2721 | err = og_json_parse_string(value, ¶ms->id); |
---|
[3e6623d] | 2722 | params->flags |= OG_REST_PARAM_ID; |
---|
| 2723 | } else if (!strcmp(key, "name")) { |
---|
[f72c984] | 2724 | err = og_json_parse_string(value, ¶ms->name); |
---|
[3e6623d] | 2725 | params->flags |= OG_REST_PARAM_NAME; |
---|
| 2726 | } else if (!strcmp(key, "repository")) { |
---|
[f72c984] | 2727 | err = og_json_parse_string(value, ¶ms->repository); |
---|
[3e6623d] | 2728 | params->flags |= OG_REST_PARAM_REPO; |
---|
| 2729 | } else if (!strcmp(key, "sync_params")) { |
---|
| 2730 | err = og_json_parse_sync_params(value, params); |
---|
| 2731 | } |
---|
[f72c984] | 2732 | |
---|
| 2733 | if (err < 0) |
---|
| 2734 | break; |
---|
| 2735 | } |
---|
| 2736 | |
---|
[3e6623d] | 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 | |
---|
[f72c984] | 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 | |
---|
[d521845] | 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) { |
---|
[bd07a93] | 2792 | if (!strcmp(key, "clients")) { |
---|
[d521845] | 2793 | err = og_json_parse_clients(value, params); |
---|
[bd07a93] | 2794 | } else if (!strcmp(key, "disk")) { |
---|
[d521845] | 2795 | err = og_json_parse_string(value, ¶ms->disk); |
---|
[bd07a93] | 2796 | params->flags |= OG_REST_PARAM_DISK; |
---|
| 2797 | } else if (!strcmp(key, "partition")) { |
---|
[d521845] | 2798 | err = og_json_parse_string(value, ¶ms->partition); |
---|
[bd07a93] | 2799 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
| 2800 | } else if (!strcmp(key, "id")) { |
---|
[d521845] | 2801 | err = og_json_parse_string(value, ¶ms->id); |
---|
[bd07a93] | 2802 | params->flags |= OG_REST_PARAM_ID; |
---|
| 2803 | } else if (!strcmp(key, "name")) { |
---|
[d521845] | 2804 | err = og_json_parse_string(value, ¶ms->name); |
---|
[bd07a93] | 2805 | params->flags |= OG_REST_PARAM_NAME; |
---|
| 2806 | } else if (!strcmp(key, "repository")) { |
---|
[d521845] | 2807 | err = og_json_parse_string(value, ¶ms->repository); |
---|
[bd07a93] | 2808 | params->flags |= OG_REST_PARAM_REPO; |
---|
| 2809 | } else if (!strcmp(key, "profile")) { |
---|
[d521845] | 2810 | err = og_json_parse_string(value, ¶ms->profile); |
---|
[bd07a93] | 2811 | params->flags |= OG_REST_PARAM_PROFILE; |
---|
| 2812 | } else if (!strcmp(key, "type")) { |
---|
[d521845] | 2813 | err = og_json_parse_string(value, ¶ms->type); |
---|
[bd07a93] | 2814 | params->flags |= OG_REST_PARAM_TYPE; |
---|
| 2815 | } else if (!strcmp(key, "sync_params")) { |
---|
| 2816 | err = og_json_parse_sync_params(value, params); |
---|
| 2817 | } |
---|
[d521845] | 2818 | |
---|
| 2819 | if (err < 0) |
---|
| 2820 | break; |
---|
| 2821 | } |
---|
| 2822 | |
---|
[bd07a93] | 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 | |
---|
[d521845] | 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 | |
---|
[1707bc6] | 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) { |
---|
[64aef4d] | 2879 | if (!strcmp(key, "clients")) { |
---|
[1707bc6] | 2880 | err = og_json_parse_clients(value, params); |
---|
[64aef4d] | 2881 | } else if (!strcmp(key, "disk")) { |
---|
[1707bc6] | 2882 | err = og_json_parse_string(value, ¶ms->disk); |
---|
[64aef4d] | 2883 | params->flags |= OG_REST_PARAM_DISK; |
---|
| 2884 | } else if (!strcmp(key, "partition")) { |
---|
[1707bc6] | 2885 | err = og_json_parse_string(value, ¶ms->partition); |
---|
[64aef4d] | 2886 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
| 2887 | } else if (!strcmp(key, "id")) { |
---|
[1707bc6] | 2888 | err = og_json_parse_string(value, ¶ms->id); |
---|
[64aef4d] | 2889 | params->flags |= OG_REST_PARAM_ID; |
---|
| 2890 | } else if (!strcmp(key, "name")) { |
---|
[1707bc6] | 2891 | err = og_json_parse_string(value, ¶ms->name); |
---|
[64aef4d] | 2892 | params->flags |= OG_REST_PARAM_NAME; |
---|
| 2893 | } else if (!strcmp(key, "repository")) { |
---|
[1707bc6] | 2894 | err = og_json_parse_string(value, ¶ms->repository); |
---|
[64aef4d] | 2895 | params->flags |= OG_REST_PARAM_REPO; |
---|
| 2896 | } else if (!strcmp(key, "profile")) { |
---|
[1707bc6] | 2897 | err = og_json_parse_string(value, ¶ms->profile); |
---|
[64aef4d] | 2898 | params->flags |= OG_REST_PARAM_PROFILE; |
---|
| 2899 | } else if (!strcmp(key, "type")) { |
---|
[1707bc6] | 2900 | err = og_json_parse_string(value, ¶ms->type); |
---|
[64aef4d] | 2901 | params->flags |= OG_REST_PARAM_TYPE; |
---|
| 2902 | } else if (!strcmp(key, "sync_params")) { |
---|
| 2903 | err = og_json_parse_sync_params(value, params); |
---|
| 2904 | } |
---|
[1707bc6] | 2905 | |
---|
| 2906 | if (err < 0) |
---|
| 2907 | break; |
---|
| 2908 | } |
---|
| 2909 | |
---|
[64aef4d] | 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 | |
---|
[1707bc6] | 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 | |
---|
[ecce978] | 2958 | struct og_cmd { |
---|
[251b388e] | 2959 | uint32_t id; |
---|
[ecce978] | 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 | |
---|
[251b388e] | 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 | |
---|
[7242996] | 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")); |
---|
[58fc387] | 3400 | |
---|
[ecce978] | 3401 | og_cmd_legacy(task->params, cmd); |
---|
[7242996] | 3402 | |
---|
[58fc387] | 3403 | if (task->procedure_id) { |
---|
| 3404 | if (og_dbi_add_action(dbi, task, cmd)) { |
---|
| 3405 | dbi_result_free(result); |
---|
| 3406 | return -1; |
---|
| 3407 | } |
---|
| 3408 | } else { |
---|
| 3409 | cmd->id = task->task_id; |
---|
[251b388e] | 3410 | } |
---|
[7242996] | 3411 | |
---|
[251b388e] | 3412 | list_add_tail(&cmd->list, &cmd_list); |
---|
[7242996] | 3413 | } |
---|
| 3414 | |
---|
| 3415 | dbi_result_free(result); |
---|
| 3416 | |
---|
| 3417 | return 0; |
---|
| 3418 | } |
---|
| 3419 | |
---|
| 3420 | static int og_queue_task_group_clients(struct og_dbi *dbi, struct og_task *task, |
---|
| 3421 | char *query) |
---|
| 3422 | { |
---|
| 3423 | |
---|
| 3424 | const char *msglog; |
---|
| 3425 | dbi_result result; |
---|
| 3426 | |
---|
| 3427 | result = dbi_conn_queryf(dbi->conn, query); |
---|
| 3428 | if (!result) { |
---|
| 3429 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 3430 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 3431 | __func__, __LINE__, msglog); |
---|
| 3432 | return -1; |
---|
| 3433 | } |
---|
| 3434 | |
---|
| 3435 | while (dbi_result_next_row(result)) { |
---|
| 3436 | uint32_t group_id = dbi_result_get_uint(result, "idgrupo"); |
---|
| 3437 | |
---|
| 3438 | sprintf(query, "SELECT idgrupo FROM gruposordenadores " |
---|
| 3439 | "WHERE grupoid=%d", group_id); |
---|
| 3440 | if (og_queue_task_group_clients(dbi, task, query)) { |
---|
| 3441 | dbi_result_free(result); |
---|
| 3442 | return -1; |
---|
| 3443 | } |
---|
| 3444 | |
---|
| 3445 | sprintf(query,"SELECT ip, mac, idordenador FROM ordenadores " |
---|
| 3446 | "WHERE grupoid=%d", group_id); |
---|
| 3447 | if (og_queue_task_command(dbi, task, query)) { |
---|
| 3448 | dbi_result_free(result); |
---|
| 3449 | return -1; |
---|
| 3450 | } |
---|
| 3451 | |
---|
| 3452 | } |
---|
| 3453 | |
---|
| 3454 | dbi_result_free(result); |
---|
| 3455 | |
---|
| 3456 | return 0; |
---|
| 3457 | } |
---|
| 3458 | |
---|
| 3459 | static int og_queue_task_group_classrooms(struct og_dbi *dbi, |
---|
| 3460 | struct og_task *task, char *query) |
---|
| 3461 | { |
---|
| 3462 | |
---|
| 3463 | const char *msglog; |
---|
| 3464 | dbi_result result; |
---|
| 3465 | |
---|
| 3466 | result = dbi_conn_queryf(dbi->conn, query); |
---|
| 3467 | if (!result) { |
---|
| 3468 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 3469 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 3470 | __func__, __LINE__, msglog); |
---|
| 3471 | return -1; |
---|
| 3472 | } |
---|
| 3473 | |
---|
| 3474 | while (dbi_result_next_row(result)) { |
---|
| 3475 | uint32_t group_id = dbi_result_get_uint(result, "idgrupo"); |
---|
| 3476 | |
---|
| 3477 | sprintf(query, "SELECT idgrupo FROM grupos " |
---|
| 3478 | "WHERE grupoid=%d AND tipo=%d", group_id, AMBITO_GRUPOSAULAS); |
---|
| 3479 | if (og_queue_task_group_classrooms(dbi, task, query)) { |
---|
| 3480 | dbi_result_free(result); |
---|
| 3481 | return -1; |
---|
| 3482 | } |
---|
| 3483 | |
---|
[6958249] | 3484 | sprintf(query, |
---|
| 3485 | "SELECT ip,mac,idordenador " |
---|
| 3486 | "FROM ordenadores INNER JOIN aulas " |
---|
| 3487 | "WHERE ordenadores.idaula=aulas.idaula " |
---|
| 3488 | "AND aulas.grupoid=%d", |
---|
| 3489 | group_id); |
---|
| 3490 | if (og_queue_task_command(dbi, task, query)) { |
---|
[7242996] | 3491 | dbi_result_free(result); |
---|
| 3492 | return -1; |
---|
| 3493 | } |
---|
| 3494 | |
---|
| 3495 | } |
---|
| 3496 | |
---|
| 3497 | dbi_result_free(result); |
---|
| 3498 | |
---|
| 3499 | return 0; |
---|
| 3500 | } |
---|
| 3501 | |
---|
| 3502 | static int og_queue_task_clients(struct og_dbi *dbi, struct og_task *task) |
---|
| 3503 | { |
---|
| 3504 | char query[4096]; |
---|
| 3505 | |
---|
| 3506 | switch (task->type_scope) { |
---|
| 3507 | case AMBITO_CENTROS: |
---|
[6958249] | 3508 | sprintf(query, |
---|
| 3509 | "SELECT ip,mac,idordenador " |
---|
| 3510 | "FROM ordenadores INNER JOIN aulas " |
---|
| 3511 | "WHERE ordenadores.idaula=aulas.idaula " |
---|
| 3512 | "AND idcentro=%d", |
---|
| 3513 | task->scope); |
---|
| 3514 | return og_queue_task_command(dbi, task, query); |
---|
[7242996] | 3515 | case AMBITO_GRUPOSAULAS: |
---|
[6958249] | 3516 | sprintf(query, |
---|
| 3517 | "SELECT idgrupo FROM grupos " |
---|
| 3518 | "WHERE idgrupo=%i AND tipo=%d", |
---|
| 3519 | task->scope, AMBITO_GRUPOSAULAS); |
---|
[7242996] | 3520 | return og_queue_task_group_classrooms(dbi, task, query); |
---|
| 3521 | case AMBITO_AULAS: |
---|
[6958249] | 3522 | sprintf(query, |
---|
| 3523 | "SELECT ip,mac,idordenador FROM ordenadores " |
---|
| 3524 | "WHERE idaula=%d", |
---|
| 3525 | task->scope); |
---|
| 3526 | return og_queue_task_command(dbi, task, query); |
---|
[7242996] | 3527 | case AMBITO_GRUPOSORDENADORES: |
---|
[6958249] | 3528 | sprintf(query, |
---|
| 3529 | "SELECT idgrupo FROM gruposordenadores " |
---|
| 3530 | "WHERE idgrupo = %d", |
---|
| 3531 | task->scope); |
---|
[7242996] | 3532 | return og_queue_task_group_clients(dbi, task, query); |
---|
| 3533 | case AMBITO_ORDENADORES: |
---|
[6958249] | 3534 | sprintf(query, |
---|
| 3535 | "SELECT ip, mac, idordenador FROM ordenadores " |
---|
| 3536 | "WHERE idordenador = %d", |
---|
| 3537 | task->scope); |
---|
[7242996] | 3538 | return og_queue_task_command(dbi, task, query); |
---|
| 3539 | } |
---|
| 3540 | return 0; |
---|
| 3541 | } |
---|
| 3542 | |
---|
[d327da6] | 3543 | static int og_dbi_queue_procedure(struct og_dbi *dbi, struct og_task *task) |
---|
[7242996] | 3544 | { |
---|
| 3545 | uint32_t procedure_id; |
---|
| 3546 | const char *msglog; |
---|
| 3547 | dbi_result result; |
---|
| 3548 | |
---|
| 3549 | result = dbi_conn_queryf(dbi->conn, |
---|
[251b388e] | 3550 | "SELECT parametros, procedimientoid, idcomando " |
---|
[7242996] | 3551 | "FROM procedimientos_acciones " |
---|
| 3552 | "WHERE idprocedimiento=%d ORDER BY orden", task->procedure_id); |
---|
| 3553 | if (!result) { |
---|
| 3554 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 3555 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 3556 | __func__, __LINE__, msglog); |
---|
| 3557 | return -1; |
---|
| 3558 | } |
---|
| 3559 | |
---|
| 3560 | while (dbi_result_next_row(result)) { |
---|
| 3561 | procedure_id = dbi_result_get_uint(result, "procedimientoid"); |
---|
| 3562 | if (procedure_id > 0) { |
---|
| 3563 | task->procedure_id = procedure_id; |
---|
[d327da6] | 3564 | if (og_dbi_queue_procedure(dbi, task)) |
---|
[7242996] | 3565 | return -1; |
---|
| 3566 | continue; |
---|
| 3567 | } |
---|
| 3568 | |
---|
| 3569 | task->params = strdup(dbi_result_get_string(result, "parametros")); |
---|
[251b388e] | 3570 | task->command_id = dbi_result_get_uint(result, "idcomando"); |
---|
[7242996] | 3571 | if (og_queue_task_clients(dbi, task)) |
---|
| 3572 | return -1; |
---|
| 3573 | } |
---|
| 3574 | |
---|
| 3575 | dbi_result_free(result); |
---|
| 3576 | |
---|
| 3577 | return 0; |
---|
| 3578 | } |
---|
| 3579 | |
---|
[251b388e] | 3580 | static int og_dbi_queue_task(struct og_dbi *dbi, uint32_t task_id, |
---|
| 3581 | uint32_t schedule_id) |
---|
[7242996] | 3582 | { |
---|
| 3583 | struct og_task task = {}; |
---|
| 3584 | uint32_t task_id_next; |
---|
[ecce978] | 3585 | struct og_cmd *cmd; |
---|
[7242996] | 3586 | const char *msglog; |
---|
| 3587 | dbi_result result; |
---|
| 3588 | |
---|
[251b388e] | 3589 | task.schedule_id = schedule_id; |
---|
| 3590 | |
---|
[7242996] | 3591 | result = dbi_conn_queryf(dbi->conn, |
---|
| 3592 | "SELECT tareas_acciones.orden, " |
---|
| 3593 | "tareas_acciones.idprocedimiento, " |
---|
| 3594 | "tareas_acciones.tareaid, " |
---|
[251b388e] | 3595 | "tareas.idtarea, " |
---|
| 3596 | "tareas.idcentro, " |
---|
[7242996] | 3597 | "tareas.ambito, " |
---|
| 3598 | "tareas.idambito, " |
---|
| 3599 | "tareas.restrambito " |
---|
| 3600 | " FROM tareas" |
---|
| 3601 | " INNER JOIN tareas_acciones ON tareas_acciones.idtarea=tareas.idtarea" |
---|
| 3602 | " WHERE tareas_acciones.idtarea=%u ORDER BY tareas_acciones.orden ASC", task_id); |
---|
| 3603 | if (!result) { |
---|
| 3604 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 3605 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 3606 | __func__, __LINE__, msglog); |
---|
| 3607 | return -1; |
---|
| 3608 | } |
---|
| 3609 | |
---|
| 3610 | while (dbi_result_next_row(result)) { |
---|
[102ba8d] | 3611 | task_id_next = dbi_result_get_uint(result, "tareaid"); |
---|
[7242996] | 3612 | |
---|
| 3613 | if (task_id_next > 0) { |
---|
[251b388e] | 3614 | if (og_dbi_queue_task(dbi, task_id_next, schedule_id)) |
---|
[7242996] | 3615 | return -1; |
---|
| 3616 | |
---|
| 3617 | continue; |
---|
| 3618 | } |
---|
[251b388e] | 3619 | task.task_id = dbi_result_get_uint(result, "idtarea"); |
---|
| 3620 | task.center_id = dbi_result_get_uint(result, "idcentro"); |
---|
[7242996] | 3621 | task.procedure_id = dbi_result_get_uint(result, "idprocedimiento"); |
---|
| 3622 | task.type_scope = dbi_result_get_uint(result, "ambito"); |
---|
| 3623 | task.scope = dbi_result_get_uint(result, "idambito"); |
---|
| 3624 | task.filtered_scope = dbi_result_get_string(result, "restrambito"); |
---|
| 3625 | |
---|
[d327da6] | 3626 | og_dbi_queue_procedure(dbi, &task); |
---|
[7242996] | 3627 | } |
---|
| 3628 | |
---|
| 3629 | dbi_result_free(result); |
---|
| 3630 | |
---|
[ecce978] | 3631 | list_for_each_entry(cmd, &cmd_list, list) { |
---|
| 3632 | if (cmd->type != OG_CMD_WOL) |
---|
| 3633 | continue; |
---|
| 3634 | |
---|
| 3635 | if (!Levanta((char **)cmd->params.ips_array, |
---|
| 3636 | (char **)cmd->params.mac_array, |
---|
| 3637 | cmd->params.ips_array_len, |
---|
| 3638 | (char *)cmd->params.wol_type)) |
---|
| 3639 | return -1; |
---|
| 3640 | } |
---|
| 3641 | |
---|
[7242996] | 3642 | return 0; |
---|
| 3643 | } |
---|
| 3644 | |
---|
[58fc387] | 3645 | static int og_dbi_queue_command(struct og_dbi *dbi, uint32_t task_id, |
---|
| 3646 | uint32_t schedule_id) |
---|
| 3647 | { |
---|
| 3648 | struct og_task task = {}; |
---|
| 3649 | const char *msglog; |
---|
| 3650 | dbi_result result; |
---|
| 3651 | char query[4096]; |
---|
| 3652 | |
---|
| 3653 | result = dbi_conn_queryf(dbi->conn, |
---|
| 3654 | "SELECT idaccion, idcentro, idordenador, parametros " |
---|
| 3655 | "FROM acciones " |
---|
| 3656 | "WHERE sesion = %u", task_id); |
---|
| 3657 | if (!result) { |
---|
| 3658 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 3659 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 3660 | __func__, __LINE__, msglog); |
---|
| 3661 | return -1; |
---|
| 3662 | } |
---|
| 3663 | |
---|
| 3664 | while (dbi_result_next_row(result)) { |
---|
| 3665 | task.task_id = dbi_result_get_uint(result, "idaccion"); |
---|
| 3666 | task.center_id = dbi_result_get_uint(result, "idcentro"); |
---|
| 3667 | task.scope = dbi_result_get_uint(result, "idordenador"); |
---|
| 3668 | task.params = strdup(dbi_result_get_string(result, "parametros")); |
---|
| 3669 | |
---|
| 3670 | sprintf(query, |
---|
| 3671 | "SELECT ip, mac, idordenador FROM ordenadores " |
---|
| 3672 | "WHERE idordenador = %d", |
---|
| 3673 | task.scope); |
---|
| 3674 | if (og_queue_task_command(dbi, &task, query)) { |
---|
| 3675 | dbi_result_free(result); |
---|
| 3676 | return -1; |
---|
| 3677 | } |
---|
| 3678 | } |
---|
| 3679 | |
---|
| 3680 | dbi_result_free(result); |
---|
| 3681 | |
---|
| 3682 | return 0; |
---|
| 3683 | } |
---|
| 3684 | |
---|
| 3685 | void og_schedule_run(unsigned int task_id, unsigned int schedule_id, |
---|
| 3686 | enum og_schedule_type type) |
---|
[d327da6] | 3687 | { |
---|
| 3688 | struct og_msg_params params = {}; |
---|
| 3689 | bool duplicated = false; |
---|
| 3690 | struct og_cmd *cmd; |
---|
| 3691 | struct og_dbi *dbi; |
---|
| 3692 | unsigned int i; |
---|
| 3693 | |
---|
| 3694 | dbi = og_dbi_open(&dbi_config); |
---|
| 3695 | if (!dbi) { |
---|
| 3696 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 3697 | __func__, __LINE__); |
---|
| 3698 | return; |
---|
| 3699 | } |
---|
[58fc387] | 3700 | |
---|
| 3701 | switch (type) { |
---|
| 3702 | case OG_SCHEDULE_TASK: |
---|
| 3703 | og_dbi_queue_task(dbi, task_id, schedule_id); |
---|
| 3704 | break; |
---|
| 3705 | case OG_SCHEDULE_COMMAND: |
---|
| 3706 | og_dbi_queue_command(dbi, task_id, schedule_id); |
---|
| 3707 | break; |
---|
| 3708 | } |
---|
[d327da6] | 3709 | og_dbi_close(dbi); |
---|
| 3710 | |
---|
| 3711 | list_for_each_entry(cmd, &cmd_list, list) { |
---|
| 3712 | for (i = 0; i < params.ips_array_len; i++) { |
---|
| 3713 | if (!strncmp(cmd->ip, params.ips_array[i], |
---|
| 3714 | OG_DB_IP_MAXLEN)) { |
---|
| 3715 | duplicated = true; |
---|
| 3716 | break; |
---|
| 3717 | } |
---|
| 3718 | } |
---|
| 3719 | |
---|
| 3720 | if (!duplicated) |
---|
| 3721 | params.ips_array[params.ips_array_len++] = cmd->ip; |
---|
| 3722 | else |
---|
| 3723 | duplicated = false; |
---|
| 3724 | } |
---|
| 3725 | |
---|
[ecce978] | 3726 | og_send_request(OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, ¶ms, NULL); |
---|
[d327da6] | 3727 | } |
---|
| 3728 | |
---|
[7242996] | 3729 | static int og_cmd_task_post(json_t *element, struct og_msg_params *params) |
---|
| 3730 | { |
---|
| 3731 | struct og_cmd *cmd; |
---|
| 3732 | struct og_dbi *dbi; |
---|
| 3733 | const char *key; |
---|
| 3734 | json_t *value; |
---|
| 3735 | int err; |
---|
| 3736 | |
---|
| 3737 | if (json_typeof(element) != JSON_OBJECT) |
---|
| 3738 | return -1; |
---|
| 3739 | |
---|
| 3740 | json_object_foreach(element, key, value) { |
---|
| 3741 | if (!strcmp(key, "task")) { |
---|
| 3742 | err = og_json_parse_string(value, ¶ms->task_id); |
---|
| 3743 | params->flags |= OG_REST_PARAM_TASK; |
---|
| 3744 | } |
---|
| 3745 | |
---|
| 3746 | if (err < 0) |
---|
| 3747 | break; |
---|
| 3748 | } |
---|
| 3749 | |
---|
| 3750 | if (!og_msg_params_validate(params, OG_REST_PARAM_TASK)) |
---|
| 3751 | return -1; |
---|
| 3752 | |
---|
| 3753 | dbi = og_dbi_open(&dbi_config); |
---|
| 3754 | if (!dbi) { |
---|
| 3755 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 3756 | __func__, __LINE__); |
---|
| 3757 | return -1; |
---|
| 3758 | } |
---|
| 3759 | |
---|
[251b388e] | 3760 | og_dbi_queue_task(dbi, atoi(params->task_id), 0); |
---|
[7242996] | 3761 | og_dbi_close(dbi); |
---|
| 3762 | |
---|
| 3763 | list_for_each_entry(cmd, &cmd_list, list) |
---|
| 3764 | params->ips_array[params->ips_array_len++] = cmd->ip; |
---|
| 3765 | |
---|
[ecce978] | 3766 | return og_send_request(OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, params, |
---|
| 3767 | NULL); |
---|
[7242996] | 3768 | } |
---|
| 3769 | |
---|
[d327da6] | 3770 | static int og_dbi_schedule_get(void) |
---|
| 3771 | { |
---|
| 3772 | uint32_t schedule_id, task_id; |
---|
| 3773 | struct og_schedule_time time; |
---|
| 3774 | struct og_dbi *dbi; |
---|
| 3775 | const char *msglog; |
---|
| 3776 | dbi_result result; |
---|
| 3777 | |
---|
| 3778 | dbi = og_dbi_open(&dbi_config); |
---|
| 3779 | if (!dbi) { |
---|
| 3780 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 3781 | __func__, __LINE__); |
---|
| 3782 | return -1; |
---|
| 3783 | } |
---|
| 3784 | |
---|
| 3785 | result = dbi_conn_queryf(dbi->conn, |
---|
| 3786 | "SELECT idprogramacion, tipoaccion, identificador, " |
---|
| 3787 | "sesion, annos, meses, diario, dias, semanas, horas, " |
---|
| 3788 | "ampm, minutos FROM programaciones " |
---|
| 3789 | "WHERE suspendida = 0"); |
---|
| 3790 | if (!result) { |
---|
| 3791 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 3792 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 3793 | __func__, __LINE__, msglog); |
---|
[d7c20c8] | 3794 | og_dbi_close(dbi); |
---|
[d327da6] | 3795 | return -1; |
---|
| 3796 | } |
---|
| 3797 | |
---|
| 3798 | while (dbi_result_next_row(result)) { |
---|
| 3799 | memset(&time, 0, sizeof(time)); |
---|
| 3800 | schedule_id = dbi_result_get_uint(result, "idprogramacion"); |
---|
| 3801 | task_id = dbi_result_get_uint(result, "identificador"); |
---|
| 3802 | time.years = dbi_result_get_uint(result, "annos"); |
---|
| 3803 | time.months = dbi_result_get_uint(result, "meses"); |
---|
| 3804 | time.weeks = dbi_result_get_uint(result, "semanas"); |
---|
| 3805 | time.week_days = dbi_result_get_uint(result, "dias"); |
---|
| 3806 | time.days = dbi_result_get_uint(result, "diario"); |
---|
| 3807 | time.hours = dbi_result_get_uint(result, "horas"); |
---|
| 3808 | time.am_pm = dbi_result_get_uint(result, "ampm"); |
---|
| 3809 | time.minutes = dbi_result_get_uint(result, "minutos"); |
---|
[23dc851] | 3810 | time.on_start = true; |
---|
[d327da6] | 3811 | |
---|
[6b58007] | 3812 | og_schedule_create(schedule_id, task_id, OG_SCHEDULE_TASK, |
---|
| 3813 | &time); |
---|
[d327da6] | 3814 | } |
---|
| 3815 | |
---|
| 3816 | dbi_result_free(result); |
---|
[d7c20c8] | 3817 | og_dbi_close(dbi); |
---|
[d327da6] | 3818 | |
---|
| 3819 | return 0; |
---|
| 3820 | } |
---|
| 3821 | |
---|
| 3822 | static int og_dbi_schedule_create(struct og_dbi *dbi, |
---|
| 3823 | struct og_msg_params *params, |
---|
[58fc387] | 3824 | uint32_t *schedule_id, |
---|
| 3825 | enum og_schedule_type schedule_type) |
---|
[d327da6] | 3826 | { |
---|
[58fc387] | 3827 | uint8_t suspended = 0; |
---|
| 3828 | uint32_t session = 0; |
---|
[d327da6] | 3829 | const char *msglog; |
---|
| 3830 | dbi_result result; |
---|
[58fc387] | 3831 | uint8_t type; |
---|
| 3832 | |
---|
| 3833 | switch (schedule_type) { |
---|
| 3834 | case OG_SCHEDULE_TASK: |
---|
| 3835 | type = 3; |
---|
| 3836 | break; |
---|
| 3837 | case OG_SCHEDULE_COMMAND: |
---|
| 3838 | session = atoi(params->task_id); |
---|
| 3839 | type = 1; |
---|
| 3840 | break; |
---|
| 3841 | } |
---|
[d327da6] | 3842 | |
---|
| 3843 | result = dbi_conn_queryf(dbi->conn, |
---|
| 3844 | "INSERT INTO programaciones (tipoaccion," |
---|
| 3845 | " identificador, nombrebloque, annos, meses," |
---|
[bb38557] | 3846 | " semanas, dias, diario, horas, ampm, minutos," |
---|
[58fc387] | 3847 | " suspendida, sesion) VALUES (%d, %s, '%s'," |
---|
| 3848 | " %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)", |
---|
| 3849 | type, params->task_id, params->name, |
---|
[bb38557] | 3850 | params->time.years, params->time.months, |
---|
| 3851 | params->time.weeks, params->time.week_days, |
---|
| 3852 | params->time.days, params->time.hours, |
---|
| 3853 | params->time.am_pm, params->time.minutes, |
---|
[58fc387] | 3854 | suspended, session); |
---|
[d327da6] | 3855 | if (!result) { |
---|
| 3856 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 3857 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 3858 | __func__, __LINE__, msglog); |
---|
| 3859 | return -1; |
---|
| 3860 | } |
---|
| 3861 | dbi_result_free(result); |
---|
| 3862 | |
---|
| 3863 | *schedule_id = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
| 3864 | |
---|
| 3865 | return 0; |
---|
| 3866 | } |
---|
| 3867 | |
---|
| 3868 | static int og_dbi_schedule_update(struct og_dbi *dbi, |
---|
| 3869 | struct og_msg_params *params) |
---|
| 3870 | { |
---|
| 3871 | const char *msglog; |
---|
| 3872 | dbi_result result; |
---|
| 3873 | uint8_t type = 3; |
---|
| 3874 | |
---|
| 3875 | result = dbi_conn_queryf(dbi->conn, |
---|
| 3876 | "UPDATE programaciones SET tipoaccion=%d, " |
---|
| 3877 | "identificador='%s', nombrebloque='%s', " |
---|
| 3878 | "annos=%d, meses=%d, " |
---|
| 3879 | "diario=%d, horas=%d, ampm=%d, minutos=%d " |
---|
| 3880 | "WHERE idprogramacion='%s'", |
---|
| 3881 | type, params->task_id, params->name, |
---|
| 3882 | params->time.years, params->time.months, |
---|
| 3883 | params->time.days, params->time.hours, |
---|
| 3884 | params->time.am_pm, params->time.minutes, |
---|
| 3885 | params->id); |
---|
| 3886 | |
---|
| 3887 | if (!result) { |
---|
| 3888 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 3889 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 3890 | __func__, __LINE__, msglog); |
---|
| 3891 | return -1; |
---|
| 3892 | } |
---|
| 3893 | dbi_result_free(result); |
---|
| 3894 | |
---|
| 3895 | return 0; |
---|
| 3896 | } |
---|
| 3897 | |
---|
| 3898 | static int og_dbi_schedule_delete(struct og_dbi *dbi, uint32_t id) |
---|
| 3899 | { |
---|
| 3900 | const char *msglog; |
---|
| 3901 | dbi_result result; |
---|
| 3902 | |
---|
| 3903 | result = dbi_conn_queryf(dbi->conn, |
---|
| 3904 | "DELETE FROM programaciones WHERE idprogramacion=%d", |
---|
| 3905 | id); |
---|
| 3906 | if (!result) { |
---|
| 3907 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 3908 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 3909 | __func__, __LINE__, msglog); |
---|
| 3910 | return -1; |
---|
| 3911 | } |
---|
| 3912 | dbi_result_free(result); |
---|
| 3913 | |
---|
| 3914 | return 0; |
---|
| 3915 | } |
---|
| 3916 | |
---|
[53ef484] | 3917 | struct og_db_schedule { |
---|
| 3918 | uint32_t id; |
---|
| 3919 | uint32_t task_id; |
---|
| 3920 | const char *name; |
---|
| 3921 | struct og_schedule_time time; |
---|
| 3922 | uint32_t week_days; |
---|
| 3923 | uint32_t weeks; |
---|
| 3924 | uint32_t suspended; |
---|
| 3925 | uint32_t session; |
---|
| 3926 | }; |
---|
| 3927 | |
---|
| 3928 | static int og_dbi_schedule_get_json(struct og_dbi *dbi, json_t *root, |
---|
| 3929 | const char *task_id, const char *schedule_id) |
---|
| 3930 | { |
---|
| 3931 | struct og_db_schedule schedule; |
---|
| 3932 | json_t *obj, *array; |
---|
| 3933 | const char *msglog; |
---|
| 3934 | dbi_result result; |
---|
| 3935 | int err = 0; |
---|
| 3936 | |
---|
| 3937 | if (task_id) { |
---|
| 3938 | result = dbi_conn_queryf(dbi->conn, |
---|
| 3939 | "SELECT idprogramacion," |
---|
| 3940 | " identificador, nombrebloque," |
---|
| 3941 | " annos, meses, diario, dias," |
---|
| 3942 | " semanas, horas, ampm," |
---|
| 3943 | " minutos,suspendida, sesion " |
---|
| 3944 | "FROM programaciones " |
---|
| 3945 | "WHERE identificador=%d", |
---|
| 3946 | atoi(task_id)); |
---|
| 3947 | } else if (schedule_id) { |
---|
| 3948 | result = dbi_conn_queryf(dbi->conn, |
---|
| 3949 | "SELECT idprogramacion," |
---|
| 3950 | " identificador, nombrebloque," |
---|
| 3951 | " annos, meses, diario, dias," |
---|
| 3952 | " semanas, horas, ampm," |
---|
| 3953 | " minutos,suspendida, sesion " |
---|
| 3954 | "FROM programaciones " |
---|
| 3955 | "WHERE idprogramacion=%d", |
---|
| 3956 | atoi(schedule_id)); |
---|
| 3957 | } else { |
---|
| 3958 | result = dbi_conn_queryf(dbi->conn, |
---|
| 3959 | "SELECT idprogramacion," |
---|
| 3960 | " identificador, nombrebloque," |
---|
| 3961 | " annos, meses, diario, dias," |
---|
| 3962 | " semanas, horas, ampm," |
---|
| 3963 | " minutos,suspendida, sesion " |
---|
| 3964 | "FROM programaciones"); |
---|
| 3965 | } |
---|
| 3966 | |
---|
| 3967 | if (!result) { |
---|
| 3968 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 3969 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 3970 | __func__, __LINE__, msglog); |
---|
| 3971 | return -1; |
---|
| 3972 | } |
---|
| 3973 | |
---|
| 3974 | array = json_array(); |
---|
| 3975 | if (!array) |
---|
| 3976 | return -1; |
---|
| 3977 | |
---|
| 3978 | while (dbi_result_next_row(result)) { |
---|
| 3979 | schedule.id = dbi_result_get_uint(result, "idprogramacion"); |
---|
| 3980 | schedule.task_id = dbi_result_get_uint(result, "identificador"); |
---|
| 3981 | schedule.name = dbi_result_get_string(result, "nombrebloque"); |
---|
| 3982 | schedule.time.years = dbi_result_get_uint(result, "annos"); |
---|
| 3983 | schedule.time.months = dbi_result_get_uint(result, "meses"); |
---|
| 3984 | schedule.time.days = dbi_result_get_uint(result, "diario"); |
---|
| 3985 | schedule.time.hours = dbi_result_get_uint(result, "horas"); |
---|
| 3986 | schedule.time.am_pm = dbi_result_get_uint(result, "ampm"); |
---|
| 3987 | schedule.time.minutes = dbi_result_get_uint(result, "minutos"); |
---|
| 3988 | schedule.week_days = dbi_result_get_uint(result, "dias"); |
---|
| 3989 | schedule.weeks = dbi_result_get_uint(result, "semanas"); |
---|
| 3990 | schedule.suspended = dbi_result_get_uint(result, "suspendida"); |
---|
| 3991 | schedule.session = dbi_result_get_uint(result, "sesion"); |
---|
| 3992 | |
---|
| 3993 | obj = json_object(); |
---|
| 3994 | if (!obj) { |
---|
| 3995 | err = -1; |
---|
| 3996 | break; |
---|
| 3997 | } |
---|
| 3998 | json_object_set_new(obj, "id", json_integer(schedule.id)); |
---|
| 3999 | json_object_set_new(obj, "task", json_integer(schedule.task_id)); |
---|
| 4000 | json_object_set_new(obj, "name", json_string(schedule.name)); |
---|
| 4001 | json_object_set_new(obj, "years", json_integer(schedule.time.years)); |
---|
| 4002 | json_object_set_new(obj, "months", json_integer(schedule.time.months)); |
---|
| 4003 | json_object_set_new(obj, "days", json_integer(schedule.time.days)); |
---|
| 4004 | json_object_set_new(obj, "hours", json_integer(schedule.time.hours)); |
---|
| 4005 | json_object_set_new(obj, "am_pm", json_integer(schedule.time.am_pm)); |
---|
| 4006 | json_object_set_new(obj, "minutes", json_integer(schedule.time.minutes)); |
---|
| 4007 | json_object_set_new(obj, "week_days", json_integer(schedule.week_days)); |
---|
| 4008 | json_object_set_new(obj, "weeks", json_integer(schedule.weeks)); |
---|
| 4009 | json_object_set_new(obj, "suspended", json_integer(schedule.suspended)); |
---|
| 4010 | json_object_set_new(obj, "session", json_integer(schedule.session)); |
---|
| 4011 | |
---|
| 4012 | json_array_append_new(array, obj); |
---|
| 4013 | } |
---|
| 4014 | |
---|
| 4015 | json_object_set_new(root, "schedule", array); |
---|
| 4016 | |
---|
| 4017 | dbi_result_free(result); |
---|
| 4018 | |
---|
| 4019 | return err; |
---|
| 4020 | } |
---|
| 4021 | |
---|
[d327da6] | 4022 | static struct ev_loop *og_loop; |
---|
| 4023 | |
---|
[4ddd660] | 4024 | static int og_task_schedule_create(struct og_msg_params *params) |
---|
[d327da6] | 4025 | { |
---|
[58fc387] | 4026 | enum og_schedule_type type; |
---|
[d327da6] | 4027 | uint32_t schedule_id; |
---|
| 4028 | struct og_dbi *dbi; |
---|
[4ddd660] | 4029 | int err; |
---|
| 4030 | |
---|
[58fc387] | 4031 | if (!strcmp(params->type, "task")) |
---|
| 4032 | type = OG_SCHEDULE_TASK; |
---|
| 4033 | else if (!strcmp(params->type, "command")) |
---|
| 4034 | type = OG_SCHEDULE_COMMAND; |
---|
| 4035 | else |
---|
| 4036 | return -1; |
---|
| 4037 | |
---|
[4ddd660] | 4038 | dbi = og_dbi_open(&dbi_config); |
---|
| 4039 | if (!dbi) { |
---|
| 4040 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 4041 | __func__, __LINE__); |
---|
| 4042 | return -1; |
---|
| 4043 | } |
---|
| 4044 | |
---|
[58fc387] | 4045 | err = og_dbi_schedule_create(dbi, params, &schedule_id, type); |
---|
[4ddd660] | 4046 | if (err < 0) { |
---|
| 4047 | og_dbi_close(dbi); |
---|
| 4048 | return -1; |
---|
| 4049 | } |
---|
[58fc387] | 4050 | og_schedule_create(schedule_id, atoi(params->task_id), type, |
---|
[6b58007] | 4051 | ¶ms->time); |
---|
[4ddd660] | 4052 | og_schedule_refresh(og_loop); |
---|
| 4053 | og_dbi_close(dbi); |
---|
| 4054 | |
---|
| 4055 | return 0; |
---|
| 4056 | } |
---|
| 4057 | |
---|
| 4058 | static int og_cmd_schedule_create(json_t *element, struct og_msg_params *params) |
---|
| 4059 | { |
---|
[d327da6] | 4060 | const char *key; |
---|
| 4061 | json_t *value; |
---|
| 4062 | int err; |
---|
| 4063 | |
---|
| 4064 | if (json_typeof(element) != JSON_OBJECT) |
---|
| 4065 | return -1; |
---|
| 4066 | |
---|
| 4067 | json_object_foreach(element, key, value) { |
---|
| 4068 | if (!strcmp(key, "task")) { |
---|
| 4069 | err = og_json_parse_string(value, ¶ms->task_id); |
---|
| 4070 | params->flags |= OG_REST_PARAM_TASK; |
---|
| 4071 | } else if (!strcmp(key, "name")) { |
---|
| 4072 | err = og_json_parse_string(value, ¶ms->name); |
---|
| 4073 | params->flags |= OG_REST_PARAM_NAME; |
---|
[4ddd660] | 4074 | } else if (!strcmp(key, "when")) { |
---|
[d327da6] | 4075 | err = og_json_parse_time_params(value, params); |
---|
[4ddd660] | 4076 | } else if (!strcmp(key, "type")) { |
---|
| 4077 | err = og_json_parse_string(value, ¶ms->type); |
---|
| 4078 | params->flags |= OG_REST_PARAM_TYPE; |
---|
| 4079 | } |
---|
[d327da6] | 4080 | |
---|
| 4081 | if (err < 0) |
---|
| 4082 | break; |
---|
| 4083 | } |
---|
| 4084 | |
---|
| 4085 | if (!og_msg_params_validate(params, OG_REST_PARAM_TASK | |
---|
| 4086 | OG_REST_PARAM_NAME | |
---|
| 4087 | OG_REST_PARAM_TIME_YEARS | |
---|
| 4088 | OG_REST_PARAM_TIME_MONTHS | |
---|
[bb38557] | 4089 | OG_REST_PARAM_TIME_WEEKS | |
---|
| 4090 | OG_REST_PARAM_TIME_WEEK_DAYS | |
---|
[d327da6] | 4091 | OG_REST_PARAM_TIME_DAYS | |
---|
| 4092 | OG_REST_PARAM_TIME_HOURS | |
---|
| 4093 | OG_REST_PARAM_TIME_MINUTES | |
---|
[58fc387] | 4094 | OG_REST_PARAM_TIME_AM_PM | |
---|
| 4095 | OG_REST_PARAM_TYPE)) |
---|
[d327da6] | 4096 | return -1; |
---|
| 4097 | |
---|
[4ddd660] | 4098 | return og_task_schedule_create(params); |
---|
[d327da6] | 4099 | } |
---|
| 4100 | |
---|
| 4101 | static int og_cmd_schedule_update(json_t *element, struct og_msg_params *params) |
---|
| 4102 | { |
---|
| 4103 | struct og_dbi *dbi; |
---|
| 4104 | const char *key; |
---|
| 4105 | json_t *value; |
---|
| 4106 | int err; |
---|
| 4107 | |
---|
| 4108 | if (json_typeof(element) != JSON_OBJECT) |
---|
| 4109 | return -1; |
---|
| 4110 | |
---|
| 4111 | json_object_foreach(element, key, value) { |
---|
| 4112 | if (!strcmp(key, "id")) { |
---|
| 4113 | err = og_json_parse_string(value, ¶ms->id); |
---|
| 4114 | params->flags |= OG_REST_PARAM_ID; |
---|
| 4115 | } else if (!strcmp(key, "task")) { |
---|
| 4116 | err = og_json_parse_string(value, ¶ms->task_id); |
---|
| 4117 | params->flags |= OG_REST_PARAM_TASK; |
---|
| 4118 | } else if (!strcmp(key, "name")) { |
---|
| 4119 | err = og_json_parse_string(value, ¶ms->name); |
---|
| 4120 | params->flags |= OG_REST_PARAM_NAME; |
---|
[53ef484] | 4121 | } else if (!strcmp(key, "when")) |
---|
[d327da6] | 4122 | err = og_json_parse_time_params(value, params); |
---|
| 4123 | |
---|
| 4124 | if (err < 0) |
---|
| 4125 | break; |
---|
| 4126 | } |
---|
| 4127 | |
---|
| 4128 | if (!og_msg_params_validate(params, OG_REST_PARAM_ID | |
---|
| 4129 | OG_REST_PARAM_TASK | |
---|
| 4130 | OG_REST_PARAM_NAME | |
---|
| 4131 | OG_REST_PARAM_TIME_YEARS | |
---|
| 4132 | OG_REST_PARAM_TIME_MONTHS | |
---|
| 4133 | OG_REST_PARAM_TIME_DAYS | |
---|
| 4134 | OG_REST_PARAM_TIME_HOURS | |
---|
| 4135 | OG_REST_PARAM_TIME_MINUTES | |
---|
| 4136 | OG_REST_PARAM_TIME_AM_PM)) |
---|
| 4137 | return -1; |
---|
| 4138 | |
---|
| 4139 | dbi = og_dbi_open(&dbi_config); |
---|
| 4140 | if (!dbi) { |
---|
| 4141 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 4142 | __func__, __LINE__); |
---|
| 4143 | return -1; |
---|
| 4144 | } |
---|
| 4145 | |
---|
| 4146 | err = og_dbi_schedule_update(dbi, params); |
---|
| 4147 | og_dbi_close(dbi); |
---|
| 4148 | |
---|
| 4149 | if (err < 0) |
---|
| 4150 | return err; |
---|
| 4151 | |
---|
| 4152 | og_schedule_update(og_loop, atoi(params->id), atoi(params->task_id), |
---|
| 4153 | ¶ms->time); |
---|
| 4154 | og_schedule_refresh(og_loop); |
---|
| 4155 | |
---|
| 4156 | return err; |
---|
| 4157 | } |
---|
| 4158 | |
---|
| 4159 | static int og_cmd_schedule_delete(json_t *element, struct og_msg_params *params) |
---|
| 4160 | { |
---|
| 4161 | struct og_dbi *dbi; |
---|
| 4162 | const char *key; |
---|
| 4163 | json_t *value; |
---|
| 4164 | int err; |
---|
| 4165 | |
---|
| 4166 | if (json_typeof(element) != JSON_OBJECT) |
---|
| 4167 | return -1; |
---|
| 4168 | |
---|
| 4169 | json_object_foreach(element, key, value) { |
---|
| 4170 | if (!strcmp(key, "id")) { |
---|
| 4171 | err = og_json_parse_string(value, ¶ms->id); |
---|
| 4172 | params->flags |= OG_REST_PARAM_ID; |
---|
| 4173 | } else { |
---|
| 4174 | return -1; |
---|
| 4175 | } |
---|
| 4176 | |
---|
| 4177 | if (err < 0) |
---|
| 4178 | break; |
---|
| 4179 | } |
---|
| 4180 | |
---|
| 4181 | if (!og_msg_params_validate(params, OG_REST_PARAM_ID)) |
---|
| 4182 | return -1; |
---|
| 4183 | |
---|
| 4184 | dbi = og_dbi_open(&dbi_config); |
---|
| 4185 | if (!dbi) { |
---|
| 4186 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 4187 | __func__, __LINE__); |
---|
| 4188 | return -1; |
---|
| 4189 | } |
---|
| 4190 | |
---|
| 4191 | err = og_dbi_schedule_delete(dbi, atoi(params->id)); |
---|
| 4192 | og_dbi_close(dbi); |
---|
| 4193 | |
---|
| 4194 | og_schedule_delete(og_loop, atoi(params->id)); |
---|
| 4195 | |
---|
| 4196 | return err; |
---|
| 4197 | } |
---|
| 4198 | |
---|
[53ef484] | 4199 | static int og_cmd_schedule_get(json_t *element, struct og_msg_params *params, |
---|
| 4200 | char *buffer_reply) |
---|
| 4201 | { |
---|
| 4202 | struct og_buffer og_buffer = { |
---|
| 4203 | .data = buffer_reply, |
---|
| 4204 | }; |
---|
| 4205 | json_t *schedule_root; |
---|
| 4206 | struct og_dbi *dbi; |
---|
| 4207 | const char *key; |
---|
| 4208 | json_t *value; |
---|
| 4209 | int err; |
---|
| 4210 | |
---|
| 4211 | if (element) { |
---|
| 4212 | if (json_typeof(element) != JSON_OBJECT) |
---|
| 4213 | return -1; |
---|
| 4214 | |
---|
| 4215 | json_object_foreach(element, key, value) { |
---|
| 4216 | if (!strcmp(key, "task")) { |
---|
| 4217 | err = og_json_parse_string(value, |
---|
| 4218 | ¶ms->task_id); |
---|
| 4219 | } else if (!strcmp(key, "id")) { |
---|
| 4220 | err = og_json_parse_string(value, ¶ms->id); |
---|
| 4221 | } else { |
---|
| 4222 | return -1; |
---|
| 4223 | } |
---|
| 4224 | |
---|
| 4225 | if (err < 0) |
---|
| 4226 | break; |
---|
| 4227 | } |
---|
| 4228 | } |
---|
| 4229 | |
---|
| 4230 | dbi = og_dbi_open(&dbi_config); |
---|
| 4231 | if (!dbi) { |
---|
| 4232 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 4233 | __func__, __LINE__); |
---|
| 4234 | return -1; |
---|
| 4235 | } |
---|
| 4236 | |
---|
| 4237 | schedule_root = json_object(); |
---|
| 4238 | if (!schedule_root) { |
---|
| 4239 | og_dbi_close(dbi); |
---|
| 4240 | return -1; |
---|
| 4241 | } |
---|
| 4242 | |
---|
| 4243 | err = og_dbi_schedule_get_json(dbi, schedule_root, |
---|
| 4244 | params->task_id, params->id); |
---|
| 4245 | og_dbi_close(dbi); |
---|
| 4246 | |
---|
| 4247 | if (err >= 0) |
---|
| 4248 | json_dump_callback(schedule_root, og_json_dump_clients, &og_buffer, 0); |
---|
| 4249 | |
---|
| 4250 | json_decref(schedule_root); |
---|
| 4251 | |
---|
| 4252 | return err; |
---|
| 4253 | } |
---|
| 4254 | |
---|
[e804134] | 4255 | static int og_client_method_not_found(struct og_client *cli) |
---|
| 4256 | { |
---|
| 4257 | /* To meet RFC 7231, this function MUST generate an Allow header field |
---|
| 4258 | * containing the correct methods. For example: "Allow: POST\r\n" |
---|
| 4259 | */ |
---|
| 4260 | char buf[] = "HTTP/1.1 405 Method Not Allowed\r\n" |
---|
| 4261 | "Content-Length: 0\r\n\r\n"; |
---|
| 4262 | |
---|
| 4263 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
| 4264 | |
---|
| 4265 | return -1; |
---|
| 4266 | } |
---|
| 4267 | |
---|
[391f9be] | 4268 | static int og_client_bad_request(struct og_client *cli) |
---|
| 4269 | { |
---|
| 4270 | char buf[] = "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n"; |
---|
| 4271 | |
---|
| 4272 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
| 4273 | |
---|
| 4274 | return -1; |
---|
| 4275 | } |
---|
| 4276 | |
---|
[1a08c06] | 4277 | static int og_client_not_found(struct og_client *cli) |
---|
| 4278 | { |
---|
| 4279 | char buf[] = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; |
---|
| 4280 | |
---|
| 4281 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
| 4282 | |
---|
| 4283 | return -1; |
---|
| 4284 | } |
---|
| 4285 | |
---|
[2ccec278] | 4286 | static int og_client_not_authorized(struct og_client *cli) |
---|
| 4287 | { |
---|
[f06fcf6f] | 4288 | char buf[] = "HTTP/1.1 401 Unauthorized\r\n" |
---|
| 4289 | "WWW-Authenticate: Basic\r\n" |
---|
| 4290 | "Content-Length: 0\r\n\r\n"; |
---|
[2ccec278] | 4291 | |
---|
| 4292 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
| 4293 | |
---|
| 4294 | return -1; |
---|
| 4295 | } |
---|
| 4296 | |
---|
[d6852fb] | 4297 | static int og_server_internal_error(struct og_client *cli) |
---|
| 4298 | { |
---|
| 4299 | char buf[] = "HTTP/1.1 500 Internal Server Error\r\n" |
---|
| 4300 | "Content-Length: 0\r\n\r\n"; |
---|
| 4301 | |
---|
| 4302 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
| 4303 | |
---|
| 4304 | return -1; |
---|
| 4305 | } |
---|
| 4306 | |
---|
[4d7fd58] | 4307 | static int og_client_payload_too_large(struct og_client *cli) |
---|
| 4308 | { |
---|
| 4309 | char buf[] = "HTTP/1.1 413 Payload Too Large\r\n" |
---|
| 4310 | "Content-Length: 0\r\n\r\n"; |
---|
| 4311 | |
---|
| 4312 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
| 4313 | |
---|
| 4314 | return -1; |
---|
| 4315 | } |
---|
| 4316 | |
---|
[611f470] | 4317 | #define OG_MSG_RESPONSE_MAXLEN 65536 |
---|
| 4318 | |
---|
[ea03692] | 4319 | static int og_client_ok(struct og_client *cli, char *buf_reply) |
---|
[1a08c06] | 4320 | { |
---|
[611f470] | 4321 | char buf[OG_MSG_RESPONSE_MAXLEN] = {}; |
---|
[5bbcedc] | 4322 | int err = 0, len; |
---|
[ea03692] | 4323 | |
---|
[5bbcedc] | 4324 | len = snprintf(buf, sizeof(buf), |
---|
| 4325 | "HTTP/1.1 200 OK\r\nContent-Length: %ld\r\n\r\n%s", |
---|
| 4326 | strlen(buf_reply), buf_reply); |
---|
[d6852fb] | 4327 | if (len >= (int)sizeof(buf)) |
---|
| 4328 | err = og_server_internal_error(cli); |
---|
[1a08c06] | 4329 | |
---|
| 4330 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
| 4331 | |
---|
[5bbcedc] | 4332 | return err; |
---|
[1a08c06] | 4333 | } |
---|
| 4334 | |
---|
| 4335 | static int og_client_state_process_payload_rest(struct og_client *cli) |
---|
| 4336 | { |
---|
[611f470] | 4337 | char buf_reply[OG_MSG_RESPONSE_MAXLEN] = {}; |
---|
[1a08c06] | 4338 | struct og_msg_params params = {}; |
---|
| 4339 | enum og_rest_method method; |
---|
[b3467f7] | 4340 | const char *cmd, *body; |
---|
[1a08c06] | 4341 | json_error_t json_err; |
---|
| 4342 | json_t *root = NULL; |
---|
| 4343 | int err = 0; |
---|
| 4344 | |
---|
[2cced2b3] | 4345 | syslog(LOG_DEBUG, "%s:%hu %.32s ...\n", |
---|
| 4346 | inet_ntoa(cli->addr.sin_addr), |
---|
| 4347 | ntohs(cli->addr.sin_port), cli->buf); |
---|
| 4348 | |
---|
[1a08c06] | 4349 | if (!strncmp(cli->buf, "GET", strlen("GET"))) { |
---|
| 4350 | method = OG_METHOD_GET; |
---|
| 4351 | cmd = cli->buf + strlen("GET") + 2; |
---|
| 4352 | } else if (!strncmp(cli->buf, "POST", strlen("POST"))) { |
---|
| 4353 | method = OG_METHOD_POST; |
---|
| 4354 | cmd = cli->buf + strlen("POST") + 2; |
---|
| 4355 | } else |
---|
[e804134] | 4356 | return og_client_method_not_found(cli); |
---|
[1a08c06] | 4357 | |
---|
| 4358 | body = strstr(cli->buf, "\r\n\r\n") + 4; |
---|
| 4359 | |
---|
[2ccec278] | 4360 | if (strcmp(cli->auth_token, auth_token)) { |
---|
| 4361 | syslog(LOG_ERR, "wrong Authentication key\n"); |
---|
| 4362 | return og_client_not_authorized(cli); |
---|
| 4363 | } |
---|
| 4364 | |
---|
[b3467f7] | 4365 | if (cli->content_length) { |
---|
[1a08c06] | 4366 | root = json_loads(body, 0, &json_err); |
---|
| 4367 | if (!root) { |
---|
| 4368 | syslog(LOG_ERR, "malformed json line %d: %s\n", |
---|
| 4369 | json_err.line, json_err.text); |
---|
| 4370 | return og_client_not_found(cli); |
---|
| 4371 | } |
---|
| 4372 | } |
---|
| 4373 | |
---|
| 4374 | if (!strncmp(cmd, "clients", strlen("clients"))) { |
---|
[ea03692] | 4375 | if (method != OG_METHOD_POST && |
---|
| 4376 | method != OG_METHOD_GET) |
---|
[e804134] | 4377 | return og_client_method_not_found(cli); |
---|
[ea03692] | 4378 | |
---|
| 4379 | if (method == OG_METHOD_POST && !root) { |
---|
[1a08c06] | 4380 | syslog(LOG_ERR, "command clients with no payload\n"); |
---|
[391f9be] | 4381 | return og_client_bad_request(cli); |
---|
[1a08c06] | 4382 | } |
---|
[ea03692] | 4383 | switch (method) { |
---|
| 4384 | case OG_METHOD_POST: |
---|
| 4385 | err = og_cmd_post_clients(root, ¶ms); |
---|
| 4386 | break; |
---|
| 4387 | case OG_METHOD_GET: |
---|
| 4388 | err = og_cmd_get_clients(root, ¶ms, buf_reply); |
---|
| 4389 | break; |
---|
[ecce978] | 4390 | default: |
---|
| 4391 | return og_client_bad_request(cli); |
---|
[ea03692] | 4392 | } |
---|
[73c4bdff] | 4393 | } else if (!strncmp(cmd, "wol", strlen("wol"))) { |
---|
| 4394 | if (method != OG_METHOD_POST) |
---|
[e804134] | 4395 | return og_client_method_not_found(cli); |
---|
[73c4bdff] | 4396 | |
---|
| 4397 | if (!root) { |
---|
| 4398 | syslog(LOG_ERR, "command wol with no payload\n"); |
---|
[391f9be] | 4399 | return og_client_bad_request(cli); |
---|
[73c4bdff] | 4400 | } |
---|
| 4401 | err = og_cmd_wol(root, ¶ms); |
---|
[775f6f0] | 4402 | } else if (!strncmp(cmd, "shell/run", strlen("shell/run"))) { |
---|
| 4403 | if (method != OG_METHOD_POST) |
---|
[e804134] | 4404 | return og_client_method_not_found(cli); |
---|
[775f6f0] | 4405 | |
---|
| 4406 | if (!root) { |
---|
| 4407 | syslog(LOG_ERR, "command run with no payload\n"); |
---|
[391f9be] | 4408 | return og_client_bad_request(cli); |
---|
[775f6f0] | 4409 | } |
---|
| 4410 | err = og_cmd_run_post(root, ¶ms); |
---|
[165cea3] | 4411 | } else if (!strncmp(cmd, "shell/output", strlen("shell/output"))) { |
---|
| 4412 | if (method != OG_METHOD_POST) |
---|
[e804134] | 4413 | return og_client_method_not_found(cli); |
---|
[165cea3] | 4414 | |
---|
| 4415 | if (!root) { |
---|
| 4416 | syslog(LOG_ERR, "command output with no payload\n"); |
---|
[391f9be] | 4417 | return og_client_bad_request(cli); |
---|
[165cea3] | 4418 | } |
---|
| 4419 | |
---|
| 4420 | err = og_cmd_run_get(root, ¶ms, buf_reply); |
---|
[22ab637] | 4421 | } else if (!strncmp(cmd, "session", strlen("session"))) { |
---|
| 4422 | if (method != OG_METHOD_POST) |
---|
[e804134] | 4423 | return og_client_method_not_found(cli); |
---|
[22ab637] | 4424 | |
---|
| 4425 | if (!root) { |
---|
| 4426 | syslog(LOG_ERR, "command session with no payload\n"); |
---|
[391f9be] | 4427 | return og_client_bad_request(cli); |
---|
[22ab637] | 4428 | } |
---|
| 4429 | err = og_cmd_session(root, ¶ms); |
---|
[b231a5a] | 4430 | } else if (!strncmp(cmd, "poweroff", strlen("poweroff"))) { |
---|
| 4431 | if (method != OG_METHOD_POST) |
---|
[e804134] | 4432 | return og_client_method_not_found(cli); |
---|
[b231a5a] | 4433 | |
---|
| 4434 | if (!root) { |
---|
| 4435 | syslog(LOG_ERR, "command poweroff with no payload\n"); |
---|
[391f9be] | 4436 | return og_client_bad_request(cli); |
---|
[b231a5a] | 4437 | } |
---|
| 4438 | err = og_cmd_poweroff(root, ¶ms); |
---|
[68270b3] | 4439 | } else if (!strncmp(cmd, "reboot", strlen("reboot"))) { |
---|
| 4440 | if (method != OG_METHOD_POST) |
---|
[e804134] | 4441 | return og_client_method_not_found(cli); |
---|
[68270b3] | 4442 | |
---|
| 4443 | if (!root) { |
---|
| 4444 | syslog(LOG_ERR, "command reboot with no payload\n"); |
---|
[391f9be] | 4445 | return og_client_bad_request(cli); |
---|
[68270b3] | 4446 | } |
---|
| 4447 | err = og_cmd_reboot(root, ¶ms); |
---|
[5513435] | 4448 | } else if (!strncmp(cmd, "stop", strlen("stop"))) { |
---|
| 4449 | if (method != OG_METHOD_POST) |
---|
[e804134] | 4450 | return og_client_method_not_found(cli); |
---|
[5513435] | 4451 | |
---|
| 4452 | if (!root) { |
---|
| 4453 | syslog(LOG_ERR, "command stop with no payload\n"); |
---|
[391f9be] | 4454 | return og_client_bad_request(cli); |
---|
[5513435] | 4455 | } |
---|
| 4456 | err = og_cmd_stop(root, ¶ms); |
---|
[b317d24] | 4457 | } else if (!strncmp(cmd, "refresh", strlen("refresh"))) { |
---|
| 4458 | if (method != OG_METHOD_POST) |
---|
[e804134] | 4459 | return og_client_method_not_found(cli); |
---|
[b317d24] | 4460 | |
---|
| 4461 | if (!root) { |
---|
| 4462 | syslog(LOG_ERR, "command refresh with no payload\n"); |
---|
[391f9be] | 4463 | return og_client_bad_request(cli); |
---|
[b317d24] | 4464 | } |
---|
| 4465 | err = og_cmd_refresh(root, ¶ms); |
---|
[1316c877] | 4466 | } else if (!strncmp(cmd, "hardware", strlen("hardware"))) { |
---|
| 4467 | if (method != OG_METHOD_POST) |
---|
[e804134] | 4468 | return og_client_method_not_found(cli); |
---|
[1316c877] | 4469 | |
---|
| 4470 | if (!root) { |
---|
| 4471 | syslog(LOG_ERR, "command hardware with no payload\n"); |
---|
[391f9be] | 4472 | return og_client_bad_request(cli); |
---|
[1316c877] | 4473 | } |
---|
| 4474 | err = og_cmd_hardware(root, ¶ms); |
---|
[a0f41fc] | 4475 | } else if (!strncmp(cmd, "software", strlen("software"))) { |
---|
| 4476 | if (method != OG_METHOD_POST) |
---|
[e804134] | 4477 | return og_client_method_not_found(cli); |
---|
[a0f41fc] | 4478 | |
---|
| 4479 | if (!root) { |
---|
| 4480 | syslog(LOG_ERR, "command software with no payload\n"); |
---|
[391f9be] | 4481 | return og_client_bad_request(cli); |
---|
[a0f41fc] | 4482 | } |
---|
| 4483 | err = og_cmd_software(root, ¶ms); |
---|
[7258dac] | 4484 | } else if (!strncmp(cmd, "image/create/basic", |
---|
| 4485 | strlen("image/create/basic"))) { |
---|
| 4486 | if (method != OG_METHOD_POST) |
---|
| 4487 | return og_client_method_not_found(cli); |
---|
| 4488 | |
---|
| 4489 | if (!root) { |
---|
| 4490 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
| 4491 | return og_client_bad_request(cli); |
---|
| 4492 | } |
---|
| 4493 | err = og_cmd_create_basic_image(root, ¶ms); |
---|
[f72c984] | 4494 | } else if (!strncmp(cmd, "image/create/incremental", |
---|
| 4495 | strlen("image/create/incremental"))) { |
---|
| 4496 | if (method != OG_METHOD_POST) |
---|
| 4497 | return og_client_method_not_found(cli); |
---|
| 4498 | |
---|
| 4499 | if (!root) { |
---|
| 4500 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
| 4501 | return og_client_bad_request(cli); |
---|
| 4502 | } |
---|
| 4503 | err = og_cmd_create_incremental_image(root, ¶ms); |
---|
[7f2dd15] | 4504 | } else if (!strncmp(cmd, "image/create", strlen("image/create"))) { |
---|
| 4505 | if (method != OG_METHOD_POST) |
---|
| 4506 | return og_client_method_not_found(cli); |
---|
| 4507 | |
---|
| 4508 | if (!root) { |
---|
| 4509 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
| 4510 | return og_client_bad_request(cli); |
---|
| 4511 | } |
---|
| 4512 | err = og_cmd_create_image(root, ¶ms); |
---|
[d521845] | 4513 | } else if (!strncmp(cmd, "image/restore/basic", |
---|
| 4514 | strlen("image/restore/basic"))) { |
---|
| 4515 | if (method != OG_METHOD_POST) |
---|
| 4516 | return og_client_method_not_found(cli); |
---|
| 4517 | |
---|
| 4518 | if (!root) { |
---|
| 4519 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
| 4520 | return og_client_bad_request(cli); |
---|
| 4521 | } |
---|
| 4522 | err = og_cmd_restore_basic_image(root, ¶ms); |
---|
[1707bc6] | 4523 | } else if (!strncmp(cmd, "image/restore/incremental", |
---|
| 4524 | strlen("image/restore/incremental"))) { |
---|
| 4525 | if (method != OG_METHOD_POST) |
---|
| 4526 | return og_client_method_not_found(cli); |
---|
| 4527 | |
---|
| 4528 | if (!root) { |
---|
| 4529 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
| 4530 | return og_client_bad_request(cli); |
---|
| 4531 | } |
---|
| 4532 | err = og_cmd_restore_incremental_image(root, ¶ms); |
---|
[fc15dd1] | 4533 | } else if (!strncmp(cmd, "image/restore", strlen("image/restore"))) { |
---|
| 4534 | if (method != OG_METHOD_POST) |
---|
| 4535 | return og_client_method_not_found(cli); |
---|
| 4536 | |
---|
| 4537 | if (!root) { |
---|
| 4538 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
| 4539 | return og_client_bad_request(cli); |
---|
| 4540 | } |
---|
| 4541 | err = og_cmd_restore_image(root, ¶ms); |
---|
[9381fdf] | 4542 | } else if (!strncmp(cmd, "setup", strlen("setup"))) { |
---|
[2385710e] | 4543 | if (method != OG_METHOD_POST) |
---|
| 4544 | return og_client_method_not_found(cli); |
---|
| 4545 | |
---|
| 4546 | if (!root) { |
---|
| 4547 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
| 4548 | return og_client_bad_request(cli); |
---|
| 4549 | } |
---|
[9381fdf] | 4550 | err = og_cmd_setup(root, ¶ms); |
---|
[3ca392f] | 4551 | } else if (!strncmp(cmd, "run/schedule", strlen("run/schedule"))) { |
---|
| 4552 | if (method != OG_METHOD_POST) |
---|
| 4553 | return og_client_method_not_found(cli); |
---|
| 4554 | |
---|
| 4555 | if (!root) { |
---|
| 4556 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
| 4557 | return og_client_bad_request(cli); |
---|
| 4558 | } |
---|
| 4559 | |
---|
| 4560 | err = og_cmd_run_schedule(root, ¶ms); |
---|
[7242996] | 4561 | } else if (!strncmp(cmd, "task/run", strlen("task/run"))) { |
---|
| 4562 | if (method != OG_METHOD_POST) |
---|
| 4563 | return og_client_method_not_found(cli); |
---|
| 4564 | |
---|
| 4565 | if (!root) { |
---|
| 4566 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
| 4567 | return og_client_bad_request(cli); |
---|
| 4568 | } |
---|
| 4569 | err = og_cmd_task_post(root, ¶ms); |
---|
[d327da6] | 4570 | } else if (!strncmp(cmd, "schedule/create", |
---|
| 4571 | strlen("schedule/create"))) { |
---|
| 4572 | if (method != OG_METHOD_POST) |
---|
| 4573 | return og_client_method_not_found(cli); |
---|
| 4574 | |
---|
| 4575 | if (!root) { |
---|
| 4576 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
| 4577 | return og_client_bad_request(cli); |
---|
| 4578 | } |
---|
| 4579 | err = og_cmd_schedule_create(root, ¶ms); |
---|
| 4580 | } else if (!strncmp(cmd, "schedule/delete", |
---|
| 4581 | strlen("schedule/delete"))) { |
---|
| 4582 | if (method != OG_METHOD_POST) |
---|
| 4583 | return og_client_method_not_found(cli); |
---|
| 4584 | |
---|
| 4585 | if (!root) { |
---|
| 4586 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
| 4587 | return og_client_bad_request(cli); |
---|
| 4588 | } |
---|
| 4589 | err = og_cmd_schedule_delete(root, ¶ms); |
---|
| 4590 | } else if (!strncmp(cmd, "schedule/update", |
---|
| 4591 | strlen("schedule/update"))) { |
---|
| 4592 | if (method != OG_METHOD_POST) |
---|
| 4593 | return og_client_method_not_found(cli); |
---|
| 4594 | |
---|
| 4595 | if (!root) { |
---|
| 4596 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
| 4597 | return og_client_bad_request(cli); |
---|
| 4598 | } |
---|
| 4599 | err = og_cmd_schedule_update(root, ¶ms); |
---|
[53ef484] | 4600 | } else if (!strncmp(cmd, "schedule/get", |
---|
| 4601 | strlen("schedule/get"))) { |
---|
| 4602 | if (method != OG_METHOD_POST) |
---|
| 4603 | return og_client_method_not_found(cli); |
---|
| 4604 | |
---|
| 4605 | err = og_cmd_schedule_get(root, ¶ms, buf_reply); |
---|
[1a08c06] | 4606 | } else { |
---|
[ca239ad] | 4607 | syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd); |
---|
[1a08c06] | 4608 | err = og_client_not_found(cli); |
---|
| 4609 | } |
---|
| 4610 | |
---|
[ea03692] | 4611 | if (root) |
---|
| 4612 | json_decref(root); |
---|
[1a08c06] | 4613 | |
---|
[0c1ff40] | 4614 | if (err < 0) |
---|
[2f3d7f53] | 4615 | return og_client_bad_request(cli); |
---|
[0c1ff40] | 4616 | |
---|
| 4617 | err = og_client_ok(cli, buf_reply); |
---|
| 4618 | if (err < 0) { |
---|
| 4619 | syslog(LOG_ERR, "HTTP response to %s:%hu is too large\n", |
---|
| 4620 | inet_ntoa(cli->addr.sin_addr), |
---|
| 4621 | ntohs(cli->addr.sin_port)); |
---|
| 4622 | } |
---|
[1a08c06] | 4623 | |
---|
| 4624 | return err; |
---|
| 4625 | } |
---|
| 4626 | |
---|
| 4627 | static int og_client_state_recv_hdr_rest(struct og_client *cli) |
---|
| 4628 | { |
---|
[b3467f7] | 4629 | char *ptr; |
---|
[1a08c06] | 4630 | |
---|
[b3467f7] | 4631 | ptr = strstr(cli->buf, "\r\n\r\n"); |
---|
| 4632 | if (!ptr) |
---|
[1a08c06] | 4633 | return 0; |
---|
| 4634 | |
---|
[b3467f7] | 4635 | cli->msg_len = ptr - cli->buf + 4; |
---|
| 4636 | |
---|
| 4637 | ptr = strstr(cli->buf, "Content-Length: "); |
---|
| 4638 | if (ptr) { |
---|
| 4639 | sscanf(ptr, "Content-Length: %i[^\r\n]", &cli->content_length); |
---|
[8793b71] | 4640 | if (cli->content_length < 0) |
---|
| 4641 | return -1; |
---|
[b3467f7] | 4642 | cli->msg_len += cli->content_length; |
---|
| 4643 | } |
---|
| 4644 | |
---|
[2ccec278] | 4645 | ptr = strstr(cli->buf, "Authorization: "); |
---|
| 4646 | if (ptr) |
---|
[ba5651bc] | 4647 | sscanf(ptr, "Authorization: %63[^\r\n]", cli->auth_token); |
---|
[2ccec278] | 4648 | |
---|
[1a08c06] | 4649 | return 1; |
---|
| 4650 | } |
---|
| 4651 | |
---|
[c90cda4] | 4652 | static int og_client_recv(struct og_client *cli, int events) |
---|
[07c51e2] | 4653 | { |
---|
[c90cda4] | 4654 | struct ev_io *io = &cli->io; |
---|
[07c51e2] | 4655 | int ret; |
---|
[9baecf8] | 4656 | |
---|
[2e0c063] | 4657 | if (events & EV_ERROR) { |
---|
| 4658 | syslog(LOG_ERR, "unexpected error event from client %s:%hu\n", |
---|
| 4659 | inet_ntoa(cli->addr.sin_addr), |
---|
| 4660 | ntohs(cli->addr.sin_port)); |
---|
[c90cda4] | 4661 | return 0; |
---|
[2e0c063] | 4662 | } |
---|
| 4663 | |
---|
[9baecf8] | 4664 | ret = recv(io->fd, cli->buf + cli->buf_len, |
---|
| 4665 | sizeof(cli->buf) - cli->buf_len, 0); |
---|
[2e0c063] | 4666 | if (ret <= 0) { |
---|
| 4667 | if (ret < 0) { |
---|
| 4668 | syslog(LOG_ERR, "error reading from client %s:%hu (%s)\n", |
---|
| 4669 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port), |
---|
| 4670 | strerror(errno)); |
---|
| 4671 | } else { |
---|
[0e16db2] | 4672 | syslog(LOG_DEBUG, "closed connection by %s:%hu\n", |
---|
[2e0c063] | 4673 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
| 4674 | } |
---|
[c90cda4] | 4675 | return ret; |
---|
[2e0c063] | 4676 | } |
---|
| 4677 | |
---|
[c90cda4] | 4678 | return ret; |
---|
| 4679 | } |
---|
| 4680 | |
---|
| 4681 | static void og_client_read_cb(struct ev_loop *loop, struct ev_io *io, int events) |
---|
| 4682 | { |
---|
| 4683 | struct og_client *cli; |
---|
| 4684 | int ret; |
---|
| 4685 | |
---|
| 4686 | cli = container_of(io, struct og_client, io); |
---|
| 4687 | |
---|
| 4688 | ret = og_client_recv(cli, events); |
---|
| 4689 | if (ret <= 0) |
---|
| 4690 | goto close; |
---|
| 4691 | |
---|
[2e0c063] | 4692 | if (cli->keepalive_idx >= 0) |
---|
| 4693 | return; |
---|
[9baecf8] | 4694 | |
---|
| 4695 | ev_timer_again(loop, &cli->timer); |
---|
| 4696 | |
---|
| 4697 | cli->buf_len += ret; |
---|
[d8a2d5f] | 4698 | if (cli->buf_len >= sizeof(cli->buf)) { |
---|
| 4699 | syslog(LOG_ERR, "client request from %s:%hu is too long\n", |
---|
| 4700 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
[4d7fd58] | 4701 | og_client_payload_too_large(cli); |
---|
[d8a2d5f] | 4702 | goto close; |
---|
| 4703 | } |
---|
[9baecf8] | 4704 | |
---|
| 4705 | switch (cli->state) { |
---|
| 4706 | case OG_CLIENT_RECEIVING_HEADER: |
---|
[71e678b] | 4707 | ret = og_client_state_recv_hdr_rest(cli); |
---|
[39c3261] | 4708 | if (ret < 0) |
---|
[9baecf8] | 4709 | goto close; |
---|
[39c3261] | 4710 | if (!ret) |
---|
| 4711 | return; |
---|
[9baecf8] | 4712 | |
---|
| 4713 | cli->state = OG_CLIENT_RECEIVING_PAYLOAD; |
---|
| 4714 | /* Fall through. */ |
---|
| 4715 | case OG_CLIENT_RECEIVING_PAYLOAD: |
---|
| 4716 | /* Still not enough data to process request. */ |
---|
| 4717 | if (cli->buf_len < cli->msg_len) |
---|
| 4718 | return; |
---|
| 4719 | |
---|
| 4720 | cli->state = OG_CLIENT_PROCESSING_REQUEST; |
---|
| 4721 | /* fall through. */ |
---|
| 4722 | case OG_CLIENT_PROCESSING_REQUEST: |
---|
[71e678b] | 4723 | ret = og_client_state_process_payload_rest(cli); |
---|
| 4724 | if (ret < 0) { |
---|
| 4725 | syslog(LOG_ERR, "Failed to process HTTP request from %s:%hu\n", |
---|
| 4726 | inet_ntoa(cli->addr.sin_addr), |
---|
| 4727 | ntohs(cli->addr.sin_port)); |
---|
[0c1ff40] | 4728 | } |
---|
[07c51e2] | 4729 | if (ret < 0) |
---|
[9baecf8] | 4730 | goto close; |
---|
| 4731 | |
---|
[2e0c063] | 4732 | if (cli->keepalive_idx < 0) { |
---|
[0e16db2] | 4733 | syslog(LOG_DEBUG, "server closing connection to %s:%hu\n", |
---|
[2e0c063] | 4734 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
[9baecf8] | 4735 | goto close; |
---|
[2e0c063] | 4736 | } else { |
---|
[0e16db2] | 4737 | syslog(LOG_DEBUG, "leaving client %s:%hu in keepalive mode\n", |
---|
[2e0c063] | 4738 | inet_ntoa(cli->addr.sin_addr), |
---|
| 4739 | ntohs(cli->addr.sin_port)); |
---|
| 4740 | og_client_keepalive(loop, cli); |
---|
| 4741 | og_client_reset_state(cli); |
---|
| 4742 | } |
---|
[9baecf8] | 4743 | break; |
---|
[13e48b4] | 4744 | default: |
---|
| 4745 | syslog(LOG_ERR, "unknown state, critical internal error\n"); |
---|
| 4746 | goto close; |
---|
[f997cc1] | 4747 | } |
---|
[9baecf8] | 4748 | return; |
---|
| 4749 | close: |
---|
| 4750 | ev_timer_stop(loop, &cli->timer); |
---|
[2e0c063] | 4751 | og_client_release(loop, cli); |
---|
[f997cc1] | 4752 | } |
---|
| 4753 | |
---|
[c90cda4] | 4754 | enum og_agent_state { |
---|
| 4755 | OG_AGENT_RECEIVING_HEADER = 0, |
---|
| 4756 | OG_AGENT_RECEIVING_PAYLOAD, |
---|
| 4757 | OG_AGENT_PROCESSING_RESPONSE, |
---|
| 4758 | }; |
---|
[1a08c06] | 4759 | |
---|
[c90cda4] | 4760 | static int og_agent_state_recv_hdr_rest(struct og_client *cli) |
---|
[9baecf8] | 4761 | { |
---|
[c90cda4] | 4762 | char *ptr; |
---|
[9baecf8] | 4763 | |
---|
[c90cda4] | 4764 | ptr = strstr(cli->buf, "\r\n\r\n"); |
---|
| 4765 | if (!ptr) |
---|
| 4766 | return 0; |
---|
[9baecf8] | 4767 | |
---|
[c90cda4] | 4768 | cli->msg_len = ptr - cli->buf + 4; |
---|
[9baecf8] | 4769 | |
---|
[c90cda4] | 4770 | ptr = strstr(cli->buf, "Content-Length: "); |
---|
| 4771 | if (ptr) { |
---|
| 4772 | sscanf(ptr, "Content-Length: %i[^\r\n]", &cli->content_length); |
---|
| 4773 | if (cli->content_length < 0) |
---|
| 4774 | return -1; |
---|
| 4775 | cli->msg_len += cli->content_length; |
---|
[9baecf8] | 4776 | } |
---|
| 4777 | |
---|
[c90cda4] | 4778 | return 1; |
---|
[9baecf8] | 4779 | } |
---|
| 4780 | |
---|
[c90cda4] | 4781 | static void og_agent_reset_state(struct og_client *cli) |
---|
[588052e] | 4782 | { |
---|
[c90cda4] | 4783 | cli->state = OG_AGENT_RECEIVING_HEADER; |
---|
| 4784 | cli->buf_len = 0; |
---|
| 4785 | cli->content_length = 0; |
---|
| 4786 | memset(cli->buf, 0, sizeof(cli->buf)); |
---|
| 4787 | } |
---|
| 4788 | |
---|
| 4789 | static int og_dbi_get_computer_info(struct og_computer *computer, |
---|
| 4790 | struct in_addr addr) |
---|
| 4791 | { |
---|
| 4792 | const char *msglog; |
---|
| 4793 | struct og_dbi *dbi; |
---|
| 4794 | dbi_result result; |
---|
| 4795 | |
---|
| 4796 | dbi = og_dbi_open(&dbi_config); |
---|
| 4797 | if (!dbi) { |
---|
| 4798 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 4799 | __func__, __LINE__); |
---|
| 4800 | return -1; |
---|
| 4801 | } |
---|
| 4802 | result = dbi_conn_queryf(dbi->conn, |
---|
| 4803 | "SELECT ordenadores.idordenador," |
---|
| 4804 | " ordenadores.nombreordenador," |
---|
| 4805 | " ordenadores.idaula," |
---|
| 4806 | " centros.idcentro FROM ordenadores " |
---|
| 4807 | "INNER JOIN aulas ON aulas.idaula=ordenadores.idaula " |
---|
| 4808 | "INNER JOIN centros ON centros.idcentro=aulas.idcentro " |
---|
| 4809 | "WHERE ordenadores.ip='%s'", inet_ntoa(addr)); |
---|
| 4810 | if (!result) { |
---|
| 4811 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 4812 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 4813 | __func__, __LINE__, msglog); |
---|
[d7c20c8] | 4814 | og_dbi_close(dbi); |
---|
[c90cda4] | 4815 | return -1; |
---|
| 4816 | } |
---|
| 4817 | if (!dbi_result_next_row(result)) { |
---|
| 4818 | syslog(LOG_ERR, "client does not exist in database (%s:%d)\n", |
---|
| 4819 | __func__, __LINE__); |
---|
| 4820 | dbi_result_free(result); |
---|
[d7c20c8] | 4821 | og_dbi_close(dbi); |
---|
[c90cda4] | 4822 | return -1; |
---|
| 4823 | } |
---|
| 4824 | |
---|
| 4825 | computer->id = dbi_result_get_uint(result, "idordenador"); |
---|
| 4826 | computer->center = dbi_result_get_uint(result, "idcentro"); |
---|
| 4827 | computer->room = dbi_result_get_uint(result, "idaula"); |
---|
| 4828 | strncpy(computer->name, |
---|
| 4829 | dbi_result_get_string(result, "nombreordenador"), |
---|
| 4830 | OG_COMPUTER_NAME_MAXLEN); |
---|
| 4831 | |
---|
| 4832 | dbi_result_free(result); |
---|
| 4833 | og_dbi_close(dbi); |
---|
| 4834 | |
---|
| 4835 | return 0; |
---|
| 4836 | } |
---|
| 4837 | |
---|
| 4838 | static int og_resp_probe(struct og_client *cli, json_t *data) |
---|
| 4839 | { |
---|
[ecce978] | 4840 | const char *status = NULL; |
---|
[c90cda4] | 4841 | const char *key; |
---|
| 4842 | json_t *value; |
---|
| 4843 | int err = 0; |
---|
| 4844 | |
---|
| 4845 | if (json_typeof(data) != JSON_OBJECT) |
---|
| 4846 | return -1; |
---|
| 4847 | |
---|
| 4848 | json_object_foreach(data, key, value) { |
---|
| 4849 | if (!strcmp(key, "status")) { |
---|
[ecce978] | 4850 | err = og_json_parse_string(value, &status); |
---|
[c90cda4] | 4851 | if (err < 0) |
---|
| 4852 | return err; |
---|
| 4853 | } else { |
---|
| 4854 | return -1; |
---|
| 4855 | } |
---|
| 4856 | } |
---|
| 4857 | |
---|
[ecce978] | 4858 | if (!strcmp(status, "BSY")) |
---|
| 4859 | cli->status = OG_CLIENT_STATUS_BUSY; |
---|
| 4860 | else if (!strcmp(status, "OPG")) |
---|
| 4861 | cli->status = OG_CLIENT_STATUS_OGLIVE; |
---|
| 4862 | |
---|
[c90cda4] | 4863 | return status ? 0 : -1; |
---|
| 4864 | } |
---|
| 4865 | |
---|
| 4866 | static int og_resp_shell_run(struct og_client *cli, json_t *data) |
---|
| 4867 | { |
---|
| 4868 | const char *output = NULL; |
---|
| 4869 | char filename[4096]; |
---|
| 4870 | const char *key; |
---|
| 4871 | json_t *value; |
---|
| 4872 | int err = -1; |
---|
| 4873 | FILE *file; |
---|
| 4874 | |
---|
| 4875 | if (json_typeof(data) != JSON_OBJECT) |
---|
| 4876 | return -1; |
---|
| 4877 | |
---|
| 4878 | json_object_foreach(data, key, value) { |
---|
| 4879 | if (!strcmp(key, "out")) { |
---|
| 4880 | err = og_json_parse_string(value, &output); |
---|
| 4881 | if (err < 0) |
---|
| 4882 | return err; |
---|
| 4883 | } else { |
---|
| 4884 | return -1; |
---|
| 4885 | } |
---|
| 4886 | } |
---|
| 4887 | |
---|
| 4888 | if (!output) { |
---|
| 4889 | syslog(LOG_ERR, "%s:%d: malformed json response\n", |
---|
| 4890 | __FILE__, __LINE__); |
---|
| 4891 | return -1; |
---|
| 4892 | } |
---|
| 4893 | |
---|
| 4894 | sprintf(filename, "/tmp/_Seconsola_%s", inet_ntoa(cli->addr.sin_addr)); |
---|
| 4895 | file = fopen(filename, "wt"); |
---|
| 4896 | if (!file) { |
---|
| 4897 | syslog(LOG_ERR, "cannot open file %s: %s\n", |
---|
| 4898 | filename, strerror(errno)); |
---|
| 4899 | return -1; |
---|
| 4900 | } |
---|
| 4901 | |
---|
| 4902 | fprintf(file, "%s", output); |
---|
| 4903 | fclose(file); |
---|
| 4904 | |
---|
| 4905 | return 0; |
---|
| 4906 | } |
---|
| 4907 | |
---|
| 4908 | struct og_computer_legacy { |
---|
| 4909 | char center[OG_DB_INT_MAXLEN + 1]; |
---|
| 4910 | char id[OG_DB_INT_MAXLEN + 1]; |
---|
| 4911 | char hardware[8192]; |
---|
| 4912 | }; |
---|
| 4913 | |
---|
| 4914 | static int og_resp_hardware(json_t *data, struct og_client *cli) |
---|
| 4915 | { |
---|
| 4916 | struct og_computer_legacy legacy = {}; |
---|
| 4917 | const char *hardware = NULL; |
---|
| 4918 | struct og_computer computer; |
---|
| 4919 | struct og_dbi *dbi; |
---|
| 4920 | const char *key; |
---|
| 4921 | json_t *value; |
---|
| 4922 | int err = 0; |
---|
| 4923 | bool res; |
---|
| 4924 | |
---|
| 4925 | if (json_typeof(data) != JSON_OBJECT) |
---|
| 4926 | return -1; |
---|
| 4927 | |
---|
| 4928 | json_object_foreach(data, key, value) { |
---|
| 4929 | if (!strcmp(key, "hardware")) { |
---|
| 4930 | err = og_json_parse_string(value, &hardware); |
---|
| 4931 | if (err < 0) |
---|
| 4932 | return -1; |
---|
| 4933 | } else { |
---|
| 4934 | return -1; |
---|
| 4935 | } |
---|
| 4936 | } |
---|
| 4937 | |
---|
| 4938 | if (!hardware) { |
---|
| 4939 | syslog(LOG_ERR, "malformed response json\n"); |
---|
| 4940 | return -1; |
---|
| 4941 | } |
---|
| 4942 | |
---|
| 4943 | err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr); |
---|
| 4944 | if (err < 0) |
---|
| 4945 | return -1; |
---|
| 4946 | |
---|
| 4947 | snprintf(legacy.center, sizeof(legacy.center), "%d", computer.center); |
---|
| 4948 | snprintf(legacy.id, sizeof(legacy.id), "%d", computer.id); |
---|
| 4949 | snprintf(legacy.hardware, sizeof(legacy.hardware), "%s", hardware); |
---|
| 4950 | |
---|
| 4951 | dbi = og_dbi_open(&dbi_config); |
---|
| 4952 | if (!dbi) { |
---|
| 4953 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 4954 | __func__, __LINE__); |
---|
| 4955 | return -1; |
---|
| 4956 | } |
---|
| 4957 | |
---|
| 4958 | res = actualizaHardware(dbi, legacy.hardware, legacy.id, computer.name, |
---|
| 4959 | legacy.center); |
---|
| 4960 | og_dbi_close(dbi); |
---|
| 4961 | |
---|
| 4962 | if (!res) { |
---|
| 4963 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
| 4964 | return -1; |
---|
| 4965 | } |
---|
| 4966 | |
---|
| 4967 | return 0; |
---|
| 4968 | } |
---|
| 4969 | |
---|
| 4970 | struct og_software_legacy { |
---|
| 4971 | char software[8192]; |
---|
| 4972 | char center[OG_DB_INT_MAXLEN + 1]; |
---|
| 4973 | char part[OG_DB_SMALLINT_MAXLEN + 1]; |
---|
| 4974 | char id[OG_DB_INT_MAXLEN + 1]; |
---|
| 4975 | }; |
---|
| 4976 | |
---|
| 4977 | static int og_resp_software(json_t *data, struct og_client *cli) |
---|
| 4978 | { |
---|
| 4979 | struct og_software_legacy legacy = {}; |
---|
| 4980 | const char *partition = NULL; |
---|
| 4981 | const char *software = NULL; |
---|
| 4982 | struct og_computer computer; |
---|
| 4983 | struct og_dbi *dbi; |
---|
| 4984 | const char *key; |
---|
| 4985 | json_t *value; |
---|
| 4986 | int err = 0; |
---|
| 4987 | bool res; |
---|
| 4988 | |
---|
| 4989 | if (json_typeof(data) != JSON_OBJECT) |
---|
| 4990 | return -1; |
---|
| 4991 | |
---|
| 4992 | json_object_foreach(data, key, value) { |
---|
| 4993 | if (!strcmp(key, "software")) |
---|
| 4994 | err = og_json_parse_string(value, &software); |
---|
| 4995 | else if (!strcmp(key, "partition")) |
---|
| 4996 | err = og_json_parse_string(value, &partition); |
---|
| 4997 | else |
---|
| 4998 | return -1; |
---|
| 4999 | |
---|
| 5000 | if (err < 0) |
---|
| 5001 | return -1; |
---|
| 5002 | } |
---|
| 5003 | |
---|
| 5004 | if (!software || !partition) { |
---|
| 5005 | syslog(LOG_ERR, "malformed response json\n"); |
---|
| 5006 | return -1; |
---|
| 5007 | } |
---|
| 5008 | |
---|
| 5009 | err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr); |
---|
| 5010 | if (err < 0) |
---|
| 5011 | return -1; |
---|
| 5012 | |
---|
| 5013 | snprintf(legacy.software, sizeof(legacy.software), "%s", software); |
---|
| 5014 | snprintf(legacy.part, sizeof(legacy.part), "%s", partition); |
---|
| 5015 | snprintf(legacy.id, sizeof(legacy.id), "%d", computer.id); |
---|
| 5016 | snprintf(legacy.center, sizeof(legacy.center), "%d", computer.center); |
---|
| 5017 | |
---|
| 5018 | dbi = og_dbi_open(&dbi_config); |
---|
| 5019 | if (!dbi) { |
---|
| 5020 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 5021 | __func__, __LINE__); |
---|
| 5022 | return -1; |
---|
| 5023 | } |
---|
| 5024 | |
---|
| 5025 | res = actualizaSoftware(dbi, legacy.software, legacy.part, legacy.id, |
---|
| 5026 | computer.name, legacy.center); |
---|
| 5027 | og_dbi_close(dbi); |
---|
| 5028 | |
---|
| 5029 | if (!res) { |
---|
| 5030 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
| 5031 | return -1; |
---|
| 5032 | } |
---|
| 5033 | |
---|
| 5034 | return 0; |
---|
| 5035 | } |
---|
| 5036 | |
---|
| 5037 | #define OG_PARAMS_RESP_REFRESH (OG_PARAM_PART_DISK | \ |
---|
| 5038 | OG_PARAM_PART_NUMBER | \ |
---|
| 5039 | OG_PARAM_PART_CODE | \ |
---|
| 5040 | OG_PARAM_PART_FILESYSTEM | \ |
---|
| 5041 | OG_PARAM_PART_OS | \ |
---|
| 5042 | OG_PARAM_PART_SIZE | \ |
---|
| 5043 | OG_PARAM_PART_USED_SIZE) |
---|
| 5044 | |
---|
| 5045 | static int og_json_parse_partition_array(json_t *value, |
---|
| 5046 | struct og_partition *partitions) |
---|
| 5047 | { |
---|
| 5048 | json_t *element; |
---|
| 5049 | int i, err; |
---|
| 5050 | |
---|
| 5051 | if (json_typeof(value) != JSON_ARRAY) |
---|
| 5052 | return -1; |
---|
| 5053 | |
---|
| 5054 | for (i = 0; i < json_array_size(value) && i < OG_PARTITION_MAX; i++) { |
---|
| 5055 | element = json_array_get(value, i); |
---|
| 5056 | |
---|
| 5057 | err = og_json_parse_partition(element, &partitions[i], |
---|
| 5058 | OG_PARAMS_RESP_REFRESH); |
---|
| 5059 | if (err < 0) |
---|
| 5060 | return err; |
---|
| 5061 | } |
---|
| 5062 | |
---|
| 5063 | return 0; |
---|
| 5064 | } |
---|
| 5065 | |
---|
| 5066 | static int og_resp_refresh(json_t *data, struct og_client *cli) |
---|
| 5067 | { |
---|
| 5068 | struct og_partition partitions[OG_PARTITION_MAX] = {}; |
---|
| 5069 | const char *serial_number = NULL; |
---|
| 5070 | struct og_partition disk_setup; |
---|
| 5071 | struct og_computer computer; |
---|
| 5072 | char cfg[1024] = {}; |
---|
| 5073 | struct og_dbi *dbi; |
---|
| 5074 | const char *key; |
---|
| 5075 | unsigned int i; |
---|
| 5076 | json_t *value; |
---|
| 5077 | int err = 0; |
---|
| 5078 | bool res; |
---|
| 5079 | |
---|
| 5080 | if (json_typeof(data) != JSON_OBJECT) |
---|
| 5081 | return -1; |
---|
| 5082 | |
---|
| 5083 | json_object_foreach(data, key, value) { |
---|
| 5084 | if (!strcmp(key, "disk_setup")) { |
---|
| 5085 | err = og_json_parse_partition(value, |
---|
| 5086 | &disk_setup, |
---|
| 5087 | OG_PARAMS_RESP_REFRESH); |
---|
| 5088 | } else if (!strcmp(key, "partition_setup")) { |
---|
| 5089 | err = og_json_parse_partition_array(value, partitions); |
---|
| 5090 | } else if (!strcmp(key, "serial_number")) { |
---|
| 5091 | err = og_json_parse_string(value, &serial_number); |
---|
| 5092 | } else { |
---|
| 5093 | return -1; |
---|
| 5094 | } |
---|
| 5095 | |
---|
| 5096 | if (err < 0) |
---|
| 5097 | return err; |
---|
| 5098 | } |
---|
| 5099 | |
---|
| 5100 | err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr); |
---|
| 5101 | if (err < 0) |
---|
| 5102 | return -1; |
---|
| 5103 | |
---|
| 5104 | if (strlen(serial_number) > 0) |
---|
| 5105 | snprintf(cfg, sizeof(cfg), "ser=%s\n", serial_number); |
---|
| 5106 | |
---|
| 5107 | if (!disk_setup.disk || !disk_setup.number || !disk_setup.code || |
---|
| 5108 | !disk_setup.filesystem || !disk_setup.os || !disk_setup.size || |
---|
| 5109 | !disk_setup.used_size) |
---|
| 5110 | return -1; |
---|
| 5111 | |
---|
| 5112 | snprintf(cfg + strlen(cfg), sizeof(cfg) - strlen(cfg), |
---|
| 5113 | "disk=%s\tpar=%s\tcpt=%s\tfsi=%s\tsoi=%s\ttam=%s\tuso=%s\n", |
---|
| 5114 | disk_setup.disk, disk_setup.number, disk_setup.code, |
---|
| 5115 | disk_setup.filesystem, disk_setup.os, disk_setup.size, |
---|
| 5116 | disk_setup.used_size); |
---|
| 5117 | |
---|
| 5118 | for (i = 0; i < OG_PARTITION_MAX; i++) { |
---|
| 5119 | if (!partitions[i].disk || !partitions[i].number || |
---|
| 5120 | !partitions[i].code || !partitions[i].filesystem || |
---|
| 5121 | !partitions[i].os || !partitions[i].size || |
---|
| 5122 | !partitions[i].used_size) |
---|
| 5123 | continue; |
---|
| 5124 | |
---|
| 5125 | snprintf(cfg + strlen(cfg), sizeof(cfg) - strlen(cfg), |
---|
| 5126 | "disk=%s\tpar=%s\tcpt=%s\tfsi=%s\tsoi=%s\ttam=%s\tuso=%s\n", |
---|
| 5127 | partitions[i].disk, partitions[i].number, |
---|
| 5128 | partitions[i].code, partitions[i].filesystem, |
---|
| 5129 | partitions[i].os, partitions[i].size, |
---|
| 5130 | partitions[i].used_size); |
---|
| 5131 | } |
---|
| 5132 | |
---|
| 5133 | dbi = og_dbi_open(&dbi_config); |
---|
| 5134 | if (!dbi) { |
---|
| 5135 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 5136 | __func__, __LINE__); |
---|
| 5137 | return -1; |
---|
| 5138 | } |
---|
| 5139 | res = actualizaConfiguracion(dbi, cfg, computer.id); |
---|
| 5140 | og_dbi_close(dbi); |
---|
| 5141 | |
---|
| 5142 | if (!res) { |
---|
| 5143 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
| 5144 | return -1; |
---|
| 5145 | } |
---|
| 5146 | |
---|
| 5147 | return 0; |
---|
| 5148 | } |
---|
| 5149 | |
---|
| 5150 | static int og_resp_image_create(json_t *data, struct og_client *cli) |
---|
| 5151 | { |
---|
| 5152 | struct og_software_legacy soft_legacy; |
---|
| 5153 | struct og_image_legacy img_legacy; |
---|
| 5154 | const char *partition = NULL; |
---|
| 5155 | const char *software = NULL; |
---|
| 5156 | const char *image_id = NULL; |
---|
| 5157 | struct og_computer computer; |
---|
| 5158 | const char *disk = NULL; |
---|
| 5159 | const char *code = NULL; |
---|
| 5160 | const char *name = NULL; |
---|
| 5161 | const char *repo = NULL; |
---|
| 5162 | struct og_dbi *dbi; |
---|
| 5163 | const char *key; |
---|
| 5164 | json_t *value; |
---|
| 5165 | int err = 0; |
---|
| 5166 | bool res; |
---|
| 5167 | |
---|
| 5168 | if (json_typeof(data) != JSON_OBJECT) |
---|
| 5169 | return -1; |
---|
| 5170 | |
---|
| 5171 | json_object_foreach(data, key, value) { |
---|
| 5172 | if (!strcmp(key, "software")) |
---|
| 5173 | err = og_json_parse_string(value, &software); |
---|
| 5174 | else if (!strcmp(key, "partition")) |
---|
| 5175 | err = og_json_parse_string(value, &partition); |
---|
| 5176 | else if (!strcmp(key, "disk")) |
---|
| 5177 | err = og_json_parse_string(value, &disk); |
---|
| 5178 | else if (!strcmp(key, "code")) |
---|
| 5179 | err = og_json_parse_string(value, &code); |
---|
| 5180 | else if (!strcmp(key, "id")) |
---|
| 5181 | err = og_json_parse_string(value, &image_id); |
---|
| 5182 | else if (!strcmp(key, "name")) |
---|
| 5183 | err = og_json_parse_string(value, &name); |
---|
| 5184 | else if (!strcmp(key, "repository")) |
---|
| 5185 | err = og_json_parse_string(value, &repo); |
---|
| 5186 | else |
---|
| 5187 | return -1; |
---|
| 5188 | |
---|
| 5189 | if (err < 0) |
---|
| 5190 | return err; |
---|
| 5191 | } |
---|
| 5192 | |
---|
| 5193 | if (!software || !partition || !disk || !code || !image_id || !name || |
---|
| 5194 | !repo) { |
---|
| 5195 | syslog(LOG_ERR, "malformed response json\n"); |
---|
| 5196 | return -1; |
---|
| 5197 | } |
---|
| 5198 | |
---|
| 5199 | err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr); |
---|
| 5200 | if (err < 0) |
---|
| 5201 | return -1; |
---|
| 5202 | |
---|
| 5203 | snprintf(soft_legacy.center, sizeof(soft_legacy.center), "%d", |
---|
| 5204 | computer.center); |
---|
| 5205 | snprintf(soft_legacy.software, sizeof(soft_legacy.software), "%s", |
---|
| 5206 | software); |
---|
| 5207 | snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%s", |
---|
| 5208 | image_id); |
---|
| 5209 | snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%d", computer.id); |
---|
| 5210 | snprintf(img_legacy.part, sizeof(img_legacy.part), "%s", partition); |
---|
| 5211 | snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%s", disk); |
---|
| 5212 | snprintf(img_legacy.code, sizeof(img_legacy.code), "%s", code); |
---|
| 5213 | snprintf(img_legacy.name, sizeof(img_legacy.name), "%s", name); |
---|
| 5214 | snprintf(img_legacy.repo, sizeof(img_legacy.repo), "%s", repo); |
---|
| 5215 | |
---|
| 5216 | dbi = og_dbi_open(&dbi_config); |
---|
| 5217 | if (!dbi) { |
---|
| 5218 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 5219 | __func__, __LINE__); |
---|
| 5220 | return -1; |
---|
| 5221 | } |
---|
| 5222 | |
---|
| 5223 | res = actualizaSoftware(dbi, |
---|
| 5224 | soft_legacy.software, |
---|
| 5225 | img_legacy.part, |
---|
| 5226 | soft_legacy.id, |
---|
| 5227 | computer.name, |
---|
| 5228 | soft_legacy.center); |
---|
| 5229 | if (!res) { |
---|
| 5230 | og_dbi_close(dbi); |
---|
| 5231 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
| 5232 | return -1; |
---|
| 5233 | } |
---|
| 5234 | |
---|
| 5235 | res = actualizaCreacionImagen(dbi, |
---|
| 5236 | img_legacy.image_id, |
---|
| 5237 | img_legacy.disk, |
---|
| 5238 | img_legacy.part, |
---|
| 5239 | img_legacy.code, |
---|
| 5240 | img_legacy.repo, |
---|
| 5241 | soft_legacy.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_resp_image_restore(json_t *data, struct og_client *cli) |
---|
| 5253 | { |
---|
| 5254 | struct og_software_legacy soft_legacy; |
---|
| 5255 | struct og_image_legacy img_legacy; |
---|
| 5256 | const char *partition = NULL; |
---|
| 5257 | const char *image_id = NULL; |
---|
| 5258 | struct og_computer computer; |
---|
| 5259 | const char *disk = NULL; |
---|
| 5260 | dbi_result query_result; |
---|
| 5261 | struct og_dbi *dbi; |
---|
| 5262 | const char *key; |
---|
| 5263 | json_t *value; |
---|
| 5264 | int err = 0; |
---|
| 5265 | bool res; |
---|
| 5266 | |
---|
| 5267 | if (json_typeof(data) != JSON_OBJECT) |
---|
| 5268 | return -1; |
---|
| 5269 | |
---|
| 5270 | json_object_foreach(data, key, value) { |
---|
| 5271 | if (!strcmp(key, "partition")) |
---|
| 5272 | err = og_json_parse_string(value, &partition); |
---|
| 5273 | else if (!strcmp(key, "disk")) |
---|
| 5274 | err = og_json_parse_string(value, &disk); |
---|
| 5275 | else if (!strcmp(key, "image_id")) |
---|
| 5276 | err = og_json_parse_string(value, &image_id); |
---|
| 5277 | else |
---|
| 5278 | return -1; |
---|
| 5279 | |
---|
| 5280 | if (err < 0) |
---|
| 5281 | return err; |
---|
| 5282 | } |
---|
| 5283 | |
---|
| 5284 | if (!partition || !disk || !image_id) { |
---|
| 5285 | syslog(LOG_ERR, "malformed response json\n"); |
---|
| 5286 | return -1; |
---|
| 5287 | } |
---|
| 5288 | |
---|
| 5289 | err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr); |
---|
| 5290 | if (err < 0) |
---|
| 5291 | return -1; |
---|
| 5292 | |
---|
| 5293 | snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%s", |
---|
| 5294 | image_id); |
---|
| 5295 | snprintf(img_legacy.part, sizeof(img_legacy.part), "%s", partition); |
---|
| 5296 | snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%s", disk); |
---|
| 5297 | snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%d", computer.id); |
---|
| 5298 | |
---|
| 5299 | dbi = og_dbi_open(&dbi_config); |
---|
| 5300 | if (!dbi) { |
---|
| 5301 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 5302 | __func__, __LINE__); |
---|
| 5303 | return -1; |
---|
| 5304 | } |
---|
| 5305 | |
---|
| 5306 | query_result = dbi_conn_queryf(dbi->conn, |
---|
| 5307 | "SELECT idperfilsoft FROM imagenes " |
---|
| 5308 | " WHERE idimagen='%s'", |
---|
| 5309 | image_id); |
---|
| 5310 | if (!query_result) { |
---|
| 5311 | og_dbi_close(dbi); |
---|
| 5312 | syslog(LOG_ERR, "failed to query database\n"); |
---|
| 5313 | return -1; |
---|
| 5314 | } |
---|
| 5315 | if (!dbi_result_next_row(query_result)) { |
---|
| 5316 | dbi_result_free(query_result); |
---|
| 5317 | og_dbi_close(dbi); |
---|
| 5318 | syslog(LOG_ERR, "software profile does not exist in database\n"); |
---|
| 5319 | return -1; |
---|
| 5320 | } |
---|
| 5321 | snprintf(img_legacy.software_id, sizeof(img_legacy.software_id), |
---|
| 5322 | "%d", dbi_result_get_uint(query_result, "idperfilsoft")); |
---|
| 5323 | dbi_result_free(query_result); |
---|
| 5324 | |
---|
| 5325 | res = actualizaRestauracionImagen(dbi, |
---|
| 5326 | img_legacy.image_id, |
---|
| 5327 | img_legacy.disk, |
---|
| 5328 | img_legacy.part, |
---|
| 5329 | soft_legacy.id, |
---|
| 5330 | img_legacy.software_id); |
---|
| 5331 | og_dbi_close(dbi); |
---|
| 5332 | |
---|
| 5333 | if (!res) { |
---|
| 5334 | syslog(LOG_ERR, "Problem updating client configuration\n"); |
---|
| 5335 | return -1; |
---|
| 5336 | } |
---|
| 5337 | |
---|
| 5338 | return 0; |
---|
| 5339 | } |
---|
| 5340 | |
---|
[21434ba] | 5341 | static int og_dbi_update_action(struct og_client *cli, bool success) |
---|
| 5342 | { |
---|
| 5343 | char end_date_string[24]; |
---|
| 5344 | struct tm *end_date; |
---|
| 5345 | const char *msglog; |
---|
| 5346 | struct og_dbi *dbi; |
---|
| 5347 | uint8_t status = 2; |
---|
| 5348 | dbi_result result; |
---|
| 5349 | time_t now; |
---|
| 5350 | |
---|
| 5351 | if (!cli->last_cmd_id) |
---|
| 5352 | return 0; |
---|
| 5353 | |
---|
| 5354 | dbi = og_dbi_open(&dbi_config); |
---|
| 5355 | if (!dbi) { |
---|
| 5356 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
| 5357 | __func__, __LINE__); |
---|
| 5358 | return -1; |
---|
| 5359 | } |
---|
| 5360 | |
---|
| 5361 | time(&now); |
---|
| 5362 | end_date = localtime(&now); |
---|
| 5363 | |
---|
| 5364 | sprintf(end_date_string, "%hu/%hhu/%hhu %hhu:%hhu:%hhu", |
---|
| 5365 | end_date->tm_year + 1900, end_date->tm_mon + 1, |
---|
| 5366 | end_date->tm_mday, end_date->tm_hour, end_date->tm_min, |
---|
| 5367 | end_date->tm_sec); |
---|
| 5368 | result = dbi_conn_queryf(dbi->conn, |
---|
| 5369 | "UPDATE acciones SET fechahorafin='%s', " |
---|
| 5370 | "estado=%d, resultado=%d WHERE idaccion=%d", |
---|
| 5371 | end_date_string, ACCION_FINALIZADA, |
---|
| 5372 | status - success, cli->last_cmd_id); |
---|
| 5373 | |
---|
| 5374 | if (!result) { |
---|
| 5375 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 5376 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 5377 | __func__, __LINE__, msglog); |
---|
[d7c20c8] | 5378 | og_dbi_close(dbi); |
---|
[21434ba] | 5379 | return -1; |
---|
| 5380 | } |
---|
| 5381 | cli->last_cmd_id = 0; |
---|
| 5382 | dbi_result_free(result); |
---|
| 5383 | og_dbi_close(dbi); |
---|
| 5384 | |
---|
| 5385 | return 0; |
---|
| 5386 | } |
---|
| 5387 | |
---|
[c90cda4] | 5388 | static int og_agent_state_process_response(struct og_client *cli) |
---|
| 5389 | { |
---|
| 5390 | json_error_t json_err; |
---|
| 5391 | json_t *root; |
---|
| 5392 | int err = -1; |
---|
| 5393 | char *body; |
---|
| 5394 | |
---|
[b8774e7] | 5395 | if (!strncmp(cli->buf, "HTTP/1.0 202 Accepted", |
---|
| 5396 | strlen("HTTP/1.0 202 Accepted"))) { |
---|
| 5397 | og_dbi_update_action(cli, true); |
---|
| 5398 | return 1; |
---|
| 5399 | } |
---|
| 5400 | |
---|
[21434ba] | 5401 | if (strncmp(cli->buf, "HTTP/1.0 200 OK", strlen("HTTP/1.0 200 OK"))) { |
---|
| 5402 | og_dbi_update_action(cli, false); |
---|
[c90cda4] | 5403 | return -1; |
---|
[21434ba] | 5404 | } |
---|
| 5405 | og_dbi_update_action(cli, true); |
---|
[c90cda4] | 5406 | |
---|
[ecce978] | 5407 | if (!cli->content_length) { |
---|
| 5408 | cli->last_cmd = OG_CMD_UNSPEC; |
---|
[c90cda4] | 5409 | return 0; |
---|
[ecce978] | 5410 | } |
---|
[c90cda4] | 5411 | |
---|
| 5412 | body = strstr(cli->buf, "\r\n\r\n") + 4; |
---|
| 5413 | |
---|
| 5414 | root = json_loads(body, 0, &json_err); |
---|
| 5415 | if (!root) { |
---|
| 5416 | syslog(LOG_ERR, "%s:%d: malformed json line %d: %s\n", |
---|
| 5417 | __FILE__, __LINE__, json_err.line, json_err.text); |
---|
| 5418 | return -1; |
---|
| 5419 | } |
---|
| 5420 | |
---|
[ecce978] | 5421 | switch (cli->last_cmd) { |
---|
| 5422 | case OG_CMD_PROBE: |
---|
[c90cda4] | 5423 | err = og_resp_probe(cli, root); |
---|
[ecce978] | 5424 | break; |
---|
| 5425 | case OG_CMD_SHELL_RUN: |
---|
[c90cda4] | 5426 | err = og_resp_shell_run(cli, root); |
---|
[ecce978] | 5427 | break; |
---|
| 5428 | case OG_CMD_HARDWARE: |
---|
[c90cda4] | 5429 | err = og_resp_hardware(root, cli); |
---|
[ecce978] | 5430 | break; |
---|
| 5431 | case OG_CMD_SOFTWARE: |
---|
[c90cda4] | 5432 | err = og_resp_software(root, cli); |
---|
[ecce978] | 5433 | break; |
---|
| 5434 | case OG_CMD_REFRESH: |
---|
| 5435 | err = og_resp_refresh(root, cli); |
---|
| 5436 | break; |
---|
| 5437 | case OG_CMD_SETUP: |
---|
[c90cda4] | 5438 | err = og_resp_refresh(root, cli); |
---|
[ecce978] | 5439 | break; |
---|
| 5440 | case OG_CMD_IMAGE_CREATE: |
---|
[c90cda4] | 5441 | err = og_resp_image_create(root, cli); |
---|
[ecce978] | 5442 | break; |
---|
| 5443 | case OG_CMD_IMAGE_RESTORE: |
---|
[c90cda4] | 5444 | err = og_resp_image_restore(root, cli); |
---|
[ecce978] | 5445 | break; |
---|
| 5446 | default: |
---|
[c90cda4] | 5447 | err = -1; |
---|
[ecce978] | 5448 | break; |
---|
| 5449 | } |
---|
| 5450 | |
---|
| 5451 | cli->last_cmd = OG_CMD_UNSPEC; |
---|
[c90cda4] | 5452 | |
---|
| 5453 | return err; |
---|
| 5454 | } |
---|
| 5455 | |
---|
[ecce978] | 5456 | static void og_agent_deliver_pending_cmd(struct og_client *cli) |
---|
| 5457 | { |
---|
| 5458 | const struct og_cmd *cmd; |
---|
| 5459 | |
---|
| 5460 | cmd = og_cmd_find(inet_ntoa(cli->addr.sin_addr)); |
---|
| 5461 | if (!cmd) |
---|
| 5462 | return; |
---|
| 5463 | |
---|
| 5464 | og_send_request(cmd->method, cmd->type, &cmd->params, cmd->json); |
---|
[21434ba] | 5465 | cli->last_cmd_id = cmd->id; |
---|
[ecce978] | 5466 | |
---|
| 5467 | og_cmd_free(cmd); |
---|
| 5468 | } |
---|
| 5469 | |
---|
[c90cda4] | 5470 | static void og_agent_read_cb(struct ev_loop *loop, struct ev_io *io, int events) |
---|
| 5471 | { |
---|
| 5472 | struct og_client *cli; |
---|
| 5473 | int ret; |
---|
| 5474 | |
---|
| 5475 | cli = container_of(io, struct og_client, io); |
---|
| 5476 | |
---|
| 5477 | ret = og_client_recv(cli, events); |
---|
| 5478 | if (ret <= 0) |
---|
| 5479 | goto close; |
---|
| 5480 | |
---|
| 5481 | ev_timer_again(loop, &cli->timer); |
---|
| 5482 | |
---|
| 5483 | cli->buf_len += ret; |
---|
| 5484 | if (cli->buf_len >= sizeof(cli->buf)) { |
---|
| 5485 | syslog(LOG_ERR, "client request from %s:%hu is too long\n", |
---|
| 5486 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
| 5487 | goto close; |
---|
| 5488 | } |
---|
| 5489 | |
---|
| 5490 | switch (cli->state) { |
---|
| 5491 | case OG_AGENT_RECEIVING_HEADER: |
---|
| 5492 | ret = og_agent_state_recv_hdr_rest(cli); |
---|
| 5493 | if (ret < 0) |
---|
| 5494 | goto close; |
---|
| 5495 | if (!ret) |
---|
| 5496 | return; |
---|
| 5497 | |
---|
| 5498 | cli->state = OG_AGENT_RECEIVING_PAYLOAD; |
---|
| 5499 | /* Fall through. */ |
---|
| 5500 | case OG_AGENT_RECEIVING_PAYLOAD: |
---|
| 5501 | /* Still not enough data to process request. */ |
---|
| 5502 | if (cli->buf_len < cli->msg_len) |
---|
| 5503 | return; |
---|
| 5504 | |
---|
| 5505 | cli->state = OG_AGENT_PROCESSING_RESPONSE; |
---|
| 5506 | /* fall through. */ |
---|
| 5507 | case OG_AGENT_PROCESSING_RESPONSE: |
---|
| 5508 | ret = og_agent_state_process_response(cli); |
---|
| 5509 | if (ret < 0) { |
---|
| 5510 | syslog(LOG_ERR, "Failed to process HTTP request from %s:%hu\n", |
---|
| 5511 | inet_ntoa(cli->addr.sin_addr), |
---|
| 5512 | ntohs(cli->addr.sin_port)); |
---|
| 5513 | goto close; |
---|
[b8774e7] | 5514 | } else if (ret == 0) { |
---|
| 5515 | og_agent_deliver_pending_cmd(cli); |
---|
[c90cda4] | 5516 | } |
---|
[ecce978] | 5517 | |
---|
[c90cda4] | 5518 | syslog(LOG_DEBUG, "leaving client %s:%hu in keepalive mode\n", |
---|
| 5519 | inet_ntoa(cli->addr.sin_addr), |
---|
| 5520 | ntohs(cli->addr.sin_port)); |
---|
| 5521 | og_agent_reset_state(cli); |
---|
| 5522 | break; |
---|
| 5523 | default: |
---|
| 5524 | syslog(LOG_ERR, "unknown state, critical internal error\n"); |
---|
| 5525 | goto close; |
---|
| 5526 | } |
---|
| 5527 | return; |
---|
| 5528 | close: |
---|
| 5529 | ev_timer_stop(loop, &cli->timer); |
---|
| 5530 | og_client_release(loop, cli); |
---|
| 5531 | } |
---|
| 5532 | |
---|
| 5533 | static void og_client_timer_cb(struct ev_loop *loop, ev_timer *timer, int events) |
---|
| 5534 | { |
---|
| 5535 | struct og_client *cli; |
---|
| 5536 | |
---|
| 5537 | cli = container_of(timer, struct og_client, timer); |
---|
| 5538 | if (cli->keepalive_idx >= 0) { |
---|
| 5539 | ev_timer_again(loop, &cli->timer); |
---|
| 5540 | return; |
---|
| 5541 | } |
---|
| 5542 | syslog(LOG_ERR, "timeout request for client %s:%hu\n", |
---|
| 5543 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
| 5544 | |
---|
| 5545 | og_client_release(loop, cli); |
---|
| 5546 | } |
---|
| 5547 | |
---|
[f5fa6dd] | 5548 | static void og_agent_send_refresh(struct og_client *cli) |
---|
[c90cda4] | 5549 | { |
---|
| 5550 | struct og_msg_params params; |
---|
| 5551 | int err; |
---|
| 5552 | |
---|
| 5553 | params.ips_array[0] = inet_ntoa(cli->addr.sin_addr); |
---|
| 5554 | params.ips_array_len = 1; |
---|
| 5555 | |
---|
[f5fa6dd] | 5556 | err = og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, ¶ms, NULL); |
---|
[c90cda4] | 5557 | if (err < 0) { |
---|
[f5fa6dd] | 5558 | syslog(LOG_ERR, "Can't send refresh to: %s\n", |
---|
[c90cda4] | 5559 | params.ips_array[0]); |
---|
| 5560 | } else { |
---|
[f5fa6dd] | 5561 | syslog(LOG_INFO, "Sent refresh to: %s\n", |
---|
[c90cda4] | 5562 | params.ips_array[0]); |
---|
| 5563 | } |
---|
| 5564 | } |
---|
| 5565 | |
---|
[71e678b] | 5566 | static int socket_rest, socket_agent_rest; |
---|
[c90cda4] | 5567 | |
---|
| 5568 | static void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, |
---|
| 5569 | int events) |
---|
| 5570 | { |
---|
| 5571 | struct sockaddr_in client_addr; |
---|
| 5572 | socklen_t addrlen = sizeof(client_addr); |
---|
| 5573 | struct og_client *cli; |
---|
| 5574 | int client_sd; |
---|
| 5575 | |
---|
| 5576 | if (events & EV_ERROR) |
---|
| 5577 | return; |
---|
| 5578 | |
---|
| 5579 | client_sd = accept(io->fd, (struct sockaddr *)&client_addr, &addrlen); |
---|
| 5580 | if (client_sd < 0) { |
---|
| 5581 | syslog(LOG_ERR, "cannot accept client connection\n"); |
---|
| 5582 | return; |
---|
| 5583 | } |
---|
| 5584 | |
---|
| 5585 | cli = (struct og_client *)calloc(1, sizeof(struct og_client)); |
---|
| 5586 | if (!cli) { |
---|
| 5587 | close(client_sd); |
---|
| 5588 | return; |
---|
| 5589 | } |
---|
| 5590 | memcpy(&cli->addr, &client_addr, sizeof(client_addr)); |
---|
| 5591 | if (io->fd == socket_agent_rest) |
---|
| 5592 | cli->keepalive_idx = 0; |
---|
| 5593 | else |
---|
| 5594 | cli->keepalive_idx = -1; |
---|
| 5595 | |
---|
| 5596 | if (io->fd == socket_rest) |
---|
| 5597 | cli->rest = true; |
---|
| 5598 | else if (io->fd == socket_agent_rest) |
---|
| 5599 | cli->agent = true; |
---|
| 5600 | |
---|
| 5601 | syslog(LOG_DEBUG, "connection from client %s:%hu\n", |
---|
| 5602 | inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port)); |
---|
| 5603 | |
---|
| 5604 | if (io->fd == socket_agent_rest) |
---|
| 5605 | ev_io_init(&cli->io, og_agent_read_cb, client_sd, EV_READ); |
---|
| 5606 | else |
---|
| 5607 | ev_io_init(&cli->io, og_client_read_cb, client_sd, EV_READ); |
---|
| 5608 | |
---|
| 5609 | ev_io_start(loop, &cli->io); |
---|
| 5610 | if (io->fd == socket_agent_rest) { |
---|
| 5611 | ev_timer_init(&cli->timer, og_client_timer_cb, |
---|
| 5612 | OG_AGENT_CLIENT_TIMEOUT, 0.); |
---|
| 5613 | } else { |
---|
| 5614 | ev_timer_init(&cli->timer, og_client_timer_cb, |
---|
| 5615 | OG_CLIENT_TIMEOUT, 0.); |
---|
| 5616 | } |
---|
| 5617 | ev_timer_start(loop, &cli->timer); |
---|
| 5618 | list_add(&cli->list, &client_list); |
---|
| 5619 | |
---|
[f5fa6dd] | 5620 | if (io->fd == socket_agent_rest) { |
---|
| 5621 | og_agent_send_refresh(cli); |
---|
| 5622 | } |
---|
[c90cda4] | 5623 | } |
---|
| 5624 | |
---|
| 5625 | static int og_socket_server_init(const char *port) |
---|
| 5626 | { |
---|
| 5627 | struct sockaddr_in local; |
---|
| 5628 | int sd, on = 1; |
---|
[588052e] | 5629 | |
---|
| 5630 | sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
---|
| 5631 | if (sd < 0) { |
---|
| 5632 | syslog(LOG_ERR, "cannot create main socket\n"); |
---|
| 5633 | return -1; |
---|
| 5634 | } |
---|
| 5635 | setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(int)); |
---|
| 5636 | |
---|
| 5637 | local.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
| 5638 | local.sin_family = AF_INET; |
---|
| 5639 | local.sin_port = htons(atoi(port)); |
---|
| 5640 | |
---|
| 5641 | if (bind(sd, (struct sockaddr *) &local, sizeof(local)) < 0) { |
---|
[438afb2] | 5642 | close(sd); |
---|
[588052e] | 5643 | syslog(LOG_ERR, "cannot bind socket\n"); |
---|
| 5644 | return -1; |
---|
| 5645 | } |
---|
| 5646 | |
---|
| 5647 | listen(sd, 250); |
---|
| 5648 | |
---|
| 5649 | return sd; |
---|
| 5650 | } |
---|
| 5651 | |
---|
[9baecf8] | 5652 | int main(int argc, char *argv[]) |
---|
| 5653 | { |
---|
[71e678b] | 5654 | struct ev_io ev_io_server_rest, ev_io_agent_rest; |
---|
[9baecf8] | 5655 | int i; |
---|
[3ec149c] | 5656 | |
---|
[d327da6] | 5657 | og_loop = ev_default_loop(0); |
---|
| 5658 | |
---|
[4797e93] | 5659 | if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) |
---|
| 5660 | exit(EXIT_FAILURE); |
---|
| 5661 | |
---|
[b11b753] | 5662 | openlog("ogAdmServer", LOG_PID, LOG_DAEMON); |
---|
[13e48b4] | 5663 | |
---|
[3ec149c] | 5664 | /*-------------------------------------------------------------------------------------------------------- |
---|
| 5665 | Validación de parámetros de ejecución y lectura del fichero de configuración del servicio |
---|
| 5666 | ---------------------------------------------------------------------------------------------------------*/ |
---|
| 5667 | if (!validacionParametros(argc, argv, 1)) // Valida parámetros de ejecución |
---|
| 5668 | exit(EXIT_FAILURE); |
---|
| 5669 | |
---|
| 5670 | if (!tomaConfiguracion(szPathFileCfg)) { // Toma parametros de configuracion |
---|
| 5671 | exit(EXIT_FAILURE); |
---|
| 5672 | } |
---|
| 5673 | |
---|
| 5674 | /*-------------------------------------------------------------------------------------------------------- |
---|
| 5675 | // Inicializa array de información de los clientes |
---|
| 5676 | ---------------------------------------------------------------------------------------------------------*/ |
---|
| 5677 | for (i = 0; i < MAXIMOS_CLIENTES; i++) { |
---|
| 5678 | tbsockets[i].ip[0] = '\0'; |
---|
[2e0c063] | 5679 | tbsockets[i].cli = NULL; |
---|
[3ec149c] | 5680 | } |
---|
| 5681 | /*-------------------------------------------------------------------------------------------------------- |
---|
| 5682 | Creación y configuración del socket del servicio |
---|
| 5683 | ---------------------------------------------------------------------------------------------------------*/ |
---|
[9baecf8] | 5684 | |
---|
[1a08c06] | 5685 | socket_rest = og_socket_server_init("8888"); |
---|
| 5686 | if (socket_rest < 0) |
---|
| 5687 | exit(EXIT_FAILURE); |
---|
| 5688 | |
---|
| 5689 | ev_io_init(&ev_io_server_rest, og_server_accept_cb, socket_rest, EV_READ); |
---|
[d327da6] | 5690 | ev_io_start(og_loop, &ev_io_server_rest); |
---|
[1a08c06] | 5691 | |
---|
[c90cda4] | 5692 | socket_agent_rest = og_socket_server_init("8889"); |
---|
| 5693 | if (socket_agent_rest < 0) |
---|
| 5694 | exit(EXIT_FAILURE); |
---|
| 5695 | |
---|
| 5696 | ev_io_init(&ev_io_agent_rest, og_server_accept_cb, socket_agent_rest, EV_READ); |
---|
[d327da6] | 5697 | ev_io_start(og_loop, &ev_io_agent_rest); |
---|
| 5698 | |
---|
| 5699 | if (og_dbi_schedule_get() < 0) |
---|
| 5700 | exit(EXIT_FAILURE); |
---|
| 5701 | |
---|
| 5702 | og_schedule_next(og_loop); |
---|
[c90cda4] | 5703 | |
---|
[3ec149c] | 5704 | infoLog(1); // Inicio de sesión |
---|
[9baecf8] | 5705 | |
---|
[256fbf2] | 5706 | /* old log file has been deprecated. */ |
---|
| 5707 | og_log(97, false); |
---|
| 5708 | |
---|
[13e48b4] | 5709 | syslog(LOG_INFO, "Waiting for connections\n"); |
---|
| 5710 | |
---|
[9baecf8] | 5711 | while (1) |
---|
[d327da6] | 5712 | ev_loop(og_loop, 0); |
---|
[9baecf8] | 5713 | |
---|
[3ec149c] | 5714 | exit(EXIT_SUCCESS); |
---|
| 5715 | } |
---|