Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slave host asset handling (master) #736

Merged
merged 7 commits into from
Sep 4, 2019
Merged
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Change get_tickets to use the status text for filtering. [#697](https://github.com/greenbone/gvmd/pull/697)
- Made checks to prevent duplicate user names stricter. [#708](https://github.com/greenbone/gvmd/pull/708) [#722](https://github.com/greenbone/gvmd/pull/722)
- Send delete command to ospd after stopping the task. [#710](https://github.com/greenbone/gvmd/pull/710)
- Check whether hosts are alive and have results when adding them in slave scans. [#717](https://github.com/greenbone/gvmd/pull/717)
- Check whether hosts are alive and have results when adding them in slave scans. [#717](https://github.com/greenbone/gvmd/pull/717) [#736](https://github.com/greenbone/gvmd/pull/736)


### Fixed
Expand Down
79 changes: 53 additions & 26 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -31399,7 +31399,7 @@ report_host_ip (const char *host)
int
report_host_noticeable (report_t report, const gchar *host)
{
report_host_t report_host;
report_host_t report_host = 0;

sql_int64 (&report_host,
"SELECT id FROM report_hosts"
Expand Down Expand Up @@ -48748,7 +48748,7 @@ int
update_from_slave (task_t task, entity_t get_report, entity_t *report,
int *next_result)
{
entity_t entity, host_start, start;
entity_t entity, host, start;
entities_t results, hosts, entities;
int current_commit_size;
GString *buffer;
Expand Down Expand Up @@ -48782,34 +48782,20 @@ update_from_slave (task_t task, entity_t get_report, entity_t *report,
sql_begin_immediate ();
hosts = (*report)->entities;
current_commit_size = 0;
while ((host_start = first_entity (hosts)))
while ((host = first_entity (hosts)))
{
if (strcmp (entity_name (host_start), "host") == 0)
if (strcmp (entity_name (host), "host") == 0)
{
entity_t ip, end;
char *uuid;
entity_t ip;

ip = entity_child (host_start, "ip");
ip = entity_child (host, "ip");
if (ip == NULL)
goto rollback_fail;

start = entity_child (host_start, "start");
start = entity_child (host, "start");
if (start == NULL)
goto rollback_fail;

end = entity_child (host_start, "end");
if (end
&& entity_text (end)
&& strcmp (entity_text (end), "")
&& report_host_noticeable (global_current_report,
entity_text (ip)))
{
uuid = report_uuid (global_current_report);
host_notice (entity_text (ip), "ip", entity_text (ip),
"Report Host", uuid, 1, 1);
free (uuid);
}

set_scan_host_start_time (global_current_report,
entity_text (ip),
entity_text (start));
Expand Down Expand Up @@ -48843,14 +48829,14 @@ update_from_slave (task_t task, entity_t get_report, entity_t *report,
{
if (strcmp (entity_name (entity), "result") == 0)
{
entity_t host, hostname, port, nvt, threat, description;
entity_t result_host, hostname, port, nvt, threat, description;
const char *oid;

host = entity_child (entity, "host");
if (host == NULL)
result_host = entity_child (entity, "host");
if (result_host == NULL)
goto rollback_fail;

hostname = entity_child (host, "hostname");
hostname = entity_child (result_host, "hostname");

port = entity_child (entity, "port");
if (port == NULL)
Expand All @@ -48873,7 +48859,7 @@ update_from_slave (task_t task, entity_t get_report, entity_t *report,

buffer_insert (buffer,
task,
entity_text (host),
entity_text (result_host),
hostname ? entity_text (hostname) : "",
entity_text (port),
oid,
Expand All @@ -48898,6 +48884,47 @@ update_from_slave (task_t task, entity_t get_report, entity_t *report,
update_from_slave_insert (buffer, global_current_report);
g_string_free (buffer, TRUE);
sql_commit ();

sql_begin_immediate ();
current_commit_size = 0;
hosts = (*report)->entities;
while ((host = first_entity (hosts)))
{
if (strcmp (entity_name (host), "host") == 0)
{
entity_t ip, end;
char *uuid;

ip = entity_child (host, "ip");
if (ip == NULL)
goto rollback_fail;

end = entity_child (host, "end");
if (end
&& entity_text (end)
&& strcmp (entity_text (end), "")
&& report_host_noticeable (global_current_report,
entity_text (ip)))
{
uuid = report_uuid (global_current_report);
host_notice (entity_text (ip), "ip", entity_text (ip),
"Report Host", uuid, 1, 1);
free (uuid);

current_commit_size++;
if (slave_commit_size
&& current_commit_size >= slave_commit_size)
{
sql_commit ();
sql_begin_immediate ();
current_commit_size = 0;
}
}
}

hosts = next_entities (hosts);
}
sql_commit ();
return 0;

rollback_fail:
Expand Down