From dc68d0f1354c11185f87f7111130fc4aa367af60 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Tue, 10 Oct 2023 17:05:33 +0200 Subject: rest: add POST /client/repo Set a client repository in the OpenGnsys database using the endpoint POST /client/repo. Expects "clients" and "id" parameters in the request payload. POST /client/repo { "clients": [...], "id": "2" } Response: 200 OK --- src/rest.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) (limited to 'src') diff --git a/src/rest.c b/src/rest.c index 9f0a0da..de2d9ed 100644 --- a/src/rest.c +++ b/src/rest.c @@ -1414,6 +1414,97 @@ static int og_dbi_update_client_entorno(struct og_dbi *dbi, return 0; } +static int og_dbi_update_client_repo(struct og_dbi *dbi, + const char *mac, + const char *repo_id) +{ + const char *msglog; + dbi_result result; + + result = dbi_conn_queryf(dbi->conn, + "UPDATE ordenadores SET idrepositorio=%s " + "WHERE mac='%s'", + repo_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_cmd_post_client_repo(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, ¶ms->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_repo(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_post_client_server(json_t *element, struct og_msg_params *params, char *buffer_reply) @@ -6122,6 +6213,20 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } + } else if (!strncmp(cmd, "client/repo", strlen("client/repo"))) { + switch (method) { + case OG_METHOD_POST: + if (!root) { + syslog(LOG_ERR, "client post repo command with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + err = og_cmd_post_client_repo(root, ¶ms, buf_reply); + break; + default: + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } } else if (!strncmp(cmd, "client/setup", strlen("client/setup"))) { if (method != OG_METHOD_GET) { -- cgit v1.2.3-18-g5258