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