From 5144e73fad92d23ae706894a4357436c3fe89355 Mon Sep 17 00:00:00 2001
From: Amin Ben Romdhane <amin.benromdhane@iopsys.eu>
Date: Wed, 2 Oct 2024 15:30:39 +0200
Subject: [PATCH] Align with new code-analysis image

---
 .gitlab-ci.yml                    |   8 +-
 gitlab-ci/compile.sh              |   7 --
 gitlab-ci/install-dependencies.sh |   7 ++
 gitlab-ci/shared.sh               |   5 +-
 src/dynamicdnsmngr.c              | 156 +++++++++---------------------
 5 files changed, 56 insertions(+), 127 deletions(-)
 delete mode 100755 gitlab-ci/compile.sh
 create mode 100755 gitlab-ci/install-dependencies.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index be83197..526f491 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,14 +1,10 @@
 variables:
-  DEBUG: 'TRUE'
-  SOURCE_FOLDER: "./src"
-  FLAWFINDER_OPTIONS: "-m 4 --error-level=5"
-  CPPCHECK_OPTIONS: " --enable=all --inline-suppr"
-  COMPILE: "./gitlab-ci/compile.sh"
+  SOURCE_FOLDER: "src"
 
 include:
   - project: 'iopsys/gitlab-ci-pipeline'
     file: '/static-code-analysis.yml'
-    ref: '0.31'
+    ref: '1.2'
 
 stages:
     - static_code_analysis
diff --git a/gitlab-ci/compile.sh b/gitlab-ci/compile.sh
deleted file mode 100755
index 7e12f5f..0000000
--- a/gitlab-ci/compile.sh
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/bash
-
-source ./gitlab-ci/shared.sh
-
-install_bbfd
-
-exec_cmd_verbose make -C ./src
diff --git a/gitlab-ci/install-dependencies.sh b/gitlab-ci/install-dependencies.sh
new file mode 100755
index 0000000..285cc18
--- /dev/null
+++ b/gitlab-ci/install-dependencies.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+echo "install dependencies for tests"
+source ./gitlab-ci/shared.sh
+
+# install bbfdm
+install_bbfdm
diff --git a/gitlab-ci/shared.sh b/gitlab-ci/shared.sh
index f4b3349..90fb803 100644
--- a/gitlab-ci/shared.sh
+++ b/gitlab-ci/shared.sh
@@ -20,9 +20,9 @@ function exec_cmd_verbose()
 	fi
 }
 
-function install_bbfd()
+function install_bbfdm()
 {
-	[ -d "/opt/dev/bbfdm" ] && rm -rf /opt/dev/bbfdm
+	[ -d "/opt/dev/bbfdm" ] && return 0
 
 	if [ -n "${BBFDM_BRANCH}" ]; then
 		exec_cmd git clone -b ${BBFDM_BRANCH} https://dev.iopsys.eu/bbf/bbfdm.git /opt/dev/bbfdm
@@ -33,5 +33,4 @@ function install_bbfd()
 	cd /opt/dev/bbfdm
 	./gitlab-ci/install-dependencies.sh install
 	./gitlab-ci/setup.sh install
-	cd -
 }
diff --git a/src/dynamicdnsmngr.c b/src/dynamicdnsmngr.c
index a677b92..2e68ddf 100644
--- a/src/dynamicdnsmngr.c
+++ b/src/dynamicdnsmngr.c
@@ -22,11 +22,12 @@ DM_MAP_OBJ tDynamicObj[] = {
 
 static bool service_exists(const char *service)
 {
+	struct uci_section *s = NULL;
 	bool exist = false;
-	char *service_name = NULL;
-	struct uci_section *s = NULL, *stmp = NULL;
 
-	uci_foreach_sections_safe("ddnsmngr", "server", stmp, s) {
+	uci_foreach_sections("ddnsmngr", "server", s) {
+		char *service_name = NULL;
+
 		dmuci_get_value_by_section_string(s, "service", &service_name);
 		if (DM_STRCMP(service_name, service) == 0) {
 			exist = true;
@@ -126,51 +127,25 @@ static int addObjDynamicDNSClient(char *refparam, struct dmctx *ctx, void *data,
 static int delObjDynamicDNSClient(char *refparam, struct dmctx *ctx, void *data, char *instance, unsigned char del_action)
 {
 	struct uci_section *s = NULL, *stmp = NULL;
-	char *client = NULL, *value = NULL;
-
-	switch (del_action) {
-		case DEL_INST:
-			// Remove all hosts configured for this client
-			client = section_name(((struct dm_data *)data)->config_section);
-			uci_foreach_sections_safe("ddnsmngr", "host", stmp, s) {
-				value = NULL;
-				dmuci_get_value_by_section_string(s, "dm_parent", &value);
-				if (DM_STRCMP(value, client) == 0) {
-					struct uci_section *dmmap_section = NULL;
-					get_dmmap_section_of_config_section("dmmap_ddnsmngr", "host", section_name(s), &dmmap_section);
-
-					dmuci_delete_by_section(s, NULL, NULL);
-					dmuci_delete_by_section(dmmap_section, NULL, NULL);
-				}
-			}
 
-			// Remove the client
-			dmuci_delete_by_section(((struct dm_data *)data)->config_section, NULL, NULL);
-			dmuci_delete_by_section(((struct dm_data *)data)->dmmap_section, NULL, NULL);
+	// Remove all hosts configured for this client
+	char *client = section_name(((struct dm_data *)data)->config_section);
+	uci_foreach_sections_safe("ddnsmngr", "host", stmp, s) {
+		char *value = NULL;
 
-			break;
-		case DEL_ALL:
-			// Remove all clients
-			uci_foreach_sections_safe("ddnsmngr", "client", stmp, s) {
-				struct uci_section *dmmap_section = NULL;
-
-				get_dmmap_section_of_config_section("dmmap_ddnsmngr", "client", section_name(s), &dmmap_section);
-				dmuci_delete_by_section(dmmap_section, NULL, NULL);
-				dmuci_delete_by_section(s, NULL, NULL);
-			}
+		dmuci_get_value_by_section_string(s, "dm_parent", &value);
+		if (DM_STRCMP(value, client) == 0) {
+			struct uci_section *dmmap_section = NULL;
+			get_dmmap_section_of_config_section("dmmap_ddnsmngr", "host", section_name(s), &dmmap_section);
 
-			// Remove all hosts
-			stmp = NULL;
-			s = NULL;
-			uci_foreach_sections_safe("ddnsmngr", "host", stmp, s) {
-				struct uci_section *dmmap_section = NULL;
-
-				get_dmmap_section_of_config_section("dmmap_ddnsmngr", "host", section_name(s), &dmmap_section);
-				dmuci_delete_by_section(dmmap_section, NULL, NULL);
-				dmuci_delete_by_section(s, NULL, NULL);
-			}
-			break;
+			dmuci_delete_by_section(s, NULL, NULL);
+			dmuci_delete_by_section(dmmap_section, NULL, NULL);
+		}
 	}
+
+	// Remove the client
+	dmuci_delete_by_section(((struct dm_data *)data)->config_section, NULL, NULL);
+	dmuci_delete_by_section(((struct dm_data *)data)->dmmap_section, NULL, NULL);
 	return 0;
 }
 
@@ -193,44 +168,25 @@ static int addObjDynamicDNSServer(char *refparam, struct dmctx *ctx, void *data,
 
 static int delObjDynamicDNSServer(char *refparam, struct dmctx *ctx, void *data, char *instance, unsigned char del_action)
 {
+	struct uci_section *s = NULL;
 	char *service_name = NULL;
-	char *client_service = NULL;
-	struct uci_section *stmp = NULL, *s = NULL;
 
-	switch (del_action) {
-		case DEL_INST:
-			dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "service", &service_name);
-			dmuci_delete_by_section(((struct dm_data *)data)->config_section, NULL, NULL);
-			dmuci_delete_by_section(((struct dm_data *)data)->dmmap_section, NULL, NULL);
-
-			if (DM_STRLEN(service_name) == 0)
-				break;
-
-			// Now need to remove the service if configured to any client
-			uci_foreach_sections_safe("ddnsmngr", "client", stmp, s) {
-				dmuci_get_value_by_section_string(s, "service_name", &client_service);
-				if (DM_STRCMP(service_name, client_service) == 0)
-					dmuci_set_value_by_section(s, "service_name", "");
-			}
-			break;
-		case DEL_ALL:
-			uci_foreach_sections_safe("ddnsmngr", "server", stmp, s) {
-				struct uci_section *dmmap_section = NULL;
+	dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "service", &service_name);
+	dmuci_delete_by_section(((struct dm_data *)data)->config_section, NULL, NULL);
+	dmuci_delete_by_section(((struct dm_data *)data)->dmmap_section, NULL, NULL);
 
-				get_dmmap_section_of_config_section("dmmap_ddnsmngr", "server", section_name(s), &dmmap_section);
-				dmuci_delete_by_section(s, NULL, NULL);
-				dmuci_delete_by_section(dmmap_section, NULL, NULL);
-			}
+	if (DM_STRLEN(service_name) == 0)
+		return 0;
 
-			// Now delete service_name from all clients since all servers are deleted
-			stmp = NULL;
-			s = NULL;
+	// Now need to remove the service if configured to any client
+	uci_foreach_sections("ddnsmngr", "client", s) {
+		char *client_service = NULL;
 
-			// cppcheck-suppress unknownMacro
-			uci_foreach_sections_safe("ddnsmngr", "client", stmp, s)
-				dmuci_set_value_by_section(s, "service_name", "");
-			break;
+		dmuci_get_value_by_section_string(s, "service_name", &client_service);
+		if (DM_STRCMP(service_name, client_service) == 0)
+			dmuci_set_value_by_section(s, "service_name", "");
 	}
+
 	return 0;
 }
 
@@ -259,30 +215,8 @@ static int addObjDynamicDNSClientHostname(char *refparam, struct dmctx *ctx, voi
 
 static int delObjDynamicDNSClientHostname(char *refparam, struct dmctx *ctx, void *data, char *instance, unsigned char del_action)
 {
-	struct uci_section *s = NULL, *stmp = NULL;
-	char *client_s = NULL, *host_client = NULL;
-
-	switch (del_action) {
-		case DEL_INST:
-			dmuci_delete_by_section(((struct dm_data *)data)->config_section, NULL, NULL);
-			dmuci_delete_by_section(((struct dm_data *)data)->dmmap_section, NULL, NULL);
-			break;
-		case DEL_ALL:
-			client_s = section_name(((struct dm_data *)data)->config_section);
-
-			uci_foreach_sections_safe("ddnsmngr", "host", stmp, s) {
-				dmuci_get_value_by_section_string(s, "dm_parent", &host_client);
-				if (DM_STRCMP(client_s, host_client) != 0)
-					continue;
-
-				struct uci_section *dmmap_section = NULL;
-				get_dmmap_section_of_config_section("dmmap_ddnsmngr", "host", section_name(s), &dmmap_section);
-
-				dmuci_delete_by_section(dmmap_section, NULL, NULL);
-				dmuci_delete_by_section(s, NULL, NULL);
-			}
-			break;
-	}
+	dmuci_delete_by_section(((struct dm_data *)data)->config_section, NULL, NULL);
+	dmuci_delete_by_section(((struct dm_data *)data)->dmmap_section, NULL, NULL);
 	return 0;
 }
 
@@ -338,12 +272,12 @@ static int get_DynamicDNSClient_Status(char *refparam, struct dmctx *ctx, void *
 
 	char *enable = dmuci_get_value_by_section_fallback_def(((struct dm_data *)data)->config_section, "enabled", "0");
 	if (DM_STRCMP(enable, "0") == 0) {
-		*value = "Disabled";
+		*value = dmstrdup("Disabled");
 		return 0;
 	}
 
 	char *client_sec = section_name(((struct dm_data *)data)->config_section);
-	*value = "Error";
+	*value = dmstrdup("Error");
 
 	dmubus_call("ddnsmngr", "client_status", UBUS_ARGS{{"client", client_sec, String}}, 1, &res);
 	if (res != NULL) {
@@ -357,7 +291,7 @@ static int get_DynamicDNSClient_LastError(char *refparam, struct dmctx *ctx, voi
 {
 	json_object *res = NULL;
 
-	*value = "NO_ERROR";
+	*value = dmstrdup("NO_ERROR");
 
 	char *enable = dmuci_get_value_by_section_fallback_def(((struct dm_data *)data)->config_section, "enabled", "0");
 	if (DM_STRCMP(enable, "0") == 0) {
@@ -530,12 +464,12 @@ static int get_DynamicDNSClientHostname_Status(char *refparam, struct dmctx *ctx
 
 	char *enable = dmuci_get_value_by_section_fallback_def(((struct dm_data *)data)->config_section, "enabled", "0");
 	if (DM_STRCMP(enable, "0") == 0) {
-		*value = "Disabled";
+		*value = dmstrdup("Disabled");
 		return 0;
 	}
 
 	char *host_sec = section_name(((struct dm_data *)data)->config_section);
-	*value = "Error";
+	*value = dmstrdup("Error");
 
 	dmubus_call("ddnsmngr", "host_status", UBUS_ARGS{{"host", host_sec, String}}, 1, &res);
 	if (res != NULL) {
@@ -570,7 +504,7 @@ static int get_DynamicDNSClientHostname_LastUpdate(char *refparam, struct dmctx
 	json_object *res = NULL;
 
 	char *host_sec = section_name(((struct dm_data *)data)->config_section);
-	*value = "0001-01-01T00:00:00Z";
+	*value = dmstrdup("0001-01-01T00:00:00Z");
 
 	dmubus_call("ddnsmngr", "last_update", UBUS_ARGS{{"host", host_sec, String}}, 1, &res);
 	if (res != NULL) {
@@ -644,7 +578,7 @@ static int set_DynamicDNSServer_ServiceName(char *refparam, struct dmctx *ctx, v
 	char file_path[128] = {0};
 	char *curr_service = NULL;
 	char *client_service = NULL;
-	struct uci_section *s = NULL, *stmp = NULL;
+	struct uci_section *s = NULL;
 
 	switch (action)	{
 		case VALUECHECK:
@@ -680,7 +614,7 @@ static int set_DynamicDNSServer_ServiceName(char *refparam, struct dmctx *ctx, v
 				break;
 
 			// Now need to modify all clients configured with older service name
-			uci_foreach_sections_safe("ddnsmngr", "client", stmp, s) {
+			uci_foreach_sections("ddnsmngr", "client", s) {
 				dmuci_get_value_by_section_string(s, "service_name", &client_service);
 				if (DM_STRCMP(curr_service, client_service) == 0) {
 					dmuci_set_value_by_section(s, "service_name", value);
@@ -693,7 +627,7 @@ static int set_DynamicDNSServer_ServiceName(char *refparam, struct dmctx *ctx, v
 
 static int get_DynamicDNSServer_SupportedProtocols(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
 {
-	*value = "HTTP,HTTPS";
+	*value = dmstrdup("HTTP,HTTPS");
 	return 0;
 }
 
@@ -702,7 +636,7 @@ static int get_DynamicDNSServer_Protocol(char *refparam, struct dmctx *ctx, void
 	char *service = NULL;
 	char file_path[128] = {0};
 
-	*value = "HTTP";
+	*value = dmstrdup("HTTP");
 
 	dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "service", &service);
 	snprintf(file_path, sizeof(file_path), "%s/%s.json", DDNS_SERVICES_DEFAULT, service);
@@ -713,7 +647,7 @@ static int get_DynamicDNSServer_Protocol(char *refparam, struct dmctx *ctx, void
 
 	char *ipv4_url = dmjson_get_value(json, 2, "ipv4", "url");
 	if (DM_STRNCMP(ipv4_url, "https", 5) == 0)
-		*value = "HTTPS";
+		*value = dmstrdup("HTTPS");
 
 	json_object_put(json);
 
-- 
GitLab