From 24c8b940e614fa795b679628be2e6c0d68bd41e5 Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Thu, 8 Oct 2020 12:37:09 +0200 Subject: Add REST POST /client/delete method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This method deletes a client from the DB. Request: POST /client/delete { "clients": ["192.168.56.30"] } Response: 200 OK Co-authored-by: Javier Sánchez Parra --- src/rest.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'src') diff --git a/src/rest.c b/src/rest.c index a51134a..a69e5be 100644 --- a/src/rest.c +++ b/src/rest.c @@ -1351,6 +1351,56 @@ static int og_cmd_post_client_add(json_t *element, return 0; } +static int og_cmd_post_client_delete(json_t *element, + struct og_msg_params *params) +{ + const char *key, *msglog; + struct og_dbi *dbi; + dbi_result result; + unsigned int i; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "clients")) + err = og_json_parse_clients(value, params); + else + err = -1; + + if (err < 0) + break; + } + + if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) + 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++) { + result = dbi_conn_queryf(dbi->conn, + "DELETE FROM ordenadores WHERE ip='%s'", + params->ips_array[i]); + + 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; + } + + dbi_result_free(result); + } + + og_dbi_close(dbi); + return 0; +} + static int og_cmd_stop(json_t *element, struct og_msg_params *params) { const char *key; @@ -3536,6 +3586,17 @@ int og_client_state_process_payload_rest(struct og_client *cli) } err = og_cmd_post_client_add(root, ¶ms, buf_reply); + } else if (!strncmp(cmd, "client/delete", strlen("client/delete"))) { + if (method != OG_METHOD_POST) + return og_client_method_not_found(cli); + + if (!root) { + syslog(LOG_ERR, + "command client delete with no payload\n"); + return og_client_bad_request(cli); + } + + err = og_cmd_post_client_delete(root, ¶ms); } else if (!strncmp(cmd, "wol", strlen("wol"))) { if (method != OG_METHOD_POST) return og_client_method_not_found(cli); -- cgit v1.2.3-18-g5258