src: infer server IP when it has more than one IP address

infer server IP for a given client. User does not have to attach a client to
a given server IP anymore through ordenadores.identorno.

this simplifies previous work to allow a server to have more than one IP address

a0a3470682 ('#1074 rest: set_mode: add support for different ogserver addresses')
44745a3f22 ('rest: add POST client/server method')

POST /client/server is removed, it is only used by ogcli and explicit association
between server and client is not required.

server_id json attribute is also removed in GET client/info.
master
OpenGnSys Support Team 2024-08-28 12:45:46 +02:00
parent 9ee9d66b7c
commit cd9650108a
3 changed files with 96 additions and 117 deletions

View File

@ -76,7 +76,6 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
" ordenadores.oglivedir," " ordenadores.oglivedir,"
" ordenadores.inremotepc," " ordenadores.inremotepc,"
" ordenadores.maintenance," " ordenadores.maintenance,"
" ordenadores.identorno,"
" aulas.netmask," " aulas.netmask,"
" centros.idcentro " " centros.idcentro "
"FROM ordenadores " "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")); dbi_result_get_string(result, "ip"));
snprintf(computer->mac, sizeof(computer->mac), "%s", snprintf(computer->mac, sizeof(computer->mac), "%s",
dbi_result_get_string(result, "mac")); dbi_result_get_string(result, "mac"));
computer->server_id = dbi_result_get_uint(result, "identorno");
computer->room = dbi_result_get_uint(result, "idaula"); computer->room = dbi_result_get_uint(result, "idaula");
computer->center = dbi_result_get_uint(result, "idcentro"); computer->center = dbi_result_get_uint(result, "idcentro");
computer->hardware_id = dbi_result_get_uint(result, "idperfilhard"); 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; 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, int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint32_t repo_id,
const char *client_ip, char *repository_ip) const char *client_ip, char *repository_ip)
{ {

View File

@ -82,7 +82,6 @@ struct og_legacy_partition {
struct og_computer { struct og_computer {
unsigned int procedure_id; unsigned int procedure_id;
unsigned int hardware_id; unsigned int hardware_id;
unsigned int server_id;
unsigned int repo_id; unsigned int repo_id;
unsigned int center; unsigned int center;
unsigned int room; 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, int og_dbi_get_repository_ip(const struct og_dbi *dbi, const uint32_t repo_id,
const char *client_ip, char *repository_ip); 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 #endif

View File

@ -1508,7 +1508,6 @@ struct og_boot_mode_params {
const char *dns; const char *dns;
const char *proxy; const char *proxy;
const char *ip; const char *ip;
const char *ipserveradm;
const char *nombreaula; const char *nombreaula;
const char *oglivedir; const char *oglivedir;
const char *hardprofile; 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] = {}; char repository_ip[OG_DB_IP_MAXLEN + 1] = {};
struct og_boot_mode_params boot_params = {}; struct og_boot_mode_params boot_params = {};
char server_ip[OG_DB_IP_MAXLEN + 1] = {};
const char *res_prefix = " vga="; const char *res_prefix = " vga=";
const char *res_value = "788"; const char *res_value = "788";
char *lang = getenv("LANG"); 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, result = dbi_conn_queryf(dbi->conn,
"SELECT " "SELECT "
"ordenadores.ip AS ip, " "ordenadores.ip AS ip, "
"entornos.ipserveradm AS ipserveradm, "
"aulas.router AS router, " "aulas.router AS router, "
"aulas.netmask AS netmask, " "aulas.netmask AS netmask, "
"ordenadores.nombreordenador AS nombreordenador, " "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 aulas USING(idaula) "
"JOIN centros USING(idcentro) " "JOIN centros USING(idcentro) "
"JOIN entidades USING(identidad) " "JOIN entidades USING(identidad) "
"JOIN entornos USING(identorno) "
"LEFT JOIN repositorios USING(idrepositorio) " "LEFT JOIN repositorios USING(idrepositorio) "
"LEFT JOIN perfileshard USING(idperfilhard) " "LEFT JOIN perfileshard USING(idperfilhard) "
"LEFT JOIN menus USING(idmenu) " "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.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.router = dbi_result_get_string_copy(result, "router");
boot_params.netmask = dbi_result_get_string_copy(result, "netmask"); boot_params.netmask = dbi_result_get_string_copy(result, "netmask");
boot_params.nombreordenador = dbi_result_get_string_copy(result, "nombreordenador"); 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); 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, if (og_dbi_get_repository_ip(dbi, boot_params.repoid, boot_params.ip,
repository_ip) < 0) { repository_ip) < 0) {
err = -1; 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" " 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", "%s%s%s%s%s%s%s%s%s%s%s%s",
lang, 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, boot_params.nombreaula,
repository_ip, repository_ip,
boot_params.ipserveradm, boot_params.ipserveradm, boot_params.ipserveradm, server_ip, server_ip, server_ip,
boot_params.oglivedir, boot_params.oglivedir,
boot_params.is_prof ? "true" : "false", 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.hardprofile[0] ? " hardprofile=" : "", boot_params.hardprofile[0] ? boot_params.hardprofile: "",
boot_params.ntp[0] ? " ogntp=" : "", boot_params.ntp[0] ? boot_params.ntp : "", boot_params.ntp[0] ? " ogntp=" : "", boot_params.ntp[0] ? boot_params.ntp : "",
boot_params.dns[0] ? " ogdns=" : "", boot_params.dns[0] ? boot_params.dns : "", 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); res_prefix, res_value);
err_out: err_out:
free((void *)boot_params.ip); free((void *)boot_params.ip);
free((void *)boot_params.ipserveradm);
free((void *)boot_params.router); free((void *)boot_params.router);
free((void *)boot_params.netmask); free((void *)boot_params.netmask);
free((void *)boot_params.nombreordenador); free((void *)boot_params.nombreordenador);
@ -1895,29 +1896,6 @@ static int og_cmd_get_client_setup(json_t *element,
return 0; 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, static int og_dbi_update_client_repo(struct og_dbi *dbi,
const char *mac, const char *mac,
const char *repo_id) const char *repo_id)
@ -2009,74 +1987,6 @@ static int og_cmd_post_client_repo(json_t *element,
return 0; 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, &params->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, static int og_cmd_get_center_info(json_t *element,
struct og_msg_params *params, struct og_msg_params *params,
char *buffer_reply) char *buffer_reply)
@ -2190,7 +2100,6 @@ static int og_cmd_get_client_info(json_t *element,
json_string(computer.serial_number)); json_string(computer.serial_number));
json_object_set_new(root, "hardware_id", json_object_set_new(root, "hardware_id",
json_integer(computer.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, "netdriver", json_string(computer.netdriver));
json_object_set_new(root, "maintenance", json_boolean(computer.maintenance)); json_object_set_new(root, "maintenance", json_boolean(computer.maintenance));
json_object_set_new(root, "netiface", json_string(computer.netiface)); 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, &params, buf_reply); err = og_cmd_get_client_setup(root, &params, 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, &params, buf_reply);
break;
default:
err = og_client_method_not_found(cli);
goto err_process_rest_payload;
}
} else if (!strncmp(cmd, "client/info", } else if (!strncmp(cmd, "client/info",
strlen("client/info"))) { strlen("client/info"))) {
if (method != OG_METHOD_GET) { if (method != OG_METHOD_GET) {