diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client.c | 9 | ||||
-rw-r--r-- | src/rest.c | 240 | ||||
-rw-r--r-- | src/rest.h | 2 | ||||
-rw-r--r-- | src/schema.c | 34 |
4 files changed, 281 insertions, 4 deletions
diff --git a/src/client.c b/src/client.c index 817a9c0..7e787e4 100644 --- a/src/client.c +++ b/src/client.c @@ -1120,14 +1120,16 @@ static int og_resp_image_create(json_t *data, struct og_client *cli) soft_legacy.center); if (!res) { og_dbi_close(dbi); - syslog(LOG_ERR, "Problem updating client configuration\n"); + syslog(LOG_ERR, "Problem updating software inventory (%s:%u)\n", + __FILE__, __LINE__); return -1; } res = og_dbi_update_image(dbi, &img_legacy, soft_legacy.id); if (!res) { og_dbi_close(dbi); - syslog(LOG_ERR, "Problem updating client configuration\n"); + syslog(LOG_ERR, "Problem updating image (%s:%u)\n", + __FILE__, __LINE__); return -1; } @@ -1137,7 +1139,8 @@ static int og_resp_image_create(json_t *data, struct og_client *cli) og_dbi_close(dbi); if (res) { - syslog(LOG_ERR, "Problem updating image info\n"); + syslog(LOG_ERR, "Problem updating image (%s:%u)\n", + __FILE__, __LINE__); return -1; } @@ -4607,8 +4607,12 @@ static int og_cmd_oglive_list(char *buffer_reply) struct og_buffer og_buffer = { .data = buffer_reply }; + + const char *msglog, *live_name, *live_datetime; + json_t *root, *live_entry, *oglive_array; json_error_t json_err; - json_t *root; + struct og_dbi *dbi; + dbi_result result; root = json_load_file(OG_LIVE_JSON_FILE_PATH, 0, &json_err); if (!root) { @@ -4617,6 +4621,58 @@ static int og_cmd_oglive_list(char *buffer_reply) return -1; } + oglive_array = json_object_get(root, "oglive"); + if (!oglive_array || !json_is_array(oglive_array)) { + syslog(LOG_ERR, "Expected 'oglive' to be a JSON array\n"); + json_decref(root); + return -1; + } + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", + __func__, __LINE__); + json_decref(root); + return -1; + } + + result = dbi_conn_queryf(dbi->conn, + "SELECT name, " + "DATE_FORMAT(creation_date, '%%a %%b %%d %%H:%%i:%%s %%Y') AS formatted_date " + "FROM oglive"); + + 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); + json_decref(root); + return -1; + } + + while (dbi_result_next_row(result) > 0) { + live_name = dbi_result_get_string(result, "name"); + live_datetime = dbi_result_get_string(result, "formatted_date"); + + live_entry = json_object(); + if (!live_entry) { + syslog(LOG_ERR, "Cannot allocate JSON object (%s:%d)\n", + __func__, __LINE__); + dbi_result_free(result); + og_dbi_close(dbi); + json_decref(root); + return -1; + } + + json_object_set_new(live_entry, "name", json_string(live_name)); + json_object_set_new(live_entry, "date", json_string(live_datetime)); + + json_array_append_new(oglive_array, live_entry); + } + + dbi_result_free(result); + og_dbi_close(dbi); + if (json_dump_callback(root, og_json_dump_clients, &og_buffer, JSON_ENSURE_ASCII)) { json_decref(root); return -1; @@ -5600,6 +5656,160 @@ static int og_cmd_oglive_set(json_t *element, struct og_msg_params *params) return 0; } +static int og_cmd_oglive_add(json_t *element, struct og_msg_params *params) +{ + const char *oglive_str; + bool is_update = false; + int is_default_value; + const char *msglog; + struct og_dbi *dbi; + uint64_t flags = 0; + dbi_result result; + const char *key; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "name")) { + err = og_json_parse_string(value, &oglive_str); + flags |= OG_REST_PARAM_NAME; + } else { + err = -1; + } + + if (err < 0) + return err; + } + + if (!og_flags_validate(flags, OG_REST_PARAM_NAME)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + result = dbi_conn_queryf(dbi->conn, + "SELECT name FROM oglive WHERE name = '%s'", + oglive_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; + } + + if (dbi_result_next_row(result)) { + is_update = true; + syslog(LOG_INFO, "Found oglive '%s' in database, updating entry", oglive_str); + } + + dbi_result_free(result); + + is_default_value = 0; + + if (is_update) + result = dbi_conn_queryf(dbi->conn, + "UPDATE oglive SET creation_date = NOW(), is_default = %d " + "WHERE name = '%s'", + is_default_value, + oglive_str); + else + result = dbi_conn_queryf(dbi->conn, + "INSERT INTO oglive (name, creation_date, is_default) " + "VALUES ('%s', NOW(), %d)", + oglive_str, + is_default_value); + 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_oglive_delete(json_t *element, struct og_msg_params *params) +{ + const char *oglive_str; + const char *msglog; + struct og_dbi *dbi; + uint64_t flags = 0; + dbi_result result; + const char *key; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "name")) { + err = og_json_parse_string(value, &oglive_str); + flags |= OG_REST_PARAM_NAME; + } else { + err = -1; + } + + if (err < 0) + return err; + } + + if (!og_flags_validate(flags, OG_REST_PARAM_NAME)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + result = dbi_conn_queryf(dbi->conn, + "SELECT oglivedir FROM ordenadores WHERE oglivedir = '%s'", + oglive_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; + } + + if (dbi_result_next_row(result)) { + syslog(LOG_ERR, "failed to delete oglive '%s', live in use by a client", oglive_str); + dbi_result_free(result); + og_dbi_close(dbi); + return -1; + } + + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "DELETE FROM oglive WHERE name = '%s'", + oglive_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; + } + + if (dbi_result_get_numrows_affected(result) == 0) + syslog(LOG_WARNING, "No live entries found with name %s in database ", oglive_str); + + dbi_result_free(result); + og_dbi_close(dbi); + + return 0; +} + static int og_cmd_get_server_stats(char *buffer_reply) { json_t *root, *time_obj, *memory, *swap; @@ -5786,6 +5996,8 @@ struct { [OG_URI_EFI] = { "efi", }, [OG_URI_PART_SETUP] = { "setup", }, [OG_URI_OGLIVE_LIST] = { "oglive/list", }, + [OG_URI_OGLIVE_ADD] = { "oglive/add", }, + [OG_URI_OGLIVE_DELETE] = { "oglive/delete", }, [OG_URI_OGLIVE_SET] = { "oglive/set", }, [OG_URI_CENTER_ADD] = { "center/add", }, [OG_URI_CENTER_UPDATE] = { "center/update", }, @@ -6433,6 +6645,32 @@ int og_client_state_process_payload_rest(struct og_client *cli) } err = og_cmd_oglive_list(buf_reply); + } else if (!strncmp(cmd, "oglive/add", strlen("oglive/add"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command oglive add with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + err = og_cmd_oglive_add(root, ¶ms); + } else if (!strncmp(cmd, "oglive/delete", strlen("oglive/delete"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command oglive delete with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + err = og_cmd_oglive_delete(root, ¶ms); } else if (!strncmp(cmd, "oglive/set", strlen("oglive/set"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -142,6 +142,8 @@ enum og_rest_uri { OG_URI_CACHE_FETCH, OG_URI_PART_SETUP, OG_URI_OGLIVE_LIST, + OG_URI_OGLIVE_ADD, + OG_URI_OGLIVE_DELETE, OG_URI_OGLIVE_SET, OG_URI_CENTER_ADD, OG_URI_CENTER_UPDATE, diff --git a/src/schema.c b/src/schema.c index f7fceae..1c2428c 100644 --- a/src/schema.c +++ b/src/schema.c @@ -464,6 +464,39 @@ static int og_dbi_schema_v11(struct og_dbi *dbi) return 0; } +static int og_dbi_schema_v12(struct og_dbi *dbi) +{ + const char *msglog; + dbi_result result; + + syslog(LOG_DEBUG, "Creating table oglive\n"); + result = dbi_conn_query(dbi->conn, "CREATE TABLE `oglive` (" + "`id` BIGINT NOT NULL AUTO_INCREMENT," + "`name` VARCHAR(100)," + "`creation_date` DATETIME NOT NULL," + "`is_default` BOOLEAN NOT NULL," + "PRIMARY KEY (`id`)" + ")"); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_INFO, "Error when creating oglive (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + dbi_result_free(result); + + result = dbi_conn_query(dbi->conn, "UPDATE version SET version = 12"); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_INFO, "Could not update version row (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + dbi_result_free(result); + + return 0; +} + static struct og_schema_version { int version; int (*update)(struct og_dbi *dbi); @@ -479,6 +512,7 @@ static struct og_schema_version { { .version = 9, .update = og_dbi_schema_v9, }, { .version = 10, .update = og_dbi_schema_v10,}, { .version = 11, .update = og_dbi_schema_v11,}, + { .version = 12, .update = og_dbi_schema_v12,}, { 0, NULL }, }; |