From f716c20c52f903a43ba4a1c04928ea02b3e35be7 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 12 Feb 2026 16:44:23 +0200 Subject: [PATCH 1/4] Change: move clean_hosts to manage_utils.c --- src/manage_sql.c | 118 -------------------------------------------- src/manage_sql.h | 3 -- src/manage_utils.c | 119 +++++++++++++++++++++++++++++++++++++++++++++ src/manage_utils.h | 3 ++ 4 files changed, 122 insertions(+), 121 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 7ab8af2b6..2e1462178 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -441,28 +441,6 @@ parse_iso_time (const char *text_time) return parse_iso_time_tz (text_time, current_credentials.timezone); } -/** - * @brief Find a string in an array. - * - * @param[in] array Array. - * @param[in] string String. - * - * @return The string from the array if found, else NULL. - */ -static gchar* -array_find_string (array_t *array, const gchar *string) -{ - guint index; - for (index = 0; index < array->len; index++) - { - gchar *ele; - ele = (gchar*) g_ptr_array_index (array, index); - if (ele && (strcmp (ele, string) == 0)) - return ele; - } - return NULL; -} - /** * @brief Find a string in a glib style string vector. * @@ -20394,102 +20372,6 @@ manage_count_hosts (const char *given_hosts, const char *exclude_hosts) manage_max_hosts ()); } -/** - * @brief Trim leading and trailing space from a hosts string. - * - * @param[in] string String. May be modified. - * - * @return Either string or some address within string. - */ -static gchar * -trim_hosts (gchar *string) -{ - gchar *host, *end; - - /* Trim leading and trailing space. */ - host = string; - while ((*host == ' ') || (*host == '\t')) - host++; - end = host; - while (*end) - { - if ((*end == ' ') || (*end == '\t')) - { - *end = '\0'; - break; - } - end++; - } - return host; -} - -/** - * @brief Clean a hosts string. - * - * @param[in] given_hosts String describing hosts. - * @param[out] max Max number of hosts, adjusted for duplicates. - * - * @return Freshly allocated new hosts string, or NULL on error. - */ -gchar* -clean_hosts (const char *given_hosts, int *max) -{ - array_t *clean_array; - GString *clean; - gchar **split, **point, *hosts, *hosts_start, *host; - guint index; - - /* Treat newlines like commas. */ - hosts = hosts_start = g_strdup (given_hosts); - while (*hosts) - { - if (*hosts == '\n') *hosts = ','; - hosts++; - } - - split = g_strsplit (hosts_start, ",", 0); - g_free (hosts_start); - point = split; - - if ((point == NULL) || (*point == NULL)) - { - g_strfreev (split); - return g_strdup (""); - } - - clean_array = make_array (); - while (*point) - { - host = trim_hosts (*point); - - if (*host) - { - /* Prevent simple duplicates. */ - if (array_find_string (clean_array, host) == NULL) - array_add (clean_array, host); - else if (max) - (*max)--; - } - - point += 1; - } - - clean = g_string_new (""); - - host = (gchar*) g_ptr_array_index (clean_array, 0); - if (host) - g_string_append_printf (clean, "%s", host); - - for (index = 1; index < clean_array->len; index++) - { - host = (gchar*) g_ptr_array_index (clean_array, index); - if (host) - g_string_append_printf (clean, ", %s", host); - } - - return g_string_free (clean, FALSE); -} - /** * @brief Validate a single port. * diff --git a/src/manage_sql.h b/src/manage_sql.h index d2d0efeef..fee7d6dc3 100644 --- a/src/manage_sql.h +++ b/src/manage_sql.h @@ -578,7 +578,4 @@ ldap_auth_enabled (); int radius_auth_enabled (); -gchar* -clean_hosts (const char *, int *); - #endif /* not _GVMD_MANAGE_SQL_H */ diff --git a/src/manage_utils.c b/src/manage_utils.c index 74943af4d..24f6ff66e 100644 --- a/src/manage_utils.c +++ b/src/manage_utils.c @@ -14,6 +14,7 @@ #include /* for assert */ #include +#include #include #include #include /* for sscanf */ @@ -1206,6 +1207,124 @@ clean_hosts_string (const char *hosts) return g_string_free (new_hosts, FALSE); } +/** + * @brief Trim leading and trailing space from a hosts string. + * + * @param[in] string String. May be modified. + * + * @return Either string or some address within string. + */ +static gchar * +trim_hosts (gchar *string) +{ + gchar *host, *end; + + /* Trim leading and trailing space. */ + host = string; + while ((*host == ' ') || (*host == '\t')) + host++; + end = host; + while (*end) + { + if ((*end == ' ') || (*end == '\t')) + { + *end = '\0'; + break; + } + end++; + } + return host; +} + +/** + * @brief Find a string in an array. + * + * @param[in] array Array. + * @param[in] string String. + * + * @return The string from the array if found, else NULL. + */ +static gchar* +array_find_string (array_t *array, const gchar *string) +{ + guint index; + for (index = 0; index < array->len; index++) + { + gchar *ele; + ele = (gchar*) g_ptr_array_index (array, index); + if (ele && (strcmp (ele, string) == 0)) + return ele; + } + return NULL; +} + +/** + * @brief Clean a hosts string. + * + * @param[in] given_hosts String describing hosts. + * @param[out] max Max number of hosts, adjusted for duplicates. + * + * @return Freshly allocated new hosts string, or NULL on error. + */ +gchar* +clean_hosts (const char *given_hosts, int *max) +{ + array_t *clean_array; + GString *clean; + gchar **split, **point, *hosts, *hosts_start, *host; + guint index; + + /* Treat newlines like commas. */ + hosts = hosts_start = g_strdup (given_hosts); + while (*hosts) + { + if (*hosts == '\n') *hosts = ','; + hosts++; + } + + split = g_strsplit (hosts_start, ",", 0); + g_free (hosts_start); + point = split; + + if ((point == NULL) || (*point == NULL)) + { + g_strfreev (split); + return g_strdup (""); + } + + clean_array = make_array (); + while (*point) + { + host = trim_hosts (*point); + + if (*host) + { + /* Prevent simple duplicates. */ + if (array_find_string (clean_array, host) == NULL) + array_add (clean_array, host); + else if (max) + (*max)--; + } + + point += 1; + } + + clean = g_string_new (""); + + host = (gchar*) g_ptr_array_index (clean_array, 0); + if (host) + g_string_append_printf (clean, "%s", host); + + for (index = 1; index < clean_array->len; index++) + { + host = (gchar*) g_ptr_array_index (clean_array, index); + if (host) + g_string_append_printf (clean, ", %s", host); + } + + return g_string_free (clean, FALSE); +} + /** * @brief Join an array of gchar* with a separator, skipping NULL/empty entries. * diff --git a/src/manage_utils.h b/src/manage_utils.h index 416f00e89..6b2feafb8 100644 --- a/src/manage_utils.h +++ b/src/manage_utils.h @@ -79,6 +79,9 @@ icalendar_first_time_from_vcalendar (icalcomponent *, icaltimezone *); gchar * clean_hosts_string (const char *); +gchar* +clean_hosts (const char *, int *); + gchar * concat_error_messages (const GPtrArray *errors, const gchar *sep, const gchar *prefix); From a24b34808b3ff9d5d8975c2a73d866e082f8a37e Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 12 Feb 2026 16:51:25 +0200 Subject: [PATCH 2/4] Change: move find_target_with_permission to dedicated files --- src/manage.h | 3 --- src/manage_sql.c | 16 ---------------- src/manage_sql_targets.c | 17 +++++++++++++++++ src/manage_targets.h | 3 +++ 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/manage.h b/src/manage.h index 9947d71cb..584212c8a 100644 --- a/src/manage.h +++ b/src/manage.h @@ -1618,9 +1618,6 @@ prognosis_iterator_description (iterator_t*); int manage_count_hosts (const char *, const char *); -gboolean -find_target_with_permission (const char *, target_t *, const char *); - int create_target (const char*, const char*, const char*, const char*, const char*, const char *, const char*, credential_t, credential_t, diff --git a/src/manage_sql.c b/src/manage_sql.c index 2e1462178..f4b7e9ea2 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -20340,22 +20340,6 @@ modify_task (const gchar *task_id, const gchar *name, /* Targets. */ -/** - * @brief Find a target for a specific permission, given a UUID. - * - * @param[in] uuid UUID of target. - * @param[out] target Target return, 0 if successfully failed to find target. - * @param[in] permission Permission. - * - * @return FALSE on success (including if failed to find target), TRUE on error. - */ -gboolean -find_target_with_permission (const char* uuid, target_t* target, - const char *permission) -{ - return find_resource_with_permission ("target", uuid, target, permission, 0); -} - /** * @brief Return number of hosts described by a hosts string. * diff --git a/src/manage_sql_targets.c b/src/manage_sql_targets.c index b1686ca22..b34f9a7a4 100644 --- a/src/manage_sql_targets.c +++ b/src/manage_sql_targets.c @@ -4,6 +4,7 @@ */ #include "manage_sql_targets.h" +#include "manage_sql_resources.h" #include "sql.h" /** @@ -13,6 +14,22 @@ * The Targets SQL for the GVM management layer. */ +/** + * @brief Find a target for a specific permission, given a UUID. + * + * @param[in] uuid UUID of target. + * @param[out] target Target return, 0 if successfully failed to find target. + * @param[in] permission Permission. + * + * @return FALSE on success (including if failed to find target), TRUE on error. + */ +gboolean +find_target_with_permission (const char* uuid, target_t* target, + const char *permission) +{ + return find_resource_with_permission ("target", uuid, target, permission, 0); +} + /** * @brief Return the UUID of a target. * diff --git a/src/manage_targets.h b/src/manage_targets.h index 412195ae0..19e895644 100644 --- a/src/manage_targets.h +++ b/src/manage_targets.h @@ -14,6 +14,9 @@ manage_max_hosts (); void manage_set_max_hosts (int); +gboolean +find_target_with_permission (const char *, target_t *, const char *); + char* target_uuid (target_t); From b6b7b39a5ddf535d7b6a65c00eafdac3a5d7bee6 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 12 Feb 2026 16:54:11 +0200 Subject: [PATCH 3/4] Change: move copy_target to dedicated files --- src/manage.h | 3 --- src/manage_sql.c | 37 ------------------------------------- src/manage_sql_targets.c | 39 +++++++++++++++++++++++++++++++++++++++ src/manage_targets.h | 3 +++ 4 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/manage.h b/src/manage.h index 584212c8a..93d08fd8c 100644 --- a/src/manage.h +++ b/src/manage.h @@ -1627,9 +1627,6 @@ create_target (const char*, const char*, const char*, const char*, const char*, GPtrArray *, const char *, const char *, target_t*); -int -copy_target (const char*, const char*, const char *, target_t*); - int modify_target (const char*, const char*, const char*, const char*, const char*, const char*, const char*, const char*, const char*, const char*, diff --git a/src/manage_sql.c b/src/manage_sql.c index f4b7e9ea2..37b094eca 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -20948,43 +20948,6 @@ create_target (const char* name, const char* asset_hosts_filter, return 0; } -/** - * @brief Create a target from an existing target. - * - * @param[in] name Name of new target. NULL to copy from existing. - * @param[in] comment Comment on new target. NULL to copy from existing. - * @param[in] target_id UUID of existing target. - * @param[out] new_target New target. - * - * @return 0 success, 1 target exists already, 2 failed to find existing - * target, 99 permission denied, -1 error. - */ -int -copy_target (const char* name, const char* comment, const char *target_id, - target_t* new_target) -{ - int ret; - target_t old_target; - - assert (new_target); - - ret = copy_resource ("target", name, comment, target_id, - "hosts, exclude_hosts, port_list, reverse_lookup_only," - " reverse_lookup_unify, alive_test," - " allow_simultaneous_ips", - 1, new_target, &old_target); - if (ret) - return ret; - - sql ("INSERT INTO targets_login_data (target, type, credential, port)" - " SELECT %llu, type, credential, port" - " FROM targets_login_data" - " WHERE target = %llu;", - *new_target, old_target); - - return 0; -} - /** * @brief Delete a target. * diff --git a/src/manage_sql_targets.c b/src/manage_sql_targets.c index b34f9a7a4..04603ba5a 100644 --- a/src/manage_sql_targets.c +++ b/src/manage_sql_targets.c @@ -7,6 +7,8 @@ #include "manage_sql_resources.h" #include "sql.h" +#include + /** * @file * @brief GVM management layer: Targets SQL @@ -341,3 +343,40 @@ target_krb5_credential (target_t target) { return target_credential (target, "krb5"); } + +/** + * @brief Create a target from an existing target. + * + * @param[in] name Name of new target. NULL to copy from existing. + * @param[in] comment Comment on new target. NULL to copy from existing. + * @param[in] target_id UUID of existing target. + * @param[out] new_target New target. + * + * @return 0 success, 1 target exists already, 2 failed to find existing + * target, 99 permission denied, -1 error. + */ +int +copy_target (const char* name, const char* comment, const char *target_id, + target_t* new_target) +{ + int ret; + target_t old_target; + + assert (new_target); + + ret = copy_resource ("target", name, comment, target_id, + "hosts, exclude_hosts, port_list, reverse_lookup_only," + " reverse_lookup_unify, alive_test," + " allow_simultaneous_ips", + 1, new_target, &old_target); + if (ret) + return ret; + + sql ("INSERT INTO targets_login_data (target, type, credential, port)" + " SELECT %llu, type, credential, port" + " FROM targets_login_data" + " WHERE target = %llu;", + *new_target, old_target); + + return 0; +} diff --git a/src/manage_targets.h b/src/manage_targets.h index 19e895644..d86dc725b 100644 --- a/src/manage_targets.h +++ b/src/manage_targets.h @@ -47,4 +47,7 @@ target_allow_simultaneous_ips (target_t); char* target_ssh_port (target_t); +int +copy_target (const char *, const char *, const char *, target_t *); + #endif /* not _GVMD_MANAGE_TARGETS_H */ From a6e9bef1cfc4b17ddcc4629a5de569fae0671811 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 12 Feb 2026 17:49:02 +0200 Subject: [PATCH 4/4] Format clean_hosts header --- src/manage_utils.c | 2 +- src/manage_utils.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/manage_utils.c b/src/manage_utils.c index 24f6ff66e..71b3bd001 100644 --- a/src/manage_utils.c +++ b/src/manage_utils.c @@ -1266,7 +1266,7 @@ array_find_string (array_t *array, const gchar *string) * * @return Freshly allocated new hosts string, or NULL on error. */ -gchar* +gchar * clean_hosts (const char *given_hosts, int *max) { array_t *clean_array; diff --git a/src/manage_utils.h b/src/manage_utils.h index 6b2feafb8..f1172dd85 100644 --- a/src/manage_utils.h +++ b/src/manage_utils.h @@ -79,7 +79,7 @@ icalendar_first_time_from_vcalendar (icalcomponent *, icaltimezone *); gchar * clean_hosts_string (const char *); -gchar* +gchar * clean_hosts (const char *, int *); gchar *