From fc0f19fe30559f5c9c916789bc8690d13d798d64 Mon Sep 17 00:00:00 2001 From: Javier Hernandez Date: Wed, 22 Nov 2023 12:23:47 +0100 Subject: rest: add /client/update Add POST client/update request to update client, this is identified by its ip address, with the data provided in the payload. --- src/rest.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/src/rest.c b/src/rest.c index 7aab57a..6b361d5 100644 --- a/src/rest.c +++ b/src/rest.c @@ -1722,6 +1722,119 @@ static int og_cmd_get_client_info(json_t *element, return 0; } +static int og_cmd_post_client_update(json_t *element, + struct og_msg_params *params, + char *buffer_reply) +{ + struct og_computer computer = {}; + const char *key, *msglog; + struct og_dbi *dbi; + dbi_result result; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "ip")) { + err = og_json_parse_string_copy(value, + computer.ip, + sizeof(computer.ip)); + params->flags |= OG_REST_PARAM_ADDR; + } else if (!strcmp(key, "serial_number")) { + err = og_json_parse_string_copy(value, + computer.serial_number, + sizeof(computer.serial_number)); + } else if (!strcmp(key, "netdriver")) { + err = og_json_parse_string_copy(value, + computer.netdriver, + sizeof(computer.netdriver)); + } else if (!strcmp(key, "maintenance")) { + err = og_json_parse_bool(value, &computer.maintenance); + } else if (!strcmp(key, "netiface")) { + err = og_json_parse_string_copy(value, + computer.netiface, + sizeof(computer.netiface)); + } else if (!strcmp(key, "repo_id")) { + err = og_json_parse_uint(value, &computer.repo_id); + } else if (!strcmp(key, "netmask")) { + err = og_json_parse_string_copy(value, + computer.netmask, + sizeof(computer.netmask)); + } else if (!strcmp(key, "remote")) { + err = og_json_parse_bool(value, &computer.remote); + } else if (!strcmp(key, "room")) { + err = og_json_parse_uint(value, &computer.room); + } else if (!strcmp(key, "name")) { + err = og_json_parse_string_copy(value, + computer.name, + sizeof(computer.name)); + } else if (!strcmp(key, "mac")) { + err = og_json_parse_string_copy(value, + computer.mac, + sizeof(computer.mac)); + } + + if (err < 0) + return err; + } + 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; + } + + result = dbi_conn_queryf(dbi->conn, + "SELECT ip FROM ordenadores WHERE ip='%s'", + computer.ip); + 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_get_numrows(result) == 0) { + syslog(LOG_ERR, "client with that IP does not exist: %s\n", + computer.ip); + dbi_result_free(result); + og_dbi_close(dbi); + return -1; + } + result = dbi_conn_queryf(dbi->conn, + "UPDATE ordenadores" + " SET numserie='%s'," + " netdriver='%s'," + " maintenance=%u," + " netiface='%s'," + " idrepositorio=%u," + " mascara='%s'," + " inremotepc=%u," + " idaula=%u," + " nombreordenador='%s'," + " mac='%s'" + " WHERE ip='%s';", + computer.serial_number, + computer.netdriver, computer.maintenance, + computer.netiface, computer.repo_id, + computer.netmask, computer.remote, + computer.room, computer.name, + computer.mac, computer.ip); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to update client in 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_post_client_add(json_t *element, struct og_msg_params *params, char *buffer_reply) @@ -6424,6 +6537,20 @@ 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/update", strlen("client/update"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command client info with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + + err = og_cmd_post_client_update(root, ¶ms, buf_reply); } else if (!strncmp(cmd, "client/delete", strlen("client/delete"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); -- cgit v1.2.3-18-g5258