Skip to content

Commit

Permalink
Add connect_main_kb() function and use it.
Browse files Browse the repository at this point in the history
Release the connection after using it.
  • Loading branch information
jjnicola committed Feb 18, 2020
1 parent 7313dac commit 31d65d9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fix hanging scans. [#423](https://github.com/greenbone/openvas/pull/423)
- Improve signal handling when update vhosts list. [#426](https://github.com/greenbone/openvas/pull/426)
- Wait for all children instead of waiting just for one a time. [#429](https://github.com/greenbone/openvas/pull/429)
- Release redis connection. [[#452](https://github.com/greenbone/openvas/pull/452)

[7.0.1]: https://github.com/greenbone/openvas/compare/v7.0.0...openvas-7.0

Expand Down
61 changes: 32 additions & 29 deletions src/attack.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,38 @@ enum net_scan_status
PRIVATE FUNCTIONS
********************************************************/
/**
* @brief Connect to the main kb. Must be released with
* kb_lnk_reset() after use.
*
* @param[out] main_kb The connection to the kb.
* @return 0 on success, -1 on failure.
*/
static int
connect_main_kb (kb_t *main_kb)
{
int i = atoi (prefs_get ("ov_maindbid"));

*main_kb = kb_direct_conn (prefs_get ("db_address"), i);
if (main_kb)
return 0;

g_warning ("Not possible to get the main kb connection.");
return -1;
}

/**
* @brief Add the Host KB index to the list of readable KBs
* used by ospd-openvas.
*/
static int
static void
set_kb_readable (int host_kb_index)
{
int i = atoi (prefs_get ("ov_maindbid"));
kb_t main_kb = NULL;

main_kb = kb_direct_conn (prefs_get ("db_address"), i);
if (main_kb)
{
kb_item_add_int_unique (main_kb, "internal/dbindex", host_kb_index);
return 0;
}
g_warning ("Not possible to add the kb index %d to the list of "
"ready to read kb",
host_kb_index);
return -1;
connect_main_kb (&main_kb);
kb_item_add_int_unique (main_kb, "internal/dbindex", host_kb_index);
kb_lnk_reset (main_kb);
}

/**
Expand All @@ -132,21 +143,15 @@ set_kb_readable (int host_kb_index)
static void
set_scan_status (char *status)
{
int i = atoi (prefs_get ("ov_maindbid"));
kb_t main_kb = NULL;
char buffer[96];
char *scan_id = NULL;

main_kb = kb_direct_conn (prefs_get ("db_address"), i);
if (main_kb)
{
char buffer[96];
char *scan_id = kb_item_get_str (main_kb, ("internal/scanid"));

snprintf (buffer, sizeof (buffer), "internal/%s", scan_id);
kb_item_set_str (main_kb, buffer, status, 0);

return;
}
g_warning ("Not possible to set the scan as finished");
connect_main_kb (&main_kb);
scan_id = kb_item_get_str (main_kb, ("internal/scanid"));
snprintf (buffer, sizeof (buffer), "internal/%s", scan_id);
kb_item_set_str (main_kb, buffer, status, 0);
kb_lnk_reset (main_kb);
}

/**
Expand Down Expand Up @@ -984,12 +989,11 @@ check_kb_access (void)
static void
handle_scan_stop_signal ()
{
int i = atoi (prefs_get ("ov_maindbid"));
kb_t main_kb = NULL;
char *pid;

global_scan_stop = 1;
main_kb = kb_direct_conn (prefs_get ("db_address"), i);
connect_main_kb (&main_kb);
pid = kb_item_get_str (main_kb, ("internal/ovas_pid"));
kb_lnk_reset (main_kb);

Expand Down Expand Up @@ -1087,15 +1091,14 @@ attack_network (struct scan_globals *globals, kb_t *network_kb)
if (plugins_init_error > 0)
{
char buf[96];
int i = atoi (prefs_get ("ov_maindbid"));
kb_t main_kb = NULL;

sprintf (buf,
"%d errors were found during the plugin scheduling. "
"Some plugins have not been launched.",
plugins_init_error);

main_kb = kb_direct_conn (prefs_get ("db_address"), i);
connect_main_kb (&main_kb);
error_message_to_client2 (main_kb, buf, NULL);
kb_lnk_reset (main_kb);
}
Expand Down

0 comments on commit 31d65d9

Please sign in to comment.