From 128bba364ab73661b72a2ba1ce7c0299d0b25b47 Mon Sep 17 00:00:00 2001 From: Javier Hernandez Date: Tue, 16 Jan 2024 11:37:10 +0100 Subject: rest: Check no client owns provided mac address Reply with an error if user tries to create a client with a mac that is already owned by another client Write to syslog info about the client that owns the mac: client's name, ip, mac address and room --- src/rest.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/rest.c b/src/rest.c index be92fa9..8ba9837 100644 --- a/src/rest.c +++ b/src/rest.c @@ -2116,6 +2116,7 @@ static int og_cmd_post_client_add(json_t *element, struct og_msg_params *params, char *buffer_reply) { + const char *room_name, *computer_name, *computer_ip, *hwaddr; struct og_computer computer = {}; const char *key, *msglog; struct og_dbi *dbi; @@ -2209,6 +2210,34 @@ static int og_cmd_post_client_add(json_t *element, } dbi_result_free(result); + result = dbi_conn_queryf(dbi->conn, + "SELECT nombreordenador, ip, nombreaula" + " FROM ordenadores" + " INNER JOIN aulas on ordenadores.idaula = aulas.idaula" + " WHERE mac='%s'", + computer.mac); + + 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; + } + + if (dbi_result_next_row(result)) { + room_name = dbi_result_get_string(result, "nombreaula"); + computer_name = dbi_result_get_string(result, "nombreordenador"); + computer_ip = dbi_result_get_string(result, "ip"); + hwaddr = dbi_result_get_string(result, "mac"); + syslog(LOG_ERR, "Failed to add client %s because %s (%s) in room %s already owns MAC address %s\n", + computer.ip, computer_ip, computer_name, room_name, hwaddr); + dbi_result_free(result); + og_dbi_close(dbi); + return -1; + } + dbi_result_free(result); + result = dbi_conn_queryf(dbi->conn, "INSERT INTO ordenadores(" " nombreordenador," -- cgit v1.2.3-18-g5258