From 8a23579cabb747cc6662b1b1bafefb7ba79e22b8 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Mon, 8 Jul 2024 12:22:55 +0200 Subject: rest: update client ip in /client/update Use idordenador as discriminator value instead of the client's ip. Update client ip if the new ip is not used by other clients. --- src/rest.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/rest.c') diff --git a/src/rest.c b/src/rest.c index 8563fd2..48caf3b 100644 --- a/src/rest.c +++ b/src/rest.c @@ -2199,6 +2199,8 @@ static int og_cmd_post_client_update(json_t *element, computer.ip, sizeof(computer.ip)); params->flags |= OG_REST_PARAM_ADDR; + } else if (!strcmp(key, "id")) { + err = og_json_parse_uint(value, &computer.id); } else if (!strcmp(key, "serial_number")) { err = og_json_parse_string_copy(value, computer.serial_number, @@ -2251,8 +2253,8 @@ static int og_cmd_post_client_update(json_t *element, } result = dbi_conn_queryf(dbi->conn, - "SELECT ip FROM ordenadores WHERE ip='%s'", - computer.ip); + "SELECT ip FROM ordenadores WHERE ip='%s' AND idordenador<>'%u'", + computer.ip, computer.id); if (!result) { dbi_conn_error(dbi->conn, &msglog); syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", @@ -2260,8 +2262,8 @@ static int og_cmd_post_client_update(json_t *element, og_dbi_close(dbi); return -1; } - if (dbi_result_get_numrows(result) == 0) { - syslog(LOG_ERR, "client with that IP does not exist: %s\n", + if (dbi_result_get_numrows(result) > 0) { + syslog(LOG_ERR, "client with IP %s already exist\n", computer.ip); dbi_result_free(result); og_dbi_close(dbi); @@ -2271,8 +2273,8 @@ static int og_cmd_post_client_update(json_t *element, dbi_result_free(result); result = dbi_conn_queryf(dbi->conn, - "SELECT ip FROM ordenadores WHERE ip<>'%s' AND mac='%s'", - computer.ip, computer.mac); + "SELECT ip FROM ordenadores WHERE mac='%s' AND idordenador<>'%u'", + computer.mac, computer.id); if (!result) { dbi_conn_error(dbi->conn, &msglog); syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", @@ -2294,6 +2296,7 @@ static int og_cmd_post_client_update(json_t *element, result = dbi_conn_queryf(dbi->conn, "UPDATE ordenadores" " SET numserie='%s'," + " ip='%s'," " netdriver='%s'," " maintenance=%u," " netiface='%s'," @@ -2304,14 +2307,15 @@ static int og_cmd_post_client_update(json_t *element, " nombreordenador='%s'," " mac='%s'," " arranque='%s'" - " WHERE ip='%s';", + " WHERE idordenador='%u';", computer.serial_number, + computer.ip, computer.netdriver, computer.maintenance, computer.netiface, computer.repo_id, computer.netmask, computer.remote, computer.room, computer.name, computer.mac, computer.boot, - computer.ip); + computer.id); if (!result) { dbi_conn_error(dbi->conn, &msglog); syslog(LOG_ERR, "failed to update client in database (%s:%d) %s\n", -- cgit v1.2.3-18-g5258