Skip to content

Commit

Permalink
Merge branch 'master' into vts-hash-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
timopollmeier authored Jul 8, 2021
2 parents ca30e38 + 717f61e commit 909f5a8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [21.4.3] (Unreleased)
### Added
### Changed
- Use less report cache SQL when adding results [#1618](https://github.com/greenbone/gvmd/pull/1618)

### Deprecated
### Removed
### Fixed
- Fix sending prefs for whole, growing VT families [#1603](https://github.com/greenbone/gvmd/pull/1603)

[Unreleased]: https://github.com/greenbone/gvmd/compare/v21.4.2...gvmd-21.04

Expand Down Expand Up @@ -120,6 +123,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Removed
### Fixed
- Fix VTs hash check and add --dump-vt-verification [#1611](https://github.com/greenbone/gvmd/pull/1611)
- Fix memory errors in modify_permission [#1613](https://github.com/greenbone/gvmd/pull/1613)

[Unreleased]: https://github.com/greenbone/gvmd/compare/v20.8.2...gvmd-20.08

Expand Down
27 changes: 9 additions & 18 deletions src/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -2483,7 +2483,7 @@ launch_osp_openvas_task (task_t task, target_t target, const char *scan_id,
gchar *clean_hosts, *clean_exclude_hosts, *clean_finished_hosts_str;
int alive_test, reverse_lookup_only, reverse_lookup_unify;
osp_target_t *osp_target;
GSList *osp_targets, *vts, *vt_groups;
GSList *osp_targets, *vts;
GHashTable *vts_hash_table;
osp_credential_t *ssh_credential, *smb_credential, *esxi_credential;
osp_credential_t *snmp_credential;
Expand Down Expand Up @@ -2624,7 +2624,6 @@ launch_osp_openvas_task (task_t task, target_t target, const char *scan_id,

/* Setup vulnerability tests (without preferences) */
vts = NULL;
vt_groups = NULL;
vts_hash_table
= g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
/* Value is freed in vts list. */
Expand All @@ -2634,18 +2633,7 @@ launch_osp_openvas_task (task_t task, target_t target, const char *scan_id,
while (next (&families))
{
const char *family = family_iterator_name (&families);
if (family && config_family_entire_and_growing (config, family))
{
gchar *filter;
osp_vt_group_t *vt_group;

filter = g_strdup_printf ("family=%s", family);
vt_group = osp_vt_group_new (filter);
g_free (filter);

vt_groups = g_slist_prepend (vt_groups, vt_group);
}
else if (family)
if (family)
{
iterator_t nvts;
init_nvt_iterator (&nvts, 0, config, family, NULL, 1, NULL);
Expand Down Expand Up @@ -2723,13 +2711,12 @@ launch_osp_openvas_task (task_t task, target_t target, const char *scan_id,
g_slist_free_full (osp_targets, (GDestroyNotify) osp_target_free);
// Credentials are freed with target
g_slist_free_full (vts, (GDestroyNotify) osp_vt_single_free);
g_slist_free_full (vt_groups, (GDestroyNotify) osp_vt_group_free);
g_hash_table_destroy (scanner_options);
return -1;
}

start_scan_opts.targets = osp_targets;
start_scan_opts.vt_groups = vt_groups;
start_scan_opts.vt_groups = NULL;
start_scan_opts.vts = vts;
start_scan_opts.scanner_params = scanner_options;
start_scan_opts.scan_id = scan_id;
Expand All @@ -2742,7 +2729,6 @@ launch_osp_openvas_task (task_t task, target_t target, const char *scan_id,
g_slist_free_full (osp_targets, (GDestroyNotify) osp_target_free);
// Credentials are freed with target
g_slist_free_full (vts, (GDestroyNotify) osp_vt_single_free);
g_slist_free_full (vt_groups, (GDestroyNotify) osp_vt_group_free);
g_hash_table_destroy (scanner_options);
return ret;
}
Expand Down Expand Up @@ -3061,9 +3047,11 @@ cve_scan_host (task_t task, report_t report, gvm_host_t *gvm_host)
{
iterator_t prognosis;
int prognosis_report_host, start_time;
GArray *results;

/* Add report_host with prognosis results and host details. */

results = g_array_new (TRUE, TRUE, sizeof (result_t));
start_time = time (NULL);
prognosis_report_host = 0;
init_host_prognosis_iterator (&prognosis, report_host);
Expand Down Expand Up @@ -3136,12 +3124,15 @@ cve_scan_host (task_t task, report_t report, gvm_host_t *gvm_host)
result = make_cve_result (task, ip, cve, severity, desc);
g_free (desc);

report_add_result (report, result);
g_array_append_val (results, result);

g_string_free (locations, TRUE);
}
cleanup_iterator (&prognosis);

report_add_results_array (report, results);
g_array_free (results, TRUE);

if (prognosis_report_host)
{
/* Complete the report_host. */
Expand Down
3 changes: 3 additions & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,9 @@ create_report (array_t*, const char *, const char *, const char *, const char *,
void
report_add_result (report_t, result_t);

void
report_add_results_array (report_t, GArray *);

char*
report_uuid (report_t);

Expand Down
69 changes: 65 additions & 4 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -21004,6 +21004,59 @@ report_add_result (report_t report, result_t result)
report, report);
}

/**
* @brief Add results from an array to a report.
*
* @param[in] report The report to add the results to.
* @param[in] results GArray containing the row ids of the results to add.
*/
void
report_add_results_array (report_t report, GArray *results)
{
GString *array_sql;
int index;

if (report == 0 || results == NULL || results->len == 0)
return;

array_sql = g_string_new ("(");
for (index = 0; index < results->len; index++)
{
result_t result;
result = g_array_index (results, result_t, index);

if (index)
g_string_append (array_sql, ", ");
g_string_append_printf (array_sql, "%llu", result);
}
g_string_append_c (array_sql, ')');

sql ("UPDATE results SET report = %llu,"
" owner = (SELECT reports.owner"
" FROM reports WHERE id = %llu)"
" WHERE id IN %s;",
report, report, array_sql->str);

for (index = 0; index < results->len; index++)
{
result_t result;
result = g_array_index (results, result_t, index);

report_add_result_for_buffer (report, result);
}

sql ("UPDATE report_counts"
" SET end_time = (SELECT coalesce(min(overrides.end_time), 0)"
" FROM overrides, results"
" WHERE overrides.nvt = results.nvt"
" AND results.report = %llu"
" AND overrides.end_time >= m_now ())"
" WHERE report = %llu AND override = 1;",
report, report);

g_string_free (array_sql, TRUE);
}

/**
* @brief Filter columns for report iterator.
*/
Expand Down Expand Up @@ -28742,6 +28795,7 @@ parse_osp_report (task_t task, report_t report, const char *report_xml)
char *defs_file = NULL;
time_t start_time, end_time;
gboolean has_results = FALSE;
GArray *results_array;

assert (task);
assert (report);
Expand All @@ -28755,6 +28809,7 @@ parse_osp_report (task_t task, report_t report, const char *report_xml)

sql_begin_immediate ();
/* Set the report's start and end times. */
results_array = g_array_new (TRUE, TRUE, sizeof (result_t));
start_time = 0;
str = entity_attribute (entity, "start_time");
if (str)
Expand Down Expand Up @@ -28876,7 +28931,7 @@ parse_osp_report (task_t task, report_t report, const char *report_xml)
severity_str ?: severity,
qod_int,
path);
report_add_result (report, result);
g_array_append_val (results_array, result);
}
g_free (nvt_id);
g_free (desc);
Expand All @@ -28885,11 +28940,16 @@ parse_osp_report (task_t task, report_t report, const char *report_xml)
}

if (has_results)
sql ("UPDATE reports SET modification_time = m_now() WHERE id = %llu;",
report);
{
sql ("UPDATE reports SET modification_time = m_now() WHERE id = %llu;",
report);
report_add_results_array (report, results_array);
}


end_parse_osp_report:
sql_commit ();
g_array_free (results_array, TRUE);
g_free (defs_file);
free_entity (entity);
}
Expand Down Expand Up @@ -43631,6 +43691,7 @@ modify_permission (const char *permission_id, const char *name_arg,
free (new_resource_id);
free (existing_subject_type);
free (new_subject_id);
g_free (subject_where_old);
sql_rollback ();
return ret;
}
Expand Down Expand Up @@ -43683,7 +43744,6 @@ modify_permission (const char *permission_id, const char *name_arg,
|| (resource_id == NULL));

quoted_name = sql_quote (name);
g_free (name);

sql ("UPDATE permissions SET"
" name = '%s',"
Expand Down Expand Up @@ -43767,6 +43827,7 @@ modify_permission (const char *permission_id, const char *name_arg,
free (new_resource_id);
free (existing_subject_type);
free (new_subject_id);
g_free (name);
free (old_name);
free (old_resource_type);
g_free (subject_where);
Expand Down

0 comments on commit 909f5a8

Please sign in to comment.