From e81685bba0d9f1d6a28c0375f118df57667bedf0 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Mon, 30 Sep 2024 14:39:36 +0200 Subject: rest: do not allow two repositories to use the same IP address repository need to have unique IP address, otherwise bail out. --- src/rest.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/rest.c b/src/rest.c index f233f6b..a9eae33 100644 --- a/src/rest.c +++ b/src/rest.c @@ -4783,6 +4783,38 @@ static int og_cmd_post_center_delete(json_t *element, return 0; } +static int og_repo_list_find_ips(const char *ips_array[], uint32_t ips_array_len, + uint32_t exclude_repo_id) +{ + struct og_repo *repo; + LIST_HEAD(repo_list); + struct in_addr addr; + uint32_t i; + + if (og_repo_list(&repo_list) < 0) + return -1; + + for (i = 0; i < ips_array_len; i++) { + if (!inet_aton(ips_array[i], &addr)) + return -1; + + list_for_each_entry(repo, &repo_list, list) { + if (exclude_repo_id && exclude_repo_id == repo->id) + continue; + + if (addr.s_addr == repo->addr[i].s_addr) { + syslog(LOG_ERR, "Cannot add new repository, IP %s is already in used by repository '%s'\n", + ips_array[i], repo->name); + return -1; + } + } + } + + og_repo_free_list(&repo_list); + + return 0; +} + static int og_cmd_post_repository_update(json_t *element, struct og_msg_params *params, char *buffer_reply) @@ -4815,6 +4847,11 @@ static int og_cmd_post_repository_update(json_t *element, OG_REST_PARAM_ADDR)) return -1; + if (og_repo_list_find_ips(params->ips_array, params->ips_array_len, repo_id)) { + syslog(LOG_ERR, "Repository IP already in use, cannot define overlapping\n"); + return -1; + } + dbi = og_dbi_open(&ogconfig.db); if (!dbi) { syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", @@ -4899,6 +4936,11 @@ static int og_cmd_post_repository_add(json_t *element, OG_REST_PARAM_NAME)) return -1; + if (og_repo_list_find_ips(params->ips_array, params->ips_array_len, 0)) { + syslog(LOG_ERR, "Repository IP already in use, cannot define overlapping\n"); + return -1; + } + dbi = og_dbi_open(&ogconfig.db); if (!dbi) { syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", -- cgit v1.2.3-18-g5258