diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/dbi.c | 87 | ||||
-rw-r--r-- | src/dbi.h | 3 | ||||
-rw-r--r-- | src/rest.c | 123 |
3 files changed, 96 insertions, 117 deletions
@@ -76,7 +76,6 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, " ordenadores.oglivedir," " ordenadores.inremotepc," " ordenadores.maintenance," - " ordenadores.identorno," " aulas.netmask," " centros.idcentro " "FROM ordenadores " @@ -105,7 +104,6 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, dbi_result_get_string(result, "ip")); snprintf(computer->mac, sizeof(computer->mac), "%s", dbi_result_get_string(result, "mac")); - computer->server_id = dbi_result_get_uint(result, "identorno"); computer->room = dbi_result_get_uint(result, "idaula"); computer->center = dbi_result_get_uint(result, "idcentro"); computer->hardware_id = dbi_result_get_uint(result, "idperfilhard"); @@ -269,6 +267,91 @@ bool og_dbi_get_image(struct og_dbi *dbi, struct og_image *image) return false; } +int og_dbi_get_server_ip(const struct og_dbi *dbi, const char *client_ip, + char *res_server_ip) +{ + const char *msglog, *netmask_ip, *server_ip; + struct in_addr client, netmask, server; + bool found = false; + dbi_result result; + + if (inet_aton(client_ip, &client) < 0) { + syslog(LOG_ERR, "failed to parse client IP (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + result = dbi_conn_queryf(dbi->conn, + "SELECT aulas.netmask FROM aulas " + "INNER JOIN ordenadores ON ordenadores.idaula = aulas.idaula " + "WHERE ordenadores.ip = '%s'", client_ip); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to get netmask (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + if (!dbi_result_next_row(result)) { + syslog(LOG_ERR, "netmask does not exist in database (%s:%d)\n", + __func__, __LINE__); + dbi_result_free(result); + return -1; + } + + netmask_ip = dbi_result_get_string(result, "netmask"); + if (inet_aton(netmask_ip, &netmask) < 0) { + syslog(LOG_ERR, "failed to parse netmask (%s:%d)\n", + __func__, __LINE__); + dbi_result_free(result); + return -1; + } + + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "SELECT ipserveradm FROM entornos"); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + while (dbi_result_next_row(result) > 0) { + server_ip = dbi_result_get_string(result, "ipserveradm"); + + if (inet_aton(server_ip, &server) < 0) { + syslog(LOG_ERR, "failed to get repository IP (%s:%d)\n", + __func__, __LINE__); + dbi_result_free(result); + return -1; + } + client.s_addr &= netmask.s_addr; + + if (client.s_addr != (server.s_addr & netmask.s_addr)) + continue; + + found = true; + break; + } + + if (!found) { + syslog(LOG_ERR, + "cannot find server IP for client %s (%s:%d)\n", + client_ip, __func__, __LINE__); + dbi_result_free(result); + return -1; + } + + snprintf(res_server_ip, OG_DB_IP_MAXLEN + 1, "%s", server_ip); + dbi_result_free(result); + + syslog(LOG_INFO, "using server IP %s for computer %s\n", + res_server_ip, client_ip); + + return 0; +} int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint32_t repo_id, const char *client_ip, char *repository_ip) { @@ -82,7 +82,6 @@ struct og_legacy_partition { struct og_computer { unsigned int procedure_id; unsigned int hardware_id; - unsigned int server_id; unsigned int repo_id; unsigned int center; unsigned int room; @@ -140,5 +139,7 @@ int og_dbi_schema_update(void); int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint32_t repo_id, const char *client_ip, char *repository_ip); +int og_dbi_get_server_ip(const struct og_dbi *dbi, const char *client_ip, + char *server_ip); #endif @@ -1508,7 +1508,6 @@ struct og_boot_mode_params { const char *dns; const char *proxy; const char *ip; - const char *ipserveradm; const char *nombreaula; const char *oglivedir; const char *hardprofile; @@ -1527,6 +1526,7 @@ static int og_get_client_mode_params(struct og_dbi *dbi, const char *mac, char * { char repository_ip[OG_DB_IP_MAXLEN + 1] = {}; struct og_boot_mode_params boot_params = {}; + char server_ip[OG_DB_IP_MAXLEN + 1] = {}; const char *res_prefix = " vga="; const char *res_value = "788"; char *lang = getenv("LANG"); @@ -1537,7 +1537,6 @@ static int og_get_client_mode_params(struct og_dbi *dbi, const char *mac, char * result = dbi_conn_queryf(dbi->conn, "SELECT " "ordenadores.ip AS ip, " - "entornos.ipserveradm AS ipserveradm, " "aulas.router AS router, " "aulas.netmask AS netmask, " "ordenadores.nombreordenador AS nombreordenador, " @@ -1557,7 +1556,6 @@ static int og_get_client_mode_params(struct og_dbi *dbi, const char *mac, char * "JOIN aulas USING(idaula) " "JOIN centros USING(idcentro) " "JOIN entidades USING(identidad) " - "JOIN entornos USING(identorno) " "LEFT JOIN repositorios USING(idrepositorio) " "LEFT JOIN perfileshard USING(idperfilhard) " "LEFT JOIN menus USING(idmenu) " @@ -1581,7 +1579,6 @@ static int og_get_client_mode_params(struct og_dbi *dbi, const char *mac, char * } boot_params.ip = dbi_result_get_string_copy(result, "ip"); - boot_params.ipserveradm = dbi_result_get_string_copy(result, "ipserveradm"); boot_params.router = dbi_result_get_string_copy(result, "router"); boot_params.netmask = dbi_result_get_string_copy(result, "netmask"); boot_params.nombreordenador = dbi_result_get_string_copy(result, "nombreordenador"); @@ -1606,6 +1603,11 @@ static int og_get_client_mode_params(struct og_dbi *dbi, const char *mac, char * dbi_result_free(result); + if (og_dbi_get_server_ip(dbi, boot_params.ip, server_ip) < 0) { + err = -1; + goto err_out; + } + if (og_dbi_get_repository_ip(dbi, boot_params.repoid, boot_params.ip, repository_ip) < 0) { err = -1; @@ -1616,13 +1618,13 @@ static int og_get_client_mode_params(struct og_dbi *dbi, const char *mac, char * " LANG=%s ip=%s:%s:%s:%s:%s:%s:none group=%s ogrepo=%s oglive=%s oglog=%s ogshare=%s oglivedir=%s ogprof=%s server=%s" "%s%s%s%s%s%s%s%s%s%s%s%s", lang, - boot_params.ip, boot_params.ipserveradm, boot_params.router, boot_params.netmask, boot_params.nombreordenador, boot_params.netiface, + boot_params.ip, server_ip, boot_params.router, boot_params.netmask, boot_params.nombreordenador, boot_params.netiface, boot_params.nombreaula, repository_ip, - boot_params.ipserveradm, boot_params.ipserveradm, boot_params.ipserveradm, + server_ip, server_ip, server_ip, boot_params.oglivedir, boot_params.is_prof ? "true" : "false", - boot_params.ipserveradm, + server_ip, boot_params.hardprofile[0] ? " hardprofile=" : "", boot_params.hardprofile[0] ? boot_params.hardprofile: "", boot_params.ntp[0] ? " ogntp=" : "", boot_params.ntp[0] ? boot_params.ntp : "", boot_params.dns[0] ? " ogdns=" : "", boot_params.dns[0] ? boot_params.dns : "", @@ -1631,7 +1633,6 @@ static int og_get_client_mode_params(struct og_dbi *dbi, const char *mac, char * res_prefix, res_value); err_out: free((void *)boot_params.ip); - free((void *)boot_params.ipserveradm); free((void *)boot_params.router); free((void *)boot_params.netmask); free((void *)boot_params.nombreordenador); @@ -1895,29 +1896,6 @@ static int og_cmd_get_client_setup(json_t *element, return 0; } -static int og_dbi_update_client_entorno(struct og_dbi *dbi, - const char *mac, - const char *server_id) -{ - const char *msglog; - dbi_result result; - - result = dbi_conn_queryf(dbi->conn, - "UPDATE ordenadores SET identorno=%s " - "WHERE mac='%s'", - server_id, mac); - - if (!result) { - dbi_conn_error(dbi->conn, &msglog); - syslog(LOG_ERR, "failed to update client's server (%s:%d) %s\n", - __func__, __LINE__, msglog); - return -1; - } - - dbi_result_free(result); - return 0; -} - static int og_dbi_update_client_repo(struct og_dbi *dbi, const char *mac, const char *repo_id) @@ -2009,74 +1987,6 @@ static int og_cmd_post_client_repo(json_t *element, return 0; } -static int og_cmd_post_client_server(json_t *element, - struct og_msg_params *params, - char *buffer_reply) -{ - char ips_str[(OG_DB_IP_MAXLEN + 1) * OG_CLIENTS_MAX + 1] = {}; - const char *key, *msglog, *mac; - int ips_str_len = 0; - struct og_dbi *dbi; - dbi_result result; - int err = 0, i; - json_t *value; - - json_object_foreach(element, key, value) { - if (!strcmp(key, "clients")) { - err = og_json_parse_clients(value, params); - } else if (!strcmp(key, "id")) { - err = og_json_parse_string(value, ¶ms->id); - params->flags |= OG_REST_PARAM_ID; - } - - if (err < 0) - return err; - } - - if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | - OG_REST_PARAM_ID)) - return -1; - - dbi = og_dbi_open(&ogconfig.db); - if (!dbi) { - syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", - __func__, __LINE__); - return -1; - } - - for (i = 0; i < params->ips_array_len; ++i) { - ips_str_len += snprintf(ips_str + ips_str_len, - sizeof(ips_str) - ips_str_len, - "'%s',", params->ips_array[i]); - } - ips_str[ips_str_len - 1] = '\0'; - - result = dbi_conn_queryf(dbi->conn, - "SELECT mac FROM ordenadores " - "WHERE ip IN (%s)", ips_str); - if (!result) { - dbi_conn_error(dbi->conn, &msglog); - syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", - __func__, __LINE__, msglog); - og_dbi_close(dbi); - return -1; - } - - while (dbi_result_next_row(result)) { - mac = dbi_result_get_string(result, "mac"); - err = og_dbi_update_client_entorno(dbi, mac, params->id); - if (err != 0) { - dbi_result_free(result); - og_dbi_close(dbi); - return -1; - } - } - - dbi_result_free(result); - og_dbi_close(dbi); - return 0; -} - static int og_cmd_get_center_info(json_t *element, struct og_msg_params *params, char *buffer_reply) @@ -2190,7 +2100,6 @@ static int og_cmd_get_client_info(json_t *element, json_string(computer.serial_number)); json_object_set_new(root, "hardware_id", json_integer(computer.hardware_id)); - json_object_set_new(root, "server_id", json_integer(computer.server_id)); json_object_set_new(root, "netdriver", json_string(computer.netdriver)); json_object_set_new(root, "maintenance", json_boolean(computer.maintenance)); json_object_set_new(root, "netiface", json_string(computer.netiface)); @@ -8561,20 +8470,6 @@ int og_client_state_process_payload_rest(struct og_client *cli) } err = og_cmd_get_client_setup(root, ¶ms, buf_reply); - } else if (!strncmp(cmd, "client/server", strlen("client/server"))) { - switch (method) { - case OG_METHOD_POST: - if (!root) { - syslog(LOG_ERR, "client post server command with no payload\n"); - err = og_client_bad_request(cli); - goto err_process_rest_payload; - } - err = og_cmd_post_client_server(root, ¶ms, buf_reply); - break; - default: - err = og_client_method_not_found(cli); - goto err_process_rest_payload; - } } else if (!strncmp(cmd, "client/info", strlen("client/info"))) { if (method != OG_METHOD_GET) { |