From c46b4bbc7a0810027abdb41064ffca9c01e675b7 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 29 Jun 2026 15:15:14 +0200 Subject: [PATCH 01/66] added initial migration document for `target_type` in tasks table --- docs/tasks-target-type.md | 861 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 861 insertions(+) create mode 100644 docs/tasks-target-type.md diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md new file mode 100644 index 0000000000..c4896e1df0 --- /dev/null +++ b/docs/tasks-target-type.md @@ -0,0 +1,861 @@ +# Migrate to a `target_type` column for `tasks` + +## What + +Modernize `gvmd`'s code- and database. Specifically the `tasks` components, by introducing the concept of a +`target_type`. + +## Why + +Currently, we add columns to the `tasks` table, for different types of targets. + +The columns `oci_image_target`, `agent_group` and `web_application_target` are artifacts of this phenomenon. +In addition to these `*target` columns, each comes with their related `*_location` column, indicating +whether this resource is found in the trash. + +```mermaid +erDiagram + tasks }o--|| targets: joins + tasks }o--|| oci_image_targets: joins + tasks }o--|| agent_groups: joins + tasks }o--|| web_application_targets: joins + + tasks { + int target + int target_location + int oci_image_target + int oci_image_target_location + int agent_group + int agent_group_location + int web_application_target + int web_application_target + } + targets { + int id + } + oci_image_targets { + int id + } + agent_groups { + int id + } + web_application_targets { + int id + } +``` + +This makes it cumbersome to add a new type of target. Also, it is quite confusing, as the column `target` is still in +place, but refers only to the legacy definition of a target. + +By introducing a `target_type` column, and migrating the then unnecessary columns `oci_image_target`, `agent_group` +and `web_application_target` accordingly, we make intent more clear and concise. + +After introducing the `target_type` column, we keep the different target tables, as shown below. +This will make it a lot easier to introduce further target types in the future, without adding a lot of +duplicate code for handling tasks. + +```mermaid +erDiagram + tasks }o--|| targets: joins + tasks }o--|| oci_image_targets: joins + tasks }o--|| agent_groups: joins + tasks }o--|| web_application_targets: joins + + tasks { + int target_type + int target + int target_location + } + targets { + int id + } + oci_image_targets { + int id + } + agent_groups { + int id + } + web_application_targets { + int id + } +``` + +# Target Types + +These are the proposed target types, part of the initial migration + +| Target Type | Integer Value | Reference Table | Notes | +|-------------------------------|---------------|---------------------------|----------------------| +| `TARGET_TYPE_IMPORT_TASK` | 0 | N/A | `target` must be `0` | +| `TARGET_TYPE_REGULAR` | 1 | `targets` | N/A | +| `TARGET_TYPE_AGENT_GROUP` | 2 | `agent_groups` | N/A | +| `TARGET_TYPE_OCI_IMAGE` | 3 | `oci_image_targets` | N/A | +| `TARGET_TYPE_WEB_APPLICATION` | 4 | `web_application_targets` | N/A | + +# Migrations + +## Data migration + +While migrating the data seems rather trivial, we should opt for creating a database backup +prior to migrating the columns and data. + +The migration consists of multiple steps: + +1. ```postgresql + ALTER TABLE tasks ADD target_type INTEGER + ``` +2. Pseudocode for migrating table contents + ```python + start_transaction() + for row in tasks: + if row.agent_group: + row.target = row.agent_group + row.target_location = row.agent_group_location + row.target_type = TARGET_TYPE_AGENT_GROUP + elif row.oci_image_target: + row.target = row.oci_image_target + row.target_location = row.oci_image_target_location + row.target_type = TARGET_TYPE_OCI_IMAGE + elif row.web_application_target: + row.target = row.web_application_target + row.target_location = row.web_application_target_location + row.target_type = TARGET_TYPE_WEB_APPLICATION + elif row.target != 0: + row.target_type = TARGET_TYPE_REGULAR + else: + row.target_type = TARGET_TYPE_IMPORT_TASK + commit() + ``` +3. ```postgresql + ALTER TABLE tasks DROP COLUMN oci_image_target, DROP COLUMN oci_image_target_location, DROP COLUMN web_application_target, DROP COLUMN web_application_target_location + ``` + +**Concerns** + +- If for some reason, multiple `*target` fields in any combination are set (e.g. `target` *and* `oci_image_target`), + we cannot imply which target is correct. While this should never happen, we should be ready + for this to happen anyway. + +## Code migration + +[manage_pg.c#L1293](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1293) + +```c++ +" SELECT CASE" +" WHEN EXISTS (SELECT id" +" FROM tasks" +" WHERE id = (SELECT task" +" FROM reports WHERE id = $1)" +" AND oci_image_target IS NOT NULL)" +``` + +Migration: Add additional `if (db_version >= ...)` +=> `WHERE target_type = {{TARGET_TYPE_OCI_IMAGE}}` + +--- + +[manage_pg.c#L1447](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1447) + +```c++ +" SELECT CASE" +" WHEN (SELECT target = 0 " +" AND agent_group = 0 " +" AND oci_image_target = 0 " +" AND web_application_target = 0 " +" FROM tasks WHERE id = $1)" +``` + +[manage_pg.c#L1473](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1473) + +```c++ +" SELECT CASE" +" WHEN (SELECT target = 0 " +" AND agent_group = 0 " +" AND oci_image_target = 0 " +" FROM tasks WHERE id = $1)" +``` + +Migration: Add additional `if (db_version >= ...)` +=> `target = 0 AND target_type = {{TARGET_TYPE_IMPORT_TASK}}`, omit other columns + +--- + +[manage_pg.c#L1562](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1562) + +```c++ + /* Skip running and import tasks. */ + " WHEN (SELECT run_status = %u OR target = 0" + " FROM tasks WHERE id = $1)" + +``` + +Migration => `OR target_type = {{TARGET_TYPE_IMPORT_TASK}}` + +--- + +[manage_sql.c#L5866](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5866) + +```c++ +void +set_task_agent_group_and_location (task_t task, agent_group_t agent_group) +{ + sql ("UPDATE tasks SET agent_group = %llu, agent_group_location = 0," + " modification_time = m_now ()" + " WHERE id = %llu;", + agent_group, + task); +} +``` + +Migration => + +--- + +[manage_sql.c#L5884](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5884) + +```c++ +int +agent_group_tasks_exist_by_scanner (scanner_t scanner) +{ + return !!sql_int ( + "SELECT COUNT(*) FROM tasks" + " WHERE agent_group IN (" + " SELECT id FROM agent_groups WHERE scanner = %llu)" + " AND agent_group_location = " + G_STRINGIFY (LOCATION_TABLE) + " AND hidden = 0;", + scanner); +} +``` + +Migration => + +--- + +[manage_sql.c#L5903](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5903) + +```c++ +int +agent_group_hidden_tasks_exist_by_scanner (scanner_t scanner) +{ + return !!sql_int ( + "SELECT COUNT(*) FROM tasks " + "WHERE hidden != 0 " + " AND (" + " (agent_group_location = %d" + " AND agent_group IN (" + " SELECT id FROM agent_groups WHERE scanner = %llu ))" + " OR " + " (agent_group_location = %d " + " AND agent_group IN (" + " SELECT id FROM agent_groups_trash WHERE scanner = %llu ))" + ");", + LOCATION_TABLE, scanner, LOCATION_TRASH, scanner); +} +``` + +Migration => + +--- + +[manage_sql.c#L5931](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5931) + +```c++ +switch (sql_int64 (&agent_group, + "SELECT agent_group FROM tasks WHERE id = %llu;", + task)) +``` + +Migration => + +--- + +[manage_sql.c#L5954](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5954) + +```c++ +int +task_agent_group_in_trash (task_t task) +{ + return sql_int ("SELECT agent_group_location = " + G_STRINGIFY (LOCATION_TRASH) + " FROM tasks WHERE id = %llu;", + task); +} +``` + +Migration => + +--- + +[manage_sql.c#L5976](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5976) + +```c++ +switch (sql_int64 (&oci_image_target, + "SELECT oci_image_target FROM tasks WHERE id = %llu;", + task)) +``` + +Migration => + +--- + +[manage_sql.c#L5999](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5999) + +```c++ +int +task_oci_image_target_in_trash (task_t task) +{ + return sql_int ("SELECT oci_image_target_location = " + G_STRINGIFY (LOCATION_TRASH) + " FROM tasks WHERE id = %llu;", + task); +} +``` + +Migration => + +--- + +[manage_sql.c#L6014](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6014) + +```c++ +void +set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) +{ + sql ("UPDATE tasks SET oci_image_target = %llu," + " oci_image_target_location = 0," + " modification_time = m_now ()" + " WHERE id = %llu;", + oci_image_target, + task); +} +``` + +Migration => + +--- + +[manage_sql.c#L6035](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6035) + +```c++ +web_application_target_t +task_web_application_target (task_t task) +{ + web_application_target_t web_application_target = 0; + sql_int64_ps (&web_application_target, + "SELECT web_application_target FROM tasks" + " WHERE id = $1;", + SQL_RESOURCE_PARAM(task), + NULL); + return web_application_target; +} +``` + +Migration => + +--- + +[manage_sql.c#L6054](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6054) + +```c++ +int +task_web_application_target_in_trash (task_t task) +{ + return sql_int_ps ("SELECT web_application_target_location = " + G_STRINGIFY (LOCATION_TRASH) + " FROM tasks WHERE id = $1;", + SQL_RESOURCE_PARAM(task), + NULL); +} +``` + +Migration => + +--- + +[manage_sql.c#L6070](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6070) + +```c++ +void +set_task_web_application_target (task_t task, web_application_target_t web_application_target) +{ + sql_ps ("UPDATE tasks SET web_application_target = $1," + " web_application_target_location = 0," + " modification_time = m_now ()" + " WHERE id = $2;", + SQL_RESOURCE_PARAM(web_application_target), + SQL_RESOURCE_PARAM(task), + NULL); +} +``` + +Migration => + +--- + +[manage_sql_agent_groups.c#L59](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L59) + +```c++L6015 +static int +agent_group_in_use_in_hidden_task (agent_group_t agent_group) +{ + return !!sql_int ("SELECT COUNT(*) FROM tasks " + "WHERE hidden != 0 AND agent_group = %llu;", + agent_group); +} +``` + +Migration => + +--- + +[manage_sql_agent_groups.c#L1029](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L1029) + +```c++ +int +agent_group_in_use (agent_group_t agent_group) +{ + return !!sql_int ("SELECT count(*) FROM tasks" + " WHERE agent_group = %llu" + " AND agent_group_location = " + G_STRINGIFY (LOCATION_TABLE) + " AND hidden = 0;", + agent_group); +} + +int +trash_agent_group_in_use (agent_group_t agent_group) +{ + return !!sql_int ("SELECT count(*) FROM tasks" + " WHERE agent_group = %llu" + " AND agent_group_location = " + G_STRINGIFY (LOCATION_TRASH), + agent_group); +} +``` + +Migration => + +--- + +[manage_sql_filters.c#L1286](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_filters.c#L1286) + +```c++ + if ((strcmp (type, "report") == 0) + && (strcmp (keyword->string, "status") == 0)) + g_string_append_printf + (order, + " ORDER BY" + " (CASE WHEN (SELECT " + " ( " + " target = 0 " + " and COALESCE( agent_group, 0) = 0 " + " and COALESCE( oci_image_target, 0) = 0" + " ) " + " FROM tasks" + " WHERE tasks.id = task)" + " THEN '0'" + " ELSE run_status_name (scan_run_status)" + " || (SELECT CAST (temp / 100 AS text)" + " || CAST (temp / 10 AS text)" + " || CAST (temp %% 10 as text)" + " FROM (SELECT report_progress (id) AS temp)" + " AS temp_sub)" + " END)" + " ASC"); + else if ((strcmp (type, "task") == 0) + && (strcmp (keyword->string, "status") == 0)) + g_string_append_printf + (order, + " ORDER BY" + " (CASE WHEN ( target = 0 " + " and COALESCE( agent_group, 0) = 0 " + " and COALESCE( oci_image_target, 0) = 0" + " ) " + " THEN '0'" + " ELSE run_status_name (run_status)" + " || (SELECT CAST (temp / 100 AS text)" + " || CAST (temp / 10 AS text)" + " || CAST (temp %% 10 as text)" + " FROM (SELECT report_progress (id) AS temp" + " FROM reports" + " WHERE task = tasks.id" + " ORDER BY creation_time DESC LIMIT 1)" + " AS temp_sub)" + " END)" + " ASC"); +``` + +Migration => + +--- + +[manage_sql_filters.c#L1491](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_filters.c#L1491) + +```c++ +if ((strcmp (type, "report") == 0) + && (strcmp (keyword->string, "status") == 0)) + g_string_append_printf + (order, + " ORDER BY" + " (CASE WHEN (SELECT " + " ( " + " target = 0 " + " and COALESCE( agent_group, 0) = 0 " + " and COALESCE( oci_image_target, 0) = 0" + " ) " + " FROM tasks" + " WHERE tasks.id = task)" + " THEN '0'" + " ELSE run_status_name (scan_run_status)" + " || (SELECT CAST (temp / 100 AS text)" + " || CAST (temp / 10 AS text)" + " || CAST (temp %% 10 as text)" + " FROM (SELECT report_progress (id) AS temp)" + " AS temp_sub)" + " END)" + " DESC"); + else if ((strcmp (type, "task") == 0) + && (strcmp (keyword->string, "status") == 0)) + g_string_append_printf + (order, + " ORDER BY" + " (CASE WHEN ( target = 0 " + " and COALESCE( agent_group, 0) = 0 " + " and COALESCE( oci_image_target, 0) = 0" + " ) " + " THEN '0'" + " ELSE run_status_name (run_status)" + " || (SELECT CAST (temp / 100 AS text)" + " || CAST (temp / 10 AS text)" + " || CAST (temp %% 10 as text)" + " FROM (SELECT report_progress (id) AS temp" + " FROM reports" + " WHERE task = tasks.id" + " ORDER BY creation_time DESC LIMIT 1)" + " AS temp_sub)" + " END)" + " DESC"); +``` + +Migration => + +--- + +[manage_sql_oci_image_targets.c#L427](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L427) + +```c++ +if (sql_int ("SELECT count(*) FROM tasks" + " WHERE oci_image_target = %llu" + " AND oci_image_target_location = " + G_STRINGIFY (LOCATION_TRASH) ";", + oci_image_target)) +{ + sql_rollback (); + return 1; +} +``` + +Migration => use `target_type` and regular fields instead + +--- + +[manage_sql_oci_image_targets.c#L451](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L451) + +```c++ +if (sql_int ("SELECT count(*) FROM tasks" + " WHERE oci_image_target = %llu" + " AND oci_image_target_location = " + G_STRINGIFY (LOCATION_TABLE) + " AND hidden = 0;", + oci_image_target)) +{ + sql_rollback (); + return 1; +} +``` + +Migration => use `target_type` and regular fields instead + +--- + +[manage_sql_oci_image_targets.c#L490](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L490) + +```c++ + else if (sql_int ("SELECT count(*) FROM tasks" + " WHERE oci_image_target = %llu" + " AND oci_image_target_location = " + G_STRINGIFY (LOCATION_TABLE), + oci_image_target)) + { + sql_rollback (); + return 1; + } +``` + +Migration => What about the trash can in general? Do we want to use `targets_trash` for every target_type? Probably yes. +So we need to get rid of all the `*_target_trash` tables and replace the queries for "copying" to the trash table +accordingly. +This also requires changes to => delete_target () `m̀anage_sql_targets.c#L416` + +--- + +[manage_sql_oci_image_targets.c#L906](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L906) + +```c++ +int +oci_image_target_in_use (oci_image_target_t oci_image_target) +{ + return !!sql_int ("SELECT count(*) FROM tasks" + " WHERE oci_image_target = %llu" + " AND oci_image_target_location = " + G_STRINGIFY (LOCATION_TABLE) + " AND hidden = 0;", + oci_image_target); +} + +int +trash_oci_image_target_in_use (oci_image_target_t oci_image_target) +{ + return !!sql_int ("SELECT count(*) FROM tasks" + " WHERE oci_image_target = %llu" + " AND oci_image_target_location = " + G_STRINGIFY (LOCATION_TRASH), + oci_image_target); +} +``` + +Migration => + +--- + +[manage_sql_oci_image_targets.c#L979](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L979) + +```c++ + init_iterator (iterator, + "%s" + " SELECT name, uuid, %s FROM tasks" + " WHERE oci_image_target = %llu" + " AND hidden = 0" + " ORDER BY name ASC;", + with_clause ? with_clause : "", + available, + oci_image_target); +``` + +Migration => + +--- + +[manage_sql_web_application_targets.c#L422](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L422) + +```c++ +int +delete_web_application_target ( + const char *web_application_target_id, int ultimate) +``` + +Migration => merge with delete_target (...) + +--- + +[manage_sql_web_application_targets.c#L989](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L989) + +```c++ +int +web_application_target_in_use (web_application_target_t web_application_target) +{ + return !!sql_int_ps ("SELECT count(*) FROM tasks" + " WHERE web_application_target = $1" + " AND web_application_target_location = " + G_STRINGIFY (LOCATION_TABLE) + " AND hidden = 0;", + SQL_RESOURCE_PARAM (web_application_target), + NULL); +} + +int +trash_web_application_target_in_use ( + web_application_target_t web_application_target) +{ + return !!sql_int_ps ("SELECT count(*) FROM tasks" + " WHERE web_application_target = $1" + " AND web_application_target_location = " + G_STRINGIFY (LOCATION_TRASH) ";", + SQL_RESOURCE_PARAM (web_application_target), + NULL); +} +``` + +Migration => + +--- + +[manage_sql_web_application_targets.c#L1055](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L1055) + +```c++ +init_iterator (iterator, + "%s" + " SELECT name, uuid, %s FROM tasks" + " WHERE web_application_target = %llu" + " AND hidden = 0" + " ORDER BY name ASC;", + with_clause ? with_clause : "", + available, + web_application_target); +``` + +Migration => `target_type` + +--- + +[manage_sql.c#L18107](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L18107) + +```c++ +task_t +make_task (char* name, char* comment, int in_assets, int event) +``` + +Migration => set `target_type` + +--- + +[manage_sql.c#L5850](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5850) + +```c++ +void +set_task_target (task_t task, target_t target) +{ + sql ("UPDATE tasks SET target = %llu, modification_time = m_now ()" + " WHERE id = %llu;", + target, + task); +} +``` + +Migration => set `target_type` + +[manage_sql.c#L6014](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6014) + +```c++ +void +set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) +{ + sql ("UPDATE tasks SET oci_image_target = %llu," + " oci_image_target_location = 0," + " modification_time = m_now ()" + " WHERE id = %llu;", + oci_image_target, + task); +} +``` + +Migration => set `target_type`, `target` and `target_location` + +--- + +[manage_sql.c#L6070](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6070) + +```c++ +void +set_task_web_application_target (task_t task, web_application_target_t web_application_target) +{ + sql_ps ("UPDATE tasks SET web_application_target = $1," + " web_application_target_location = 0," + " modification_time = m_now ()" + " WHERE id = $2;", + SQL_RESOURCE_PARAM(web_application_target), + SQL_RESOURCE_PARAM(task), + NULL); +} +``` + +Migration => set `target_type` + +--- + +[manage_sql.c#L18228](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L18228) + +```c++ +copy_task(...) +... + + ret = copy_resource_lock ("task", name, comment, task_id, + "config, target, oci_image_target," + " web_application_target," + " schedule, schedule_periods," + " scanner, schedule_next_time," + " config_location, target_location," + " schedule_location, scanner_location," + " oci_image_target_location," + " web_application_target_location," + " usage_type," + " agent_group, agent_group_location," + " alterable", + 1, &new, &old); + +``` + +Migration => remove unused fields, copy `target_type` instead + +--- + +[manage_sql_oci_image_targets.c#L385](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L385) + +```c++ +int +delete_oci_image_target (const char *oci_image_target_id, int ultimate) +``` + +Migration => `UPDATE tasks SET target = ..., target_type = ...` + +--- + +[manage_sql_oci_image_targets.c#L524](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L524) + +```c++ +int +restore_oci_image_target (const char *oci_image_target_id) +``` + +Migration => `UPDATE tasks SET target = ..., target_type = ...` + +--- + +[manage_sql_oci_image_targets.c#L580](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L580) +```c++ +/* Update the (oci image|web application) target in any tasks. */ +sql ("UPDATE tasks" + " SET oci_image_target = %llu," + " oci_image_target_location = " G_STRINGIFY (LOCATION_TABLE) + " WHERE oci_image_target = %llu" + " AND oci_image_target_location = " G_STRINGIFY (LOCATION_TRASH), + oci_image_target, + resource); +``` + +=> This SQL query is run quite often in a similar fashion for all kinds of targets on restoring. +Probably makes sense to create a function for this + +--- + +[manage_sql_web_application_targets.c#L422](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L422) + +```c++ +int +delete_web_application_target ( + const char *web_application_target_id, int ultimate) +``` + +Migration => `UPDATE tasks SET target = ..., target_type = ...` + +--- + +[manage_sql_web_application_targets.c#L583](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L583) + +```c++ +int +restore_web_application_target (const char *web_application_target_id) +``` + +Migration => `UPDATE tasks SET target = ..., target_type = ...` From edb3467babf65ef946fa304c9adf98ec3c52ea22 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 30 Jun 2026 09:56:13 +0200 Subject: [PATCH 02/66] sorted code migrations into categories --- docs/tasks-target-type.md | 656 ++++++++++++++++++++------------------ 1 file changed, 348 insertions(+), 308 deletions(-) diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md index c4896e1df0..111a817cb8 100644 --- a/docs/tasks-target-type.md +++ b/docs/tasks-target-type.md @@ -138,6 +138,26 @@ The migration consists of multiple steps: ## Code migration +As per usual with these kinds of refactorings, some recurring changes have a pattern. + +```postgresql +WHERE oci_image_target IS NOT NULL +=> + +WHERE target_type = {{TARGET_TYPE_OCI_IMAGE}} +``` + +```postgresql +WHERE web_application_target = $1 +=> + +WHERE target = $1 +AND target_type = {{TARGET_TYPE_WEB_APPLICATION}} +``` + +
+ General functions + [manage_pg.c#L1293](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1293) ```c++ @@ -164,7 +184,6 @@ Migration: Add additional `if (db_version >= ...)` " AND web_application_target = 0 " " FROM tasks WHERE id = $1)" ``` - [manage_pg.c#L1473](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1473) ```c++ @@ -189,10 +208,173 @@ Migration: Add additional `if (db_version >= ...)` ``` -Migration => `OR target_type = {{TARGET_TYPE_IMPORT_TASK}}` +Migration => `AND target_type = {{TARGET_TYPE_IMPORT_TASK}}` + +--- + +[manage_sql_filters.c#L1286](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_filters.c#L1286) + +```c++ + if ((strcmp (type, "report") == 0) + && (strcmp (keyword->string, "status") == 0)) + g_string_append_printf + (order, + " ORDER BY" + " (CASE WHEN (SELECT " + " ( " + " target = 0 " + " and COALESCE( agent_group, 0) = 0 " + " and COALESCE( oci_image_target, 0) = 0" + " ) " + " FROM tasks" + " WHERE tasks.id = task)" + " THEN '0'" + " ELSE run_status_name (scan_run_status)" + " || (SELECT CAST (temp / 100 AS text)" + " || CAST (temp / 10 AS text)" + " || CAST (temp %% 10 as text)" + " FROM (SELECT report_progress (id) AS temp)" + " AS temp_sub)" + " END)" + " ASC"); + else if ((strcmp (type, "task") == 0) + && (strcmp (keyword->string, "status") == 0)) + g_string_append_printf + (order, + " ORDER BY" + " (CASE WHEN ( target = 0 " + " and COALESCE( agent_group, 0) = 0 " + " and COALESCE( oci_image_target, 0) = 0" + " ) " + " THEN '0'" + " ELSE run_status_name (run_status)" + " || (SELECT CAST (temp / 100 AS text)" + " || CAST (temp / 10 AS text)" + " || CAST (temp %% 10 as text)" + " FROM (SELECT report_progress (id) AS temp" + " FROM reports" + " WHERE task = tasks.id" + " ORDER BY creation_time DESC LIMIT 1)" + " AS temp_sub)" + " END)" + " ASC"); +``` + +Migration => --- +[manage_sql_filters.c#L1491](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_filters.c#L1491) + +```c++ +if ((strcmp (type, "report") == 0) + && (strcmp (keyword->string, "status") == 0)) + g_string_append_printf + (order, + " ORDER BY" + " (CASE WHEN (SELECT " + " ( " + " target = 0 " + " and COALESCE( agent_group, 0) = 0 " + " and COALESCE( oci_image_target, 0) = 0" + " ) " + " FROM tasks" + " WHERE tasks.id = task)" + " THEN '0'" + " ELSE run_status_name (scan_run_status)" + " || (SELECT CAST (temp / 100 AS text)" + " || CAST (temp / 10 AS text)" + " || CAST (temp %% 10 as text)" + " FROM (SELECT report_progress (id) AS temp)" + " AS temp_sub)" + " END)" + " DESC"); + else if ((strcmp (type, "task") == 0) + && (strcmp (keyword->string, "status") == 0)) + g_string_append_printf + (order, + " ORDER BY" + " (CASE WHEN ( target = 0 " + " and COALESCE( agent_group, 0) = 0 " + " and COALESCE( oci_image_target, 0) = 0" + " ) " + " THEN '0'" + " ELSE run_status_name (run_status)" + " || (SELECT CAST (temp / 100 AS text)" + " || CAST (temp / 10 AS text)" + " || CAST (temp %% 10 as text)" + " FROM (SELECT report_progress (id) AS temp" + " FROM reports" + " WHERE task = tasks.id" + " ORDER BY creation_time DESC LIMIT 1)" + " AS temp_sub)" + " END)" + " DESC"); +``` + +Migration => + +--- + +[manage_sql.c#L18107](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L18107) + +```c++ +task_t +make_task (char* name, char* comment, int in_assets, int event) +``` + +Migration => set `target_type` + +--- + +[manage_sql.c#L5850](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5850) + +```c++ +void +set_task_target (task_t task, target_t target) +{ + sql ("UPDATE tasks SET target = %llu, modification_time = m_now ()" + " WHERE id = %llu;", + target, + task); +} +``` + +Migration => set `target_type` + +--- + +[manage_sql.c#L18228](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L18228) + +```c++ +copy_task(...) +... + + ret = copy_resource_lock ("task", name, comment, task_id, + "config, target, oci_image_target," + " web_application_target," + " schedule, schedule_periods," + " scanner, schedule_next_time," + " config_location, target_location," + " schedule_location, scanner_location," + " oci_image_target_location," + " web_application_target_location," + " usage_type," + " agent_group, agent_group_location," + " alterable", + 1, &new, &old); + +``` + +Migration => remove unused fields, copy `target_type` instead + +--- + +
+ +
+ Agent groups + [manage_sql.c#L5866](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5866) ```c++ @@ -207,7 +389,7 @@ set_task_agent_group_and_location (task_t task, agent_group_t agent_group) } ``` -Migration => +Migration => --- @@ -287,115 +469,9 @@ Migration => --- -[manage_sql.c#L5976](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5976) - -```c++ -switch (sql_int64 (&oci_image_target, - "SELECT oci_image_target FROM tasks WHERE id = %llu;", - task)) -``` - -Migration => - ---- - -[manage_sql.c#L5999](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5999) - -```c++ -int -task_oci_image_target_in_trash (task_t task) -{ - return sql_int ("SELECT oci_image_target_location = " - G_STRINGIFY (LOCATION_TRASH) - " FROM tasks WHERE id = %llu;", - task); -} -``` - -Migration => - ---- - -[manage_sql.c#L6014](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6014) - -```c++ -void -set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) -{ - sql ("UPDATE tasks SET oci_image_target = %llu," - " oci_image_target_location = 0," - " modification_time = m_now ()" - " WHERE id = %llu;", - oci_image_target, - task); -} -``` - -Migration => - ---- - -[manage_sql.c#L6035](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6035) - -```c++ -web_application_target_t -task_web_application_target (task_t task) -{ - web_application_target_t web_application_target = 0; - sql_int64_ps (&web_application_target, - "SELECT web_application_target FROM tasks" - " WHERE id = $1;", - SQL_RESOURCE_PARAM(task), - NULL); - return web_application_target; -} -``` - -Migration => - ---- - -[manage_sql.c#L6054](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6054) - -```c++ -int -task_web_application_target_in_trash (task_t task) -{ - return sql_int_ps ("SELECT web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) - " FROM tasks WHERE id = $1;", - SQL_RESOURCE_PARAM(task), - NULL); -} -``` - -Migration => - ---- - -[manage_sql.c#L6070](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6070) - -```c++ -void -set_task_web_application_target (task_t task, web_application_target_t web_application_target) -{ - sql_ps ("UPDATE tasks SET web_application_target = $1," - " web_application_target_location = 0," - " modification_time = m_now ()" - " WHERE id = $2;", - SQL_RESOURCE_PARAM(web_application_target), - SQL_RESOURCE_PARAM(task), - NULL); -} -``` - -Migration => - ---- - [manage_sql_agent_groups.c#L59](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L59) -```c++L6015 +```c++ static int agent_group_in_use_in_hidden_task (agent_group_t agent_group) { @@ -438,104 +514,53 @@ Migration => --- -[manage_sql_filters.c#L1286](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_filters.c#L1286) +
-```c++ - if ((strcmp (type, "report") == 0) - && (strcmp (keyword->string, "status") == 0)) - g_string_append_printf - (order, - " ORDER BY" - " (CASE WHEN (SELECT " - " ( " - " target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " - " FROM tasks" - " WHERE tasks.id = task)" - " THEN '0'" - " ELSE run_status_name (scan_run_status)" - " || (SELECT CAST (temp / 100 AS text)" - " || CAST (temp / 10 AS text)" - " || CAST (temp %% 10 as text)" - " FROM (SELECT report_progress (id) AS temp)" - " AS temp_sub)" - " END)" - " ASC"); - else if ((strcmp (type, "task") == 0) - && (strcmp (keyword->string, "status") == 0)) - g_string_append_printf - (order, - " ORDER BY" - " (CASE WHEN ( target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " - " THEN '0'" - " ELSE run_status_name (run_status)" - " || (SELECT CAST (temp / 100 AS text)" - " || CAST (temp / 10 AS text)" - " || CAST (temp %% 10 as text)" - " FROM (SELECT report_progress (id) AS temp" - " FROM reports" - " WHERE task = tasks.id" - " ORDER BY creation_time DESC LIMIT 1)" - " AS temp_sub)" - " END)" - " ASC"); -``` - -Migration => - ---- - -[manage_sql_filters.c#L1491](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_filters.c#L1491) - -```c++ -if ((strcmp (type, "report") == 0) - && (strcmp (keyword->string, "status") == 0)) - g_string_append_printf - (order, - " ORDER BY" - " (CASE WHEN (SELECT " - " ( " - " target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " - " FROM tasks" - " WHERE tasks.id = task)" - " THEN '0'" - " ELSE run_status_name (scan_run_status)" - " || (SELECT CAST (temp / 100 AS text)" - " || CAST (temp / 10 AS text)" - " || CAST (temp %% 10 as text)" - " FROM (SELECT report_progress (id) AS temp)" - " AS temp_sub)" - " END)" - " DESC"); - else if ((strcmp (type, "task") == 0) - && (strcmp (keyword->string, "status") == 0)) - g_string_append_printf - (order, - " ORDER BY" - " (CASE WHEN ( target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " - " THEN '0'" - " ELSE run_status_name (run_status)" - " || (SELECT CAST (temp / 100 AS text)" - " || CAST (temp / 10 AS text)" - " || CAST (temp %% 10 as text)" - " FROM (SELECT report_progress (id) AS temp" - " FROM reports" - " WHERE task = tasks.id" - " ORDER BY creation_time DESC LIMIT 1)" - " AS temp_sub)" - " END)" - " DESC"); +
+ OCI Image targets + +[manage_sql.c#L5976](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5976) + +```c++ +switch (sql_int64 (&oci_image_target, + "SELECT oci_image_target FROM tasks WHERE id = %llu;", + task)) +``` + +Migration => + +--- + +[manage_sql.c#L5999](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5999) + +```c++ +int +task_oci_image_target_in_trash (task_t task) +{ + return sql_int ("SELECT oci_image_target_location = " + G_STRINGIFY (LOCATION_TRASH) + " FROM tasks WHERE id = %llu;", + task); +} +``` + +Migration => + +--- + +[manage_sql.c#L6014](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6014) + +```c++ +void +set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) +{ + sql ("UPDATE tasks SET oci_image_target = %llu," + " oci_image_target_location = 0," + " modification_time = m_now ()" + " WHERE id = %llu;", + oci_image_target, + task); +} ``` Migration => @@ -647,110 +672,104 @@ Migration => --- -[manage_sql_web_application_targets.c#L422](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L422) +[manage_sql.c#L6014](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6014) ```c++ -int -delete_web_application_target ( - const char *web_application_target_id, int ultimate) +void +set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) +{ + sql ("UPDATE tasks SET oci_image_target = %llu," + " oci_image_target_location = 0," + " modification_time = m_now ()" + " WHERE id = %llu;", + oci_image_target, + task); +} ``` -Migration => merge with delete_target (...) +Migration => set `target_type`, `target` and `target_location` --- -[manage_sql_web_application_targets.c#L989](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L989) +[manage_sql_oci_image_targets.c#L385](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L385) ```c++ int -web_application_target_in_use (web_application_target_t web_application_target) -{ - return !!sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - SQL_RESOURCE_PARAM (web_application_target), - NULL); -} - -int -trash_web_application_target_in_use ( - web_application_target_t web_application_target) -{ - return !!sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) ";", - SQL_RESOURCE_PARAM (web_application_target), - NULL); -} +delete_oci_image_target (const char *oci_image_target_id, int ultimate) ``` -Migration => +Migration => `UPDATE tasks SET target = ..., target_type = ...` --- -[manage_sql_web_application_targets.c#L1055](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L1055) +[manage_sql_oci_image_targets.c#L524](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L524) ```c++ -init_iterator (iterator, - "%s" - " SELECT name, uuid, %s FROM tasks" - " WHERE web_application_target = %llu" - " AND hidden = 0" - " ORDER BY name ASC;", - with_clause ? with_clause : "", - available, - web_application_target); +int +restore_oci_image_target (const char *oci_image_target_id) ``` -Migration => `target_type` +Migration => `UPDATE tasks SET target = ..., target_type = ...` --- -[manage_sql.c#L18107](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L18107) - +[manage_sql_oci_image_targets.c#L580](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L580) ```c++ -task_t -make_task (char* name, char* comment, int in_assets, int event) +/* Update the (oci image|web application) target in any tasks. */ +sql ("UPDATE tasks" + " SET oci_image_target = %llu," + " oci_image_target_location = " G_STRINGIFY (LOCATION_TABLE) + " WHERE oci_image_target = %llu" + " AND oci_image_target_location = " G_STRINGIFY (LOCATION_TRASH), + oci_image_target, + resource); ``` -Migration => set `target_type` +=> This SQL query is run quite often in a similar fashion for all kinds of targets on restoring. +Probably makes sense to create a function for this --- -[manage_sql.c#L5850](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5850) +
+ +
+ Web application targets + +[manage_sql.c#L6035](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6035) ```c++ -void -set_task_target (task_t task, target_t target) +web_application_target_t +task_web_application_target (task_t task) { - sql ("UPDATE tasks SET target = %llu, modification_time = m_now ()" - " WHERE id = %llu;", - target, - task); + web_application_target_t web_application_target = 0; + sql_int64_ps (&web_application_target, + "SELECT web_application_target FROM tasks" + " WHERE id = $1;", + SQL_RESOURCE_PARAM(task), + NULL); + return web_application_target; } ``` -Migration => set `target_type` +Migration => -[manage_sql.c#L6014](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6014) +--- + +[manage_sql.c#L6054](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6054) ```c++ -void -set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) +int +task_web_application_target_in_trash (task_t task) { - sql ("UPDATE tasks SET oci_image_target = %llu," - " oci_image_target_location = 0," - " modification_time = m_now ()" - " WHERE id = %llu;", - oci_image_target, - task); + return sql_int_ps ("SELECT web_application_target_location = " + G_STRINGIFY (LOCATION_TRASH) + " FROM tasks WHERE id = $1;", + SQL_RESOURCE_PARAM(task), + NULL); } ``` -Migration => set `target_type`, `target` and `target_location` +Migration => --- @@ -770,72 +789,89 @@ set_task_web_application_target (task_t task, web_application_target_t web_appli } ``` -Migration => set `target_type` +Migration => --- -[manage_sql.c#L18228](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L18228) +[manage_sql_web_application_targets.c#L422](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L422) ```c++ -copy_task(...) -... - - ret = copy_resource_lock ("task", name, comment, task_id, - "config, target, oci_image_target," - " web_application_target," - " schedule, schedule_periods," - " scanner, schedule_next_time," - " config_location, target_location," - " schedule_location, scanner_location," - " oci_image_target_location," - " web_application_target_location," - " usage_type," - " agent_group, agent_group_location," - " alterable", - 1, &new, &old); - +int +delete_web_application_target ( + const char *web_application_target_id, int ultimate) ``` -Migration => remove unused fields, copy `target_type` instead +Migration => merge with delete_target (...) --- -[manage_sql_oci_image_targets.c#L385](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L385) +[manage_sql_web_application_targets.c#L989](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L989) ```c++ int -delete_oci_image_target (const char *oci_image_target_id, int ultimate) +web_application_target_in_use (web_application_target_t web_application_target) +{ + return !!sql_int_ps ("SELECT count(*) FROM tasks" + " WHERE web_application_target = $1" + " AND web_application_target_location = " + G_STRINGIFY (LOCATION_TABLE) + " AND hidden = 0;", + SQL_RESOURCE_PARAM (web_application_target), + NULL); +} + +int +trash_web_application_target_in_use ( + web_application_target_t web_application_target) +{ + return !!sql_int_ps ("SELECT count(*) FROM tasks" + " WHERE web_application_target = $1" + " AND web_application_target_location = " + G_STRINGIFY (LOCATION_TRASH) ";", + SQL_RESOURCE_PARAM (web_application_target), + NULL); +} ``` -Migration => `UPDATE tasks SET target = ..., target_type = ...` +Migration => --- -[manage_sql_oci_image_targets.c#L524](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L524) +[manage_sql_web_application_targets.c#L1055](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L1055) ```c++ -int -restore_oci_image_target (const char *oci_image_target_id) +init_iterator (iterator, + "%s" + " SELECT name, uuid, %s FROM tasks" + " WHERE web_application_target = %llu" + " AND hidden = 0" + " ORDER BY name ASC;", + with_clause ? with_clause : "", + available, + web_application_target); ``` -Migration => `UPDATE tasks SET target = ..., target_type = ...` +Migration => `target_type` --- -[manage_sql_oci_image_targets.c#L580](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L580) +[manage_sql.c#L6070](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6070) + ```c++ -/* Update the (oci image|web application) target in any tasks. */ -sql ("UPDATE tasks" - " SET oci_image_target = %llu," - " oci_image_target_location = " G_STRINGIFY (LOCATION_TABLE) - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " G_STRINGIFY (LOCATION_TRASH), - oci_image_target, - resource); +void +set_task_web_application_target (task_t task, web_application_target_t web_application_target) +{ + sql_ps ("UPDATE tasks SET web_application_target = $1," + " web_application_target_location = 0," + " modification_time = m_now ()" + " WHERE id = $2;", + SQL_RESOURCE_PARAM(web_application_target), + SQL_RESOURCE_PARAM(task), + NULL); +} ``` -=> This SQL query is run quite often in a similar fashion for all kinds of targets on restoring. -Probably makes sense to create a function for this +Migration => set `target_type` --- @@ -859,3 +895,7 @@ restore_web_application_target (const char *web_application_target_id) ``` Migration => `UPDATE tasks SET target = ..., target_type = ...` + + +
+ From 63282c492af87829ede61cc891e3d17cb49e98e1 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 30 Jun 2026 15:35:14 +0200 Subject: [PATCH 03/66] * added task_update_[delete|restore]_target () * cleaned up OCI image migration section --- docs/tasks-target-type.md | 157 +++++++++++++++++++++----------------- 1 file changed, 86 insertions(+), 71 deletions(-) diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md index 111a817cb8..c907909ec1 100644 --- a/docs/tasks-target-type.md +++ b/docs/tasks-target-type.md @@ -155,6 +155,35 @@ WHERE target = $1 AND target_type = {{TARGET_TYPE_WEB_APPLICATION}} ``` +A group of `delete_... ()` and `restore_... ()` functions updates the respective `target` and `target_location` +in the tasks table. Since we are removing the designated columns for each target type, +we can generalize these functions. +```c++ +void +task_update_delete_target (int resource_id, int trash_id) { + sql_ps ("UPDATE tasks" + " SET target = $1," + " target_location = " G_STRINGIFY (LOCATION_TRASH) + " WHERE target = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TABLE) ";", + SQL_RESOURCE_PARAM (trash_id), + SQL_RESOURCE_PARAM (resource_id),; + NULL); +} + +void; +task_update_restore_target (int trash_id, int restored_id) { + sql_ps ("UPDATE tasks" + " SET target = $1," + " target_location = " G_STRINGIFY (LOCATION_TABLE) + " WHERE target = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TRASH) ";", + SQL_RESOURCE_PARAM (restored_id), + SQL_RESOURCE_PARAM (trash_id), + NULL); +} +``` +
General functions @@ -485,8 +514,15 @@ Migration => --- -[manage_sql_agent_groups.c#L1029](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L1029) +[manage_sql_agent_groups.c#L556](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L556) +```c++ +int +delete_agent_group (const char *agent_group_uuid, int ultimate) +``` +Migration => `update_trashcan_task_target ()` + +[manage_sql_agent_groups.c#L1029](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L1029) ```c++ int agent_group_in_use (agent_group_t agent_group) @@ -498,7 +534,14 @@ agent_group_in_use (agent_group_t agent_group) " AND hidden = 0;", agent_group); } +``` +Migration => + +--- + +[manage_sql_agent_groups.c#L1046](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L1046) +```c++ int trash_agent_group_in_use (agent_group_t agent_group) { @@ -520,7 +563,6 @@ Migration => OCI Image targets [manage_sql.c#L5976](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5976) - ```c++ switch (sql_int64 (&oci_image_target, "SELECT oci_image_target FROM tasks WHERE id = %llu;", @@ -532,7 +574,6 @@ Migration => --- [manage_sql.c#L5999](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5999) - ```c++ int task_oci_image_target_in_trash (task_t task) @@ -549,7 +590,6 @@ Migration => --- [manage_sql.c#L6014](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6014) - ```c++ void set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) @@ -563,12 +603,11 @@ set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) } ``` -Migration => +Migration => set `target_type`, `target` and `target_location` --- [manage_sql_oci_image_targets.c#L427](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L427) - ```c++ if (sql_int ("SELECT count(*) FROM tasks" " WHERE oci_image_target = %llu" @@ -586,7 +625,6 @@ Migration => use `target_type` and regular fields instead --- [manage_sql_oci_image_targets.c#L451](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L451) - ```c++ if (sql_int ("SELECT count(*) FROM tasks" " WHERE oci_image_target = %llu" @@ -604,8 +642,24 @@ Migration => use `target_type` and regular fields instead --- -[manage_sql_oci_image_targets.c#L490](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L490) +[manage_sql_oci_image_targets.c#L473](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L473) +```c++ +sql ("UPDATE tasks" + " SET oci_image_target = %llu," + " oci_image_target_location = " + G_STRINGIFY (LOCATION_TRASH) + " WHERE oci_image_target = %llu" + " AND oci_image_target_location = " + G_STRINGIFY (LOCATION_TABLE) ";", + sql_last_insert_id (), + oci_image_target); +``` + +Migration => `task_update_delete_target ()` +--- + +[manage_sql_oci_image_targets.c#L490](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L490) ```c++ else if (sql_int ("SELECT count(*) FROM tasks" " WHERE oci_image_target = %llu" @@ -618,15 +672,27 @@ Migration => use `target_type` and regular fields instead } ``` -Migration => What about the trash can in general? Do we want to use `targets_trash` for every target_type? Probably yes. -So we need to get rid of all the `*_target_trash` tables and replace the queries for "copying" to the trash table -accordingly. -This also requires changes to => delete_target () `m̀anage_sql_targets.c#L416` +Migration => --- -[manage_sql_oci_image_targets.c#L906](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L906) +[manage_sql_oci_image_targets.c#L581](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L581) +```c++ +sql ("UPDATE tasks" + " SET oci_image_target = %llu," + " oci_image_target_location = " G_STRINGIFY (LOCATION_TABLE) + " WHERE oci_image_target = %llu" + " AND oci_image_target_location = " G_STRINGIFY (LOCATION_TRASH), + oci_image_target, + resource); +``` +=> `task_update_restored_target ()` + + +--- + +[manage_sql_oci_image_targets.c#L906](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L906) ```c++ int oci_image_target_in_use (oci_image_target_t oci_image_target) @@ -638,7 +704,14 @@ oci_image_target_in_use (oci_image_target_t oci_image_target) " AND hidden = 0;", oci_image_target); } +``` +Migration => + +--- + +[manage_sql_oci_image_targets.c#L924](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L924) +```c++ int trash_oci_image_target_in_use (oci_image_target_t oci_image_target) { @@ -672,64 +745,6 @@ Migration => --- -[manage_sql.c#L6014](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6014) - -```c++ -void -set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) -{ - sql ("UPDATE tasks SET oci_image_target = %llu," - " oci_image_target_location = 0," - " modification_time = m_now ()" - " WHERE id = %llu;", - oci_image_target, - task); -} -``` - -Migration => set `target_type`, `target` and `target_location` - ---- - -[manage_sql_oci_image_targets.c#L385](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L385) - -```c++ -int -delete_oci_image_target (const char *oci_image_target_id, int ultimate) -``` - -Migration => `UPDATE tasks SET target = ..., target_type = ...` - ---- - -[manage_sql_oci_image_targets.c#L524](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L524) - -```c++ -int -restore_oci_image_target (const char *oci_image_target_id) -``` - -Migration => `UPDATE tasks SET target = ..., target_type = ...` - ---- - -[manage_sql_oci_image_targets.c#L580](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L580) -```c++ -/* Update the (oci image|web application) target in any tasks. */ -sql ("UPDATE tasks" - " SET oci_image_target = %llu," - " oci_image_target_location = " G_STRINGIFY (LOCATION_TABLE) - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " G_STRINGIFY (LOCATION_TRASH), - oci_image_target, - resource); -``` - -=> This SQL query is run quite often in a similar fashion for all kinds of targets on restoring. -Probably makes sense to create a function for this - ---- -
From 3cc6fbd1233f179152bed5da6078816b91c942d6 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 30 Jun 2026 15:50:49 +0200 Subject: [PATCH 04/66] cleaned up Web application code migration section --- docs/tasks-target-type.md | 160 ++++++++++++++++++++++---------------- 1 file changed, 92 insertions(+), 68 deletions(-) diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md index c907909ec1..1edf5ebf8e 100644 --- a/docs/tasks-target-type.md +++ b/docs/tasks-target-type.md @@ -188,7 +188,6 @@ task_update_restore_target (int trash_id, int restored_id) { General functions [manage_pg.c#L1293](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1293) - ```c++ " SELECT CASE" " WHEN EXISTS (SELECT id" @@ -204,7 +203,6 @@ Migration: Add additional `if (db_version >= ...)` --- [manage_pg.c#L1447](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1447) - ```c++ " SELECT CASE" " WHEN (SELECT target = 0 " @@ -213,8 +211,8 @@ Migration: Add additional `if (db_version >= ...)` " AND web_application_target = 0 " " FROM tasks WHERE id = $1)" ``` -[manage_pg.c#L1473](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1473) +[manage_pg.c#L1473](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1473) ```c++ " SELECT CASE" " WHEN (SELECT target = 0 " @@ -229,7 +227,6 @@ Migration: Add additional `if (db_version >= ...)` --- [manage_pg.c#L1562](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1562) - ```c++ /* Skip running and import tasks. */ " WHEN (SELECT run_status = %u OR target = 0" @@ -242,7 +239,6 @@ Migration => `AND target_type = {{TARGET_TYPE_IMPORT_TASK}}` --- [manage_sql_filters.c#L1286](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_filters.c#L1286) - ```c++ if ((strcmp (type, "report") == 0) && (strcmp (keyword->string, "status") == 0)) @@ -294,7 +290,6 @@ Migration => --- [manage_sql_filters.c#L1491](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_filters.c#L1491) - ```c++ if ((strcmp (type, "report") == 0) && (strcmp (keyword->string, "status") == 0)) @@ -346,7 +341,6 @@ Migration => --- [manage_sql.c#L18107](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L18107) - ```c++ task_t make_task (char* name, char* comment, int in_assets, int event) @@ -357,7 +351,6 @@ Migration => set `target_type` --- [manage_sql.c#L5850](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5850) - ```c++ void set_task_target (task_t task, target_t target) @@ -374,7 +367,6 @@ Migration => set `target_type` --- [manage_sql.c#L18228](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L18228) - ```c++ copy_task(...) ... @@ -405,7 +397,6 @@ Migration => remove unused fields, copy `target_type` instead Agent groups [manage_sql.c#L5866](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5866) - ```c++ void set_task_agent_group_and_location (task_t task, agent_group_t agent_group) @@ -423,7 +414,6 @@ Migration => --- [manage_sql.c#L5884](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5884) - ```c++ int agent_group_tasks_exist_by_scanner (scanner_t scanner) @@ -444,7 +434,6 @@ Migration => --- [manage_sql.c#L5903](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5903) - ```c++ int agent_group_hidden_tasks_exist_by_scanner (scanner_t scanner) @@ -470,7 +459,6 @@ Migration => --- [manage_sql.c#L5931](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5931) - ```c++ switch (sql_int64 (&agent_group, "SELECT agent_group FROM tasks WHERE id = %llu;", @@ -482,7 +470,6 @@ Migration => --- [manage_sql.c#L5954](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5954) - ```c++ int task_agent_group_in_trash (task_t task) @@ -499,7 +486,6 @@ Migration => --- [manage_sql_agent_groups.c#L59](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L59) - ```c++ static int agent_group_in_use_in_hidden_task (agent_group_t agent_group) @@ -728,7 +714,6 @@ Migration => --- [manage_sql_oci_image_targets.c#L979](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L979) - ```c++ init_iterator (iterator, "%s" @@ -751,7 +736,6 @@ Migration => Web application targets [manage_sql.c#L6035](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6035) - ```c++ web_application_target_t task_web_application_target (task_t task) @@ -771,7 +755,6 @@ Migration => --- [manage_sql.c#L6054](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6054) - ```c++ int task_web_application_target_in_trash (task_t task) @@ -789,7 +772,6 @@ Migration => --- [manage_sql.c#L6070](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6070) - ```c++ void set_task_web_application_target (task_t task, web_application_target_t web_application_target) @@ -804,24 +786,102 @@ set_task_web_application_target (task_t task, web_application_target_t web_appli } ``` +Migration => set `target_type` + +--- + +[manage_sql_web_application_targets.c#L465](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L465) +```c++ + if (sql_int_ps ("SELECT count(*) FROM tasks" + " WHERE web_application_target = $1" + " AND web_application_target_location = " + G_STRINGIFY (LOCATION_TRASH) ";", + SQL_RESOURCE_PARAM (web_application_target), + NULL)) + { + sql_rollback (); + return 1; + } +``` + Migration => +`task_in_use ()` function? +Or simply replace `web_application_target` and `*_location` with `target` and `target_location` --- -[manage_sql_web_application_targets.c#L422](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L422) +[manage_sql_web_application_targets.c#L497](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L497) +```c++ + if (sql_int_ps ("SELECT count(*) FROM tasks" + " WHERE web_application_target = $1" + " AND web_application_target_location = " + G_STRINGIFY (LOCATION_TABLE) + " AND hidden = 0;", + SQL_RESOURCE_PARAM (web_application_target), + NULL)) + { + sql_rollback (); + return 1; + } +``` + +Migration => +`task_in_use ()` function with optional hidden = 0 flag? +Or simply replace `web_application_target` and `*_location` with `target` and `target_location` +--- + +[manage_sql_web_application_targets.c#L522](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L522) ```c++ -int -delete_web_application_target ( - const char *web_application_target_id, int ultimate) + sql_ps ("UPDATE tasks" + " SET web_application_target = $1," + " web_application_target_location = " + G_STRINGIFY (LOCATION_TRASH) + " WHERE web_application_target = $2" + " AND web_application_target_location = " + G_STRINGIFY (LOCATION_TABLE) ";", + SQL_RESOURCE_PARAM (trash_web_application_target), + SQL_RESOURCE_PARAM (web_application_target), + NULL); ``` -Migration => merge with delete_target (...) +Migration => `update_task_delete_target ()` --- -[manage_sql_web_application_targets.c#L989](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L989) +[manage_sql_web_application_targets.c#L543](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L543) +```c++ + else if (sql_int_ps ("SELECT count(*) FROM tasks" + " WHERE web_application_target = $1" + " AND web_application_target_location = " + G_STRINGIFY (LOCATION_TABLE) ";", + SQL_RESOURCE_PARAM (web_application_target), + NULL)) +``` + +Migration => +--- + +[manage_sql_web_application_targets.c#L648](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L648) +```c++ + sql_ps ("UPDATE tasks" + " SET web_application_target = $1," + " web_application_target_location = " + G_STRINGIFY (LOCATION_TABLE) + " WHERE web_application_target = $2" + " AND web_application_target_location = " + G_STRINGIFY (LOCATION_TRASH) ";", + SQL_RESOURCE_PARAM (web_application_target), + SQL_RESOURCE_PARAM (resource), + NULL); +``` + +Migration => `update_task_restore_target ()` + +--- + +[manage_sql_web_application_targets.c#L968](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L968) ```c++ int web_application_target_in_use (web_application_target_t web_application_target) @@ -834,7 +894,14 @@ web_application_target_in_use (web_application_target_t web_application_target) SQL_RESOURCE_PARAM (web_application_target), NULL); } +``` + +Migration => + +--- +[manage_sql_web_application_targets.c#L988](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L988) +```c++ int trash_web_application_target_in_use ( web_application_target_t web_application_target) @@ -853,7 +920,6 @@ Migration => --- [manage_sql_web_application_targets.c#L1055](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L1055) - ```c++ init_iterator (iterator, "%s" @@ -870,47 +936,5 @@ Migration => `target_type` --- -[manage_sql.c#L6070](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6070) - -```c++ -void -set_task_web_application_target (task_t task, web_application_target_t web_application_target) -{ - sql_ps ("UPDATE tasks SET web_application_target = $1," - " web_application_target_location = 0," - " modification_time = m_now ()" - " WHERE id = $2;", - SQL_RESOURCE_PARAM(web_application_target), - SQL_RESOURCE_PARAM(task), - NULL); -} -``` - -Migration => set `target_type` - ---- - -[manage_sql_web_application_targets.c#L422](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L422) - -```c++ -int -delete_web_application_target ( - const char *web_application_target_id, int ultimate) -``` - -Migration => `UPDATE tasks SET target = ..., target_type = ...` - ---- - -[manage_sql_web_application_targets.c#L583](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L583) - -```c++ -int -restore_web_application_target (const char *web_application_target_id) -``` - -Migration => `UPDATE tasks SET target = ..., target_type = ...` - -
From 1124daa9f25a70033c7ded2e0d6df18c3ee4add2 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 30 Jun 2026 15:55:33 +0200 Subject: [PATCH 05/66] formatting --- docs/tasks-target-type.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md index 1edf5ebf8e..3c577afbcd 100644 --- a/docs/tasks-target-type.md +++ b/docs/tasks-target-type.md @@ -96,8 +96,8 @@ These are the proposed target types, part of the initial migration ## Data migration -While migrating the data seems rather trivial, we should opt for creating a database backup -prior to migrating the columns and data. +> :warning: While migrating the data seems rather trivial, we should opt for **creating a database backup + prior to migrating** the columns and data. The migration consists of multiple steps: From 1f248b18e119fc6a97232f546f754de5ff0f658d Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 30 Jun 2026 15:58:22 +0200 Subject: [PATCH 06/66] fixed DROP COLUMN migration step --- docs/tasks-target-type.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md index 3c577afbcd..50edfe4e71 100644 --- a/docs/tasks-target-type.md +++ b/docs/tasks-target-type.md @@ -127,7 +127,12 @@ The migration consists of multiple steps: commit() ``` 3. ```postgresql - ALTER TABLE tasks DROP COLUMN oci_image_target, DROP COLUMN oci_image_target_location, DROP COLUMN web_application_target, DROP COLUMN web_application_target_location + ALTER TABLE tasks DROP COLUMN agent_group, + DROP COLUMN agent_group_location, + DROP COLUMN oci_image_target, + DROP COLUMN oci_image_target_location, + DROP COLUMN web_application_target, + DROP COLUMN web_application_target_location ``` **Concerns** From 3b0c03e3bb253f9708ae88a670a3c1fbee73e67f Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Wed, 1 Jul 2026 10:04:13 +0200 Subject: [PATCH 07/66] formatting --- docs/tasks-target-type.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md index 50edfe4e71..8e90f05369 100644 --- a/docs/tasks-target-type.md +++ b/docs/tasks-target-type.md @@ -172,11 +172,11 @@ task_update_delete_target (int resource_id, int trash_id) { " WHERE target = $2" " AND target_location = " G_STRINGIFY (LOCATION_TABLE) ";", SQL_RESOURCE_PARAM (trash_id), - SQL_RESOURCE_PARAM (resource_id),; + SQL_RESOURCE_PARAM (resource_id), NULL); } -void; +void task_update_restore_target (int trash_id, int restored_id) { sql_ps ("UPDATE tasks" " SET target = $1," From 2edad7e87af4a8b7be5bd03c3b4cf09202da5f05 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 3 Jul 2026 12:31:54 +0200 Subject: [PATCH 08/66] added data migration from 281 => 282 --- docs/tasks-target-type.md | 14 ++--- src/manage_migrators.c | 104 ++++++++++++++++++++++++++++++++++++++ src/manage_tasks.h | 6 +++ 3 files changed, 117 insertions(+), 7 deletions(-) diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md index 8e90f05369..1cd43ad79a 100644 --- a/docs/tasks-target-type.md +++ b/docs/tasks-target-type.md @@ -84,13 +84,13 @@ erDiagram These are the proposed target types, part of the initial migration -| Target Type | Integer Value | Reference Table | Notes | -|-------------------------------|---------------|---------------------------|----------------------| -| `TARGET_TYPE_IMPORT_TASK` | 0 | N/A | `target` must be `0` | -| `TARGET_TYPE_REGULAR` | 1 | `targets` | N/A | -| `TARGET_TYPE_AGENT_GROUP` | 2 | `agent_groups` | N/A | -| `TARGET_TYPE_OCI_IMAGE` | 3 | `oci_image_targets` | N/A | -| `TARGET_TYPE_WEB_APPLICATION` | 4 | `web_application_targets` | N/A | +| Target Type | Integer Value | Reference Table | Notes | +|-------------------------------------|---------------|---------------------------|----------------------| +| `TASKS_TARGET_TYPE_IMPORT_TASK` | 0 | N/A | `target` must be `0` | +| `TASKS_TARGET_TYPE_REGULAR` | 1 | `targets` | N/A | +| `TASKS_TARGET_TYPE_AGENT_GROUP` | 2 | `agent_groups` | N/A | +| `TASKS_TARGET_TYPE_OCI_IMAGE` | 3 | `oci_image_targets` | N/A | +| `TASKS_TARGET_TYPE_WEB_APPLICATION` | 4 | `web_application_targets` | N/A | # Migrations diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 3433514063..44d3484a65 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4238,6 +4238,109 @@ migrate_280_to_281 () return 0; } +/** + * @brief Migrate the database from version 280 to version 281. + * + * @return 0 success, -1 error. + */ + +int +migrate_281_to_282 () +{ + sql_begin_immediate (); + + if (manage_db_version () != 281) + { + sql_rollback (); + return -1; + } + + /* Add target_type column to tasks table */ + sql ("ALTER TABLE tasks ADD target_type INTEGER"); + + /* Migrate data to new column */ + iterator_t tasks; + init_iterator (&tasks, "SELECT id," + " target, target_location," + " agent_group, agent_group_location," + " oci_image_target, oci_image_target_location," + " web_application_target, web_application_target_location" + " FROM tasks"); + + while (next (&tasks)) + { + const int64_t id = iterator_int64 (&tasks, 0); + const int64_t target = iterator_int64 (&tasks, 1); + const int64_t target_location = iterator_int64 (&tasks, 2); + const int64_t agent_group = iterator_int64 (&tasks, 3); + const int64_t agent_group_location = iterator_int64 (&tasks, 4); + const int64_t oci_image_target = iterator_int64 (&tasks, 5); + const int64_t oci_image_target_location = iterator_int64 (&tasks, 6); + const int64_t web_application_target = iterator_int64 (&tasks, 7); + const int64_t web_application_target_location = iterator_int64 (&tasks, 8); + + int64_t new_target = 0; + int64_t new_location = 0; + int new_target_type = TASKS_TARGET_TYPE_UNDEFINED; + + if (agent_group != 0) + { + new_target = agent_group; + new_location = agent_group_location; + new_target_type = TASKS_TARGET_TYPE_AGENT_GROUP; + } + else if (oci_image_target != 0) + { + new_target = oci_image_target; + new_location = oci_image_target_location; + new_target_type = TASKS_TARGET_TYPE_OCI_IMAGE; + } + else if (web_application_target != 0) + { + new_target = web_application_target; + new_location = web_application_target_location; + new_target_type = TASKS_TARGET_TYPE_WEB_APPLICATION; + } + else if (target != 0) + { + new_target = target; + new_location = target_location; + new_target_type = TASKS_TARGET_TYPE_REGULAR; + } + else + { + new_target = 0; + new_location = 0; + new_target_type = TASKS_TARGET_TYPE_IMPORT_TASK; + } + + sql_ps ("UPDATE tasks" + " SET target = $2, target_location = $3, target_type = $4" + " WHERE id = $1", + SQL_RESOURCE_PARAM (id), + SQL_RESOURCE_PARAM (new_target), + SQL_INT_PARAM (new_location), + SQL_INT_PARAM (new_target_type), + NULL); + } + cleanup_iterator (&tasks); + + /* Drop old, unused target columns */ + sql ("ALTER TABLE tasks DROP COLUMN agent_group," + " DROP COLUMN agent_group_location," + " DROP COLUMN oci_image_target," + " DROP COLUMN oci_image_target_location," + " DROP COLUMN web_application_target," + " DROP COLUMN web_application_target_location"); + + /* Set the database version to 281 */ + set_db_version (281); + + sql_commit(); + + return 0; +} + #undef UPDATE_DASHBOARD_SETTINGS /** @@ -4325,6 +4428,7 @@ static migrator_t database_migrators[] = { {279, migrate_278_to_279}, {280, migrate_279_to_280}, {281, migrate_280_to_281}, + {282, migrate_281_to_282}, /* End marker. */ {-1, NULL}}; diff --git a/src/manage_tasks.h b/src/manage_tasks.h index 3e503db365..91523d83c7 100644 --- a/src/manage_tasks.h +++ b/src/manage_tasks.h @@ -6,6 +6,12 @@ #ifndef _GVMD_MANAGE_TASKS_H #define _GVMD_MANAGE_TASKS_H +#define TASKS_TARGET_TYPE_UNDEFINED (-1) +#define TASKS_TARGET_TYPE_IMPORT_TASK 0 +#define TASKS_TARGET_TYPE_REGULAR 1 +#define TASKS_TARGET_TYPE_AGENT_GROUP 2 +#define TASKS_TARGET_TYPE_OCI_IMAGE 3 +#define TASKS_TARGET_TYPE_WEB_APPLICATION 4 #endif /* not _GVMD_MANAGE_TASKS_H */ From a88da34479168d4015f5d466f9bc3f573c80a676 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 3 Jul 2026 14:03:06 +0200 Subject: [PATCH 09/66] use enum instead of define for tasks_target_type --- src/manage_migrators.c | 2 +- src/manage_tasks.h | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 44d3484a65..e42843041a 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4281,7 +4281,7 @@ migrate_281_to_282 () int64_t new_target = 0; int64_t new_location = 0; - int new_target_type = TASKS_TARGET_TYPE_UNDEFINED; + tasks_target_type_t new_target_type = TASKS_TARGET_TYPE_UNDEFINED; if (agent_group != 0) { diff --git a/src/manage_tasks.h b/src/manage_tasks.h index 91523d83c7..8656e72a2b 100644 --- a/src/manage_tasks.h +++ b/src/manage_tasks.h @@ -6,12 +6,16 @@ #ifndef _GVMD_MANAGE_TASKS_H #define _GVMD_MANAGE_TASKS_H -#define TASKS_TARGET_TYPE_UNDEFINED (-1) -#define TASKS_TARGET_TYPE_IMPORT_TASK 0 -#define TASKS_TARGET_TYPE_REGULAR 1 -#define TASKS_TARGET_TYPE_AGENT_GROUP 2 -#define TASKS_TARGET_TYPE_OCI_IMAGE 3 -#define TASKS_TARGET_TYPE_WEB_APPLICATION 4 + +typedef enum tasks_target_type +{ + TASKS_TARGET_TYPE_UNDEFINED = -1, + TASKS_TARGET_TYPE_IMPORT_TASK = 0, + TASKS_TARGET_TYPE_REGULAR = 1, + TASKS_TARGET_TYPE_AGENT_GROUP = 2, + TASKS_TARGET_TYPE_OCI_IMAGE = 3, + TASKS_TARGET_TYPE_WEB_APPLICATION = 4, +} tasks_target_type_t; #endif /* not _GVMD_MANAGE_TASKS_H */ From 06c87b776ffb3ba2429d44388942202edff8c08b Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 6 Jul 2026 08:29:53 +0200 Subject: [PATCH 10/66] add target_type and remove *_target columns from create_tables() in tasks table creation --- src/manage_pg.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/manage_pg.c b/src/manage_pg.c index 8481a537fb..fd86cdbe69 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -3049,20 +3049,15 @@ create_tables () " end_time integer," " config integer," // REFERENCES configs (id) ON DELETE RESTRICT," " target integer," // REFERENCES targets (id) ON DELETE RESTRICT," + " target_type integer," " schedule integer," // REFERENCES schedules (id) ON DELETE RESTRICT," " schedule_next_time integer," " schedule_periods integer," " scanner integer," // REFERENCES scanner (id) ON DELETE RESTRICT," - " agent_group integer," // REFERENCES agent_groups (id) ON DELETE RESTRICT," " config_location integer," " target_location integer," " schedule_location integer," " scanner_location integer," - " oci_image_target integer," - " oci_image_target_location integer," - " agent_group_location integer," - " web_application_target integer," - " web_application_target_location integer," " upload_result_count integer," " alterable integer," " creation_time integer," From cfb7c77a83a918542f92a66eb352a6b790b4ada4 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 6 Jul 2026 14:20:23 +0200 Subject: [PATCH 11/66] migrated agent_group/oci_image/web_application target common functionality to generalized functions --- src/manage.h | 18 +++- src/manage_pg.c | 187 +++++++++++++++++++++++++++++++- src/manage_sql.c | 271 +++++++++++++++++++++++++++-------------------- 3 files changed, 357 insertions(+), 119 deletions(-) diff --git a/src/manage.h b/src/manage.h index d40dac0fe3..5850a09cdd 100644 --- a/src/manage.h +++ b/src/manage.h @@ -665,13 +665,25 @@ void set_task_config (task_t, config_t); target_t -task_target (task_t); +task_target (task_t, tasks_target_type_t); + +tasks_target_type_t +task_target_type (task_t); int -task_target_in_trash (task_t); +task_target_in_trash (task_t, tasks_target_type_t); + +void +set_task_target (task_t, target_t, tasks_target_type_t); + +void +set_task_target_and_location (task_t, target_t, tasks_target_type_t); + +void +task_update_delete_target (target_t, target_t, tasks_target_type_t); void -set_task_target (task_t, target_t); +task_update_restore_target (target_t, target_t, tasks_target_type_t); #if ENABLE_AGENTS void diff --git a/src/manage_pg.c b/src/manage_pg.c index fd86cdbe69..f76f659129 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -1285,7 +1285,32 @@ manage_create_sql_functions () "$$ LANGUAGE SQL;"); /* column hostname in table report_hosts was added in version 273 */ - if (current_db_version >= 273) + if (current_db_version >= 281) + { + sql ("CREATE OR REPLACE FUNCTION report_result_host_count (report integer," + " min_qod integer)" + " RETURNS bigint AS $$" + " SELECT CASE" + " WHEN EXISTS (SELECT id" + " FROM tasks" + " WHERE id = (SELECT task" + " FROM reports WHERE id = $1)" + " AND target_type = 3)" // TASKS_TARGET_TYPE_OCI_IMAGE + " THEN (SELECT count (DISTINCT id) FROM report_hosts" + " WHERE report_hosts.report = $1" + " AND EXISTS (SELECT * FROM results" + " WHERE results.host = report_hosts.host" + " AND results.hostname = report_hosts.hostname" + " AND results.qod >= $2))" + " ELSE (SELECT count (DISTINCT id) FROM report_hosts" + " WHERE report_hosts.report = $1" + " AND EXISTS (SELECT * FROM results" + " WHERE results.host = report_hosts.host" + " AND results.qod >= $2))" + " END;" + "$$ LANGUAGE SQL;"); + } + else if (current_db_version >= 273) { sql ("CREATE OR REPLACE FUNCTION report_result_host_count (report integer," " min_qod integer)" @@ -1436,8 +1461,31 @@ manage_create_sql_functions () TASK_STATUS_DONE); } + /* target_type was added in version 281 */ + if (current_db_version >= 281) + { + sql ("CREATE OR REPLACE FUNCTION task_severity (integer," // task + " integer," // overrides + " integer)" // min_qod + " RETURNS double precision AS $$" + /* Calculate the severity of a task. */ + " SELECT CASE" + " WHEN (SELECT target = 0 " + " AND target_type = 0 " // TASKS_TARGET_TYPE_IMPORT_TASK + " FROM tasks WHERE id = $1)" + " THEN CAST (NULL AS double precision)" + " ELSE" + " (SELECT report_severity ((SELECT id FROM reports" + " WHERE task = $1" + " AND scan_run_status = %u" + " ORDER BY creation_time DESC" + " LIMIT 1 OFFSET 0), $2, $3))" + " END;" + "$$ LANGUAGE SQL;", + TASK_STATUS_DONE); + } /* column web_application_target in table task was added in version 277 */ - if (current_db_version >= 277) + else if (current_db_version >= 277) { sql ("CREATE OR REPLACE FUNCTION task_severity (integer," // task " integer," // overrides @@ -1531,7 +1579,139 @@ manage_create_sql_functions () TASK_STATUS_DONE); } - sql ("CREATE OR REPLACE FUNCTION task_trend (integer, integer, integer)" + if (current_db_version >= 281) + { + sql ("CREATE OR REPLACE FUNCTION task_trend (integer, integer, integer)" + " RETURNS text AS $$" + /* Calculate the trend of a task. */ + " DECLARE" + " last_report integer;" + " second_last_report integer;" + " severity_a double precision;" + " severity_b double precision;" + " critical_a bigint;" + " critical_b bigint;" + " high_a bigint;" + " high_b bigint;" + " medium_a bigint;" + " medium_b bigint;" + " low_a bigint;" + " low_b bigint;" + " threat_a integer;" + " threat_b integer;" + " BEGIN" + " CASE" + /* Ensure there are enough reports. */ + " WHEN (SELECT count(*) <= 1 FROM reports" + " WHERE task = $1" + " AND scan_run_status = %u)" + " THEN RETURN ''::text;" + /* Get trend only for authenticated users. */ + " WHEN gvmd_user () = 0" + " THEN RETURN ''::text;" + /* Skip running and import tasks. */ + " WHEN (SELECT run_status = %u OR (target = 0 OR target_type = 0)" // TASKS_TARGET_TYPE_IMPORT_TASK + " FROM tasks WHERE id = $1)" + " THEN RETURN ''::text;" + " ELSE" + " END CASE;" + /* Check if the severity score changed. */ + " last_report := task_last_report ($1);" + " second_last_report := task_second_last_report ($1);" + " severity_a := report_severity (last_report, $2, $3);" + " severity_b := report_severity (second_last_report, $2, $3);" + " IF severity_a > severity_b THEN" + " RETURN 'up'::text;" + " ELSIF severity_b > severity_a THEN" + " RETURN 'down'::text;" + " END IF;" + /* Calculate trend. */ + " critical_a := report_severity_count (last_report, $2, $3," + " 'critical');" + " critical_b := report_severity_count (second_last_report, $2, $3," + " 'critical');" + " high_a := report_severity_count (last_report, $2, $3," + " 'high');" + " high_b := report_severity_count (second_last_report, $2, $3," + " 'high');" + " medium_a := report_severity_count (last_report, $2, $3," + " 'medium');" + " medium_b := report_severity_count (second_last_report, $2, $3," + " 'medium');" + " low_a := report_severity_count (last_report, $2, $3," + " 'low');" + " low_b := report_severity_count (second_last_report, $2, $3," + " 'low');" + " IF critical_a > 0 THEN" + " threat_a := 5;" + " ELSEIF high_a > 0 THEN" + " threat_a := 4;" + " ELSIF medium_a > 0 THEN" + " threat_a := 3;" + " ELSIF low_a > 0 THEN" + " threat_a := 2;" + " ELSE" + " threat_a := 1;" + " END IF;" + " IF critical_b > 0 THEN" + " threat_b := 5;" + " ELSEIF high_b > 0 THEN" + " threat_b := 4;" + " ELSIF medium_b > 0 THEN" + " threat_b := 3;" + " ELSIF low_b > 0 THEN" + " threat_b := 2;" + " ELSE" + " threat_b := 1;" + " END IF;" + /* Check if the threat level changed. */ + " IF threat_a > threat_b THEN" + " RETURN 'up'::text;" + " ELSIF threat_b > threat_a THEN" + " RETURN 'down'::text;" + " END IF;" + /* Check if the threat count changed. */ + " IF critical_a > 0 THEN" + " IF critical_a > critical_b THEN" + " RETURN 'more'::text;" + " ELSIF critical_a < critical_b THEN" + " RETURN 'less'::text;" + " END IF;" + " RETURN 'same'::text;" + " END IF;" + " IF high_a > 0 THEN" + " IF high_a > high_b THEN" + " RETURN 'more'::text;" + " ELSIF high_a < high_b THEN" + " RETURN 'less'::text;" + " END IF;" + " RETURN 'same'::text;" + " END IF;" + " IF medium_a > 0 THEN" + " IF medium_a > medium_b THEN" + " RETURN 'more'::text;" + " ELSIF medium_a < medium_b THEN" + " RETURN 'less'::text;" + " END IF;" + " RETURN 'same'::text;" + " END IF;" + " IF low_a > 0 THEN" + " IF low_a > low_b THEN" + " RETURN 'more'::text;" + " ELSIF low_a < low_b THEN" + " RETURN 'less'::text;" + " END IF;" + " RETURN 'same'::text;" + " END IF;" + " RETURN 'same'::text;" + " END;" + "$$ LANGUAGE plpgsql;", + TASK_STATUS_DONE, + TASK_STATUS_RUNNING); + } + else + { + sql ("CREATE OR REPLACE FUNCTION task_trend (integer, integer, integer)" " RETURNS text AS $$" /* Calculate the trend of a task. */ " DECLARE" @@ -1658,6 +1838,7 @@ manage_create_sql_functions () "$$ LANGUAGE plpgsql;", TASK_STATUS_DONE, TASK_STATUS_RUNNING); + } } sql ("CREATE OR REPLACE FUNCTION run_status_name (integer)" diff --git a/src/manage_sql.c b/src/manage_sql.c index 96710a9f97..09045c778e 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -66,6 +66,7 @@ #include "manage_sql_tickets.h" #include "manage_sql_tls_certificates.h" #include "manage_sql_users.h" +#include "manage_tasks.h" #include "manage_acl.h" #include "manage_commands.h" #include "manage_authentication.h" @@ -5924,17 +5925,21 @@ set_task_config (task_t task, config_t config) /** * @brief Return the target of a task. * - * @param[in] task Task. + * @param[in] task Task. + * @param[in] target_type Target type of task. * * @return Target of task. */ target_t -task_target (task_t task) +task_target (task_t task, tasks_target_type_t target_type) { target_t target = 0; - switch (sql_int64 (&target, - "SELECT target FROM tasks WHERE id = %llu;", - task)) + switch (sql_int64_ps (&target, + "SELECT target FROM tasks WHERE id = $1" + " AND target_type = $2;", + SQL_RESOURCE_PARAM (task), + SQL_INT_PARAM (target_type), + NULL)) { case 0: return target; @@ -5948,19 +5953,110 @@ task_target (task_t task) } } +tasks_target_type_t +task_target_type (task_t task) +{ + // sql_int_ps does not support return value through parameter + resource_t target_type = 0; + switch (sql_int64_ps (&target_type, + "SELECT target_type FROM tasks WHERE id = $1;", + SQL_RESOURCE_PARAM (task), NULL)) + { + case 0: + return (tasks_target_type_t)target_type; + break; + case 1: /* Too few rows in result of query. */ + default: /* Programming error. */ + assert (0); + case -1: + return 0; + break; + } +} + /** * @brief Set the target of a task. * - * @param[in] task Task. - * @param[in] target Target. + * @param[in] task Task. + * @param[in] target Target. + * @param[in] target_type The type of the target */ void -set_task_target (task_t task, target_t target) +set_task_target (task_t task, target_t target, tasks_target_type_t target_type) { - sql ("UPDATE tasks SET target = %llu, modification_time = m_now ()" - " WHERE id = %llu;", - target, - task); + sql_ps ("UPDATE tasks SET target = $1, target_type = $2," + " modification_time = m_now ()" + " WHERE id = $3;", + SQL_RESOURCE_PARAM (target), + SQL_INT_PARAM (target_type), + SQL_RESOURCE_PARAM (task), + NULL); +} + +/** + * @brief Set the target of a task, also updating the agent group location. + * + * @param[in] task Task. + * @param[in] target Target. + * @param[in] target_type The type of the target + */ +void +set_task_target_and_location (task_t task, target_t target, + tasks_target_type_t target_type) +{ + sql_ps ("UPDATE tasks SET target = $1, target_type = $2, target_location = 0" + " modification_time = m_now ()" + " WHERE id = $3;", + SQL_RESOURCE_PARAM (target), + SQL_INT_PARAM (target_type), + SQL_RESOURCE_PARAM (task), + NULL); +} + +/** + * @brief Update a task after a target was deleted. + * + * @param[in] trash_id The new ID of the target in trash. + * @param[in] resource_id The old ID of the target in the table. + * @param[in] target_type The type of the target being deleted + */ +void +task_update_delete_target (target_t trash_id, target_t resource_id, + tasks_target_type_t target_type) +{ + sql_ps ("UPDATE tasks" + " SET target = $1," + " target_location = " G_STRINGIFY (LOCATION_TRASH) + " WHERE target = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TABLE) + " AND target_type = $3;", + SQL_RESOURCE_PARAM (trash_id), + SQL_RESOURCE_PARAM (resource_id), + SQL_INT_PARAM (target_type), + NULL); +} + +/** + * @brief Update a task after a target was restored. + * + * @param[in] restored_id The new ID of the target in the table. + * @param[in] trash_id The old ID of the target in trash. + * @param[in] target_type The type of the target being restored + */ +void +task_update_restore_target (target_t restored_id, target_t trash_id, + tasks_target_type_t target_type) +{ + sql_ps ("UPDATE tasks" + " SET target = $1," + " target_location = " G_STRINGIFY (LOCATION_TABLE) + " WHERE target = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TRASH) + " AND target_type = $3;", + SQL_RESOURCE_PARAM (restored_id), + SQL_RESOURCE_PARAM (trash_id), + SQL_INT_PARAM (target_type), + NULL); } #if ENABLE_AGENTS @@ -5973,11 +6069,8 @@ set_task_target (task_t task, target_t target) void set_task_agent_group_and_location (task_t task, agent_group_t agent_group) { - sql ("UPDATE tasks SET agent_group = %llu, agent_group_location = 0," - " modification_time = m_now ()" - " WHERE id = %llu;", - agent_group, - task); + set_task_target_and_location (task, agent_group, + TASKS_TARGET_TYPE_AGENT_GROUP); } /** @@ -5990,14 +6083,15 @@ set_task_agent_group_and_location (task_t task, agent_group_t agent_group) int agent_group_tasks_exist_by_scanner (scanner_t scanner) { - return !!sql_int ( + return !!sql_int_ps ( "SELECT COUNT(*) FROM tasks" - " WHERE agent_group IN (" - " SELECT id FROM agent_groups WHERE scanner = %llu)" - " AND agent_group_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - scanner); + " WHERE target IN (" + " SELECT id FROM agent_groups WHERE scanner = $1)" + " AND target_type = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TABLE) " AND hidden = 0;", + SQL_RESOURCE_PARAM (scanner), + SQL_INT_PARAM (TASKS_TARGET_TYPE_AGENT_GROUP), + NULL); } /** * @brief Return whether any hidden task exists for agent groups of a scanner. @@ -6009,19 +6103,24 @@ agent_group_tasks_exist_by_scanner (scanner_t scanner) int agent_group_hidden_tasks_exist_by_scanner (scanner_t scanner) { - return !!sql_int ( + return !!sql_int_ps ( "SELECT COUNT(*) FROM tasks " "WHERE hidden != 0 " + " AND target_type = $1" " AND (" - " (agent_group_location = %d" - " AND agent_group IN (" - " SELECT id FROM agent_groups WHERE scanner = %llu ))" + " (target_location = $2" + " AND target IN (" + " SELECT id FROM agent_groups WHERE scanner = $3 ))" " OR " - " (agent_group_location = %d " - " AND agent_group IN (" - " SELECT id FROM agent_groups_trash WHERE scanner = %llu ))" + " (target_location = $4 " + " AND target IN (" + " SELECT id FROM agent_groups_trash WHERE scanner = $3 ))" ");", - LOCATION_TABLE, scanner, LOCATION_TRASH, scanner); + SQL_INT_PARAM (TASKS_TARGET_TYPE_AGENT_GROUP), + SQL_INT_PARAM (LOCATION_TABLE), + SQL_RESOURCE_PARAM (scanner), + SQL_INT_PARAM (LOCATION_TRASH), + NULL); } /** @@ -6034,21 +6133,7 @@ agent_group_hidden_tasks_exist_by_scanner (scanner_t scanner) agent_group_t task_agent_group (task_t task) { - agent_group_t agent_group = 0; - switch (sql_int64 (&agent_group, - "SELECT agent_group FROM tasks WHERE id = %llu;", - task)) - { - case 0: - return agent_group; - break; - case 1: /* Too few rows in result of query. */ - default: /* Programming error. */ - assert (0); - case -1: - return 0; - break; - } + return task_target (task, TASKS_TARGET_TYPE_AGENT_GROUP); } /** @@ -6061,10 +6146,7 @@ task_agent_group (task_t task) int task_agent_group_in_trash (task_t task) { - return sql_int ("SELECT agent_group_location = " - G_STRINGIFY (LOCATION_TRASH) - " FROM tasks WHERE id = %llu;", - task); + return task_target_in_trash (task, TASKS_TARGET_TYPE_AGENT_GROUP); } #endif /*ENABLE_AGENTS*/ @@ -6079,21 +6161,7 @@ task_agent_group_in_trash (task_t task) oci_image_target_t task_oci_image_target (task_t task) { - oci_image_target_t oci_image_target = 0; - switch (sql_int64 (&oci_image_target, - "SELECT oci_image_target FROM tasks WHERE id = %llu;", - task)) - { - case 0: - return oci_image_target; - break; - case 1: /* Too few rows in result of query. */ - default: /* Programming error. */ - assert (0); - case -1: - return 0; - break; - } + return task_target (task, TASKS_TARGET_TYPE_OCI_IMAGE); } /** @@ -6106,27 +6174,20 @@ task_oci_image_target (task_t task) int task_oci_image_target_in_trash (task_t task) { - return sql_int ("SELECT oci_image_target_location = " - G_STRINGIFY (LOCATION_TRASH) - " FROM tasks WHERE id = %llu;", - task); + return task_target_in_trash (task, TASKS_TARGET_TYPE_OCI_IMAGE); } /** * @brief Set the OCI image target of a task. * - * @param[in] task Task. - * @param[in] target Target. + * @param[in] task Task. + * @param[in] oci_image_target Target. */ void set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) { - sql ("UPDATE tasks SET oci_image_target = %llu," - " oci_image_target_location = 0," - " modification_time = m_now ()" - " WHERE id = %llu;", - oci_image_target, - task); + set_task_target_and_location (task, oci_image_target, + TASKS_TARGET_TYPE_OCI_IMAGE); } #endif @@ -6142,13 +6203,7 @@ set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) web_application_target_t task_web_application_target (task_t task) { - web_application_target_t web_application_target = 0; - sql_int64_ps (&web_application_target, - "SELECT web_application_target FROM tasks" - " WHERE id = $1;", - SQL_RESOURCE_PARAM(task), - NULL); - return web_application_target; + return task_target (task, TASKS_TARGET_TYPE_WEB_APPLICATION); } /** @@ -6161,29 +6216,20 @@ task_web_application_target (task_t task) int task_web_application_target_in_trash (task_t task) { - return sql_int_ps ("SELECT web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) - " FROM tasks WHERE id = $1;", - SQL_RESOURCE_PARAM(task), - NULL); + return task_target_in_trash (task, TASKS_TARGET_TYPE_WEB_APPLICATION); } /** * @brief Set the web application target of a task. * - * @param[in] task Task. - * @param[in] target Target. + * @param[in] task Task. + * @param[in] web_application_target Target. */ void set_task_web_application_target (task_t task, web_application_target_t web_application_target) { - sql_ps ("UPDATE tasks SET web_application_target = $1," - " web_application_target_location = 0," - " modification_time = m_now ()" - " WHERE id = $2;", - SQL_RESOURCE_PARAM(web_application_target), - SQL_RESOURCE_PARAM(task), - NULL); + set_task_target_and_location (task, web_application_target, + TASKS_TARGET_TYPE_WEB_APPLICATION); } #endif @@ -6216,16 +6262,20 @@ clear_task_asset_preferences (task_t task) /** * @brief Return whether the target of a task is in the trashcan. * - * @param[in] task Task. + * @param[in] task Task. + * @param[in] target_type Type of the target * * @return 1 if in trash, else 0. */ int -task_target_in_trash (task_t task) +task_target_in_trash (task_t task, tasks_target_type_t target_type) { - return sql_int ("SELECT target_location = " G_STRINGIFY (LOCATION_TRASH) - " FROM tasks WHERE id = %llu;", - task); + return sql_int_ps ("SELECT target_location = " G_STRINGIFY (LOCATION_TRASH) + " FROM tasks WHERE id = $1" + " AND target_type = $2;", + SQL_RESOURCE_PARAM (task), + SQL_INT_PARAM (target_type), + NULL); } /** @@ -18206,10 +18256,10 @@ make_task (char* name, char* comment, int in_assets, int event) sql ("INSERT into tasks" " (owner, uuid, name, hidden, comment, schedule," " schedule_next_time, config_location, target, target_location," - " scanner_location, schedule_location, alterable," + " target_type, scanner_location, schedule_location, alterable," " creation_time, modification_time, usage_type)" " VALUES ((SELECT id FROM users WHERE users.uuid = '%s')," - " '%s', '%s', 0, '%s', 0, 0, 0, 0, 0, 0, 0, 0, m_now ()," + " '%s', '%s', 0, '%s', 0, 0, 0, 0, 0, 0, 0, 0, 0, m_now ()," " m_now (), 'scan');", current_credentials.uuid, uuid, @@ -18317,17 +18367,12 @@ copy_task (const char* name, const char* comment, const char *task_id, sql_begin_immediate (); ret = copy_resource_lock ("task", name, comment, task_id, - "config, target, oci_image_target," - " web_application_target," + "config, target, target_type," " schedule, schedule_periods," " scanner, schedule_next_time," " config_location, target_location," " schedule_location, scanner_location," - " oci_image_target_location," - " web_application_target_location," - " usage_type," - " agent_group, agent_group_location," - " alterable", + " usage_type, alterable", 1, &new, &old); if (ret) { From 1eebe8cab0b7ef8a59cecb95d52eb1ab7fc16401 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 6 Jul 2026 14:27:56 +0200 Subject: [PATCH 12/66] fixed function calls: too few arguments; due to migration --- src/gmp.c | 10 ++++++---- src/manage.c | 6 +++--- src/manage_scan_handler.c | 4 ++-- src/manage_sql.c | 14 +++++++------- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index 3e0a150eb4..a36ee8e35e 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -20050,7 +20050,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) } index = get_iterator_resource (&tasks); - target = task_target (index); + target = task_target (index, TASKS_TARGET_TYPE_REGULAR); task_schedule_xml = get_task_schedule_xml (index); @@ -20069,7 +20069,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) else { SEND_GET_COMMON (task, &get_tasks_data->get, &tasks); - target_in_trash = task_target_in_trash (index); + target_in_trash = task_target_in_trash (index, TASKS_TARGET_TYPE_REGULAR); if (target && (target == 0) && (task_iterator_run_status (&tasks) == TASK_STATUS_RUNNING)) @@ -25887,7 +25887,8 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context, { /* Import task. */ - set_task_target (create_task_data->task, 0); + set_task_target (create_task_data->task, 0, + TASKS_TARGET_TYPE_IMPORT_TASK); set_task_usage_type (create_task_data->task, create_task_data->usage_type); SENDF_TO_CLIENT_OR_FAIL (XML_OK_CREATED_ID ("create_task"), @@ -26060,7 +26061,8 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context, error_send_to_client (error); goto create_task_fail; } - set_task_target (create_task_data->task, target); + set_task_target (create_task_data->task, target, + TASKS_TARGET_TYPE_REGULAR); } set_task_scanner (create_task_data->task, scanner); diff --git a/src/manage.c b/src/manage.c index 3786492588..7dc5e6f1e8 100644 --- a/src/manage.c +++ b/src/manage.c @@ -1707,7 +1707,7 @@ run_osp_task (task_t task, int from, char **report_id) { target_t target; - target = task_target (task); + target = task_target (task, TASKS_TARGET_TYPE_REGULAR); if (target) { char *uuid; @@ -1917,7 +1917,7 @@ run_cve_task (task_t task) { target_t target; - target = task_target (task); + target = task_target (task, TASKS_TARGET_TYPE_REGULAR); if (target) { char *uuid; @@ -7095,7 +7095,7 @@ run_openvasd_task (task_t task, int from, char **report_id) } target_t target; - target = task_target (task); + target = task_target (task, TASKS_TARGET_TYPE_REGULAR); if (target) { char *uuid; diff --git a/src/manage_scan_handler.c b/src/manage_scan_handler.c index 74c6b37a8c..ca07fb38e0 100644 --- a/src/manage_scan_handler.c +++ b/src/manage_scan_handler.c @@ -51,7 +51,7 @@ handle_queued_osp_scan (const char *scan_id, report_t report, case TASK_STATUS_REQUESTED: { int rc; - target_t target = task_target (task); + target_t target = task_target (task, TASKS_TARGET_TYPE_REGULAR); rc = handle_osp_scan_start (task, target, scan_id, start_from, TRUE, &discovery_scan); /* Set discovery flag to the report */ @@ -94,7 +94,7 @@ handle_queued_openvasd_scan (const char *scan_id, report_t report, case TASK_STATUS_REQUESTED: { int rc; - target_t target = task_target (task); + target_t target = task_target (task, TASKS_TARGET_TYPE_REGULAR); rc = handle_openvasd_scan_start (task, target, scan_id, start_from, TRUE, &discovery_scan); /* Set discovery flag to the report */ diff --git a/src/manage_sql.c b/src/manage_sql.c index 09045c778e..9e94a1f494 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -7298,7 +7298,7 @@ task_severity_double (task_t task, int overrides, int min_qod, int offset) report_t report; if (current_credentials.uuid == NULL - || task_target (task) == 0 /* import task. */) + || task_target_type (task) == TASKS_TARGET_TYPE_REGULAR/* import task. */) return SEVERITY_MISSING; report = sql_int64_0 ("SELECT id FROM reports" @@ -9501,7 +9501,7 @@ create_report (array_t *results, const char *task_id, const char *in_assets, rc = -1; else if (task == 0) rc = -4; - else if (task_target (task)) + else if (task_target (task, TASKS_TARGET_TYPE_REGULAR)) rc = -5; if (rc) { @@ -16312,8 +16312,8 @@ print_report_xml_start (report_t report, report_t delta, task_t task, comment = task_comment (task); - target = task_target (task); - if (task_target_in_trash (task)) + target = task_target (task, TASKS_TARGET_TYPE_REGULAR); + if (task_target_in_trash (task, TASKS_TARGET_TYPE_REGULAR)) { task_target_uuid = trash_target_uuid (target); task_target_name = trash_target_name (target); @@ -16351,7 +16351,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task, tsk_name ? tsk_name : "", comment ? comment : "", task_target_uuid ? task_target_uuid : "", - task_target_in_trash (task), + task_target_in_trash (task, TASKS_TARGET_TYPE_REGULAR), task_target_name ? task_target_name : "", task_target_comment ? task_target_comment : ""); @@ -19132,7 +19132,7 @@ modify_task (const gchar *task_id, const gchar *name, return MODIFY_TASK_NOT_FOUND; - if ((task_target (task) == 0 + if ((task_target_type (task) == TASKS_TARGET_TYPE_IMPORT_TASK && (agent_group_id == NULL) && (oci_image_target_id == NULL) && (web_application_target_id == NULL)) @@ -19306,7 +19306,7 @@ modify_task (const gchar *task_id, const gchar *name, else if (target == 0) return MODIFY_TASK_TARGET_NOT_FOUND; else - set_task_target (task, target); + set_task_target (task, target, TASKS_TARGET_TYPE_REGULAR); } #if ENABLE_AGENTS if (agent_group_id) { From 877a5518fc48d7c1036bb57486a50bd5ac7b97a2 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 6 Jul 2026 14:35:09 +0200 Subject: [PATCH 13/66] added `target_type` to propopsed update functions --- docs/tasks-target-type.md | 44 +++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md index 1cd43ad79a..a2359deb5c 100644 --- a/docs/tasks-target-type.md +++ b/docs/tasks-target-type.md @@ -165,27 +165,35 @@ in the tasks table. Since we are removing the designated columns for each target we can generalize these functions. ```c++ void -task_update_delete_target (int resource_id, int trash_id) { - sql_ps ("UPDATE tasks" - " SET target = $1," - " target_location = " G_STRINGIFY (LOCATION_TRASH) - " WHERE target = $2" - " AND target_location = " G_STRINGIFY (LOCATION_TABLE) ";", - SQL_RESOURCE_PARAM (trash_id), - SQL_RESOURCE_PARAM (resource_id), - NULL); +task_update_delete_target (target_t trash_id, target_t resource_id, + tasks_target_type_t target_type) +{ + sql_ps ("UPDATE tasks" + " SET target = $1," + " target_location = " G_STRINGIFY (LOCATION_TRASH) + " WHERE target = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TABLE) + " AND target_type = $3;", + SQL_RESOURCE_PARAM (trash_id), + SQL_RESOURCE_PARAM (resource_id), + SQL_INT_PARAM (target_type), + NULL); } void -task_update_restore_target (int trash_id, int restored_id) { - sql_ps ("UPDATE tasks" - " SET target = $1," - " target_location = " G_STRINGIFY (LOCATION_TABLE) - " WHERE target = $2" - " AND target_location = " G_STRINGIFY (LOCATION_TRASH) ";", - SQL_RESOURCE_PARAM (restored_id), - SQL_RESOURCE_PARAM (trash_id), - NULL); +task_update_restore_target (target_t restored_id, target_t trash_id, + tasks_target_type_t target_type) +{ + sql_ps ("UPDATE tasks" + " SET target = $1," + " target_location = " G_STRINGIFY (LOCATION_TABLE) + " WHERE target = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TRASH) + " AND target_type = $3;", + SQL_RESOURCE_PARAM (restored_id), + SQL_RESOURCE_PARAM (trash_id), + SQL_INT_PARAM (target_type), + NULL); } ``` From ec725a013e4e50548b8e13dcccaa033975fddb5a Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 6 Jul 2026 14:39:21 +0200 Subject: [PATCH 14/66] removed "drop column" part of database migration to version 281 --- src/manage_migrators.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index e42843041a..343f3713fc 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4325,14 +4325,18 @@ migrate_281_to_282 () } cleanup_iterator (&tasks); + // TODO: This step will be moved to a later migration. + // For safety reasons, we decided to keep the columns for now and only + // drop them after a certain period, when multiple customers have + // already migrated without any issues. /* Drop old, unused target columns */ - sql ("ALTER TABLE tasks DROP COLUMN agent_group," - " DROP COLUMN agent_group_location," - " DROP COLUMN oci_image_target," - " DROP COLUMN oci_image_target_location," - " DROP COLUMN web_application_target," - " DROP COLUMN web_application_target_location"); - + // sql ("ALTER TABLE tasks DROP COLUMN IF EXISTS agent_group," + // " DROP COLUMN IF EXISTS agent_group_location," + // " DROP COLUMN IF EXISTS oci_image_target," + // " DROP COLUMN IF EXISTS oci_image_target_location," + // " DROP COLUMN IF EXISTS web_application_target," + // " DROP COLUMN IF EXISTS web_application_target_location"); + // /* Set the database version to 281 */ set_db_version (281); From 274374bab36b1fa7f5208b7d976221f2a1a0dc4e Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 7 Jul 2026 09:07:06 +0200 Subject: [PATCH 15/66] updated sql filters to check `target_type` for import task --- src/manage_sql_filters.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/manage_sql_filters.c b/src/manage_sql_filters.c index 5f1a3ab886..93345926b8 100644 --- a/src/manage_sql_filters.c +++ b/src/manage_sql_filters.c @@ -1356,11 +1356,7 @@ filter_clause (const char* type, const char* filter, (order, " ORDER BY" " (CASE WHEN (SELECT " - " ( " - " target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " + " ( target_type = 0 ) " //TASKS_TARGET_TYPE_IMPORT_TASK " FROM tasks" " WHERE tasks.id = task)" " THEN '0'" @@ -1377,10 +1373,7 @@ filter_clause (const char* type, const char* filter, g_string_append_printf (order, " ORDER BY" - " (CASE WHEN ( target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " + " (CASE WHEN ( target_type = 0 ) " //TASKS_TARGET_TYPE_IMPORT_TASK " THEN '0'" " ELSE run_status_name (run_status)" " || (SELECT CAST (temp / 100 AS text)" @@ -1561,11 +1554,7 @@ filter_clause (const char* type, const char* filter, (order, " ORDER BY" " (CASE WHEN (SELECT " - " ( " - " target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " + " ( target_type = 0 ) " //TASKS_TARGET_TYPE_IMPORT_TASK " FROM tasks" " WHERE tasks.id = task)" " THEN '0'" @@ -1582,10 +1571,7 @@ filter_clause (const char* type, const char* filter, g_string_append_printf (order, " ORDER BY" - " (CASE WHEN ( target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " + " (CASE WHEN ( target_type = 0 ) " //TASKS_TARGET_TYPE_IMPORT_TASK " THEN '0'" " ELSE run_status_name (run_status)" " || (SELECT CAST (temp / 100 AS text)" From df1d49f88aa0b0f6b35ee5614cb358f4a3cafbfa Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 7 Jul 2026 09:24:46 +0200 Subject: [PATCH 16/66] updated parameter order in task_update_delete_target() function --- docs/tasks-target-type.md | 3 +-- src/manage_sql.c | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md index a2359deb5c..7f6cc0d640 100644 --- a/docs/tasks-target-type.md +++ b/docs/tasks-target-type.md @@ -165,7 +165,7 @@ in the tasks table. Since we are removing the designated columns for each target we can generalize these functions. ```c++ void -task_update_delete_target (target_t trash_id, target_t resource_id, +task_update_delete_target (target_t resource_id, target_t trash_id, tasks_target_type_t target_type) { sql_ps ("UPDATE tasks" @@ -179,7 +179,6 @@ task_update_delete_target (target_t trash_id, target_t resource_id, SQL_INT_PARAM (target_type), NULL); } - void task_update_restore_target (target_t restored_id, target_t trash_id, tasks_target_type_t target_type) diff --git a/src/manage_sql.c b/src/manage_sql.c index 9e94a1f494..6dc158c7a8 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -6016,12 +6016,12 @@ set_task_target_and_location (task_t task, target_t target, /** * @brief Update a task after a target was deleted. * - * @param[in] trash_id The new ID of the target in trash. * @param[in] resource_id The old ID of the target in the table. + * @param[in] trash_id The new ID of the target in trash. * @param[in] target_type The type of the target being deleted */ void -task_update_delete_target (target_t trash_id, target_t resource_id, +task_update_delete_target (target_t resource_id, target_t trash_id, tasks_target_type_t target_type) { sql_ps ("UPDATE tasks" From 45470be4fd596058bc871773021ff03643428315 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 7 Jul 2026 11:03:05 +0200 Subject: [PATCH 17/66] generalized task_target_in_use() functionality --- src/manage.h | 6 +++++ src/manage_sql.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/manage.h b/src/manage.h index 5850a09cdd..b8fcf11613 100644 --- a/src/manage.h +++ b/src/manage.h @@ -685,6 +685,12 @@ task_update_delete_target (target_t, target_t, tasks_target_type_t); void task_update_restore_target (target_t, target_t, tasks_target_type_t); +int task_target_in_use (target_t, tasks_target_type_t); + +int task_target_in_use_including_hidden (target_t, tasks_target_type_t); + +int task_trash_target_in_use (target_t, tasks_target_type_t); + #if ENABLE_AGENTS void set_task_agent_group_and_location (task_t task, agent_group_t agent_group); diff --git a/src/manage_sql.c b/src/manage_sql.c index 6dc158c7a8..8d24e28669 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -6059,6 +6059,72 @@ task_update_restore_target (target_t restored_id, target_t trash_id, NULL); } +/** + * @brief Return whether a target is in use by a task. + * + * @param[in] target Target. + * @param[in] target_type Type of target + * + * @return 1 if in use, else 0. + */ +int +task_target_in_use (target_t target, tasks_target_type_t target_type) +{ + return !!sql_int_ps ( + "SELECT count(*) FROM tasks" + " WHERE target = $1" + " AND target_type = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TABLE) + " AND hidden = 0;", + SQL_RESOURCE_PARAM (target), + SQL_INT_PARAM (target_type), + NULL); +} + +/** + * @brief Return whether a target is in use by a task, + * even if the task is hidden. + * + * @param[in] target Target. + * @param[in] target_type Type of target + * + * @return 1 if in use, else 0. + */ +int +task_target_in_use_including_hidden (target_t target, + tasks_target_type_t target_type) +{ + return !!sql_int_ps ( + "SELECT count(*) FROM tasks" + " WHERE target = $1" + " AND target_type = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TABLE) ";", + SQL_RESOURCE_PARAM (target), + SQL_INT_PARAM (target_type), + NULL); +} + +/** + * @brief Return whether a trashcan target is referenced by a task. + * + * @param[in] target Target. + * @param[in] target_type Type of target + * + * @return 1 if in use, else 0. + */ +int +task_trash_target_in_use (target_t target, tasks_target_type_t target_type) +{ + return !!sql_int_ps ( + "SELECT count(*) FROM tasks" + " WHERE target = $1" + " AND target_type = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TRASH) ";", + SQL_RESOURCE_PARAM (target), + SQL_INT_PARAM (target_type), + NULL); +} + #if ENABLE_AGENTS /** * @brief Set the agent group of a task, also updating the agent group location. From 5fcaa014769e2d38842ab0994e74452b9a3940f5 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 7 Jul 2026 14:59:12 +0200 Subject: [PATCH 18/66] adapted oci_image_targets functionality to new column `target_types` in tasks table --- src/manage_sql_oci_image_targets.c | 60 ++++++++---------------------- 1 file changed, 15 insertions(+), 45 deletions(-) diff --git a/src/manage_sql_oci_image_targets.c b/src/manage_sql_oci_image_targets.c index 5d124a4a8a..d579d959fb 100644 --- a/src/manage_sql_oci_image_targets.c +++ b/src/manage_sql_oci_image_targets.c @@ -424,11 +424,7 @@ delete_oci_image_target (const char *oci_image_target_id, int ultimate) } /* Check if it's in use by a task in the trashcan. */ - if (sql_int ("SELECT count(*) FROM tasks" - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TRASH) ";", - oci_image_target)) + if (trash_oci_image_target_in_use (oci_image_target)) { sql_rollback (); return 1; @@ -448,12 +444,7 @@ delete_oci_image_target (const char *oci_image_target_id, int ultimate) if (ultimate == 0) { - if (sql_int ("SELECT count(*) FROM tasks" - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - oci_image_target)) + if (oci_image_target_in_use (oci_image_target)) { sql_rollback (); return 1; @@ -470,15 +461,8 @@ delete_oci_image_target (const char *oci_image_target_id, int ultimate) oci_image_target); /* Update the location of the target in any task. */ - sql ("UPDATE tasks" - " SET oci_image_target = %llu," - " oci_image_target_location = " - G_STRINGIFY (LOCATION_TRASH) - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TABLE) ";", - sql_last_insert_id (), - oci_image_target); + task_update_delete_target (oci_image_target, sql_last_insert_id (), + TASKS_TARGET_TYPE_OCI_IMAGE); permissions_set_locations ("oci_image_target", oci_image_target, sql_last_insert_id (), @@ -487,11 +471,8 @@ delete_oci_image_target (const char *oci_image_target_id, int ultimate) sql_last_insert_id (), LOCATION_TRASH); } - else if (sql_int ("SELECT count(*) FROM tasks" - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TABLE), - oci_image_target)) + else if (task_target_in_use_including_hidden (oci_image_target, + TASKS_TARGET_TYPE_OCI_IMAGE)) { sql_rollback (); return 1; @@ -578,13 +559,8 @@ restore_oci_image_target (const char *oci_image_target_id) oci_image_target = sql_last_insert_id (); /* Update the oci image target in any tasks. */ - sql ("UPDATE tasks" - " SET oci_image_target = %llu," - " oci_image_target_location = " G_STRINGIFY (LOCATION_TABLE) - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " G_STRINGIFY (LOCATION_TRASH), - oci_image_target, - resource); + task_update_restore_target (oci_image_target, resource, + TASKS_TARGET_TYPE_OCI_IMAGE); permissions_set_locations ("oci_image_target", resource, oci_image_target, LOCATION_TABLE); @@ -906,12 +882,7 @@ trash_oci_image_target_readable (oci_image_target_t oci_image_target) int oci_image_target_in_use (oci_image_target_t oci_image_target) { - return !!sql_int ("SELECT count(*) FROM tasks" - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - oci_image_target); + return task_target_in_use (oci_image_target, TASKS_TARGET_TYPE_OCI_IMAGE); } /** @@ -924,11 +895,8 @@ oci_image_target_in_use (oci_image_target_t oci_image_target) int trash_oci_image_target_in_use (oci_image_target_t oci_image_target) { - return !!sql_int ("SELECT count(*) FROM tasks" - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TRASH), - oci_image_target); + return task_trash_target_in_use (oci_image_target, + TASKS_TARGET_TYPE_OCI_IMAGE); } /** @@ -979,12 +947,14 @@ init_oci_image_target_task_iterator (iterator_t* iterator, init_iterator (iterator, "%s" " SELECT name, uuid, %s FROM tasks" - " WHERE oci_image_target = %llu" + " WHERE target = %llu" + " AND target_type = %d" " AND hidden = 0" " ORDER BY name ASC;", with_clause ? with_clause : "", available, - oci_image_target); + oci_image_target, + TASKS_TARGET_TYPE_OCI_IMAGE); g_free (with_clause); g_free (available); From 80fe73a0162da6eee36f764bf2669e15e66f68f7 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Wed, 8 Jul 2026 12:37:09 +0200 Subject: [PATCH 19/66] updated migration document --- docs/tasks-target-type.md | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md index 7f6cc0d640..6c4f7fde73 100644 --- a/docs/tasks-target-type.md +++ b/docs/tasks-target-type.md @@ -160,6 +160,63 @@ WHERE target = $1 AND target_type = {{TARGET_TYPE_WEB_APPLICATION}} ``` +--- + +As of the new `target_type`, all current `(.*)task_target(.*)` functions, which work on "regular" target types +must respect the new `target_type` column. +A benefit of this change is that they now can be used by domain logic that affect other target types. + +One example is the `set_task_target ()` function +```c++ +void +set_task_target (task_t task, target_t target, tasks_target_type_t target_type) +{ + sql_ps ("UPDATE tasks SET target = $1, target_type = $2," + " modification_time = m_now ()" + " WHERE id = $3;", + SQL_RESOURCE_PARAM (target), + SQL_INT_PARAM (target_type), + SQL_RESOURCE_PARAM (task), + NULL); +} +``` + +--- + +Some of the functions used by `oci_image_targets`, `agent_groups` and `web_appliation_targets` can also +be generalized, to avoid duplicate code. + +```c++ +int +task_target_in_use (target_t target, tasks_target_type_t target_type) +{ + return !!sql_int_ps ( + "SELECT count(*) FROM tasks" + " WHERE target = $1" + " AND target_type = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TABLE) + " AND hidden = 0;", + SQL_RESOURCE_PARAM (target), + SQL_INT_PARAM (target_type), + NULL); +} + +int +task_trash_target_in_use (target_t target, tasks_target_type_t target_type) +{ + return !!sql_int_ps ( + "SELECT count(*) FROM tasks" + " WHERE target = $1" + " AND target_type = $2" + " AND target_location = " G_STRINGIFY (LOCATION_TRASH) ";", + SQL_RESOURCE_PARAM (target), + SQL_INT_PARAM (target_type), + NULL); +} +``` + +--- + A group of `delete_... ()` and `restore_... ()` functions updates the respective `target` and `target_location` in the tasks table. Since we are removing the designated columns for each target type, we can generalize these functions. From 7dbe4c6ebb4742c55c7f6e3d288e8b02027fb730 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Wed, 8 Jul 2026 12:38:30 +0200 Subject: [PATCH 20/66] migrated web_application_target to new target_type column --- src/manage_sql_web_application_targets.c | 71 ++++++------------------ 1 file changed, 17 insertions(+), 54 deletions(-) diff --git a/src/manage_sql_web_application_targets.c b/src/manage_sql_web_application_targets.c index 0f0f30a7b4..5a3ca47ee9 100644 --- a/src/manage_sql_web_application_targets.c +++ b/src/manage_sql_web_application_targets.c @@ -462,12 +462,7 @@ delete_web_application_target ( return 0; } - if (sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) ";", - SQL_RESOURCE_PARAM (web_application_target), - NULL)) + if (trash_web_application_target_in_use (web_application_target)) { sql_rollback (); return 1; @@ -494,13 +489,7 @@ delete_web_application_target ( { web_application_target_t trash_web_application_target; - if (sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - SQL_RESOURCE_PARAM (web_application_target), - NULL)) + if (web_application_target_in_use (web_application_target)) { sql_rollback (); return 1; @@ -519,16 +508,9 @@ delete_web_application_target ( trash_web_application_target = sql_last_insert_id (); - sql_ps ("UPDATE tasks" - " SET web_application_target = $1," - " web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) - " WHERE web_application_target = $2" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TABLE) ";", - SQL_RESOURCE_PARAM (trash_web_application_target), - SQL_RESOURCE_PARAM (web_application_target), - NULL); + task_update_delete_target (web_application_target, + trash_web_application_target, + TASKS_TARGET_TYPE_WEB_APPLICATION); permissions_set_locations ("web_application_target", web_application_target, @@ -540,12 +522,8 @@ delete_web_application_target ( trash_web_application_target, LOCATION_TRASH); } - else if (sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TABLE) ";", - SQL_RESOURCE_PARAM (web_application_target), - NULL)) + else if (task_target_in_use_including_hidden ( + web_application_target, TASKS_TARGET_TYPE_WEB_APPLICATION)) { sql_rollback (); return 1; @@ -645,16 +623,8 @@ restore_web_application_target (const char *web_application_target_id) web_application_target = sql_last_insert_id (); - sql_ps ("UPDATE tasks" - " SET web_application_target = $1," - " web_application_target_location = " - G_STRINGIFY (LOCATION_TABLE) - " WHERE web_application_target = $2" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) ";", - SQL_RESOURCE_PARAM (web_application_target), - SQL_RESOURCE_PARAM (resource), - NULL); + task_update_restore_target (web_application_target, resource, + TASKS_TARGET_TYPE_WEB_APPLICATION); permissions_set_locations ("web_application_target", resource, @@ -968,13 +938,8 @@ trash_web_application_target_readable ( int web_application_target_in_use (web_application_target_t web_application_target) { - return !!sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - SQL_RESOURCE_PARAM (web_application_target), - NULL); + return task_target_in_use (web_application_target, + TASKS_TARGET_TYPE_WEB_APPLICATION); } /** @@ -989,12 +954,8 @@ int trash_web_application_target_in_use ( web_application_target_t web_application_target) { - return !!sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) ";", - SQL_RESOURCE_PARAM (web_application_target), - NULL); + return task_trash_target_in_use (web_application_target, + TASKS_TARGET_TYPE_WEB_APPLICATION); } /** @@ -1055,12 +1016,14 @@ init_web_application_target_task_iterator ( init_iterator (iterator, "%s" " SELECT name, uuid, %s FROM tasks" - " WHERE web_application_target = %llu" + " WHERE target = %llu" + " AND target_type = %d" " AND hidden = 0" " ORDER BY name ASC;", with_clause ? with_clause : "", available, - web_application_target); + web_application_target, + TASKS_TARGET_TYPE_WEB_APPLICATION); g_free (with_clause); g_free (available); From dc6b16093a04d9710a52443d0762e1b47bd28ed6 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Wed, 8 Jul 2026 16:52:28 +0200 Subject: [PATCH 21/66] migrated agent_group to new target_type column --- src/manage_sql_agent_groups.c | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/src/manage_sql_agent_groups.c b/src/manage_sql_agent_groups.c index be1cbb510b..6ab79c3551 100644 --- a/src/manage_sql_agent_groups.c +++ b/src/manage_sql_agent_groups.c @@ -650,13 +650,8 @@ delete_agent_group (const char *agent_group_uuid, int ultimate) trash_id, agent_group); /* Update the location of the agent_group in any trashcan tasks. */ - sql ("UPDATE tasks" - " SET agent_group = %llu," - " agent_group_location = " G_STRINGIFY (LOCATION_TRASH) - " WHERE agent_group = %llu" - " AND agent_group_location = " G_STRINGIFY (LOCATION_TABLE) ";", - trash_id, - agent_group); + task_update_delete_target (agent_group, trash_id, + TASKS_TARGET_TYPE_AGENT_GROUP); permissions_set_locations ("agent_group", agent_group, trash_id, LOCATION_TRASH); tags_set_locations ("agent_group", agent_group, trash_id, LOCATION_TRASH); @@ -741,13 +736,8 @@ restore_agent_group (const char *agent_group_uuid) tags_set_locations ("agent_group", trash_id, restored_id, LOCATION_TABLE); /* Update the agent_group in any tasks. */ - sql ("UPDATE tasks" - " SET agent_group = %llu," - " agent_group_location = " G_STRINGIFY (LOCATION_TABLE) - " WHERE agent_group = %llu" - " AND agent_group_location = " G_STRINGIFY (LOCATION_TRASH), - restored_id, - trash_id); + task_update_restore_target (restored_id, trash_id, + TASKS_TARGET_TYPE_AGENT_GROUP); // Clean up trash entries @@ -1031,12 +1021,7 @@ find_agent_group_with_permission (const char *uuid, agent_group_t *agent_group, int agent_group_in_use (agent_group_t agent_group) { - return !!sql_int ("SELECT count(*) FROM tasks" - " WHERE agent_group = %llu" - " AND agent_group_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - agent_group); + return task_target_in_use (agent_group, TASKS_TARGET_TYPE_AGENT_GROUP); } /** @@ -1049,11 +1034,7 @@ agent_group_in_use (agent_group_t agent_group) int trash_agent_group_in_use (agent_group_t agent_group) { - return !!sql_int ("SELECT count(*) FROM tasks" - " WHERE agent_group = %llu" - " AND agent_group_location = " - G_STRINGIFY (LOCATION_TRASH), - agent_group); + return task_trash_target_in_use (agent_group, TASKS_TARGET_TYPE_AGENT_GROUP); } /** From 8af7226ec92390b26045352b9a1307a416d57e71 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Wed, 8 Jul 2026 17:04:43 +0200 Subject: [PATCH 22/66] removed target_type from some functions again, since task_t was used there instead of target_t --- src/gmp.c | 4 ++-- src/manage.c | 6 +++--- src/manage.h | 13 +++++++----- src/manage_scan_handler.c | 5 +++-- src/manage_sql.c | 43 +++++++++++++++++++++------------------ 5 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index a36ee8e35e..53c24a650a 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -20050,7 +20050,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) } index = get_iterator_resource (&tasks); - target = task_target (index, TASKS_TARGET_TYPE_REGULAR); + target = task_target (index); task_schedule_xml = get_task_schedule_xml (index); @@ -20069,7 +20069,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) else { SEND_GET_COMMON (task, &get_tasks_data->get, &tasks); - target_in_trash = task_target_in_trash (index, TASKS_TARGET_TYPE_REGULAR); + target_in_trash = task_target_in_trash (index); if (target && (target == 0) && (task_iterator_run_status (&tasks) == TASK_STATUS_RUNNING)) diff --git a/src/manage.c b/src/manage.c index 7dc5e6f1e8..3786492588 100644 --- a/src/manage.c +++ b/src/manage.c @@ -1707,7 +1707,7 @@ run_osp_task (task_t task, int from, char **report_id) { target_t target; - target = task_target (task, TASKS_TARGET_TYPE_REGULAR); + target = task_target (task); if (target) { char *uuid; @@ -1917,7 +1917,7 @@ run_cve_task (task_t task) { target_t target; - target = task_target (task, TASKS_TARGET_TYPE_REGULAR); + target = task_target (task); if (target) { char *uuid; @@ -7095,7 +7095,7 @@ run_openvasd_task (task_t task, int from, char **report_id) } target_t target; - target = task_target (task, TASKS_TARGET_TYPE_REGULAR); + target = task_target (task); if (target) { char *uuid; diff --git a/src/manage.h b/src/manage.h index b8fcf11613..a9154dd43b 100644 --- a/src/manage.h +++ b/src/manage.h @@ -665,13 +665,13 @@ void set_task_config (task_t, config_t); target_t -task_target (task_t, tasks_target_type_t); +task_target (task_t); tasks_target_type_t task_target_type (task_t); int -task_target_in_trash (task_t, tasks_target_type_t); +task_target_in_trash (task_t); void set_task_target (task_t, target_t, tasks_target_type_t); @@ -685,11 +685,14 @@ task_update_delete_target (target_t, target_t, tasks_target_type_t); void task_update_restore_target (target_t, target_t, tasks_target_type_t); -int task_target_in_use (target_t, tasks_target_type_t); +int +task_target_in_use (target_t, tasks_target_type_t); -int task_target_in_use_including_hidden (target_t, tasks_target_type_t); +int +task_target_in_use_including_hidden (target_t, tasks_target_type_t); -int task_trash_target_in_use (target_t, tasks_target_type_t); +int +task_trash_target_in_use (target_t, tasks_target_type_t); #if ENABLE_AGENTS void diff --git a/src/manage_scan_handler.c b/src/manage_scan_handler.c index ca07fb38e0..136380c15d 100644 --- a/src/manage_scan_handler.c +++ b/src/manage_scan_handler.c @@ -51,7 +51,7 @@ handle_queued_osp_scan (const char *scan_id, report_t report, case TASK_STATUS_REQUESTED: { int rc; - target_t target = task_target (task, TASKS_TARGET_TYPE_REGULAR); + target_t target = task_target (task); rc = handle_osp_scan_start (task, target, scan_id, start_from, TRUE, &discovery_scan); /* Set discovery flag to the report */ @@ -94,7 +94,8 @@ handle_queued_openvasd_scan (const char *scan_id, report_t report, case TASK_STATUS_REQUESTED: { int rc; - target_t target = task_target (task, TASKS_TARGET_TYPE_REGULAR); + target_t target = task_target (task); + // TODO: target_type? rc = handle_openvasd_scan_start (task, target, scan_id, start_from, TRUE, &discovery_scan); /* Set discovery flag to the report */ diff --git a/src/manage_sql.c b/src/manage_sql.c index 8d24e28669..df17dbaf3b 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -5926,19 +5926,16 @@ set_task_config (task_t task, config_t config) * @brief Return the target of a task. * * @param[in] task Task. - * @param[in] target_type Target type of task. * * @return Target of task. */ target_t -task_target (task_t task, tasks_target_type_t target_type) +task_target (task_t task) { target_t target = 0; switch (sql_int64_ps (&target, - "SELECT target FROM tasks WHERE id = $1" - " AND target_type = $2;", + "SELECT target FROM tasks WHERE id = $1", SQL_RESOURCE_PARAM (task), - SQL_INT_PARAM (target_type), NULL)) { case 0: @@ -5953,6 +5950,13 @@ task_target (task_t task, tasks_target_type_t target_type) } } +/** + * @brief Return the target type of a task. + * + * @param[in] task Task. + * + * @return Target of task. + */ tasks_target_type_t task_target_type (task_t task) { @@ -6199,7 +6203,7 @@ agent_group_hidden_tasks_exist_by_scanner (scanner_t scanner) agent_group_t task_agent_group (task_t task) { - return task_target (task, TASKS_TARGET_TYPE_AGENT_GROUP); + return task_target (task); } /** @@ -6212,7 +6216,7 @@ task_agent_group (task_t task) int task_agent_group_in_trash (task_t task) { - return task_target_in_trash (task, TASKS_TARGET_TYPE_AGENT_GROUP); + return task_target_in_trash (task); } #endif /*ENABLE_AGENTS*/ @@ -6227,7 +6231,7 @@ task_agent_group_in_trash (task_t task) oci_image_target_t task_oci_image_target (task_t task) { - return task_target (task, TASKS_TARGET_TYPE_OCI_IMAGE); + return task_target (task); } /** @@ -6240,7 +6244,7 @@ task_oci_image_target (task_t task) int task_oci_image_target_in_trash (task_t task) { - return task_target_in_trash (task, TASKS_TARGET_TYPE_OCI_IMAGE); + return task_target_in_trash (task); } /** @@ -6269,7 +6273,7 @@ set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) web_application_target_t task_web_application_target (task_t task) { - return task_target (task, TASKS_TARGET_TYPE_WEB_APPLICATION); + return task_target (task); } /** @@ -6282,7 +6286,7 @@ task_web_application_target (task_t task) int task_web_application_target_in_trash (task_t task) { - return task_target_in_trash (task, TASKS_TARGET_TYPE_WEB_APPLICATION); + return task_target_in_trash (task); } /** @@ -6329,18 +6333,15 @@ clear_task_asset_preferences (task_t task) * @brief Return whether the target of a task is in the trashcan. * * @param[in] task Task. - * @param[in] target_type Type of the target * * @return 1 if in trash, else 0. */ int -task_target_in_trash (task_t task, tasks_target_type_t target_type) +task_target_in_trash (task_t task) { return sql_int_ps ("SELECT target_location = " G_STRINGIFY (LOCATION_TRASH) - " FROM tasks WHERE id = $1" - " AND target_type = $2;", + " FROM tasks WHERE id = $1", SQL_RESOURCE_PARAM (task), - SQL_INT_PARAM (target_type), NULL); } @@ -9567,7 +9568,7 @@ create_report (array_t *results, const char *task_id, const char *in_assets, rc = -1; else if (task == 0) rc = -4; - else if (task_target (task, TASKS_TARGET_TYPE_REGULAR)) + else if (task_target (task)) rc = -5; if (rc) { @@ -16378,8 +16379,9 @@ print_report_xml_start (report_t report, report_t delta, task_t task, comment = task_comment (task); - target = task_target (task, TASKS_TARGET_TYPE_REGULAR); - if (task_target_in_trash (task, TASKS_TARGET_TYPE_REGULAR)) + target = task_target (task); + // TODO: target_type? + if (task_target_in_trash (task)) { task_target_uuid = trash_target_uuid (target); task_target_name = trash_target_name (target); @@ -16404,6 +16406,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task, progress_xml = g_strdup_printf ("%i", progress); } + // TODO: target_type? PRINT (out, "" "%s" @@ -16417,7 +16420,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task, tsk_name ? tsk_name : "", comment ? comment : "", task_target_uuid ? task_target_uuid : "", - task_target_in_trash (task, TASKS_TARGET_TYPE_REGULAR), + task_target_in_trash (task), task_target_name ? task_target_name : "", task_target_comment ? task_target_comment : ""); From f82c7480f46520a20f245a05432034f3a8e3ea53 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 10 Jul 2026 10:23:05 +0200 Subject: [PATCH 23/66] updated DROP COLUMN statements in migration document --- docs/tasks-target-type.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md index 6c4f7fde73..232abb5e56 100644 --- a/docs/tasks-target-type.md +++ b/docs/tasks-target-type.md @@ -127,12 +127,12 @@ The migration consists of multiple steps: commit() ``` 3. ```postgresql - ALTER TABLE tasks DROP COLUMN agent_group, - DROP COLUMN agent_group_location, - DROP COLUMN oci_image_target, - DROP COLUMN oci_image_target_location, - DROP COLUMN web_application_target, - DROP COLUMN web_application_target_location + ALTER TABLE tasks DROP COLUMN IF EXISTS agent_group, + DROP COLUMN IF EXISTS agent_group_location, + DROP COLUMN IF EXISTS oci_image_target, + DROP COLUMN IF EXISTS oci_image_target_location, + DROP COLUMN IF EXISTS web_application_target, + DROP COLUMN IF EXISTS web_application_target_location ``` **Concerns** From e14beb79ba129656679945afd97b30ed2d6754f8 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 13 Jul 2026 12:55:32 +0200 Subject: [PATCH 24/66] migrated GMP command --- src/gmp.c | 53 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index 53c24a650a..12e4c53010 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -20006,6 +20006,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) task_t index; gchar *progress_xml; target_t target; + tasks_target_type_t target_type; scanner_t scanner; const char *first_report_id, *last_report_id; char *config_name, *config_uuid; @@ -20051,6 +20052,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) index = get_iterator_resource (&tasks); target = task_target (index); + target_type = task_target_type (index); task_schedule_xml = get_task_schedule_xml (index); @@ -20314,24 +20316,33 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) config_name = task_config_name (index); config_uuid = task_config_uuid (index); target_available = 1; - if (target_in_trash) - { - task_target_uuid = trash_target_uuid (target); - task_target_name = trash_target_name (target); - target_available = trash_target_readable (target); - } - else if (target) + + if (target_type == TASKS_TARGET_TYPE_REGULAR) { - target_t found; - task_target_uuid = target_uuid (target); - task_target_name = target_name (target); - if (find_target_with_permission (task_target_uuid, - &found, - "get_targets")) - g_error ("%s: GET_TASKS: error finding task target," - " aborting", - __func__); - target_available = (found > 0); + if (target_in_trash) + { + task_target_uuid = trash_target_uuid (target); + task_target_name = trash_target_name (target); + target_available = trash_target_readable (target); + } + else if (target) + { + target_t found; + task_target_uuid = target_uuid (target); + task_target_name = target_name (target); + if (find_target_with_permission (task_target_uuid, + &found, + "get_targets")) + g_error ("%s: GET_TASKS: error finding task target," + " aborting", + __func__); + target_available = (found > 0); + } + else + { + task_target_uuid = NULL; + task_target_name = NULL; + } } else { @@ -20348,7 +20359,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) oci_image_target = task_oci_image_target (index); oci_image_target_in_trash = task_oci_image_target_in_trash (index); - if (oci_image_target || oci_image_target_in_trash) + if (target_type == TASKS_TARGET_TYPE_OCI_IMAGE) { oci_image_target_available = 1; if (oci_image_target_in_trash) @@ -20407,10 +20418,10 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) int group_readable = 0; char *task_agent_group_uuid = NULL; char *task_agent_group_name = NULL; - agent_group =task_agent_group (index); /* row id or 0 */ + agent_group = task_agent_group (index); /* row id or 0 */ int agent_group_in_trash = task_agent_group_in_trash (index); - if (agent_group) + if (target_type == TASKS_TARGET_TYPE_AGENT_GROUP) { if (agent_group_in_trash) { @@ -20450,7 +20461,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) web_application_target = task_web_application_target (index); web_application_target_in_trash = task_web_application_target_in_trash (index); - if (web_application_target || web_application_target_in_trash) + if (target_type == TASKS_TARGET_TYPE_WEB_APPLICATION) { web_application_target_available = 1; if (web_application_target_in_trash) From ac2d0435878a71a7e9157bd25c4d9bfbb8ee82fb Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 13 Jul 2026 13:45:13 +0200 Subject: [PATCH 25/66] added missing "," in sql command --- src/manage_sql.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index df17dbaf3b..c2c318189e 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -6008,7 +6008,7 @@ void set_task_target_and_location (task_t task, target_t target, tasks_target_type_t target_type) { - sql_ps ("UPDATE tasks SET target = $1, target_type = $2, target_location = 0" + sql_ps ("UPDATE tasks SET target = $1, target_type = $2, target_location = 0," " modification_time = m_now ()" " WHERE id = $3;", SQL_RESOURCE_PARAM (target), From 67d28916330f901d9ef837bb989da600fceceb17 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 13 Jul 2026 14:01:24 +0200 Subject: [PATCH 26/66] migrated target_in_use and trash_target_in_use functions to target_type --- src/manage_sql_targets.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/manage_sql_targets.c b/src/manage_sql_targets.c index c896dcb265..e5353722cc 100644 --- a/src/manage_sql_targets.c +++ b/src/manage_sql_targets.c @@ -2101,11 +2101,13 @@ init_target_task_iterator (iterator_t* iterator, target_t target) "%s" " SELECT name, uuid, %s FROM tasks" " WHERE target = %llu" + " AND target_type = %d" " AND hidden = 0" " ORDER BY name ASC;", with_clause ? with_clause : "", available, - target); + target, + TASKS_TARGET_TYPE_REGULAR); g_free (with_clause); g_free (available); @@ -2155,11 +2157,7 @@ target_task_iterator_readable (iterator_t* iterator) int target_in_use (target_t target) { - return !!sql_int ("SELECT count(*) FROM tasks" - " WHERE target = %llu" - " AND target_location = " G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - target); + return task_target_in_use (target, TASKS_TARGET_TYPE_REGULAR); } /** @@ -2172,10 +2170,7 @@ target_in_use (target_t target) int trash_target_in_use (target_t target) { - return !!sql_int ("SELECT count(*) FROM tasks" - " WHERE target = %llu" - " AND target_location = " G_STRINGIFY (LOCATION_TRASH), - target); + return task_trash_target_in_use (target, TASKS_TARGET_TYPE_REGULAR); } /** From 7cea9cb05f46d642030338b18ed8e41d1c39c750 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 14 Jul 2026 08:54:04 +0200 Subject: [PATCH 27/66] fixed some agent group queries to adapt target_type --- src/manage_sql.c | 39 +++++++-------------------------------- 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index c2c318189e..dea4ab7499 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -2564,13 +2564,15 @@ append_to_task_string (task_t task, const char* field, const char* value) #define TASK_AGENT_GROUP_ITERATOR_COLUMNS \ ,{ \ "(SELECT uuid FROM agent_groups" \ - " WHERE agent_groups.id = tasks.agent_group)", \ + " WHERE agent_groups.id = tasks.target" \ + " AND tasks.target_type = 2)", \ "agent_group_id", \ KEYWORD_TYPE_STRING \ }, \ { \ "(SELECT name FROM agent_groups" \ - " WHERE agent_groups.id = tasks.agent_group)", \ + " WHERE agent_groups.id = tasks.target" \ + " AND tasks.target_type = 2)", \ "agent_group", \ KEYWORD_TYPE_STRING \ } @@ -2581,35 +2583,7 @@ append_to_task_string (task_t task, const char* field, const char* value) /** * @brief Query for TASK_SEV_CASE_GUARD. */ -/** - * @brief Query for TASK_SEV_CASE_GUARD. - */ -#define TASK_NO_TARGET_BASE "target IS NULL" - -#if ENABLE_AGENTS -#define TASK_NO_AGENT_GROUP " AND agent_group IS NULL" -#else -#define TASK_NO_AGENT_GROUP "" -#endif - -#if ENABLE_CONTAINER_SCANNING -#define TASK_NO_OCI_IMAGE_TARGET " AND oci_image_target IS NULL" -#else -#define TASK_NO_OCI_IMAGE_TARGET "" -#endif - -#if ENABLE_WEB_APPLICATION_SCANNING -#define TASK_NO_WEB_APPLICATION_TARGET " AND web_application_target IS NULL" -#else -#define TASK_NO_WEB_APPLICATION_TARGET "" -#endif - -#define TASK_NO_TARGET_CTX \ -"(" TASK_NO_TARGET_BASE \ -TASK_NO_AGENT_GROUP \ -TASK_NO_OCI_IMAGE_TARGET \ -TASK_NO_WEB_APPLICATION_TARGET \ -")" +#define TASK_NO_TARGET_CTX "(target_type = 0)" /** * @brief SQL for TASK_ITERATOR_WHERE_COLUMNS_INNER. @@ -2843,7 +2817,8 @@ TASK_NO_WEB_APPLICATION_TARGET \ KEYWORD_TYPE_INTEGER \ }, \ { \ - "(SELECT name FROM targets WHERE id = target)", \ + "(SELECT name FROM targets WHERE id = target" \ + " AND tasks.target_type = 1)", \ "target", \ KEYWORD_TYPE_STRING \ }, \ From 07fccdb03cc97a62c8e510fce2173fdda445600e Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 14 Jul 2026 10:25:36 +0200 Subject: [PATCH 28/66] Accommodating for `target_type` when using `target` --- src/manage.c | 18 +++++++++++++++--- src/manage_scan_handler.c | 17 ++++++++++++++--- src/manage_sql.c | 32 ++++++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/manage.c b/src/manage.c index 3786492588..b745240879 100644 --- a/src/manage.c +++ b/src/manage.c @@ -1707,7 +1707,11 @@ run_osp_task (task_t task, int from, char **report_id) { target_t target; - target = task_target (task); + if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) + target = 0; + else + target = task_target (task); + if (target) { char *uuid; @@ -1917,7 +1921,11 @@ run_cve_task (task_t task) { target_t target; - target = task_target (task); + if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) + target = 0; + else + target = task_target (task); + if (target) { char *uuid; @@ -7095,7 +7103,11 @@ run_openvasd_task (task_t task, int from, char **report_id) } target_t target; - target = task_target (task); + if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) + target = 0; + else + target = task_target (task); + if (target) { char *uuid; diff --git a/src/manage_scan_handler.c b/src/manage_scan_handler.c index 136380c15d..b78c5eac6b 100644 --- a/src/manage_scan_handler.c +++ b/src/manage_scan_handler.c @@ -51,7 +51,13 @@ handle_queued_osp_scan (const char *scan_id, report_t report, case TASK_STATUS_REQUESTED: { int rc; - target_t target = task_target (task); + target_t target; + + if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) + target = 0; + else + target = task_target (task); + rc = handle_osp_scan_start (task, target, scan_id, start_from, TRUE, &discovery_scan); /* Set discovery flag to the report */ @@ -94,8 +100,13 @@ handle_queued_openvasd_scan (const char *scan_id, report_t report, case TASK_STATUS_REQUESTED: { int rc; - target_t target = task_target (task); - // TODO: target_type? + target_t target; + + if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) + target = 0; + else + target = task_target (task); + rc = handle_openvasd_scan_start (task, target, scan_id, start_from, TRUE, &discovery_scan); /* Set discovery flag to the report */ diff --git a/src/manage_sql.c b/src/manage_sql.c index dea4ab7499..3d9a367599 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -6178,6 +6178,8 @@ agent_group_hidden_tasks_exist_by_scanner (scanner_t scanner) agent_group_t task_agent_group (task_t task) { + if (task_target_type (task) != TASKS_TARGET_TYPE_AGENT_GROUP) + return 0; return task_target (task); } @@ -6191,6 +6193,8 @@ task_agent_group (task_t task) int task_agent_group_in_trash (task_t task) { + if (task_target_type (task) != TASKS_TARGET_TYPE_AGENT_GROUP) + return 0; return task_target_in_trash (task); } #endif /*ENABLE_AGENTS*/ @@ -6206,6 +6210,8 @@ task_agent_group_in_trash (task_t task) oci_image_target_t task_oci_image_target (task_t task) { + if (task_target_type (task) != TASKS_TARGET_TYPE_OCI_IMAGE) + return 0; return task_target (task); } @@ -6219,6 +6225,8 @@ task_oci_image_target (task_t task) int task_oci_image_target_in_trash (task_t task) { + if (task_target_type (task) != TASKS_TARGET_TYPE_OCI_IMAGE) + return 0; return task_target_in_trash (task); } @@ -6248,6 +6256,8 @@ set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) web_application_target_t task_web_application_target (task_t task) { + if (task_target_type (task) != TASKS_TARGET_TYPE_WEB_APPLICATION) + return 0; return task_target (task); } @@ -6261,6 +6271,8 @@ task_web_application_target (task_t task) int task_web_application_target_in_trash (task_t task) { + if (task_target_type (task) != TASKS_TARGET_TYPE_WEB_APPLICATION) + return 0; return task_target_in_trash (task); } @@ -16354,20 +16366,29 @@ print_report_xml_start (report_t report, report_t delta, task_t task, comment = task_comment (task); - target = task_target (task); - // TODO: target_type? - if (task_target_in_trash (task)) + if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) + target = 0; + else + target = task_target (task); + + if (target && task_target_in_trash (task)) { task_target_uuid = trash_target_uuid (target); task_target_name = trash_target_name (target); task_target_comment = trash_target_comment (target); } - else + else if (target) { task_target_uuid = target_uuid (target); task_target_name = target_name (target); task_target_comment = target_comment (target); } + else + { + task_target_uuid = NULL; + task_target_name = NULL; + task_target_comment = NULL; + } if ((target == 0) && (task_run_status (task) == TASK_STATUS_RUNNING)) @@ -16381,7 +16402,6 @@ print_report_xml_start (report_t report, report_t delta, task_t task, progress_xml = g_strdup_printf ("%i", progress); } - // TODO: target_type? PRINT (out, "" "%s" @@ -16395,7 +16415,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task, tsk_name ? tsk_name : "", comment ? comment : "", task_target_uuid ? task_target_uuid : "", - task_target_in_trash (task), + (target != 0) ? task_target_in_trash (task) : 0, task_target_name ? task_target_name : "", task_target_comment ? task_target_comment : ""); From 73108cc57fe85011f32944031dc34b89845eda51 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Wed, 29 Jul 2026 10:57:35 +0200 Subject: [PATCH 29/66] fix: Introduce target type for better internal handling of different tasks --- src/manage_migrators.c | 146 +++++++++++++++++++++++------------------ 1 file changed, 81 insertions(+), 65 deletions(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 343f3713fc..7fd680f882 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4255,75 +4255,91 @@ migrate_281_to_282 () return -1; } + /* Check for invalid data */ + int count_multiple_targets = + sql_int ("SELECT COUNT(*) FROM tasks" + " WHERE (agent_group IS NOT NULL" + " AND oci_image_target IS NOT NULL)" + " OR (agent_group IS NOT NULL" + " AND web_application_target IS NOT NULL)" + " OR (oci_image_target IS NOT NULL" + " AND web_application_target IS NOT NULL)" + " OR (target != 0 AND agent_group IS NOT NULL)" + " OR (target != 0 AND oci_image_target IS NOT NULL)" + " OR (target != 0 AND web_application_target IS NOT NULL);"); + + if (count_multiple_targets > 0) + { + g_error ("Invalid data: Multiple targets specified for a task"); + sql_rollback (); + return -1; + } + /* Add target_type column to tasks table */ - sql ("ALTER TABLE tasks ADD target_type INTEGER"); + sql ("ALTER TABLE tasks ADD COLUMN IF NOT EXISTS target_type INTEGER;"); /* Migrate data to new column */ - iterator_t tasks; - init_iterator (&tasks, "SELECT id," - " target, target_location," - " agent_group, agent_group_location," - " oci_image_target, oci_image_target_location," - " web_application_target, web_application_target_location" - " FROM tasks"); - - while (next (&tasks)) - { - const int64_t id = iterator_int64 (&tasks, 0); - const int64_t target = iterator_int64 (&tasks, 1); - const int64_t target_location = iterator_int64 (&tasks, 2); - const int64_t agent_group = iterator_int64 (&tasks, 3); - const int64_t agent_group_location = iterator_int64 (&tasks, 4); - const int64_t oci_image_target = iterator_int64 (&tasks, 5); - const int64_t oci_image_target_location = iterator_int64 (&tasks, 6); - const int64_t web_application_target = iterator_int64 (&tasks, 7); - const int64_t web_application_target_location = iterator_int64 (&tasks, 8); - - int64_t new_target = 0; - int64_t new_location = 0; - tasks_target_type_t new_target_type = TASKS_TARGET_TYPE_UNDEFINED; - - if (agent_group != 0) - { - new_target = agent_group; - new_location = agent_group_location; - new_target_type = TASKS_TARGET_TYPE_AGENT_GROUP; - } - else if (oci_image_target != 0) - { - new_target = oci_image_target; - new_location = oci_image_target_location; - new_target_type = TASKS_TARGET_TYPE_OCI_IMAGE; - } - else if (web_application_target != 0) - { - new_target = web_application_target; - new_location = web_application_target_location; - new_target_type = TASKS_TARGET_TYPE_WEB_APPLICATION; - } - else if (target != 0) - { - new_target = target; - new_location = target_location; - new_target_type = TASKS_TARGET_TYPE_REGULAR; - } - else - { - new_target = 0; - new_location = 0; - new_target_type = TASKS_TARGET_TYPE_IMPORT_TASK; - } + /* Default tasks */ + sql ("UPDATE tasks" + " SET target_type = 1" + " WHERE target != 0;"); + + /* Import tasks */ + sql ("UPDATE tasks" + " SET target_type = 0" + " WHERE target = 0" + " AND oci_image_target IS NULL" + " AND agent_group IS NULL" + " AND web_application_target IS NULL;"); + + /* Agent group tasks */ + sql ("UPDATE tasks" + " SET target = agent_group," + " target_location = agent_group_location," + " target_type = 2" + " WHERE agent_group IS NOT NULL;"); + + /* OCI image tasks */ + sql ("UPDATE tasks" + " SET target = oci_image_target," + " target_location = oci_image_target_location," + " target_type = 3" + " WHERE oci_image_target IS NOT NULL;"); + + /* Web application tasks */ + sql ("UPDATE tasks" + " SET target = web_application_target," + " target_location = web_application_target_location," + " target_type = 4" + " WHERE web_application_target IS NOT NULL;"); + + /** + * Reverting DB version 281 to 280 is possible: + * + * UPDATE tasks + * SET agent_group = target, + * agent_group_location = target_location, + * target = 0, + * target_location = NULL + * WHERE target_type = 2; + * + * UPDATE tasks + * SET oci_image_target = target, + * oci_image_target_location = target_location, + * target = 0, + * target_location = NULL + * WHERE target_type = 3; + + * UPDATE tasks + * SET web_application_target = target, + * web_application_target_location = target_location, + * target = 0, + * target_location = NULL + * WHERE target_type = 4; + * + * ALTER TABLE tasks DROP COLUMN IF EXISTS target_type; + */ - sql_ps ("UPDATE tasks" - " SET target = $2, target_location = $3, target_type = $4" - " WHERE id = $1", - SQL_RESOURCE_PARAM (id), - SQL_RESOURCE_PARAM (new_target), - SQL_INT_PARAM (new_location), - SQL_INT_PARAM (new_target_type), - NULL); - } - cleanup_iterator (&tasks); // TODO: This step will be moved to a later migration. // For safety reasons, we decided to keep the columns for now and only From 00750db7ff5051e8f268a3dead38c72e01161870 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Wed, 5 Aug 2026 11:15:57 +0200 Subject: [PATCH 30/66] removed migration document --- docs/tasks-target-type.md | 1009 ------------------------------------- 1 file changed, 1009 deletions(-) delete mode 100644 docs/tasks-target-type.md diff --git a/docs/tasks-target-type.md b/docs/tasks-target-type.md deleted file mode 100644 index 232abb5e56..0000000000 --- a/docs/tasks-target-type.md +++ /dev/null @@ -1,1009 +0,0 @@ -# Migrate to a `target_type` column for `tasks` - -## What - -Modernize `gvmd`'s code- and database. Specifically the `tasks` components, by introducing the concept of a -`target_type`. - -## Why - -Currently, we add columns to the `tasks` table, for different types of targets. - -The columns `oci_image_target`, `agent_group` and `web_application_target` are artifacts of this phenomenon. -In addition to these `*target` columns, each comes with their related `*_location` column, indicating -whether this resource is found in the trash. - -```mermaid -erDiagram - tasks }o--|| targets: joins - tasks }o--|| oci_image_targets: joins - tasks }o--|| agent_groups: joins - tasks }o--|| web_application_targets: joins - - tasks { - int target - int target_location - int oci_image_target - int oci_image_target_location - int agent_group - int agent_group_location - int web_application_target - int web_application_target - } - targets { - int id - } - oci_image_targets { - int id - } - agent_groups { - int id - } - web_application_targets { - int id - } -``` - -This makes it cumbersome to add a new type of target. Also, it is quite confusing, as the column `target` is still in -place, but refers only to the legacy definition of a target. - -By introducing a `target_type` column, and migrating the then unnecessary columns `oci_image_target`, `agent_group` -and `web_application_target` accordingly, we make intent more clear and concise. - -After introducing the `target_type` column, we keep the different target tables, as shown below. -This will make it a lot easier to introduce further target types in the future, without adding a lot of -duplicate code for handling tasks. - -```mermaid -erDiagram - tasks }o--|| targets: joins - tasks }o--|| oci_image_targets: joins - tasks }o--|| agent_groups: joins - tasks }o--|| web_application_targets: joins - - tasks { - int target_type - int target - int target_location - } - targets { - int id - } - oci_image_targets { - int id - } - agent_groups { - int id - } - web_application_targets { - int id - } -``` - -# Target Types - -These are the proposed target types, part of the initial migration - -| Target Type | Integer Value | Reference Table | Notes | -|-------------------------------------|---------------|---------------------------|----------------------| -| `TASKS_TARGET_TYPE_IMPORT_TASK` | 0 | N/A | `target` must be `0` | -| `TASKS_TARGET_TYPE_REGULAR` | 1 | `targets` | N/A | -| `TASKS_TARGET_TYPE_AGENT_GROUP` | 2 | `agent_groups` | N/A | -| `TASKS_TARGET_TYPE_OCI_IMAGE` | 3 | `oci_image_targets` | N/A | -| `TASKS_TARGET_TYPE_WEB_APPLICATION` | 4 | `web_application_targets` | N/A | - -# Migrations - -## Data migration - -> :warning: While migrating the data seems rather trivial, we should opt for **creating a database backup - prior to migrating** the columns and data. - -The migration consists of multiple steps: - -1. ```postgresql - ALTER TABLE tasks ADD target_type INTEGER - ``` -2. Pseudocode for migrating table contents - ```python - start_transaction() - for row in tasks: - if row.agent_group: - row.target = row.agent_group - row.target_location = row.agent_group_location - row.target_type = TARGET_TYPE_AGENT_GROUP - elif row.oci_image_target: - row.target = row.oci_image_target - row.target_location = row.oci_image_target_location - row.target_type = TARGET_TYPE_OCI_IMAGE - elif row.web_application_target: - row.target = row.web_application_target - row.target_location = row.web_application_target_location - row.target_type = TARGET_TYPE_WEB_APPLICATION - elif row.target != 0: - row.target_type = TARGET_TYPE_REGULAR - else: - row.target_type = TARGET_TYPE_IMPORT_TASK - commit() - ``` -3. ```postgresql - ALTER TABLE tasks DROP COLUMN IF EXISTS agent_group, - DROP COLUMN IF EXISTS agent_group_location, - DROP COLUMN IF EXISTS oci_image_target, - DROP COLUMN IF EXISTS oci_image_target_location, - DROP COLUMN IF EXISTS web_application_target, - DROP COLUMN IF EXISTS web_application_target_location - ``` - -**Concerns** - -- If for some reason, multiple `*target` fields in any combination are set (e.g. `target` *and* `oci_image_target`), - we cannot imply which target is correct. While this should never happen, we should be ready - for this to happen anyway. - -## Code migration - -As per usual with these kinds of refactorings, some recurring changes have a pattern. - -```postgresql -WHERE oci_image_target IS NOT NULL -=> - -WHERE target_type = {{TARGET_TYPE_OCI_IMAGE}} -``` - -```postgresql -WHERE web_application_target = $1 -=> - -WHERE target = $1 -AND target_type = {{TARGET_TYPE_WEB_APPLICATION}} -``` - ---- - -As of the new `target_type`, all current `(.*)task_target(.*)` functions, which work on "regular" target types -must respect the new `target_type` column. -A benefit of this change is that they now can be used by domain logic that affect other target types. - -One example is the `set_task_target ()` function -```c++ -void -set_task_target (task_t task, target_t target, tasks_target_type_t target_type) -{ - sql_ps ("UPDATE tasks SET target = $1, target_type = $2," - " modification_time = m_now ()" - " WHERE id = $3;", - SQL_RESOURCE_PARAM (target), - SQL_INT_PARAM (target_type), - SQL_RESOURCE_PARAM (task), - NULL); -} -``` - ---- - -Some of the functions used by `oci_image_targets`, `agent_groups` and `web_appliation_targets` can also -be generalized, to avoid duplicate code. - -```c++ -int -task_target_in_use (target_t target, tasks_target_type_t target_type) -{ - return !!sql_int_ps ( - "SELECT count(*) FROM tasks" - " WHERE target = $1" - " AND target_type = $2" - " AND target_location = " G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - SQL_RESOURCE_PARAM (target), - SQL_INT_PARAM (target_type), - NULL); -} - -int -task_trash_target_in_use (target_t target, tasks_target_type_t target_type) -{ - return !!sql_int_ps ( - "SELECT count(*) FROM tasks" - " WHERE target = $1" - " AND target_type = $2" - " AND target_location = " G_STRINGIFY (LOCATION_TRASH) ";", - SQL_RESOURCE_PARAM (target), - SQL_INT_PARAM (target_type), - NULL); -} -``` - ---- - -A group of `delete_... ()` and `restore_... ()` functions updates the respective `target` and `target_location` -in the tasks table. Since we are removing the designated columns for each target type, -we can generalize these functions. -```c++ -void -task_update_delete_target (target_t resource_id, target_t trash_id, - tasks_target_type_t target_type) -{ - sql_ps ("UPDATE tasks" - " SET target = $1," - " target_location = " G_STRINGIFY (LOCATION_TRASH) - " WHERE target = $2" - " AND target_location = " G_STRINGIFY (LOCATION_TABLE) - " AND target_type = $3;", - SQL_RESOURCE_PARAM (trash_id), - SQL_RESOURCE_PARAM (resource_id), - SQL_INT_PARAM (target_type), - NULL); -} -void -task_update_restore_target (target_t restored_id, target_t trash_id, - tasks_target_type_t target_type) -{ - sql_ps ("UPDATE tasks" - " SET target = $1," - " target_location = " G_STRINGIFY (LOCATION_TABLE) - " WHERE target = $2" - " AND target_location = " G_STRINGIFY (LOCATION_TRASH) - " AND target_type = $3;", - SQL_RESOURCE_PARAM (restored_id), - SQL_RESOURCE_PARAM (trash_id), - SQL_INT_PARAM (target_type), - NULL); -} -``` - -
- General functions - -[manage_pg.c#L1293](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1293) -```c++ -" SELECT CASE" -" WHEN EXISTS (SELECT id" -" FROM tasks" -" WHERE id = (SELECT task" -" FROM reports WHERE id = $1)" -" AND oci_image_target IS NOT NULL)" -``` - -Migration: Add additional `if (db_version >= ...)` -=> `WHERE target_type = {{TARGET_TYPE_OCI_IMAGE}}` - ---- - -[manage_pg.c#L1447](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1447) -```c++ -" SELECT CASE" -" WHEN (SELECT target = 0 " -" AND agent_group = 0 " -" AND oci_image_target = 0 " -" AND web_application_target = 0 " -" FROM tasks WHERE id = $1)" -``` - -[manage_pg.c#L1473](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1473) -```c++ -" SELECT CASE" -" WHEN (SELECT target = 0 " -" AND agent_group = 0 " -" AND oci_image_target = 0 " -" FROM tasks WHERE id = $1)" -``` - -Migration: Add additional `if (db_version >= ...)` -=> `target = 0 AND target_type = {{TARGET_TYPE_IMPORT_TASK}}`, omit other columns - ---- - -[manage_pg.c#L1562](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_pg.c#L1562) -```c++ - /* Skip running and import tasks. */ - " WHEN (SELECT run_status = %u OR target = 0" - " FROM tasks WHERE id = $1)" - -``` - -Migration => `AND target_type = {{TARGET_TYPE_IMPORT_TASK}}` - ---- - -[manage_sql_filters.c#L1286](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_filters.c#L1286) -```c++ - if ((strcmp (type, "report") == 0) - && (strcmp (keyword->string, "status") == 0)) - g_string_append_printf - (order, - " ORDER BY" - " (CASE WHEN (SELECT " - " ( " - " target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " - " FROM tasks" - " WHERE tasks.id = task)" - " THEN '0'" - " ELSE run_status_name (scan_run_status)" - " || (SELECT CAST (temp / 100 AS text)" - " || CAST (temp / 10 AS text)" - " || CAST (temp %% 10 as text)" - " FROM (SELECT report_progress (id) AS temp)" - " AS temp_sub)" - " END)" - " ASC"); - else if ((strcmp (type, "task") == 0) - && (strcmp (keyword->string, "status") == 0)) - g_string_append_printf - (order, - " ORDER BY" - " (CASE WHEN ( target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " - " THEN '0'" - " ELSE run_status_name (run_status)" - " || (SELECT CAST (temp / 100 AS text)" - " || CAST (temp / 10 AS text)" - " || CAST (temp %% 10 as text)" - " FROM (SELECT report_progress (id) AS temp" - " FROM reports" - " WHERE task = tasks.id" - " ORDER BY creation_time DESC LIMIT 1)" - " AS temp_sub)" - " END)" - " ASC"); -``` - -Migration => - ---- - -[manage_sql_filters.c#L1491](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_filters.c#L1491) -```c++ -if ((strcmp (type, "report") == 0) - && (strcmp (keyword->string, "status") == 0)) - g_string_append_printf - (order, - " ORDER BY" - " (CASE WHEN (SELECT " - " ( " - " target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " - " FROM tasks" - " WHERE tasks.id = task)" - " THEN '0'" - " ELSE run_status_name (scan_run_status)" - " || (SELECT CAST (temp / 100 AS text)" - " || CAST (temp / 10 AS text)" - " || CAST (temp %% 10 as text)" - " FROM (SELECT report_progress (id) AS temp)" - " AS temp_sub)" - " END)" - " DESC"); - else if ((strcmp (type, "task") == 0) - && (strcmp (keyword->string, "status") == 0)) - g_string_append_printf - (order, - " ORDER BY" - " (CASE WHEN ( target = 0 " - " and COALESCE( agent_group, 0) = 0 " - " and COALESCE( oci_image_target, 0) = 0" - " ) " - " THEN '0'" - " ELSE run_status_name (run_status)" - " || (SELECT CAST (temp / 100 AS text)" - " || CAST (temp / 10 AS text)" - " || CAST (temp %% 10 as text)" - " FROM (SELECT report_progress (id) AS temp" - " FROM reports" - " WHERE task = tasks.id" - " ORDER BY creation_time DESC LIMIT 1)" - " AS temp_sub)" - " END)" - " DESC"); -``` - -Migration => - ---- - -[manage_sql.c#L18107](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L18107) -```c++ -task_t -make_task (char* name, char* comment, int in_assets, int event) -``` - -Migration => set `target_type` - ---- - -[manage_sql.c#L5850](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5850) -```c++ -void -set_task_target (task_t task, target_t target) -{ - sql ("UPDATE tasks SET target = %llu, modification_time = m_now ()" - " WHERE id = %llu;", - target, - task); -} -``` - -Migration => set `target_type` - ---- - -[manage_sql.c#L18228](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L18228) -```c++ -copy_task(...) -... - - ret = copy_resource_lock ("task", name, comment, task_id, - "config, target, oci_image_target," - " web_application_target," - " schedule, schedule_periods," - " scanner, schedule_next_time," - " config_location, target_location," - " schedule_location, scanner_location," - " oci_image_target_location," - " web_application_target_location," - " usage_type," - " agent_group, agent_group_location," - " alterable", - 1, &new, &old); - -``` - -Migration => remove unused fields, copy `target_type` instead - ---- - -
- -
- Agent groups - -[manage_sql.c#L5866](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5866) -```c++ -void -set_task_agent_group_and_location (task_t task, agent_group_t agent_group) -{ - sql ("UPDATE tasks SET agent_group = %llu, agent_group_location = 0," - " modification_time = m_now ()" - " WHERE id = %llu;", - agent_group, - task); -} -``` - -Migration => - ---- - -[manage_sql.c#L5884](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5884) -```c++ -int -agent_group_tasks_exist_by_scanner (scanner_t scanner) -{ - return !!sql_int ( - "SELECT COUNT(*) FROM tasks" - " WHERE agent_group IN (" - " SELECT id FROM agent_groups WHERE scanner = %llu)" - " AND agent_group_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - scanner); -} -``` - -Migration => - ---- - -[manage_sql.c#L5903](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5903) -```c++ -int -agent_group_hidden_tasks_exist_by_scanner (scanner_t scanner) -{ - return !!sql_int ( - "SELECT COUNT(*) FROM tasks " - "WHERE hidden != 0 " - " AND (" - " (agent_group_location = %d" - " AND agent_group IN (" - " SELECT id FROM agent_groups WHERE scanner = %llu ))" - " OR " - " (agent_group_location = %d " - " AND agent_group IN (" - " SELECT id FROM agent_groups_trash WHERE scanner = %llu ))" - ");", - LOCATION_TABLE, scanner, LOCATION_TRASH, scanner); -} -``` - -Migration => - ---- - -[manage_sql.c#L5931](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5931) -```c++ -switch (sql_int64 (&agent_group, - "SELECT agent_group FROM tasks WHERE id = %llu;", - task)) -``` - -Migration => - ---- - -[manage_sql.c#L5954](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5954) -```c++ -int -task_agent_group_in_trash (task_t task) -{ - return sql_int ("SELECT agent_group_location = " - G_STRINGIFY (LOCATION_TRASH) - " FROM tasks WHERE id = %llu;", - task); -} -``` - -Migration => - ---- - -[manage_sql_agent_groups.c#L59](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L59) -```c++ -static int -agent_group_in_use_in_hidden_task (agent_group_t agent_group) -{ - return !!sql_int ("SELECT COUNT(*) FROM tasks " - "WHERE hidden != 0 AND agent_group = %llu;", - agent_group); -} -``` - -Migration => - ---- - -[manage_sql_agent_groups.c#L556](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L556) -```c++ -int -delete_agent_group (const char *agent_group_uuid, int ultimate) -``` - -Migration => `update_trashcan_task_target ()` - -[manage_sql_agent_groups.c#L1029](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L1029) -```c++ -int -agent_group_in_use (agent_group_t agent_group) -{ - return !!sql_int ("SELECT count(*) FROM tasks" - " WHERE agent_group = %llu" - " AND agent_group_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - agent_group); -} -``` - -Migration => - ---- - -[manage_sql_agent_groups.c#L1046](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_agent_groups.c#L1046) -```c++ -int -trash_agent_group_in_use (agent_group_t agent_group) -{ - return !!sql_int ("SELECT count(*) FROM tasks" - " WHERE agent_group = %llu" - " AND agent_group_location = " - G_STRINGIFY (LOCATION_TRASH), - agent_group); -} -``` - -Migration => - ---- - -
- -
- OCI Image targets - -[manage_sql.c#L5976](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5976) -```c++ -switch (sql_int64 (&oci_image_target, - "SELECT oci_image_target FROM tasks WHERE id = %llu;", - task)) -``` - -Migration => - ---- - -[manage_sql.c#L5999](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L5999) -```c++ -int -task_oci_image_target_in_trash (task_t task) -{ - return sql_int ("SELECT oci_image_target_location = " - G_STRINGIFY (LOCATION_TRASH) - " FROM tasks WHERE id = %llu;", - task); -} -``` - -Migration => - ---- - -[manage_sql.c#L6014](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6014) -```c++ -void -set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) -{ - sql ("UPDATE tasks SET oci_image_target = %llu," - " oci_image_target_location = 0," - " modification_time = m_now ()" - " WHERE id = %llu;", - oci_image_target, - task); -} -``` - -Migration => set `target_type`, `target` and `target_location` - ---- - -[manage_sql_oci_image_targets.c#L427](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L427) -```c++ -if (sql_int ("SELECT count(*) FROM tasks" - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TRASH) ";", - oci_image_target)) -{ - sql_rollback (); - return 1; -} -``` - -Migration => use `target_type` and regular fields instead - ---- - -[manage_sql_oci_image_targets.c#L451](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L451) -```c++ -if (sql_int ("SELECT count(*) FROM tasks" - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - oci_image_target)) -{ - sql_rollback (); - return 1; -} -``` - -Migration => use `target_type` and regular fields instead - ---- - -[manage_sql_oci_image_targets.c#L473](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L473) -```c++ -sql ("UPDATE tasks" - " SET oci_image_target = %llu," - " oci_image_target_location = " - G_STRINGIFY (LOCATION_TRASH) - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TABLE) ";", - sql_last_insert_id (), - oci_image_target); -``` - -Migration => `task_update_delete_target ()` - ---- - -[manage_sql_oci_image_targets.c#L490](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L490) -```c++ - else if (sql_int ("SELECT count(*) FROM tasks" - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TABLE), - oci_image_target)) - { - sql_rollback (); - return 1; - } -``` - -Migration => - ---- - -[manage_sql_oci_image_targets.c#L581](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L581) -```c++ -sql ("UPDATE tasks" - " SET oci_image_target = %llu," - " oci_image_target_location = " G_STRINGIFY (LOCATION_TABLE) - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " G_STRINGIFY (LOCATION_TRASH), - oci_image_target, - resource); -``` - -=> `task_update_restored_target ()` - - ---- - -[manage_sql_oci_image_targets.c#L906](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L906) -```c++ -int -oci_image_target_in_use (oci_image_target_t oci_image_target) -{ - return !!sql_int ("SELECT count(*) FROM tasks" - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - oci_image_target); -} -``` - -Migration => - ---- - -[manage_sql_oci_image_targets.c#L924](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L924) -```c++ -int -trash_oci_image_target_in_use (oci_image_target_t oci_image_target) -{ - return !!sql_int ("SELECT count(*) FROM tasks" - " WHERE oci_image_target = %llu" - " AND oci_image_target_location = " - G_STRINGIFY (LOCATION_TRASH), - oci_image_target); -} -``` - -Migration => - ---- - -[manage_sql_oci_image_targets.c#L979](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_oci_image_targets.c#L979) -```c++ - init_iterator (iterator, - "%s" - " SELECT name, uuid, %s FROM tasks" - " WHERE oci_image_target = %llu" - " AND hidden = 0" - " ORDER BY name ASC;", - with_clause ? with_clause : "", - available, - oci_image_target); -``` - -Migration => - ---- - -
- -
- Web application targets - -[manage_sql.c#L6035](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6035) -```c++ -web_application_target_t -task_web_application_target (task_t task) -{ - web_application_target_t web_application_target = 0; - sql_int64_ps (&web_application_target, - "SELECT web_application_target FROM tasks" - " WHERE id = $1;", - SQL_RESOURCE_PARAM(task), - NULL); - return web_application_target; -} -``` - -Migration => - ---- - -[manage_sql.c#L6054](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6054) -```c++ -int -task_web_application_target_in_trash (task_t task) -{ - return sql_int_ps ("SELECT web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) - " FROM tasks WHERE id = $1;", - SQL_RESOURCE_PARAM(task), - NULL); -} -``` - -Migration => - ---- - -[manage_sql.c#L6070](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql.c#L6070) -```c++ -void -set_task_web_application_target (task_t task, web_application_target_t web_application_target) -{ - sql_ps ("UPDATE tasks SET web_application_target = $1," - " web_application_target_location = 0," - " modification_time = m_now ()" - " WHERE id = $2;", - SQL_RESOURCE_PARAM(web_application_target), - SQL_RESOURCE_PARAM(task), - NULL); -} -``` - -Migration => set `target_type` - ---- - -[manage_sql_web_application_targets.c#L465](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L465) -```c++ - if (sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) ";", - SQL_RESOURCE_PARAM (web_application_target), - NULL)) - { - sql_rollback (); - return 1; - } -``` - -Migration => -`task_in_use ()` function? -Or simply replace `web_application_target` and `*_location` with `target` and `target_location` - ---- - -[manage_sql_web_application_targets.c#L497](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L497) -```c++ - if (sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - SQL_RESOURCE_PARAM (web_application_target), - NULL)) - { - sql_rollback (); - return 1; - } -``` - -Migration => -`task_in_use ()` function with optional hidden = 0 flag? -Or simply replace `web_application_target` and `*_location` with `target` and `target_location` - ---- - -[manage_sql_web_application_targets.c#L522](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L522) -```c++ - sql_ps ("UPDATE tasks" - " SET web_application_target = $1," - " web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) - " WHERE web_application_target = $2" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TABLE) ";", - SQL_RESOURCE_PARAM (trash_web_application_target), - SQL_RESOURCE_PARAM (web_application_target), - NULL); -``` - -Migration => `update_task_delete_target ()` - ---- - -[manage_sql_web_application_targets.c#L543](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L543) -```c++ - else if (sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TABLE) ";", - SQL_RESOURCE_PARAM (web_application_target), - NULL)) -``` - -Migration => - ---- - -[manage_sql_web_application_targets.c#L648](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L648) -```c++ - sql_ps ("UPDATE tasks" - " SET web_application_target = $1," - " web_application_target_location = " - G_STRINGIFY (LOCATION_TABLE) - " WHERE web_application_target = $2" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) ";", - SQL_RESOURCE_PARAM (web_application_target), - SQL_RESOURCE_PARAM (resource), - NULL); -``` - -Migration => `update_task_restore_target ()` - ---- - -[manage_sql_web_application_targets.c#L968](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L968) -```c++ -int -web_application_target_in_use (web_application_target_t web_application_target) -{ - return !!sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - SQL_RESOURCE_PARAM (web_application_target), - NULL); -} -``` - -Migration => - ---- - -[manage_sql_web_application_targets.c#L988](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L988) -```c++ -int -trash_web_application_target_in_use ( - web_application_target_t web_application_target) -{ - return !!sql_int_ps ("SELECT count(*) FROM tasks" - " WHERE web_application_target = $1" - " AND web_application_target_location = " - G_STRINGIFY (LOCATION_TRASH) ";", - SQL_RESOURCE_PARAM (web_application_target), - NULL); -} -``` - -Migration => - ---- - -[manage_sql_web_application_targets.c#L1055](https://github.com/greenbone/gvmd/blob/0e66fbe21cdfb489c3361c1ec6288d29de7cfc4f/src/manage_sql_web_application_targets.c#L1055) -```c++ -init_iterator (iterator, - "%s" - " SELECT name, uuid, %s FROM tasks" - " WHERE web_application_target = %llu" - " AND hidden = 0" - " ORDER BY name ASC;", - with_clause ? with_clause : "", - available, - web_application_target); -``` - -Migration => `target_type` - ---- - -
- From 4a20cc7b40ff6bbbf2ba146dbea913fd2c79452a Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 13:03:02 +0200 Subject: [PATCH 31/66] fix: bump database version --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b06b0e190d..20a44333d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,7 +96,7 @@ include(CPack) ## Variables -set(GVMD_DATABASE_VERSION 281) +set(GVMD_DATABASE_VERSION 282) set(GVMD_SCAP_DATABASE_VERSION 22) From 1ed89423e3d571c0d7d21606bc55ce99452a2538 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 13:06:16 +0200 Subject: [PATCH 32/66] bump database version on migration correctly --- src/manage_migrators.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 7fd680f882..3e058d558f 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4353,8 +4353,8 @@ migrate_281_to_282 () // " DROP COLUMN IF EXISTS web_application_target," // " DROP COLUMN IF EXISTS web_application_target_location"); // - /* Set the database version to 281 */ - set_db_version (281); + /* Set the database version to 282 */ + set_db_version (282); sql_commit(); From b1d27622f918384380d64d7ec0cd9a6262698433 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 13:06:50 +0200 Subject: [PATCH 33/66] fix comment --- src/manage_migrators.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 3e058d558f..7d88b5df04 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4239,7 +4239,7 @@ migrate_280_to_281 () } /** - * @brief Migrate the database from version 280 to version 281. + * @brief Migrate the database from version 281 to version 282. * * @return 0 success, -1 error. */ From 410e997351599acebd1d2308ae6bab24e2e40666 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 13:08:25 +0200 Subject: [PATCH 34/66] aligned checks for db version 282 --- src/manage_pg.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/manage_pg.c b/src/manage_pg.c index f76f659129..2d1dae6471 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -1285,7 +1285,7 @@ manage_create_sql_functions () "$$ LANGUAGE SQL;"); /* column hostname in table report_hosts was added in version 273 */ - if (current_db_version >= 281) + if (current_db_version >= 282) { sql ("CREATE OR REPLACE FUNCTION report_result_host_count (report integer," " min_qod integer)" @@ -1461,8 +1461,8 @@ manage_create_sql_functions () TASK_STATUS_DONE); } - /* target_type was added in version 281 */ - if (current_db_version >= 281) + /* target_type was added in version 282 */ + if (current_db_version >= 282) { sql ("CREATE OR REPLACE FUNCTION task_severity (integer," // task " integer," // overrides @@ -1579,7 +1579,7 @@ manage_create_sql_functions () TASK_STATUS_DONE); } - if (current_db_version >= 281) + if (current_db_version >= 282) { sql ("CREATE OR REPLACE FUNCTION task_trend (integer, integer, integer)" " RETURNS text AS $$" From 67669813919e7c3a50b0836c13edf85660c3d4bf Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 13:09:53 +0200 Subject: [PATCH 35/66] fixed wrong check for import task --- src/manage_sql.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 3d9a367599..d63f2bff75 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -7352,7 +7352,7 @@ task_severity_double (task_t task, int overrides, int min_qod, int offset) report_t report; if (current_credentials.uuid == NULL - || task_target_type (task) == TASKS_TARGET_TYPE_REGULAR/* import task. */) + || task_target_type (task) == TASKS_TARGET_TYPE_IMPORT_TASK) return SEVERITY_MISSING; report = sql_int64_0 ("SELECT id FROM reports" From a249d6d7085a96e885ee6feee00b704eb91dd7f6 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 13:19:46 +0200 Subject: [PATCH 36/66] utilize new helper functions for target_type_regular --- src/manage_sql.c | 10 +++------- src/manage_sql_targets.c | 26 ++++++-------------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index d63f2bff75..bd1047a477 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -26299,13 +26299,9 @@ manage_restore (const char *id) resource); /* Update the target in any trashcan tasks. */ - sql ("UPDATE tasks" - " SET target = %llu," - " target_location = " G_STRINGIFY (LOCATION_TABLE) - " WHERE target = %llu" - " AND target_location = " G_STRINGIFY (LOCATION_TRASH), - restored_target, - resource); + task_update_restore_target (restored_target, resource, + TASKS_TARGET_TYPE_REGULAR); + permissions_set_locations ("target", resource, sql_last_insert_id (), diff --git a/src/manage_sql_targets.c b/src/manage_sql_targets.c index e5353722cc..3086e1ac11 100644 --- a/src/manage_sql_targets.c +++ b/src/manage_sql_targets.c @@ -458,10 +458,7 @@ delete_target (const char *target_id, int ultimate) } /* Check if it's in use by a task in the trashcan. */ - if (sql_int ("SELECT count(*) FROM tasks" - " WHERE target = %llu" - " AND target_location = " G_STRINGIFY (LOCATION_TRASH) ";", - target)) + if (task_trash_target_in_use (target, TASKS_TARGET_TYPE_REGULAR)) { sql_rollback (); return 1; @@ -478,11 +475,7 @@ delete_target (const char *target_id, int ultimate) if (ultimate == 0) { - if (sql_int ("SELECT count(*) FROM tasks" - " WHERE target = %llu" - " AND target_location = " G_STRINGIFY (LOCATION_TABLE) - " AND hidden = 0;", - target)) + if (task_target_in_use (target, TASKS_TARGET_TYPE_REGULAR)) { sql_rollback (); return 1; @@ -513,13 +506,8 @@ delete_target (const char *target_id, int ultimate) trash_target, target); /* Update the location of the target in any trashcan tasks. */ - sql ("UPDATE tasks" - " SET target = %llu," - " target_location = " G_STRINGIFY (LOCATION_TRASH) - " WHERE target = %llu" - " AND target_location = " G_STRINGIFY (LOCATION_TABLE) ";", - sql_last_insert_id (), - target); + task_update_delete_target (target, sql_last_insert_id (), + TASKS_TARGET_TYPE_REGULAR); permissions_set_locations ("target", target, sql_last_insert_id (), @@ -528,10 +516,8 @@ delete_target (const char *target_id, int ultimate) sql_last_insert_id (), LOCATION_TRASH); } - else if (sql_int ("SELECT count(*) FROM tasks" - " WHERE target = %llu" - " AND target_location = " G_STRINGIFY (LOCATION_TABLE), - target)) + else if (task_target_in_use_including_hidden (target, + TASKS_TARGET_TYPE_REGULAR)) { sql_rollback (); return 1; From e2e1c3a1ff2bb3e1733ccbe109732908a2ec806d Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 13:21:02 +0200 Subject: [PATCH 37/66] g_error() => g_critical(), since g_error aborts --- src/manage_migrators.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 7d88b5df04..b870c57677 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4270,7 +4270,7 @@ migrate_281_to_282 () if (count_multiple_targets > 0) { - g_error ("Invalid data: Multiple targets specified for a task"); + g_critical ("Invalid data: Multiple targets specified for a task"); sql_rollback (); return -1; } From d94b52e5099f4721d33775a993f2e4c634a5e652 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 13:25:38 +0200 Subject: [PATCH 38/66] update migration to set target_type column NOT NULL and add default value --- src/manage_migrators.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index b870c57677..43c13a65db 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4287,7 +4287,7 @@ migrate_281_to_282 () /* Import tasks */ sql ("UPDATE tasks" " SET target_type = 0" - " WHERE target = 0" + " WHERE (target = 0 OR target IS NULL)" " AND oci_image_target IS NULL" " AND agent_group IS NULL" " AND web_application_target IS NULL;"); @@ -4313,6 +4313,10 @@ migrate_281_to_282 () " target_type = 4" " WHERE web_application_target IS NOT NULL;"); + sql ("UPDATE tasks SET target_type = 0 WHERE target_type IS NULL;"); + sql ("ALTER TABLE tasks ALTER COLUMN target_type SET NOT NULL;"); + sql ("ALTER TABLE tasks ALTER COLUMN target_type SET DEFAULT 0;"); + /** * Reverting DB version 281 to 280 is possible: * From 70feb5f62efcde9d840e7f84c5cb28c8008df873 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 14:56:02 +0200 Subject: [PATCH 39/66] fixed query still referencing the now dropped column `agent_group` --- src/manage_sql_agent_groups.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/manage_sql_agent_groups.c b/src/manage_sql_agent_groups.c index 6ab79c3551..73510b5dee 100644 --- a/src/manage_sql_agent_groups.c +++ b/src/manage_sql_agent_groups.c @@ -59,9 +59,11 @@ get_scanner_by_agent_group_id (agent_group_t agent_group_id, scanner_t *scanner) static int agent_group_in_use_in_hidden_task (agent_group_t agent_group) { - return !!sql_int ("SELECT COUNT(*) FROM tasks " - "WHERE hidden != 0 AND agent_group = %llu;", - agent_group); + return !!sql_int_ps ( + "SELECT COUNT(*) FROM tasks" + " WHERE hidden != 0 AND target = $1 AND target_type = $2;", + SQL_RESOURCE_PARAM (agent_group), + SQL_INT_PARAM (TASKS_TARGET_TYPE_AGENT_GROUP)); } /** From 3e222b1dc9e8c4d088b21c7f3a6f70bfc462d652 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 14:56:17 +0200 Subject: [PATCH 40/66] fill_network_target_reference() now has an early exit, if task type is not regular --- src/manage_sql_report_common.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/manage_sql_report_common.c b/src/manage_sql_report_common.c index e2ec23c907..0ea3cac4d2 100644 --- a/src/manage_sql_report_common.c +++ b/src/manage_sql_report_common.c @@ -409,14 +409,10 @@ fill_network_target_reference ( gchar *name = NULL; gchar *comment = NULL; - target = task_target (task); - - /* - * Import tasks do not have a target. - */ - if (target == 0) + if (task_target_type(task) != TASKS_TARGET_TYPE_REGULAR) return 0; + target = task_target (task); in_trash = task_target_in_trash (task); if (in_trash) From d61688d080cdd5254de3b41eb105bdd2c770ba3c Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 15:06:52 +0200 Subject: [PATCH 41/66] unify set_task_target() and set_task_target_and_location() --- src/gmp.c | 27 ++++++++++++------- src/manage.h | 69 +++++++++++++++++++++++------------------------- src/manage_sql.c | 35 +++++------------------- 3 files changed, 57 insertions(+), 74 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index 12e4c53010..6b5f328e7c 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -167,7 +167,8 @@ */ #define G_LOG_DOMAIN "md gmp" - + + /* Static headers. */ /** @todo Exported for manage_sql.c. */ @@ -175,7 +176,8 @@ void buffer_results_xml (GString *, iterator_t *, task_t, int, int, int, int, int, int, int, const char *, iterator_t *, int, int, int, int); - + + /* Helper functions. */ /** @@ -394,7 +396,8 @@ check_public_key (const char *key_str) return ret; } - + + /* GMP parser. */ static int @@ -457,7 +460,8 @@ command_disabled (gmp_parser_t *gmp_parser, const gchar *name) return 0; } - + + /* Command data passed between parser callbacks. */ /** @@ -3635,7 +3639,8 @@ command_data_init (command_data_t *data) memset (data, 0, sizeof (command_data_t)); } - + + /* Global variables. */ /** @@ -4248,7 +4253,8 @@ xml_context = NULL; */ static GMarkupParser xml_parser; - + + /* Client state. */ /** @@ -4904,7 +4910,8 @@ set_client_state (client_state_t state) g_debug (" client state set: %i", client_state); } - + + /* XML parser handlers. */ /** @@ -25829,8 +25836,7 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context, goto create_task_fail; } - set_task_agent_group_and_location (create_task_data->task, - agent_group); + set_task_agent_group (create_task_data->task, agent_group); } #endif /* ENABLE_AGENTS */ @@ -31153,7 +31159,8 @@ gmp_xml_handle_error (/* unused */ GMarkupParseContext* context, g_debug (" XML ERROR %s", error->message); } - + + /* GMP input processor. */ /** @todo Most likely the client should get these from init_gmp_process diff --git a/src/manage.h b/src/manage.h index a9154dd43b..240c34907a 100644 --- a/src/manage.h +++ b/src/manage.h @@ -253,7 +253,7 @@ parse_ssldetails (const char *, time_t *, time_t *, gchar **, gchar **); const char* tls_certificate_format_str (gnutls_x509_crt_fmt_t certificate_format); - + /* Credentials. */ extern credentials_t current_credentials; @@ -264,7 +264,7 @@ authenticate (credentials_t*); void logout_user (); - + /* Database. */ int @@ -324,7 +324,7 @@ validate_sort_field (const gchar*, const gchar*); void manage_session_set_timezone (const char *); - + /* Task macros and structures. */ /** @@ -444,7 +444,7 @@ check_scanner_feature (scanner_type_t); scanner_type_t get_scanner_type_by_uuid (const char *); - + /* Resources. */ int @@ -456,7 +456,7 @@ trash_id_exists (const char *, const char *); int delete_resource (const char *, const char *, int); - + /* Events and Alerts. */ /** @@ -561,7 +561,7 @@ delete_resource (const char *, const char *, int); "Please contact your local system administrator if you think it\n" \ "was created or assigned erroneously.\n" - + /* Task global variables and preprocessor variables. */ /** @@ -571,7 +571,7 @@ extern task_t current_scanner_task; extern report_t global_current_report; - + /* Task code specific to the representation of tasks. */ unsigned int @@ -676,9 +676,6 @@ task_target_in_trash (task_t); void set_task_target (task_t, target_t, tasks_target_type_t); -void -set_task_target_and_location (task_t, target_t, tasks_target_type_t); - void task_update_delete_target (target_t, target_t, tasks_target_type_t); @@ -696,7 +693,7 @@ task_trash_target_in_use (target_t, tasks_target_type_t); #if ENABLE_AGENTS void -set_task_agent_group_and_location (task_t task, agent_group_t agent_group); +set_task_agent_group (task_t task, agent_group_t agent_group); int agent_group_tasks_exist_by_scanner (scanner_t scanner); @@ -931,7 +928,7 @@ config_task_iterator_uuid (iterator_t*); int config_task_iterator_readable (iterator_t*); - + /* General severity related facilities. */ const char * @@ -975,7 +972,7 @@ void severity_data_level_counts (const severity_data_t*, int*, int*, int*, int*, int*, int*, int*); - + /* General task facilities. */ const char* @@ -996,7 +993,7 @@ resume_task (const char *, char **); int move_task (const char*, const char*); - + /* Results. */ /** @@ -1567,7 +1564,7 @@ manage_report_xml_page (report_t, const get_data_t *, const gchar *); int manage_report_result_count (report_t, const get_data_t *, int *); - + /* Reports. */ void @@ -1666,7 +1663,7 @@ prognosis_iterator_cve (iterator_t*); const char* prognosis_iterator_description (iterator_t*); - + /* Configs. * * These are here because they need definitions that are still in manage.h. */ @@ -1677,7 +1674,7 @@ create_task_check_scanner_type (scanner_t); int modify_task_check_config_scanner (task_t, const char *, const char *); - + /* NVT's. */ char * @@ -1807,7 +1804,7 @@ nvt_default_timeout (const char *); int family_nvt_count (const char *); - + /* NVT selectors. */ /** @@ -1860,7 +1857,7 @@ nvt_selector_iterator_include (iterator_t*); int nvt_selector_iterator_type (iterator_t*); - + /* NVT preferences. */ void @@ -1946,7 +1943,7 @@ nvt_severity_iterator_score (iterator_t *); const char * nvt_severity_iterator_value (iterator_t *); - + /* Credentials. */ /** @@ -2169,7 +2166,7 @@ credential_value (credential_t, const char*); gchar* credential_encrypted_value (credential_t, const char*); - + /* System reports. */ /** @@ -2201,7 +2198,7 @@ int manage_system_report (const char *, const char *, const char *, const char *, const char *, char **); - + /* Scanners. */ /** @@ -2506,13 +2503,13 @@ get_schedule_timeout (); void set_schedule_timeout (int); - + /* Schema. */ int manage_schema (gchar *, gchar **, gsize *, gchar **, gchar **); - + /* Trashcan. */ int @@ -2521,7 +2518,7 @@ manage_restore (const char *); int manage_empty_trashcan (); - + /* SecInfo */ int @@ -2699,7 +2696,7 @@ init_nvt_dfn_cert_adv_iterator (iterator_t*, const char*); const char* nvt_dfn_cert_adv_iterator_name (iterator_t*); - + /* Web Application VT data */ int @@ -2736,13 +2733,13 @@ web_application_vt_ref_iterator_type (iterator_t *); const char * web_application_vt_ref_iterator_ref_id (iterator_t *); - + /* All SecInfo Data */ int secinfo_count_after (const get_data_t *, const char *, time_t, gboolean); - + /* Vulns. */ int @@ -2790,7 +2787,7 @@ manage_get_radius_info (int *, char **, char **); void manage_set_radius_info (int, gchar *, gchar *); - + /* Resource aggregates */ /** @@ -2834,7 +2831,7 @@ aggregate_iterator_value (iterator_t*); const char* aggregate_iterator_subgroup_value (iterator_t*); - + /* Feeds. */ #define NVT_FEED 1 @@ -2949,7 +2946,7 @@ manage_asset_snapshot_delete_stale (int); int manage_dump_asset_snapshot_counts (GSList *, const db_conn_info_t *); - + /* Timezone info. */ array_t * manage_get_timezones (); @@ -2957,7 +2954,7 @@ manage_get_timezones (); gboolean manage_timezone_supported (const char *); - + /* Wizards. */ int @@ -2965,7 +2962,7 @@ manage_run_wizard (const gchar *, int (*) (void*, gchar*, gchar**), void *, array_t *, int, const char*, gchar **, gchar **, gchar **); - + /* Helpers. */ gchar * @@ -2986,19 +2983,19 @@ type_filter_columns (const char *); gboolean manage_migrate_needs_timezone (GSList *, const db_conn_info_t *); - + /* Optimize. */ int manage_optimize (GSList *, const db_conn_info_t *, const gchar *); - + /* Signal management */ int manage_cancel (); - + /* General settings */ const char * get_vt_verification_collation (); diff --git a/src/manage_sql.c b/src/manage_sql.c index bd1047a477..44ad113398 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -5955,6 +5955,7 @@ task_target_type (task_t task) /** * @brief Set the target of a task. + * Also updates the location to 0 (LOCATION_TABLE) * * @param[in] task Task. * @param[in] target Target. @@ -5962,26 +5963,6 @@ task_target_type (task_t task) */ void set_task_target (task_t task, target_t target, tasks_target_type_t target_type) -{ - sql_ps ("UPDATE tasks SET target = $1, target_type = $2," - " modification_time = m_now ()" - " WHERE id = $3;", - SQL_RESOURCE_PARAM (target), - SQL_INT_PARAM (target_type), - SQL_RESOURCE_PARAM (task), - NULL); -} - -/** - * @brief Set the target of a task, also updating the agent group location. - * - * @param[in] task Task. - * @param[in] target Target. - * @param[in] target_type The type of the target - */ -void -set_task_target_and_location (task_t task, target_t target, - tasks_target_type_t target_type) { sql_ps ("UPDATE tasks SET target = $1, target_type = $2, target_location = 0," " modification_time = m_now ()" @@ -6112,10 +6093,9 @@ task_trash_target_in_use (target_t target, tasks_target_type_t target_type) * @param[in] agent_group Agent group. */ void -set_task_agent_group_and_location (task_t task, agent_group_t agent_group) +set_task_agent_group (task_t task, agent_group_t agent_group) { - set_task_target_and_location (task, agent_group, - TASKS_TARGET_TYPE_AGENT_GROUP); + set_task_target (task, agent_group, TASKS_TARGET_TYPE_AGENT_GROUP); } /** @@ -6239,8 +6219,7 @@ task_oci_image_target_in_trash (task_t task) void set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) { - set_task_target_and_location (task, oci_image_target, - TASKS_TARGET_TYPE_OCI_IMAGE); + set_task_target (task, oci_image_target, TASKS_TARGET_TYPE_OCI_IMAGE); } #endif @@ -6285,8 +6264,8 @@ task_web_application_target_in_trash (task_t task) void set_task_web_application_target (task_t task, web_application_target_t web_application_target) { - set_task_target_and_location (task, web_application_target, - TASKS_TARGET_TYPE_WEB_APPLICATION); + set_task_target (task, web_application_target, + TASKS_TARGET_TYPE_WEB_APPLICATION); } #endif @@ -19388,7 +19367,7 @@ modify_task (const gchar *task_id, const gchar *name, return MODIFY_TASK_AGENT_GROUP_NOT_FOUND; else { - set_task_agent_group_and_location (task, agent_group); + set_task_agent_group (task, agent_group); } } #endif From 34169a540e8f15d53e979b61e0b9aad110552d19 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 15:13:26 +0200 Subject: [PATCH 42/66] Migration to 282 now prints the tasks with multiple targets specified, allowing users to fix the affected tasks --- src/manage_migrators.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 43c13a65db..fc6a18022a 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4256,21 +4256,31 @@ migrate_281_to_282 () } /* Check for invalid data */ - int count_multiple_targets = - sql_int ("SELECT COUNT(*) FROM tasks" - " WHERE (agent_group IS NOT NULL" - " AND oci_image_target IS NOT NULL)" - " OR (agent_group IS NOT NULL" - " AND web_application_target IS NOT NULL)" - " OR (oci_image_target IS NOT NULL" - " AND web_application_target IS NOT NULL)" - " OR (target != 0 AND agent_group IS NOT NULL)" - " OR (target != 0 AND oci_image_target IS NOT NULL)" - " OR (target != 0 AND web_application_target IS NOT NULL);"); - + int count_multiple_targets = 0; + iterator_t tasks_multiple_targets; + init_iterator ( + &tasks_multiple_targets, + "SELECT uuid, name FROM tasks" + " WHERE (agent_group IS NOT NULL" + " AND oci_image_target IS NOT NULL)" + " OR (agent_group IS NOT NULL" + " AND web_application_target IS NOT NULL)" + " OR (oci_image_target IS NOT NULL" + " AND web_application_target IS NOT NULL)" + " OR (target != 0 AND agent_group IS NOT NULL)" + " OR (target != 0 AND oci_image_target IS NOT NULL)" + " OR (target != 0 AND web_application_target IS NOT NULL);"); + + while (next (&tasks_multiple_targets)) + { + g_critical ("Invalid data: Multiple targets specified for task %s (%s)", + iterator_string (&tasks_multiple_targets, 0), + iterator_string (&tasks_multiple_targets, 1)); + } if (count_multiple_targets > 0) { - g_critical ("Invalid data: Multiple targets specified for a task"); + g_critical ("Aborting migration, since there are %d tasks with multiple targets specified.", + count_multiple_targets); sql_rollback (); return -1; } From e072fb4d2a14791399b32f13f1ad4282dff7c652 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 15:14:01 +0200 Subject: [PATCH 43/66] fixed count not incrementing --- src/manage_migrators.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index fc6a18022a..fb79be0f1b 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4276,6 +4276,7 @@ migrate_281_to_282 () g_critical ("Invalid data: Multiple targets specified for task %s (%s)", iterator_string (&tasks_multiple_targets, 0), iterator_string (&tasks_multiple_targets, 1)); + count_multiple_targets++; } if (count_multiple_targets > 0) { From 3de3fc20c5afb0f2b557bfc234ab3bcaefe892ee Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 15:14:54 +0200 Subject: [PATCH 44/66] cleanup iterator properly --- src/manage_migrators.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index fb79be0f1b..0353a87b50 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4278,6 +4278,8 @@ migrate_281_to_282 () iterator_string (&tasks_multiple_targets, 1)); count_multiple_targets++; } + cleanup_iterator (&tasks_multiple_targets); + if (count_multiple_targets > 0) { g_critical ("Aborting migration, since there are %d tasks with multiple targets specified.", From cafb4435519440398355ba95054df9c0ac0ef1a9 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 15:16:26 +0200 Subject: [PATCH 45/66] CREATE TABLE tasks now complies with the state after migration to 282 --- src/manage_pg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manage_pg.c b/src/manage_pg.c index 2d1dae6471..08f233d4bb 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -3230,7 +3230,7 @@ create_tables () " end_time integer," " config integer," // REFERENCES configs (id) ON DELETE RESTRICT," " target integer," // REFERENCES targets (id) ON DELETE RESTRICT," - " target_type integer," + " target_type integer NOT NULL DEFAULT 0," " schedule integer," // REFERENCES schedules (id) ON DELETE RESTRICT," " schedule_next_time integer," " schedule_periods integer," From 30fbe4638d6379f0d701580600142a0adbf93df0 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 15:18:59 +0200 Subject: [PATCH 46/66] task_target_type() now returns `TASKS_TARGET_TYPE_UNDEFINED` in case of failure --- src/manage_sql.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 44ad113398..1d2191ae02 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -5948,7 +5948,7 @@ task_target_type (task_t task) default: /* Programming error. */ assert (0); case -1: - return 0; + return TASKS_TARGET_TYPE_UNDEFINED; break; } } From 108bd17859f5bc29c3e2166fcb9a29e1acec7790 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 15:47:36 +0200 Subject: [PATCH 47/66] modify_task() now throws an error if multiple target fields are set (target, agent_group, oci_image_target, web_application_target) --- src/gmp.c | 11 ++++ src/manage.h | 4 ++ src/manage_sql.c | 158 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 168 insertions(+), 5 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index 6b5f328e7c..1309bfee54 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -29040,6 +29040,17 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context, modify_task_data->task_id, "modified"); break; + case MODIFY_TASK_MULTIPLE_TARGETS: + SEND_TO_CLIENT_OR_FAIL + (XML_ERROR_SYNTAX ("modify_task", + "Only one of target, agent_group," + " oci_image_target or" + " web_application_target" + " must be provided")); + log_event_fail ("task", "Task", + modify_task_data->task_id, + "modified"); + break; default: case MODIFY_TASK_ERROR: SEND_TO_CLIENT_OR_FAIL diff --git a/src/manage.h b/src/manage.h index 240c34907a..96fb237c1f 100644 --- a/src/manage.h +++ b/src/manage.h @@ -152,6 +152,7 @@ typedef enum { MODIFY_TASK_CANNOT_SET_SCAN_MODE_VALUE = 23, MODIFY_TASK_CANNOT_SET_AJAX_SPIDER_TIMEOUT_VALUE = 24, MODIFY_TASK_TARGET_SCANNER_TYPE_MISMATCH = 25, + MODIFY_TASK_MULTIPLE_TARGETS = 26, MODIFY_TASK_ERROR = -1 } modify_task_return_t; @@ -444,6 +445,9 @@ check_scanner_feature (scanner_type_t); scanner_type_t get_scanner_type_by_uuid (const char *); +tasks_target_type_t +scanner_type_target_type (scanner_type_t); + /* Resources. */ diff --git a/src/manage_sql.c b/src/manage_sql.c index 1d2191ae02..cb643e1f64 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -19123,6 +19123,129 @@ manage_task_remove_file (const gchar *task_id, const char *name) return -1; } +/** + * @brief Get the task target type that a scanner type requires. + * + * @param[in] type Scanner type. + * + * @return The target type associated with the scanner type, + * TASKS_TARGET_TYPE_UNDEFINED if the scanner type is unknown. + */ +tasks_target_type_t +scanner_type_target_type (scanner_type_t type) +{ + switch (type) + { + case SCANNER_TYPE_OPENVAS: + case SCANNER_TYPE_OSP_SENSOR: + case SCANNER_TYPE_OPENVASD: + case SCANNER_TYPE_OPENVASD_SENSOR: + case SCANNER_TYPE_CVE: + return TASKS_TARGET_TYPE_REGULAR; + case SCANNER_TYPE_AGENT_CONTROLLER: + case SCANNER_TYPE_AGENT_CONTROLLER_SENSOR: + return TASKS_TARGET_TYPE_AGENT_GROUP; + case SCANNER_TYPE_CONTAINER_IMAGE: + return TASKS_TARGET_TYPE_OCI_IMAGE; + case SCANNER_TYPE_WEB_APPLICATION: + return TASKS_TARGET_TYPE_WEB_APPLICATION; + default: + return TASKS_TARGET_TYPE_UNDEFINED; + } +} + +/** + * @brief Whether a modify_task resource element requests a change. + * + * @param[in] resource_id The id from the request, or NULL. + * + * @return 1 if the element requests a change, else 0. + */ +static int +modify_task_resource_given (const gchar *resource_id) +{ + return resource_id && strcmp (resource_id, "0"); +} + +/** + * @brief Check that a modify_task request carries a consistent target. + * + * @param[in] task Task. + * @param[in] scanner_id Scanner from the request, or NULL. + * @param[in] target_id Target from the request, or NULL. + * @param[in] agent_group_id Agent group from the request, or NULL. + * @param[in] oci_image_target_id OCI image target, or NULL. + * @param[in] web_application_target_id Web application target, or NULL. + * + * @return 0 consistent, 1 more than one target given, 2 target kind does not + * match the scanner type, 3 failed to find scanner, -1 error. + */ +static int +modify_task_check_target_scanner (task_t task, + const gchar *scanner_id, + const gchar *target_id, + const gchar *agent_group_id, + const gchar *oci_image_target_id, + const gchar *web_application_target_id) +{ + scanner_t scanner; + tasks_target_type_t given_type, required_type; + int given_count; + + given_count = 0; + given_type = TASKS_TARGET_TYPE_UNDEFINED; + + if (modify_task_resource_given (target_id)) + { + given_count++; + given_type = TASKS_TARGET_TYPE_REGULAR; + } + if (modify_task_resource_given (agent_group_id)) + { + given_count++; + given_type = TASKS_TARGET_TYPE_AGENT_GROUP; + } + if (modify_task_resource_given (oci_image_target_id)) + { + given_count++; + given_type = TASKS_TARGET_TYPE_OCI_IMAGE; + } + if (modify_task_resource_given (web_application_target_id)) + { + given_count++; + given_type = TASKS_TARGET_TYPE_WEB_APPLICATION; + } + + if (given_count == 0) + return 0; + if (given_count > 1) + return 1; + + /* Resolve the scanner that the task will have after this request. */ + scanner = 0; + if (modify_task_resource_given (scanner_id)) + { + if (find_scanner_with_permission (scanner_id, &scanner, "get_scanners")) + return -1; + if (scanner == 0) + return 3; + } + else + scanner = task_scanner (task); + + required_type = scanner_type_target_type (scanner_type (scanner)); + + if (required_type == TASKS_TARGET_TYPE_UNDEFINED) + { + g_warning ("%s: task %llu has scanner %llu of unknown type," + " skipping target type check", + __func__, task, scanner); + return 0; + } + + return given_type == required_type ? 0 : 2; +} + /** * @brief Modify a task. * @@ -19199,6 +19322,26 @@ modify_task (const gchar *task_id, const gchar *name, return MODIFY_TASK_ERROR; } + switch (modify_task_check_target_scanner (task, scanner_id, target_id, + agent_group_id, + oci_image_target_id, + web_application_target_id)) + { + case 0: + break; + case 1: + return MODIFY_TASK_MULTIPLE_TARGETS; + case 2: + return MODIFY_TASK_TARGET_SCANNER_TYPE_MISMATCH; + case 3: + return MODIFY_TASK_SCANNER_NOT_FOUND; + default: + assert (0); + /* fallthrough */ + case -1: + return MODIFY_TASK_ERROR; + } + if (name) set_task_name (task, name); @@ -19339,9 +19482,6 @@ modify_task (const gchar *task_id, const gchar *name, else if ((task_run_status (task) != TASK_STATUS_NEW) && (task_alterable (task) == 0)) return MODIFY_TASK_TARGET_STATUS_MUST_BE_NEW; - else if (type_of_scanner == SCANNER_TYPE_CONTAINER_IMAGE - || type_of_scanner == SCANNER_TYPE_WEB_APPLICATION) - return MODIFY_TASK_TARGET_SCANNER_TYPE_MISMATCH; else if (find_target_with_permission (target_id, &target, "get_targets")) @@ -19356,7 +19496,11 @@ modify_task (const gchar *task_id, const gchar *name, agent_group_t agent_group; agent_group = 0; - if ((task_run_status(task) != TASK_STATUS_NEW) + if (strcmp (agent_group_id, "0") == 0) + { + /* Leave it as it is. */ + } + else if ((task_run_status(task) != TASK_STATUS_NEW) && (task_alterable(task) == 0)) return MODIFY_TASK_TARGET_STATUS_MUST_BE_NEW; else if (find_agent_group_with_permission(agent_group_id, @@ -19400,7 +19544,11 @@ modify_task (const gchar *task_id, const gchar *name, { web_application_target_t web_application_target = 0; - if ((task_run_status (task) != TASK_STATUS_NEW) + if (strcmp (web_application_target_id, "0") == 0) + { + /* Leave it as is. */ + } + else if ((task_run_status (task) != TASK_STATUS_NEW) && (task_alterable (task) == 0)) return MODIFY_TASK_TARGET_STATUS_MUST_BE_NEW; else if (find_web_application_target_with_permission (web_application_target_id, From 6b09dc86a0c208caad497d8f6085d40f1501899d Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 15:51:35 +0200 Subject: [PATCH 48/66] added missing NULL terminator in prepared statement call --- src/manage_sql_agent_groups.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/manage_sql_agent_groups.c b/src/manage_sql_agent_groups.c index 73510b5dee..2cb62223e4 100644 --- a/src/manage_sql_agent_groups.c +++ b/src/manage_sql_agent_groups.c @@ -63,7 +63,8 @@ agent_group_in_use_in_hidden_task (agent_group_t agent_group) "SELECT COUNT(*) FROM tasks" " WHERE hidden != 0 AND target = $1 AND target_type = $2;", SQL_RESOURCE_PARAM (agent_group), - SQL_INT_PARAM (TASKS_TARGET_TYPE_AGENT_GROUP)); + SQL_INT_PARAM (TASKS_TARGET_TYPE_AGENT_GROUP), + NULL); } /** From 040fc9c06f83f3f18aa31a14f1501eb76cabfb5f Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 17:31:52 +0200 Subject: [PATCH 49/66] target_type: keep new instances in sync with migrated ones --- src/manage_migrators.c | 2 ++ src/manage_pg.c | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 0353a87b50..1643b94df1 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4362,6 +4362,8 @@ migrate_281_to_282 () // For safety reasons, we decided to keep the columns for now and only // drop them after a certain period, when multiple customers have // already migrated without any issues. + // When they are dropped, they must also be removed from the tasks + // table in create_tables () /* Drop old, unused target columns */ // sql ("ALTER TABLE tasks DROP COLUMN IF EXISTS agent_group," // " DROP COLUMN IF EXISTS agent_group_location," diff --git a/src/manage_pg.c b/src/manage_pg.c index 08f233d4bb..2882f24a92 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -3229,21 +3229,27 @@ create_tables () " start_time integer," " end_time integer," " config integer," // REFERENCES configs (id) ON DELETE RESTRICT," - " target integer," // REFERENCES targets (id) ON DELETE RESTRICT," - " target_type integer NOT NULL DEFAULT 0," + " target integer," // May refer to several tables, see target_type. " schedule integer," // REFERENCES schedules (id) ON DELETE RESTRICT," " schedule_next_time integer," " schedule_periods integer," " scanner integer," // REFERENCES scanner (id) ON DELETE RESTRICT," + " agent_group integer," // Deprecated. " config_location integer," " target_location integer," " schedule_location integer," " scanner_location integer," + " oci_image_target integer," // Deprecated. + " oci_image_target_location integer," // Deprecated. + " agent_group_location integer," // Deprecated. + " web_application_target integer," // Deprecated. + " web_application_target_location integer," // Deprecated. " upload_result_count integer," " alterable integer," " creation_time integer," " modification_time integer," - " usage_type text);"); + " usage_type text," + " target_type integer NOT NULL DEFAULT 0);"); sql ("CREATE TABLE IF NOT EXISTS task_files" " (id SERIAL PRIMARY KEY," From ef0b9f018a0e23504dbc0475e816b9261081365e Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 17:34:27 +0200 Subject: [PATCH 50/66] fixed db version in comments --- src/manage_migrators.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 1643b94df1..2cd5e98ea5 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4331,7 +4331,7 @@ migrate_281_to_282 () sql ("ALTER TABLE tasks ALTER COLUMN target_type SET DEFAULT 0;"); /** - * Reverting DB version 281 to 280 is possible: + * Reverting data from DB version 282 to 281 is possible: * * UPDATE tasks * SET agent_group = target, From b61f0bb2d678a3f010ae0fff5d71f474d18977b7 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 09:32:04 +0200 Subject: [PATCH 51/66] fixed condition in create_report(), since target_type was introduced --- src/manage_sql.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index cb643e1f64..9323cd8ac9 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -9534,7 +9534,7 @@ create_report (array_t *results, const char *task_id, const char *in_assets, rc = -1; else if (task == 0) rc = -4; - else if (task_target (task)) + else if (task_target_type (task) != TASKS_TARGET_TYPE_IMPORT_TASK) rc = -5; if (rc) { From 7a9ebd316d9ff4c1cbcf094a455f0133adde9aad Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 10:59:00 +0200 Subject: [PATCH 52/66] handle_get_tasks(): target_in_trash is now correctly handled based on target_type --- src/gmp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/gmp.c b/src/gmp.c index 1309bfee54..11059d485f 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -20078,7 +20078,9 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) else { SEND_GET_COMMON (task, &get_tasks_data->get, &tasks); - target_in_trash = task_target_in_trash (index); + target_in_trash = (target_type == TASKS_TARGET_TYPE_REGULAR) + ? task_target_in_trash (index) + : 0; if (target && (target == 0) && (task_iterator_run_status (&tasks) == TASK_STATUS_RUNNING)) From a6faa47cddddbb5e16c46adfc7c3dae01c9cd594 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 11:00:48 +0200 Subject: [PATCH 53/66] handle_get_tasks(): fixed dead if branch, utilizing new target_type --- src/gmp.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index 11059d485f..ed39931d72 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -20081,9 +20081,8 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) target_in_trash = (target_type == TASKS_TARGET_TYPE_REGULAR) ? task_target_in_trash (index) : 0; - if (target && (target == 0) - && (task_iterator_run_status (&tasks) - == TASK_STATUS_RUNNING)) + if (target_type == TASKS_TARGET_TYPE_IMPORT_TASK + && (task_iterator_run_status (&tasks) == TASK_STATUS_RUNNING)) { progress_xml = g_strdup_printf ("%i", From d134da06afcdf6197443cada0361fc0684224539 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 11:04:59 +0200 Subject: [PATCH 54/66] print_report_xml_start(): utilizing new target_type for more clarity in if-condition --- src/manage_sql.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 9323cd8ac9..8308c46c1b 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -16333,6 +16333,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task, char *tsk_name, *task_target_uuid, *task_target_name; char *task_target_comment, *comment; target_t target; + tasks_target_type_t target_type; gchar *progress_xml; iterator_t tags; @@ -16345,7 +16346,8 @@ print_report_xml_start (report_t report, report_t delta, task_t task, comment = task_comment (task); - if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) + target_type = task_target_type (task); + if (target_type != TASKS_TARGET_TYPE_REGULAR) target = 0; else target = task_target (task); @@ -16369,7 +16371,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task, task_target_comment = NULL; } - if ((target == 0) + if (target_type == TASKS_TARGET_TYPE_IMPORT_TASK && (task_run_status (task) == TASK_STATUS_RUNNING)) progress_xml = g_strdup_printf ("%i", From c5f1c76860445a9893968ba14b12d3d382881141 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 11:10:10 +0200 Subject: [PATCH 55/66] migrated tasks.target column to NOT NULL DEFAULT 0 --- src/manage_migrators.c | 5 ++++- src/manage_pg.c | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 2cd5e98ea5..ff8efc8abb 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4298,9 +4298,10 @@ migrate_281_to_282 () " WHERE target != 0;"); /* Import tasks */ + sql("UPDATE tasks SET target = 0 WHERE target IS NULL;"); sql ("UPDATE tasks" " SET target_type = 0" - " WHERE (target = 0 OR target IS NULL)" + " WHERE target = 0" " AND oci_image_target IS NULL" " AND agent_group IS NULL" " AND web_application_target IS NULL;"); @@ -4329,6 +4330,8 @@ migrate_281_to_282 () sql ("UPDATE tasks SET target_type = 0 WHERE target_type IS NULL;"); sql ("ALTER TABLE tasks ALTER COLUMN target_type SET NOT NULL;"); sql ("ALTER TABLE tasks ALTER COLUMN target_type SET DEFAULT 0;"); + sql ("ALTER TABLE tasks ALTER COLUMN target SET NOT NULL;"); + sql ("ALTER TABLE tasks ALTER COLUMN target SET DEFAULT 0;"); /** * Reverting data from DB version 282 to 281 is possible: diff --git a/src/manage_pg.c b/src/manage_pg.c index 2882f24a92..72d93b51c3 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -3229,7 +3229,7 @@ create_tables () " start_time integer," " end_time integer," " config integer," // REFERENCES configs (id) ON DELETE RESTRICT," - " target integer," // May refer to several tables, see target_type. + " target integer NOT NULL DEFAULT 0," // May refer to several tables, see target_type. " schedule integer," // REFERENCES schedules (id) ON DELETE RESTRICT," " schedule_next_time integer," " schedule_periods integer," From bc0b0c94a26346fb8f1486063d611518c8b9e681 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 11:27:18 +0200 Subject: [PATCH 56/66] interrupt task when no regular target is available --- src/manage.c | 18 ++++++------------ src/manage.h | 3 +++ src/manage_scan_handler.c | 24 ++++++++++++++++-------- src/manage_sql.c | 34 +++++++++++++++++++++++++++++++++- 4 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/manage.c b/src/manage.c index b745240879..e5da05369e 100644 --- a/src/manage.c +++ b/src/manage.c @@ -1707,10 +1707,8 @@ run_osp_task (task_t task, int from, char **report_id) { target_t target; - if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) - target = 0; - else - target = task_target (task); + if (task_regular_target (task, &target)) + return -1; if (target) { @@ -1921,10 +1919,8 @@ run_cve_task (task_t task) { target_t target; - if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) - target = 0; - else - target = task_target (task); + if (task_regular_target (task, &target)) + return -1; if (target) { @@ -7103,10 +7099,8 @@ run_openvasd_task (task_t task, int from, char **report_id) } target_t target; - if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) - target = 0; - else - target = task_target (task); + if (task_regular_target (task, &target)) + return -1; if (target) { diff --git a/src/manage.h b/src/manage.h index 96fb237c1f..1d482adf5e 100644 --- a/src/manage.h +++ b/src/manage.h @@ -674,6 +674,9 @@ task_target (task_t); tasks_target_type_t task_target_type (task_t); +int +task_regular_target (task_t, target_t *); + int task_target_in_trash (task_t); diff --git a/src/manage_scan_handler.c b/src/manage_scan_handler.c index b78c5eac6b..13401999b4 100644 --- a/src/manage_scan_handler.c +++ b/src/manage_scan_handler.c @@ -53,10 +53,14 @@ handle_queued_osp_scan (const char *scan_id, report_t report, int rc; target_t target; - if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) - target = 0; - else - target = task_target (task); + if (task_regular_target (task, &target)) + { + set_task_interrupted (task, + "Internal error:" + " Failed to get target of task"); + set_report_scan_run_status (report, TASK_STATUS_INTERRUPTED); + return -1; + } rc = handle_osp_scan_start (task, target, scan_id, start_from, TRUE, &discovery_scan); @@ -102,10 +106,14 @@ handle_queued_openvasd_scan (const char *scan_id, report_t report, int rc; target_t target; - if (task_target_type (task) != TASKS_TARGET_TYPE_REGULAR) - target = 0; - else - target = task_target (task); + if (task_regular_target (task, &target)) + { + set_task_interrupted (task, + "Internal error:" + " Failed to get target of task"); + set_report_scan_run_status (report, TASK_STATUS_INTERRUPTED); + return -1; + } rc = handle_openvasd_scan_start (task, target, scan_id, start_from, TRUE, &discovery_scan); diff --git a/src/manage_sql.c b/src/manage_sql.c index 8308c46c1b..16bff76060 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -5930,7 +5930,7 @@ task_target (task_t task) * * @param[in] task Task. * - * @return Target of task. + * @return Target type of task, TASKS_TARGET_TYPE_UNDEFINED on error. */ tasks_target_type_t task_target_type (task_t task) @@ -5953,6 +5953,38 @@ task_target_type (task_t task) } } +/** + * @brief Get the regular target of a task. + * + * Fails if the target type of the task cannot be read, or if the task has no + * regular target. + * + * @param[in] task Task. + * @param[out] target Regular target of the task, or 0 if it has none. Also + * set to 0 on error. + * + * @return 0 success, -1 failed to get the target type of the task. + */ +int +task_regular_target (task_t task, target_t *target) +{ + assert (target); + *target = 0; + + tasks_target_type_t target_type = task_target_type (task); + if (target_type == TASKS_TARGET_TYPE_UNDEFINED) + { + g_warning ("%s: failed to get the target type of task %llu", + __func__, task); + return -1; + } + + if (target_type == TASKS_TARGET_TYPE_REGULAR) + *target = task_target (task); + + return 0; +} + /** * @brief Set the target of a task. * Also updates the location to 0 (LOCATION_TABLE) From 0bfeaf252c142791ce90e31f85093d979d004590 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 11:30:26 +0200 Subject: [PATCH 57/66] removed `target = 0` checks, when `target_type` is available --- src/manage_pg.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/manage_pg.c b/src/manage_pg.c index 72d93b51c3..20c78d07bf 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -1470,8 +1470,7 @@ manage_create_sql_functions () " RETURNS double precision AS $$" /* Calculate the severity of a task. */ " SELECT CASE" - " WHEN (SELECT target = 0 " - " AND target_type = 0 " // TASKS_TARGET_TYPE_IMPORT_TASK + " WHEN (SELECT target_type = 0" // TASKS_TARGET_TYPE_IMPORT_TASK " FROM tasks WHERE id = $1)" " THEN CAST (NULL AS double precision)" " ELSE" @@ -1610,7 +1609,7 @@ manage_create_sql_functions () " WHEN gvmd_user () = 0" " THEN RETURN ''::text;" /* Skip running and import tasks. */ - " WHEN (SELECT run_status = %u OR (target = 0 OR target_type = 0)" // TASKS_TARGET_TYPE_IMPORT_TASK + " WHEN (SELECT run_status = %u OR target_type = 0" // TASKS_TARGET_TYPE_IMPORT_TASK " FROM tasks WHERE id = $1)" " THEN RETURN ''::text;" " ELSE" From 0d4331abf734fcf5f2d75505045e346138dddd36 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 11:45:15 +0200 Subject: [PATCH 58/66] handle_get_tasks(): reduced SQL queries, since `target` is now polymorphic and may be reused --- src/gmp.c | 53 +++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index ed39931d72..6d904e8007 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -20029,6 +20029,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) report_t running_report; char *owner, *observers; int target_in_trash, scanner_in_trash; + int target_location_trash; int holes = 0, infos = 0, logs = 0, warnings = 0; int holes_2 = 0, infos_2 = 0, warnings_2 = 0; int criticals = 0, criticals_2 = 0; @@ -20078,8 +20079,14 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) else { SEND_GET_COMMON (task, &get_tasks_data->get, &tasks); + + target_location_trash + = (target_type == TASKS_TARGET_TYPE_IMPORT_TASK) + ? 0 + : task_target_in_trash (index); + target_in_trash = (target_type == TASKS_TARGET_TYPE_REGULAR) - ? task_target_in_trash (index) + ? target_location_trash : 0; if (target_type == TASKS_TARGET_TYPE_IMPORT_TASK && (task_iterator_run_status (&tasks) == TASK_STATUS_RUNNING)) @@ -20359,17 +20366,14 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) } #if ENABLE_CONTAINER_SCANNING - oci_image_target_t oci_image_target; - int oci_image_target_in_trash, oci_image_target_available; - char *task_oci_image_target_uuid, *task_oci_image_target_name; - gchar *task_oci_image_target_name_escaped; - - oci_image_target = task_oci_image_target (index); - oci_image_target_in_trash = task_oci_image_target_in_trash (index); - if (target_type == TASKS_TARGET_TYPE_OCI_IMAGE) { - oci_image_target_available = 1; + oci_image_target_t oci_image_target = target; + int oci_image_target_in_trash = target_location_trash; + int oci_image_target_available = 1; + char *task_oci_image_target_uuid, *task_oci_image_target_name; + gchar *task_oci_image_target_name_escaped; + if (oci_image_target_in_trash) { task_oci_image_target_uuid @@ -20422,15 +20426,14 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) } #endif #if ENABLE_AGENTS - agent_group_t agent_group = 0; - int group_readable = 0; - char *task_agent_group_uuid = NULL; - char *task_agent_group_name = NULL; - agent_group = task_agent_group (index); /* row id or 0 */ - int agent_group_in_trash = task_agent_group_in_trash (index); - if (target_type == TASKS_TARGET_TYPE_AGENT_GROUP) { + agent_group_t agent_group = target; + int agent_group_in_trash = target_location_trash; + int group_readable = 0; + char *task_agent_group_uuid = NULL; + char *task_agent_group_name = NULL; + if (agent_group_in_trash) { task_agent_group_uuid = trash_agent_group_uuid (agent_group); @@ -20461,17 +20464,15 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) } #endif /* ENABLE_AGENT_GROUPS */ #if ENABLE_WEB_APPLICATION_SCANNING - web_application_target_t web_application_target; - int web_application_target_in_trash, web_application_target_available; - char *task_web_application_target_uuid, *task_web_application_target_name; - gchar *task_web_application_target_name_escaped; - - web_application_target = task_web_application_target (index); - web_application_target_in_trash = task_web_application_target_in_trash (index); - if (target_type == TASKS_TARGET_TYPE_WEB_APPLICATION) { - web_application_target_available = 1; + web_application_target_t web_application_target = target; + int web_application_target_in_trash = target_location_trash; + int web_application_target_available = 1; + char *task_web_application_target_uuid; + char *task_web_application_target_name; + gchar *task_web_application_target_name_escaped; + if (web_application_target_in_trash) { task_web_application_target_uuid From 5963b092da6a9ef9309f2b47357ef33f9fc796e7 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 12:05:13 +0200 Subject: [PATCH 59/66] gmp `CREATE_TASK` now checks for multiple target types provided --- src/gmp.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index 6d904e8007..4dcb1b2668 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -25609,6 +25609,7 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context, gboolean is_agent_task = FALSE; gboolean is_container_scanning_task = FALSE; gboolean is_web_application_scanning_task = FALSE; + int targets_given; guint index; /* @todo Buffer the entire task creation and pass everything to a @@ -25778,11 +25779,23 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context, goto create_task_fail; } - if (create_task_data->target_id && create_task_data->agent_group_id) + targets_given = 0; + if (create_task_data->target_id) + targets_given++; + if (create_task_data->agent_group_id) + targets_given++; + if (create_task_data->oci_image_target_id) + targets_given++; + if (create_task_data->web_application_target_id) + targets_given++; + + if (targets_given > 1) { SEND_TO_CLIENT_OR_FAIL (XML_ERROR_SYNTAX ("create_task", - "Only one of target_id or agent_group_id must be provided")); + "Only one of target_id, agent_group_id," + " oci_image_target_id or" + " web_application_target_id may be given")); goto create_task_fail; } if (create_task_data->target_id && is_container_scanning_task) From 341f8a038eb58e337faa039006b4b39cd3687a0e Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 12:06:46 +0200 Subject: [PATCH 60/66] create index on target_type, target and target_location --- src/manage_pg.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/manage_pg.c b/src/manage_pg.c index 20c78d07bf..11b359030f 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -3869,6 +3869,10 @@ create_tables () sql ("SELECT create_index ('tag_resources_trash_by_tag'," " 'tag_resources_trash', 'tag');"); + sql ("SELECT create_index ('tasks_by_target'," + " 'tasks'," + " 'target_type, target, target_location');"); + sql ("SELECT create_index ('tls_certificate_locations_by_host_ip'," " 'tls_certificate_locations', 'host_ip')"); From 245ae0410a4bc094e16196528501bfc277841cde Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 12:07:16 +0200 Subject: [PATCH 61/66] get_report_task_progress() is now target_type aware --- src/manage_sql_report_common.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/manage_sql_report_common.c b/src/manage_sql_report_common.c index 0ea3cac4d2..786e93b550 100644 --- a/src/manage_sql_report_common.c +++ b/src/manage_sql_report_common.c @@ -246,9 +246,8 @@ fill_report_scan_information (report_t report, report_summary_base_t summary) /** * @brief Calculate report task progress. * - * Agent tasks are always reported as complete. Running upload tasks without - * a network target use their upload progress. All other tasks use the report - * progress. + * Agent tasks are always reported as complete. Running import tasks use + * their upload progress. All other tasks use the report progress. * * @param[in] report Report resource. * @param[in] task Task associated with the report. @@ -258,19 +257,17 @@ fill_report_scan_information (report_t report, report_summary_base_t summary) static int get_report_task_progress (report_t report, task_t task) { - target_t target; - if (report == 0 || task == 0) return 0; + tasks_target_type_t target_type = task_target_type (task); + #if ENABLE_AGENTS - if (task_agent_group (task) != 0) + if (target_type == TASKS_TARGET_TYPE_AGENT_GROUP) return 100; #endif - target = task_target (task); - - if (target == 0 + if (target_type == TASKS_TARGET_TYPE_IMPORT_TASK && task_run_status (task) == TASK_STATUS_RUNNING) return task_upload_progress (task); From 9b70677179a69670bee9428dd650b36155c519e4 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Mon, 17 Aug 2026 12:47:36 +0200 Subject: [PATCH 62/66] added some comments to sql numeric literals --- src/manage_sql.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 16bff76060..bad0c7a276 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -2565,14 +2565,14 @@ append_to_task_string (task_t task, const char* field, const char* value) ,{ \ "(SELECT uuid FROM agent_groups" \ " WHERE agent_groups.id = tasks.target" \ - " AND tasks.target_type = 2)", \ + " AND tasks.target_type = 2)",/*TASKS_TARGET_TYPE_AGENT_GROUP*/ \ "agent_group_id", \ KEYWORD_TYPE_STRING \ }, \ { \ "(SELECT name FROM agent_groups" \ " WHERE agent_groups.id = tasks.target" \ - " AND tasks.target_type = 2)", \ + " AND tasks.target_type = 2)",/*TASKS_TARGET_TYPE_AGENT_GROUP*/ \ "agent_group", \ KEYWORD_TYPE_STRING \ } @@ -2583,7 +2583,7 @@ append_to_task_string (task_t task, const char* field, const char* value) /** * @brief Query for TASK_SEV_CASE_GUARD. */ -#define TASK_NO_TARGET_CTX "(target_type = 0)" +#define TASK_NO_TARGET_CTX "(target_type = 0)"//TASKS_TARGET_TYPE_IMPORT_TASK /** * @brief SQL for TASK_ITERATOR_WHERE_COLUMNS_INNER. @@ -2818,7 +2818,7 @@ append_to_task_string (task_t task, const char* field, const char* value) }, \ { \ "(SELECT name FROM targets WHERE id = target" \ - " AND tasks.target_type = 1)", \ + " AND tasks.target_type = 1)",/*TASKS_TARGET_TYPE_REGULAR*/ \ "target", \ KEYWORD_TYPE_STRING \ }, \ From e78284c5c0ea2b4a098560d904c1878c4fbd6a84 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Wed, 19 Aug 2026 10:53:25 +0200 Subject: [PATCH 63/66] renamed `tasks_target_type_t` => `task_target_type_t` --- src/gmp.c | 20 +-- src/manage.h | 16 +-- src/manage_pg.c | 2 +- src/manage_sql.c | 162 +++++++++++++---------- src/manage_sql_agent_groups.c | 10 +- src/manage_sql_filters.c | 8 +- src/manage_sql_oci_image_targets.c | 12 +- src/manage_sql_report_common.c | 8 +- src/manage_sql_targets.c | 14 +- src/manage_sql_web_application_targets.c | 12 +- src/manage_tasks.h | 16 +-- 11 files changed, 151 insertions(+), 129 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index 4dcb1b2668..bf3783d2f5 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -20013,7 +20013,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) task_t index; gchar *progress_xml; target_t target; - tasks_target_type_t target_type; + task_target_type_t target_type; scanner_t scanner; const char *first_report_id, *last_report_id; char *config_name, *config_uuid; @@ -20081,14 +20081,14 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) SEND_GET_COMMON (task, &get_tasks_data->get, &tasks); target_location_trash - = (target_type == TASKS_TARGET_TYPE_IMPORT_TASK) + = (target_type == TASK_TARGET_TYPE_IMPORT_TASK) ? 0 : task_target_in_trash (index); - target_in_trash = (target_type == TASKS_TARGET_TYPE_REGULAR) + target_in_trash = (target_type == TASK_TARGET_TYPE_REGULAR) ? target_location_trash : 0; - if (target_type == TASKS_TARGET_TYPE_IMPORT_TASK + if (target_type == TASK_TARGET_TYPE_IMPORT_TASK && (task_iterator_run_status (&tasks) == TASK_STATUS_RUNNING)) { progress_xml = g_strdup_printf @@ -20332,7 +20332,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) config_uuid = task_config_uuid (index); target_available = 1; - if (target_type == TASKS_TARGET_TYPE_REGULAR) + if (target_type == TASK_TARGET_TYPE_REGULAR) { if (target_in_trash) { @@ -20366,7 +20366,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) } #if ENABLE_CONTAINER_SCANNING - if (target_type == TASKS_TARGET_TYPE_OCI_IMAGE) + if (target_type == TASK_TARGET_TYPE_OCI_IMAGE) { oci_image_target_t oci_image_target = target; int oci_image_target_in_trash = target_location_trash; @@ -20426,7 +20426,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) } #endif #if ENABLE_AGENTS - if (target_type == TASKS_TARGET_TYPE_AGENT_GROUP) + if (target_type == TASK_TARGET_TYPE_AGENT_GROUP) { agent_group_t agent_group = target; int agent_group_in_trash = target_location_trash; @@ -20464,7 +20464,7 @@ handle_get_tasks (gmp_parser_t *gmp_parser, GError **error) } #endif /* ENABLE_AGENT_GROUPS */ #if ENABLE_WEB_APPLICATION_SCANNING - if (target_type == TASKS_TARGET_TYPE_WEB_APPLICATION) + if (target_type == TASK_TARGET_TYPE_WEB_APPLICATION) { web_application_target_t web_application_target = target; int web_application_target_in_trash = target_location_trash; @@ -25920,7 +25920,7 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context, /* Import task. */ set_task_target (create_task_data->task, 0, - TASKS_TARGET_TYPE_IMPORT_TASK); + TASK_TARGET_TYPE_IMPORT_TASK); set_task_usage_type (create_task_data->task, create_task_data->usage_type); SENDF_TO_CLIENT_OR_FAIL (XML_OK_CREATED_ID ("create_task"), @@ -26094,7 +26094,7 @@ gmp_xml_handle_end_element (/* unused */ GMarkupParseContext* context, goto create_task_fail; } set_task_target (create_task_data->task, target, - TASKS_TARGET_TYPE_REGULAR); + TASK_TARGET_TYPE_REGULAR); } set_task_scanner (create_task_data->task, scanner); diff --git a/src/manage.h b/src/manage.h index 1d482adf5e..7eb0518639 100644 --- a/src/manage.h +++ b/src/manage.h @@ -445,7 +445,7 @@ check_scanner_feature (scanner_type_t); scanner_type_t get_scanner_type_by_uuid (const char *); -tasks_target_type_t +task_target_type_t scanner_type_target_type (scanner_type_t); @@ -671,7 +671,7 @@ set_task_config (task_t, config_t); target_t task_target (task_t); -tasks_target_type_t +task_target_type_t task_target_type (task_t); int @@ -681,22 +681,22 @@ int task_target_in_trash (task_t); void -set_task_target (task_t, target_t, tasks_target_type_t); +set_task_target (task_t, target_t, task_target_type_t); void -task_update_delete_target (target_t, target_t, tasks_target_type_t); +task_update_delete_target (target_t, target_t, task_target_type_t); void -task_update_restore_target (target_t, target_t, tasks_target_type_t); +task_update_restore_target (target_t, target_t, task_target_type_t); int -task_target_in_use (target_t, tasks_target_type_t); +task_target_in_use (target_t, task_target_type_t); int -task_target_in_use_including_hidden (target_t, tasks_target_type_t); +task_target_in_use_including_hidden (target_t, task_target_type_t); int -task_trash_target_in_use (target_t, tasks_target_type_t); +task_trash_target_in_use (target_t, task_target_type_t); #if ENABLE_AGENTS void diff --git a/src/manage_pg.c b/src/manage_pg.c index 11b359030f..656f4d0073 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -1295,7 +1295,7 @@ manage_create_sql_functions () " FROM tasks" " WHERE id = (SELECT task" " FROM reports WHERE id = $1)" - " AND target_type = 3)" // TASKS_TARGET_TYPE_OCI_IMAGE + " AND target_type = 3)" // TASK_TARGET_TYPE_OCI_IMAGE " THEN (SELECT count (DISTINCT id) FROM report_hosts" " WHERE report_hosts.report = $1" " AND EXISTS (SELECT * FROM results" diff --git a/src/manage_sql.c b/src/manage_sql.c index bad0c7a276..02de5ccd81 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -177,7 +177,8 @@ __cyg_profile_func_exit (void *func, void *caller) } #endif - + + /* Headers from backend specific manage_xxx.c file. */ int @@ -198,7 +199,8 @@ check_db_encryption_key (); void manage_attach_databases (); - + + /* Headers for symbols defined in manage.c which are private to libmanage. */ /** @@ -213,7 +215,8 @@ int delete_reports (task_t); int stop_task_internal (task_t); - + + /* Static headers. */ static int @@ -267,7 +270,8 @@ static void set_credential_snmp_secret (credential_t, const char *, const char *, const char *); - + + /* Variables. */ /** @@ -290,7 +294,8 @@ db_conn_info_t gvmd_db_conn_info = { NULL, NULL, NULL, NULL, 60 }; */ static gchar *vt_verification_collation = NULL; - + + /* General helpers. */ /** @@ -706,7 +711,8 @@ column_array_set (column_t *columns, const gchar *filter, gchar *select) } } - + + /* GET iterators. */ /** @@ -1835,7 +1841,8 @@ info_name_count (const char *type, const char *name) return count; } - + + /* Versions. */ /** @@ -1997,7 +2004,8 @@ set_db_version (int version) version); } - + + /* Encryption. */ /** @@ -2379,7 +2387,8 @@ validate_sort_field (const gchar *table, const gchar *sort_field) return !db_table_has_column ("public", table, sort_field); } - + + /* Task subject iterators. */ /** @@ -2465,7 +2474,8 @@ DEF_ACCESS (task_role_iterator_name, 3); DEF_ACCESS (task_role_iterator_uuid, 4); - + + /* Task functions. */ /** @@ -2565,14 +2575,14 @@ append_to_task_string (task_t task, const char* field, const char* value) ,{ \ "(SELECT uuid FROM agent_groups" \ " WHERE agent_groups.id = tasks.target" \ - " AND tasks.target_type = 2)",/*TASKS_TARGET_TYPE_AGENT_GROUP*/ \ + " AND tasks.target_type = 2)",/*TASK_TARGET_TYPE_AGENT_GROUP*/ \ "agent_group_id", \ KEYWORD_TYPE_STRING \ }, \ { \ "(SELECT name FROM agent_groups" \ " WHERE agent_groups.id = tasks.target" \ - " AND tasks.target_type = 2)",/*TASKS_TARGET_TYPE_AGENT_GROUP*/ \ + " AND tasks.target_type = 2)",/*TASK_TARGET_TYPE_AGENT_GROUP*/ \ "agent_group", \ KEYWORD_TYPE_STRING \ } @@ -2583,7 +2593,7 @@ append_to_task_string (task_t task, const char* field, const char* value) /** * @brief Query for TASK_SEV_CASE_GUARD. */ -#define TASK_NO_TARGET_CTX "(target_type = 0)"//TASKS_TARGET_TYPE_IMPORT_TASK +#define TASK_NO_TARGET_CTX "(target_type = 0)"//TASK_TARGET_TYPE_IMPORT_TASK /** * @brief SQL for TASK_ITERATOR_WHERE_COLUMNS_INNER. @@ -2818,7 +2828,7 @@ append_to_task_string (task_t task, const char* field, const char* value) }, \ { \ "(SELECT name FROM targets WHERE id = target" \ - " AND tasks.target_type = 1)",/*TASKS_TARGET_TYPE_REGULAR*/ \ + " AND tasks.target_type = 1)",/*TASK_TARGET_TYPE_REGULAR*/ \ "target", \ KEYWORD_TYPE_STRING \ }, \ @@ -5932,7 +5942,7 @@ task_target (task_t task) * * @return Target type of task, TASKS_TARGET_TYPE_UNDEFINED on error. */ -tasks_target_type_t +task_target_type_t task_target_type (task_t task) { // sql_int_ps does not support return value through parameter @@ -5942,13 +5952,13 @@ task_target_type (task_t task) SQL_RESOURCE_PARAM (task), NULL)) { case 0: - return (tasks_target_type_t)target_type; + return (task_target_type_t)target_type; break; case 1: /* Too few rows in result of query. */ default: /* Programming error. */ assert (0); case -1: - return TASKS_TARGET_TYPE_UNDEFINED; + return TASK_TARGET_TYPE_UNDEFINED; break; } } @@ -5971,15 +5981,15 @@ task_regular_target (task_t task, target_t *target) assert (target); *target = 0; - tasks_target_type_t target_type = task_target_type (task); - if (target_type == TASKS_TARGET_TYPE_UNDEFINED) + task_target_type_t target_type = task_target_type (task); + if (target_type == TASK_TARGET_TYPE_UNDEFINED) { g_warning ("%s: failed to get the target type of task %llu", __func__, task); return -1; } - if (target_type == TASKS_TARGET_TYPE_REGULAR) + if (target_type == TASK_TARGET_TYPE_REGULAR) *target = task_target (task); return 0; @@ -5994,7 +6004,7 @@ task_regular_target (task_t task, target_t *target) * @param[in] target_type The type of the target */ void -set_task_target (task_t task, target_t target, tasks_target_type_t target_type) +set_task_target (task_t task, target_t target, task_target_type_t target_type) { sql_ps ("UPDATE tasks SET target = $1, target_type = $2, target_location = 0," " modification_time = m_now ()" @@ -6014,7 +6024,7 @@ set_task_target (task_t task, target_t target, tasks_target_type_t target_type) */ void task_update_delete_target (target_t resource_id, target_t trash_id, - tasks_target_type_t target_type) + task_target_type_t target_type) { sql_ps ("UPDATE tasks" " SET target = $1," @@ -6037,7 +6047,7 @@ task_update_delete_target (target_t resource_id, target_t trash_id, */ void task_update_restore_target (target_t restored_id, target_t trash_id, - tasks_target_type_t target_type) + task_target_type_t target_type) { sql_ps ("UPDATE tasks" " SET target = $1," @@ -6060,7 +6070,7 @@ task_update_restore_target (target_t restored_id, target_t trash_id, * @return 1 if in use, else 0. */ int -task_target_in_use (target_t target, tasks_target_type_t target_type) +task_target_in_use (target_t target, task_target_type_t target_type) { return !!sql_int_ps ( "SELECT count(*) FROM tasks" @@ -6084,7 +6094,7 @@ task_target_in_use (target_t target, tasks_target_type_t target_type) */ int task_target_in_use_including_hidden (target_t target, - tasks_target_type_t target_type) + task_target_type_t target_type) { return !!sql_int_ps ( "SELECT count(*) FROM tasks" @@ -6105,7 +6115,7 @@ task_target_in_use_including_hidden (target_t target, * @return 1 if in use, else 0. */ int -task_trash_target_in_use (target_t target, tasks_target_type_t target_type) +task_trash_target_in_use (target_t target, task_target_type_t target_type) { return !!sql_int_ps ( "SELECT count(*) FROM tasks" @@ -6127,7 +6137,7 @@ task_trash_target_in_use (target_t target, tasks_target_type_t target_type) void set_task_agent_group (task_t task, agent_group_t agent_group) { - set_task_target (task, agent_group, TASKS_TARGET_TYPE_AGENT_GROUP); + set_task_target (task, agent_group, TASK_TARGET_TYPE_AGENT_GROUP); } /** @@ -6147,7 +6157,7 @@ agent_group_tasks_exist_by_scanner (scanner_t scanner) " AND target_type = $2" " AND target_location = " G_STRINGIFY (LOCATION_TABLE) " AND hidden = 0;", SQL_RESOURCE_PARAM (scanner), - SQL_INT_PARAM (TASKS_TARGET_TYPE_AGENT_GROUP), + SQL_INT_PARAM (TASK_TARGET_TYPE_AGENT_GROUP), NULL); } /** @@ -6173,7 +6183,7 @@ agent_group_hidden_tasks_exist_by_scanner (scanner_t scanner) " AND target IN (" " SELECT id FROM agent_groups_trash WHERE scanner = $3 ))" ");", - SQL_INT_PARAM (TASKS_TARGET_TYPE_AGENT_GROUP), + SQL_INT_PARAM (TASK_TARGET_TYPE_AGENT_GROUP), SQL_INT_PARAM (LOCATION_TABLE), SQL_RESOURCE_PARAM (scanner), SQL_INT_PARAM (LOCATION_TRASH), @@ -6190,7 +6200,7 @@ agent_group_hidden_tasks_exist_by_scanner (scanner_t scanner) agent_group_t task_agent_group (task_t task) { - if (task_target_type (task) != TASKS_TARGET_TYPE_AGENT_GROUP) + if (task_target_type (task) != TASK_TARGET_TYPE_AGENT_GROUP) return 0; return task_target (task); } @@ -6205,7 +6215,7 @@ task_agent_group (task_t task) int task_agent_group_in_trash (task_t task) { - if (task_target_type (task) != TASKS_TARGET_TYPE_AGENT_GROUP) + if (task_target_type (task) != TASK_TARGET_TYPE_AGENT_GROUP) return 0; return task_target_in_trash (task); } @@ -6222,7 +6232,7 @@ task_agent_group_in_trash (task_t task) oci_image_target_t task_oci_image_target (task_t task) { - if (task_target_type (task) != TASKS_TARGET_TYPE_OCI_IMAGE) + if (task_target_type (task) != TASK_TARGET_TYPE_OCI_IMAGE) return 0; return task_target (task); } @@ -6237,7 +6247,7 @@ task_oci_image_target (task_t task) int task_oci_image_target_in_trash (task_t task) { - if (task_target_type (task) != TASKS_TARGET_TYPE_OCI_IMAGE) + if (task_target_type (task) != TASK_TARGET_TYPE_OCI_IMAGE) return 0; return task_target_in_trash (task); } @@ -6251,7 +6261,7 @@ task_oci_image_target_in_trash (task_t task) void set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) { - set_task_target (task, oci_image_target, TASKS_TARGET_TYPE_OCI_IMAGE); + set_task_target (task, oci_image_target, TASK_TARGET_TYPE_OCI_IMAGE); } #endif @@ -6267,7 +6277,7 @@ set_task_oci_image_target (task_t task, oci_image_target_t oci_image_target) web_application_target_t task_web_application_target (task_t task) { - if (task_target_type (task) != TASKS_TARGET_TYPE_WEB_APPLICATION) + if (task_target_type (task) != TASK_TARGET_TYPE_WEB_APPLICATION) return 0; return task_target (task); } @@ -6282,7 +6292,7 @@ task_web_application_target (task_t task) int task_web_application_target_in_trash (task_t task) { - if (task_target_type (task) != TASKS_TARGET_TYPE_WEB_APPLICATION) + if (task_target_type (task) != TASK_TARGET_TYPE_WEB_APPLICATION) return 0; return task_target_in_trash (task); } @@ -6297,7 +6307,7 @@ void set_task_web_application_target (task_t task, web_application_target_t web_application_target) { set_task_target (task, web_application_target, - TASKS_TARGET_TYPE_WEB_APPLICATION); + TASK_TARGET_TYPE_WEB_APPLICATION); } #endif @@ -7363,7 +7373,7 @@ task_severity_double (task_t task, int overrides, int min_qod, int offset) report_t report; if (current_credentials.uuid == NULL - || task_target_type (task) == TASKS_TARGET_TYPE_IMPORT_TASK) + || task_target_type (task) == TASK_TARGET_TYPE_IMPORT_TASK) return SEVERITY_MISSING; report = sql_int64_0 ("SELECT id FROM reports" @@ -7773,7 +7783,8 @@ reschedule_task (const gchar *task_id) } } - + + /* Results. */ /** @@ -8306,7 +8317,8 @@ result_detection_reference (result_t result, report_t report, } - + + /* Prognostics. */ /** @@ -8744,7 +8756,8 @@ prognosis_iterator_cvss_double (iterator_t* iterator) DEF_ACCESS (prognosis_iterator_description, 2); DEF_ACCESS (prognosis_iterator_cpe, 3); - + + /* Reports. */ /** @@ -9566,7 +9579,7 @@ create_report (array_t *results, const char *task_id, const char *in_assets, rc = -1; else if (task == 0) rc = -4; - else if (task_target_type (task) != TASKS_TARGET_TYPE_IMPORT_TASK) + else if (task_target_type (task) != TASK_TARGET_TYPE_IMPORT_TASK) rc = -5; if (rc) { @@ -16365,7 +16378,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task, char *tsk_name, *task_target_uuid, *task_target_name; char *task_target_comment, *comment; target_t target; - tasks_target_type_t target_type; + task_target_type_t target_type; gchar *progress_xml; iterator_t tags; @@ -16379,7 +16392,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task, comment = task_comment (task); target_type = task_target_type (task); - if (target_type != TASKS_TARGET_TYPE_REGULAR) + if (target_type != TASK_TARGET_TYPE_REGULAR) target = 0; else target = task_target (task); @@ -16403,7 +16416,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task, task_target_comment = NULL; } - if (target_type == TASKS_TARGET_TYPE_IMPORT_TASK + if (target_type == TASK_TARGET_TYPE_IMPORT_TASK && (task_run_status (task) == TASK_STATUS_RUNNING)) progress_xml = g_strdup_printf ("%i", @@ -18165,7 +18178,8 @@ parse_osp_report (task_t task, report_t report, const char *report_xml) free_entity (entity); } - + + /* More task stuff. */ /** @@ -19165,7 +19179,7 @@ manage_task_remove_file (const gchar *task_id, const char *name) * @return The target type associated with the scanner type, * TASKS_TARGET_TYPE_UNDEFINED if the scanner type is unknown. */ -tasks_target_type_t +task_target_type_t scanner_type_target_type (scanner_type_t type) { switch (type) @@ -19175,16 +19189,16 @@ scanner_type_target_type (scanner_type_t type) case SCANNER_TYPE_OPENVASD: case SCANNER_TYPE_OPENVASD_SENSOR: case SCANNER_TYPE_CVE: - return TASKS_TARGET_TYPE_REGULAR; + return TASK_TARGET_TYPE_REGULAR; case SCANNER_TYPE_AGENT_CONTROLLER: case SCANNER_TYPE_AGENT_CONTROLLER_SENSOR: - return TASKS_TARGET_TYPE_AGENT_GROUP; + return TASK_TARGET_TYPE_AGENT_GROUP; case SCANNER_TYPE_CONTAINER_IMAGE: - return TASKS_TARGET_TYPE_OCI_IMAGE; + return TASK_TARGET_TYPE_OCI_IMAGE; case SCANNER_TYPE_WEB_APPLICATION: - return TASKS_TARGET_TYPE_WEB_APPLICATION; + return TASK_TARGET_TYPE_WEB_APPLICATION; default: - return TASKS_TARGET_TYPE_UNDEFINED; + return TASK_TARGET_TYPE_UNDEFINED; } } @@ -19223,31 +19237,31 @@ modify_task_check_target_scanner (task_t task, const gchar *web_application_target_id) { scanner_t scanner; - tasks_target_type_t given_type, required_type; + task_target_type_t given_type, required_type; int given_count; given_count = 0; - given_type = TASKS_TARGET_TYPE_UNDEFINED; + given_type = TASK_TARGET_TYPE_UNDEFINED; if (modify_task_resource_given (target_id)) { given_count++; - given_type = TASKS_TARGET_TYPE_REGULAR; + given_type = TASK_TARGET_TYPE_REGULAR; } if (modify_task_resource_given (agent_group_id)) { given_count++; - given_type = TASKS_TARGET_TYPE_AGENT_GROUP; + given_type = TASK_TARGET_TYPE_AGENT_GROUP; } if (modify_task_resource_given (oci_image_target_id)) { given_count++; - given_type = TASKS_TARGET_TYPE_OCI_IMAGE; + given_type = TASK_TARGET_TYPE_OCI_IMAGE; } if (modify_task_resource_given (web_application_target_id)) { given_count++; - given_type = TASKS_TARGET_TYPE_WEB_APPLICATION; + given_type = TASK_TARGET_TYPE_WEB_APPLICATION; } if (given_count == 0) @@ -19269,7 +19283,7 @@ modify_task_check_target_scanner (task_t task, required_type = scanner_type_target_type (scanner_type (scanner)); - if (required_type == TASKS_TARGET_TYPE_UNDEFINED) + if (required_type == TASK_TARGET_TYPE_UNDEFINED) { g_warning ("%s: task %llu has scanner %llu of unknown type," " skipping target type check", @@ -19332,7 +19346,7 @@ modify_task (const gchar *task_id, const gchar *name, return MODIFY_TASK_NOT_FOUND; - if ((task_target_type (task) == TASKS_TARGET_TYPE_IMPORT_TASK + if ((task_target_type (task) == TASK_TARGET_TYPE_IMPORT_TASK && (agent_group_id == NULL) && (oci_image_target_id == NULL) && (web_application_target_id == NULL)) @@ -19523,7 +19537,7 @@ modify_task (const gchar *task_id, const gchar *name, else if (target == 0) return MODIFY_TASK_TARGET_NOT_FOUND; else - set_task_target (task, target, TASKS_TARGET_TYPE_REGULAR); + set_task_target (task, target, TASK_TARGET_TYPE_REGULAR); } #if ENABLE_AGENTS if (agent_group_id) { @@ -19620,7 +19634,8 @@ modify_task (const gchar *task_id, const gchar *name, return MODIFY_TASK_OK; } - + + /* Credentials. */ /** @@ -22868,7 +22883,8 @@ credential_scanner_iterator_readable (iterator_t* iterator) return iterator_int (iterator, 2); } - + + /* Notes and Overrides. */ /** @@ -22950,7 +22966,8 @@ nvt_exists (const char* nvt) return 0; } - + + /* Scanners */ /** @@ -25355,7 +25372,8 @@ manage_get_scanners (GSList *log_config, const db_conn_info_t *database) return 0; } - + + /* Schema. */ /** @@ -25566,7 +25584,8 @@ manage_schema (gchar *format, gchar **output_return, gsize *output_length, } } - + + /* Trashcan. */ /** @@ -26461,7 +26480,7 @@ manage_restore (const char *id) /* Update the target in any trashcan tasks. */ task_update_restore_target (restored_target, resource, - TASKS_TARGET_TYPE_REGULAR); + TASK_TARGET_TYPE_REGULAR); permissions_set_locations ("target", resource, @@ -26715,7 +26734,8 @@ manage_empty_trashcan () return 0; } - + + /* Vulns. */ /** @@ -27492,7 +27512,8 @@ manage_set_radius_info (int enabled, gchar *host, gchar *key) sql_commit (); } - + + /* SQL construction */ /** @@ -28178,7 +28199,8 @@ type_build_select (const char *type, const char *columns_str, return 0; } - + + /* Optimize. */ /** diff --git a/src/manage_sql_agent_groups.c b/src/manage_sql_agent_groups.c index 2cb62223e4..d8df824820 100644 --- a/src/manage_sql_agent_groups.c +++ b/src/manage_sql_agent_groups.c @@ -63,7 +63,7 @@ agent_group_in_use_in_hidden_task (agent_group_t agent_group) "SELECT COUNT(*) FROM tasks" " WHERE hidden != 0 AND target = $1 AND target_type = $2;", SQL_RESOURCE_PARAM (agent_group), - SQL_INT_PARAM (TASKS_TARGET_TYPE_AGENT_GROUP), + SQL_INT_PARAM (TASK_TARGET_TYPE_AGENT_GROUP), NULL); } @@ -654,7 +654,7 @@ delete_agent_group (const char *agent_group_uuid, int ultimate) /* Update the location of the agent_group in any trashcan tasks. */ task_update_delete_target (agent_group, trash_id, - TASKS_TARGET_TYPE_AGENT_GROUP); + TASK_TARGET_TYPE_AGENT_GROUP); permissions_set_locations ("agent_group", agent_group, trash_id, LOCATION_TRASH); tags_set_locations ("agent_group", agent_group, trash_id, LOCATION_TRASH); @@ -740,7 +740,7 @@ restore_agent_group (const char *agent_group_uuid) /* Update the agent_group in any tasks. */ task_update_restore_target (restored_id, trash_id, - TASKS_TARGET_TYPE_AGENT_GROUP); + TASK_TARGET_TYPE_AGENT_GROUP); // Clean up trash entries @@ -1024,7 +1024,7 @@ find_agent_group_with_permission (const char *uuid, agent_group_t *agent_group, int agent_group_in_use (agent_group_t agent_group) { - return task_target_in_use (agent_group, TASKS_TARGET_TYPE_AGENT_GROUP); + return task_target_in_use (agent_group, TASK_TARGET_TYPE_AGENT_GROUP); } /** @@ -1037,7 +1037,7 @@ agent_group_in_use (agent_group_t agent_group) int trash_agent_group_in_use (agent_group_t agent_group) { - return task_trash_target_in_use (agent_group, TASKS_TARGET_TYPE_AGENT_GROUP); + return task_trash_target_in_use (agent_group, TASK_TARGET_TYPE_AGENT_GROUP); } /** diff --git a/src/manage_sql_filters.c b/src/manage_sql_filters.c index 93345926b8..7fb5fe4d36 100644 --- a/src/manage_sql_filters.c +++ b/src/manage_sql_filters.c @@ -1356,7 +1356,7 @@ filter_clause (const char* type, const char* filter, (order, " ORDER BY" " (CASE WHEN (SELECT " - " ( target_type = 0 ) " //TASKS_TARGET_TYPE_IMPORT_TASK + " ( target_type = 0 ) " //TASK_TARGET_TYPE_IMPORT_TASK " FROM tasks" " WHERE tasks.id = task)" " THEN '0'" @@ -1373,7 +1373,7 @@ filter_clause (const char* type, const char* filter, g_string_append_printf (order, " ORDER BY" - " (CASE WHEN ( target_type = 0 ) " //TASKS_TARGET_TYPE_IMPORT_TASK + " (CASE WHEN ( target_type = 0 ) " //TASK_TARGET_TYPE_IMPORT_TASK " THEN '0'" " ELSE run_status_name (run_status)" " || (SELECT CAST (temp / 100 AS text)" @@ -1554,7 +1554,7 @@ filter_clause (const char* type, const char* filter, (order, " ORDER BY" " (CASE WHEN (SELECT " - " ( target_type = 0 ) " //TASKS_TARGET_TYPE_IMPORT_TASK + " ( target_type = 0 ) " //TASK_TARGET_TYPE_IMPORT_TASK " FROM tasks" " WHERE tasks.id = task)" " THEN '0'" @@ -1571,7 +1571,7 @@ filter_clause (const char* type, const char* filter, g_string_append_printf (order, " ORDER BY" - " (CASE WHEN ( target_type = 0 ) " //TASKS_TARGET_TYPE_IMPORT_TASK + " (CASE WHEN ( target_type = 0 ) " //TASK_TARGET_TYPE_IMPORT_TASK " THEN '0'" " ELSE run_status_name (run_status)" " || (SELECT CAST (temp / 100 AS text)" diff --git a/src/manage_sql_oci_image_targets.c b/src/manage_sql_oci_image_targets.c index d579d959fb..db58ea57a1 100644 --- a/src/manage_sql_oci_image_targets.c +++ b/src/manage_sql_oci_image_targets.c @@ -462,7 +462,7 @@ delete_oci_image_target (const char *oci_image_target_id, int ultimate) /* Update the location of the target in any task. */ task_update_delete_target (oci_image_target, sql_last_insert_id (), - TASKS_TARGET_TYPE_OCI_IMAGE); + TASK_TARGET_TYPE_OCI_IMAGE); permissions_set_locations ("oci_image_target", oci_image_target, sql_last_insert_id (), @@ -472,7 +472,7 @@ delete_oci_image_target (const char *oci_image_target_id, int ultimate) LOCATION_TRASH); } else if (task_target_in_use_including_hidden (oci_image_target, - TASKS_TARGET_TYPE_OCI_IMAGE)) + TASK_TARGET_TYPE_OCI_IMAGE)) { sql_rollback (); return 1; @@ -560,7 +560,7 @@ restore_oci_image_target (const char *oci_image_target_id) /* Update the oci image target in any tasks. */ task_update_restore_target (oci_image_target, resource, - TASKS_TARGET_TYPE_OCI_IMAGE); + TASK_TARGET_TYPE_OCI_IMAGE); permissions_set_locations ("oci_image_target", resource, oci_image_target, LOCATION_TABLE); @@ -882,7 +882,7 @@ trash_oci_image_target_readable (oci_image_target_t oci_image_target) int oci_image_target_in_use (oci_image_target_t oci_image_target) { - return task_target_in_use (oci_image_target, TASKS_TARGET_TYPE_OCI_IMAGE); + return task_target_in_use (oci_image_target, TASK_TARGET_TYPE_OCI_IMAGE); } /** @@ -896,7 +896,7 @@ int trash_oci_image_target_in_use (oci_image_target_t oci_image_target) { return task_trash_target_in_use (oci_image_target, - TASKS_TARGET_TYPE_OCI_IMAGE); + TASK_TARGET_TYPE_OCI_IMAGE); } /** @@ -954,7 +954,7 @@ init_oci_image_target_task_iterator (iterator_t* iterator, with_clause ? with_clause : "", available, oci_image_target, - TASKS_TARGET_TYPE_OCI_IMAGE); + TASK_TARGET_TYPE_OCI_IMAGE); g_free (with_clause); g_free (available); diff --git a/src/manage_sql_report_common.c b/src/manage_sql_report_common.c index 786e93b550..2bec0bd00c 100644 --- a/src/manage_sql_report_common.c +++ b/src/manage_sql_report_common.c @@ -260,14 +260,14 @@ get_report_task_progress (report_t report, task_t task) if (report == 0 || task == 0) return 0; - tasks_target_type_t target_type = task_target_type (task); + task_target_type_t target_type = task_target_type (task); #if ENABLE_AGENTS - if (target_type == TASKS_TARGET_TYPE_AGENT_GROUP) + if (target_type == TASK_TARGET_TYPE_AGENT_GROUP) return 100; #endif - if (target_type == TASKS_TARGET_TYPE_IMPORT_TASK + if (target_type == TASK_TARGET_TYPE_IMPORT_TASK && task_run_status (task) == TASK_STATUS_RUNNING) return task_upload_progress (task); @@ -406,7 +406,7 @@ fill_network_target_reference ( gchar *name = NULL; gchar *comment = NULL; - if (task_target_type(task) != TASKS_TARGET_TYPE_REGULAR) + if (task_target_type(task) != TASK_TARGET_TYPE_REGULAR) return 0; target = task_target (task); diff --git a/src/manage_sql_targets.c b/src/manage_sql_targets.c index 3086e1ac11..28ce6740be 100644 --- a/src/manage_sql_targets.c +++ b/src/manage_sql_targets.c @@ -458,7 +458,7 @@ delete_target (const char *target_id, int ultimate) } /* Check if it's in use by a task in the trashcan. */ - if (task_trash_target_in_use (target, TASKS_TARGET_TYPE_REGULAR)) + if (task_trash_target_in_use (target, TASK_TARGET_TYPE_REGULAR)) { sql_rollback (); return 1; @@ -475,7 +475,7 @@ delete_target (const char *target_id, int ultimate) if (ultimate == 0) { - if (task_target_in_use (target, TASKS_TARGET_TYPE_REGULAR)) + if (task_target_in_use (target, TASK_TARGET_TYPE_REGULAR)) { sql_rollback (); return 1; @@ -507,7 +507,7 @@ delete_target (const char *target_id, int ultimate) /* Update the location of the target in any trashcan tasks. */ task_update_delete_target (target, sql_last_insert_id (), - TASKS_TARGET_TYPE_REGULAR); + TASK_TARGET_TYPE_REGULAR); permissions_set_locations ("target", target, sql_last_insert_id (), @@ -517,7 +517,7 @@ delete_target (const char *target_id, int ultimate) LOCATION_TRASH); } else if (task_target_in_use_including_hidden (target, - TASKS_TARGET_TYPE_REGULAR)) + TASK_TARGET_TYPE_REGULAR)) { sql_rollback (); return 1; @@ -2093,7 +2093,7 @@ init_target_task_iterator (iterator_t* iterator, target_t target) with_clause ? with_clause : "", available, target, - TASKS_TARGET_TYPE_REGULAR); + TASK_TARGET_TYPE_REGULAR); g_free (with_clause); g_free (available); @@ -2143,7 +2143,7 @@ target_task_iterator_readable (iterator_t* iterator) int target_in_use (target_t target) { - return task_target_in_use (target, TASKS_TARGET_TYPE_REGULAR); + return task_target_in_use (target, TASK_TARGET_TYPE_REGULAR); } /** @@ -2156,7 +2156,7 @@ target_in_use (target_t target) int trash_target_in_use (target_t target) { - return task_trash_target_in_use (target, TASKS_TARGET_TYPE_REGULAR); + return task_trash_target_in_use (target, TASK_TARGET_TYPE_REGULAR); } /** diff --git a/src/manage_sql_web_application_targets.c b/src/manage_sql_web_application_targets.c index 5a3ca47ee9..222b4d5f77 100644 --- a/src/manage_sql_web_application_targets.c +++ b/src/manage_sql_web_application_targets.c @@ -510,7 +510,7 @@ delete_web_application_target ( task_update_delete_target (web_application_target, trash_web_application_target, - TASKS_TARGET_TYPE_WEB_APPLICATION); + TASK_TARGET_TYPE_WEB_APPLICATION); permissions_set_locations ("web_application_target", web_application_target, @@ -523,7 +523,7 @@ delete_web_application_target ( LOCATION_TRASH); } else if (task_target_in_use_including_hidden ( - web_application_target, TASKS_TARGET_TYPE_WEB_APPLICATION)) + web_application_target, TASK_TARGET_TYPE_WEB_APPLICATION)) { sql_rollback (); return 1; @@ -624,7 +624,7 @@ restore_web_application_target (const char *web_application_target_id) web_application_target = sql_last_insert_id (); task_update_restore_target (web_application_target, resource, - TASKS_TARGET_TYPE_WEB_APPLICATION); + TASK_TARGET_TYPE_WEB_APPLICATION); permissions_set_locations ("web_application_target", resource, @@ -939,7 +939,7 @@ int web_application_target_in_use (web_application_target_t web_application_target) { return task_target_in_use (web_application_target, - TASKS_TARGET_TYPE_WEB_APPLICATION); + TASK_TARGET_TYPE_WEB_APPLICATION); } /** @@ -955,7 +955,7 @@ trash_web_application_target_in_use ( web_application_target_t web_application_target) { return task_trash_target_in_use (web_application_target, - TASKS_TARGET_TYPE_WEB_APPLICATION); + TASK_TARGET_TYPE_WEB_APPLICATION); } /** @@ -1023,7 +1023,7 @@ init_web_application_target_task_iterator ( with_clause ? with_clause : "", available, web_application_target, - TASKS_TARGET_TYPE_WEB_APPLICATION); + TASK_TARGET_TYPE_WEB_APPLICATION); g_free (with_clause); g_free (available); diff --git a/src/manage_tasks.h b/src/manage_tasks.h index 8656e72a2b..cfba3555f8 100644 --- a/src/manage_tasks.h +++ b/src/manage_tasks.h @@ -7,15 +7,15 @@ #define _GVMD_MANAGE_TASKS_H -typedef enum tasks_target_type +typedef enum task_target_type { - TASKS_TARGET_TYPE_UNDEFINED = -1, - TASKS_TARGET_TYPE_IMPORT_TASK = 0, - TASKS_TARGET_TYPE_REGULAR = 1, - TASKS_TARGET_TYPE_AGENT_GROUP = 2, - TASKS_TARGET_TYPE_OCI_IMAGE = 3, - TASKS_TARGET_TYPE_WEB_APPLICATION = 4, -} tasks_target_type_t; + TASK_TARGET_TYPE_UNDEFINED = -1, + TASK_TARGET_TYPE_IMPORT_TASK = 0, + TASK_TARGET_TYPE_REGULAR = 1, + TASK_TARGET_TYPE_AGENT_GROUP = 2, + TASK_TARGET_TYPE_OCI_IMAGE = 3, + TASK_TARGET_TYPE_WEB_APPLICATION = 4, +} task_target_type_t; #endif /* not _GVMD_MANAGE_TASKS_H */ From 2a5dede4a10cc83f45e57746559377f45efb442a Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Wed, 19 Aug 2026 10:57:39 +0200 Subject: [PATCH 64/66] added explanatory comments for db version checks --- src/manage_pg.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/manage_pg.c b/src/manage_pg.c index 656f4d0073..2abea59f2d 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -1284,7 +1284,7 @@ manage_create_sql_functions () " WHERE report_hosts.report = $1;" "$$ LANGUAGE SQL;"); - /* column hostname in table report_hosts was added in version 273 */ + /* column target_type in table tasks was added in version 282 */ if (current_db_version >= 282) { sql ("CREATE OR REPLACE FUNCTION report_result_host_count (report integer," @@ -1310,6 +1310,7 @@ manage_create_sql_functions () " END;" "$$ LANGUAGE SQL;"); } + /* column hostname in table report_hosts was added in version 273 */ else if (current_db_version >= 273) { sql ("CREATE OR REPLACE FUNCTION report_result_host_count (report integer," @@ -1578,6 +1579,7 @@ manage_create_sql_functions () TASK_STATUS_DONE); } + /* column target_type in table tasks was added in version 282 */ if (current_db_version >= 282) { sql ("CREATE OR REPLACE FUNCTION task_trend (integer, integer, integer)" From d831ae8dc74c828b18108b7f856cf4c3e6ff63bf Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Thu, 20 Aug 2026 13:01:39 +0200 Subject: [PATCH 65/66] added migration path for tasks with multiple target columns set --- src/manage_migrators.c | 119 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 108 insertions(+), 11 deletions(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index ff8efc8abb..501e5922ff 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4255,12 +4255,27 @@ migrate_281_to_282 () return -1; } - /* Check for invalid data */ + /* Tasks with multiple targets are resolved by keeping only the target that + * matches the type of the scanner of the task. If none of the targets + * matches the scanner type, all of them are removed, so that the task + * becomes an import task (a note is added to its comment, so users know + * why this has happened). */ int count_multiple_targets = 0; iterator_t tasks_multiple_targets; init_iterator ( &tasks_multiple_targets, - "SELECT uuid, name FROM tasks" + "SELECT uuid, name, id, target, agent_group, oci_image_target," + " web_application_target," + " target_location, agent_group_location," + " oci_image_target_location, web_application_target_location," + " CASE WHEN coalesce (scanner_location, 0)" + " = " G_STRINGIFY (LOCATION_TRASH) + " THEN (SELECT type FROM scanners_trash" + " WHERE scanners_trash.id = tasks.scanner)" + " ELSE (SELECT type FROM scanners" + " WHERE scanners.id = tasks.scanner)" + " END" + " FROM tasks" " WHERE (agent_group IS NOT NULL" " AND oci_image_target IS NOT NULL)" " OR (agent_group IS NOT NULL" @@ -4273,20 +4288,102 @@ migrate_281_to_282 () while (next (&tasks_multiple_targets)) { - g_critical ("Invalid data: Multiple targets specified for task %s (%s)", - iterator_string (&tasks_multiple_targets, 0), - iterator_string (&tasks_multiple_targets, 1)); + g_warning ("Invalid data: Multiple targets specified for task %s (%s)", + iterator_string (&tasks_multiple_targets, 0), + iterator_string (&tasks_multiple_targets, 1)); count_multiple_targets++; + + resource_t task = iterator_int64 (&tasks_multiple_targets, 2); + resource_t target_id = iterator_int64 (&tasks_multiple_targets, 3); + resource_t agent_group_id = iterator_int64 (&tasks_multiple_targets, 4); + resource_t oci_image_target_id = + iterator_int64 (&tasks_multiple_targets, 5); + resource_t web_application_target_id = + iterator_int64 (&tasks_multiple_targets, 6); + int target_loc = iterator_int (&tasks_multiple_targets, 7); + int agent_group_loc = iterator_int (&tasks_multiple_targets, 8); + int oci_image_target_loc = iterator_int (&tasks_multiple_targets, 9); + int web_application_target_loc = + iterator_int (&tasks_multiple_targets, 10); + + scanner_type_t scanner_type = iterator_int (&tasks_multiple_targets, 11); + task_target_type_t target_type = scanner_type_target_type (scanner_type); + + resource_t kept_target; + switch (target_type) + { + case TASK_TARGET_TYPE_REGULAR: + g_warning(" => scanner type (%d), the target type is set to regular", scanner_type); + kept_target = target_id; + break; + case TASK_TARGET_TYPE_AGENT_GROUP: + g_warning(" => scanner type (%d), the target type is set to agent group", scanner_type); + kept_target = agent_group_id; + break; + case TASK_TARGET_TYPE_OCI_IMAGE: + g_warning(" => scanner type (%d), the target type is set to oci image", scanner_type); + kept_target = oci_image_target_id; + break; + case TASK_TARGET_TYPE_WEB_APPLICATION: + g_warning(" => scanner type (%d), the target type is set to web application", scanner_type); + kept_target = web_application_target_id; + break; + default: + g_warning (" => scanner type matches no target, the target type is set to import task"); + kept_target = 0; + target_type = TASK_TARGET_TYPE_IMPORT_TASK; + } + + /* Remove the targets that do not match the scanner type, preparing the + * data for the migration running right after. */ + sql_ps ("UPDATE tasks" + " SET target = $1," + " target_location = $2," + " agent_group = $3," + " agent_group_location = $4," + " oci_image_target = $5," + " oci_image_target_location = $6," + " web_application_target = $7," + " web_application_target_location = $8" + " WHERE id = $9;", + target_type == TASK_TARGET_TYPE_REGULAR + ? SQL_RESOURCE_PARAM (target_id) : SQL_RESOURCE_PARAM (0), + target_type == TASK_TARGET_TYPE_REGULAR + ? SQL_RESOURCE_PARAM (target_loc) : SQL_NULL_PARAM, + target_type == TASK_TARGET_TYPE_AGENT_GROUP + ? SQL_RESOURCE_PARAM (agent_group_id) : SQL_NULL_PARAM, + target_type == TASK_TARGET_TYPE_AGENT_GROUP + ? SQL_RESOURCE_PARAM (agent_group_loc) : SQL_NULL_PARAM, + target_type == TASK_TARGET_TYPE_OCI_IMAGE + ? SQL_RESOURCE_PARAM (oci_image_target_id) : SQL_NULL_PARAM, + target_type == TASK_TARGET_TYPE_OCI_IMAGE + ? SQL_RESOURCE_PARAM (oci_image_target_loc) : SQL_NULL_PARAM, + target_type == TASK_TARGET_TYPE_WEB_APPLICATION + ? SQL_RESOURCE_PARAM (web_application_target_id) + : SQL_NULL_PARAM, + target_type == TASK_TARGET_TYPE_WEB_APPLICATION + ? SQL_RESOURCE_PARAM (web_application_target_loc) + : SQL_NULL_PARAM, + SQL_RESOURCE_PARAM (task), + NULL); + + /* Add an explanatory comment to tasks that have no targets matching + * their scanner type */ + if (target_type == TASK_TARGET_TYPE_IMPORT_TASK) + sql ("UPDATE tasks" + " SET comment = coalesce (comment || ' ', '')" + " || 'Note: Multiple targets were assigned to this" + " task, but none of them matched the type of its scanner, so the" + " targets were removed and the task became an import task.'" + " WHERE id = %llu;", + task); } cleanup_iterator (&tasks_multiple_targets); if (count_multiple_targets > 0) - { - g_critical ("Aborting migration, since there are %d tasks with multiple targets specified.", - count_multiple_targets); - sql_rollback (); - return -1; - } + g_warning ("Resolved %d tasks with multiple targets specified," + " using the type of the scanner of each task.", + count_multiple_targets); /* Add target_type column to tasks table */ sql ("ALTER TABLE tasks ADD COLUMN IF NOT EXISTS target_type INTEGER;"); From b9c7af62be940533022fd7035562d2ce885c734f Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Thu, 20 Aug 2026 13:17:25 +0200 Subject: [PATCH 66/66] fixed migration logic + formatting --- src/manage_migrators.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/manage_migrators.c b/src/manage_migrators.c index 501e5922ff..7de972c410 100644 --- a/src/manage_migrators.c +++ b/src/manage_migrators.c @@ -4313,27 +4313,30 @@ migrate_281_to_282 () switch (target_type) { case TASK_TARGET_TYPE_REGULAR: - g_warning(" => scanner type (%d), the target type is set to regular", scanner_type); + g_warning (" => scanner type (%d), the target type is set to regular", scanner_type); kept_target = target_id; break; case TASK_TARGET_TYPE_AGENT_GROUP: - g_warning(" => scanner type (%d), the target type is set to agent group", scanner_type); + g_warning (" => scanner type (%d), the target type is set to agent group", scanner_type); kept_target = agent_group_id; break; case TASK_TARGET_TYPE_OCI_IMAGE: - g_warning(" => scanner type (%d), the target type is set to oci image", scanner_type); + g_warning (" => scanner type (%d), the target type is set to oci image", scanner_type); kept_target = oci_image_target_id; break; case TASK_TARGET_TYPE_WEB_APPLICATION: - g_warning(" => scanner type (%d), the target type is set to web application", scanner_type); + g_warning (" => scanner type (%d), the target type is set to web application", scanner_type); kept_target = web_application_target_id; break; default: g_warning (" => scanner type matches no target, the target type is set to import task"); kept_target = 0; - target_type = TASK_TARGET_TYPE_IMPORT_TASK; } + /* Scanner type does not match target type */ + if (kept_target == 0) + target_type = TASK_TARGET_TYPE_IMPORT_TASK; + /* Remove the targets that do not match the scanner type, preparing the * data for the migration running right after. */ sql_ps ("UPDATE tasks"