Skip to content

Commit

Permalink
Merge pull request #681 from janowagner/resolve_tags
Browse files Browse the repository at this point in the history
Resolve tags: solution and solution_type
  • Loading branch information
mattmundell authored Aug 11, 2019
2 parents 8bfa109 + 08cd580 commit 79e39dd
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 41 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ include (CPack)

## Variables

set (GVMD_DATABASE_VERSION 215)
set (GVMD_DATABASE_VERSION 216)

set (GVMD_SCAP_DATABASE_VERSION 15)

Expand Down
24 changes: 23 additions & 1 deletion src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -10170,10 +10170,30 @@ results_xml_append_nvt (iterator_t *results, GString *buffer, int cert_loaded)
else
{
const char *cvss_base = result_iterator_nvt_cvss_base (results);
GString *tags = g_string_new (result_iterator_nvt_tag (results));

if (!cvss_base && !strcmp (oid, "0"))
cvss_base = "0.0";

/* Add the elements that are expected as part of the pipe-separated tag list
* via API although internally already explicitely stored. Once the API is
* extended to have these elements explicitely, they do not need to be
* added to this string anymore. */
if (result_iterator_nvt_solution (results))
{
if (tags->str)
g_string_append_printf (tags, "|solution=%s", result_iterator_nvt_solution (results));
else
g_string_append_printf (tags, "solution=%s", result_iterator_nvt_solution (results));
}
if (result_iterator_nvt_solution_type (results))
{
if (tags->str)
g_string_append_printf (tags, "|solution_type=%s", result_iterator_nvt_solution_type (results));
else
g_string_append_printf (tags, "solution_type=%s", result_iterator_nvt_solution_type (results));
}

buffer_xml_append_printf (buffer,
"<nvt oid=\"%s\">"
"<type>nvt</type>"
Expand All @@ -10185,14 +10205,16 @@ results_xml_append_nvt (iterator_t *results, GString *buffer, int cert_loaded)
result_iterator_nvt_name (results) ?: oid,
result_iterator_nvt_family (results) ?: "",
cvss_base ?: "",
result_iterator_nvt_tag (results) ?: "");
tags->str ?: "");

buffer_xml_append_printf (buffer, "<refs>");
result_iterator_nvt_refs_append (buffer, results);
results_xml_append_cert (buffer, oid, cert_loaded,
result_iterator_has_cert_bunds (results),
result_iterator_has_dfn_certs (results));
buffer_xml_append_printf (buffer, "</refs>");

g_string_free (tags, TRUE);
}

}
Expand Down
28 changes: 25 additions & 3 deletions src/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -7374,7 +7374,7 @@ get_nvti_xml (iterator_t *nvts, int details, int pref_count,
if (details)
{
int tag_count;
GString *refs_str, *tags_str, *buffer;
GString *refs_str, *tags_str, *buffer, *nvt_tags;
iterator_t cert_refs_iterator, tags;
gchar *tag_name_esc, *tag_value_esc, *tag_comment_esc;
char *default_timeout = nvt_default_timeout (oid);
Expand All @@ -7384,6 +7384,28 @@ get_nvti_xml (iterator_t *nvts, int details, int pref_count,

#undef DEF

nvt_tags = g_string_new (tag_text);
g_free (tag_text);

/* Add the elements that are expected as part of the pipe-separated tag list
* via API although internally already explicitely stored. Once the API is
* extended to have these elements explicitely, they do not need to be
* added to this string anymore. */
if (nvt_iterator_solution (nvts))
{
if (nvt_tags->str)
g_string_append_printf (nvt_tags, "|solution=%s", nvt_iterator_solution (nvts));
else
g_string_append_printf (nvt_tags, "solution=%s", result_iterator_nvt_solution (nvts));
}
if (nvt_iterator_solution_type (nvts))
{
if (nvt_tags->str)
g_string_append_printf (nvt_tags, "|solution_type=%s", nvt_iterator_solution_type (nvts));
else
g_string_append_printf (nvt_tags, "solution_type=%s", nvt_iterator_solution_type (nvts));
}

refs_str = g_string_new ("");

if (manage_cert_loaded())
Expand Down Expand Up @@ -7495,12 +7517,12 @@ get_nvti_xml (iterator_t *nvts, int details, int pref_count,
nvt_iterator_qod (nvts),
nvt_iterator_qod_type (nvts),
refs_str->str,
tag_text,
nvt_tags->str,
pref_count,
timeout ? timeout : "",
default_timeout ? default_timeout : "");
g_free (family_text);
g_free (tag_text);
g_string_free(nvt_tags, 1);
g_string_free(refs_str, 1);
g_string_free(tags_str, 1);

Expand Down
9 changes: 9 additions & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,12 @@ result_iterator_nvt_oid (iterator_t*);
const char*
result_iterator_nvt_name (iterator_t *);

const char*
result_iterator_nvt_solution (iterator_t *);

const char*
result_iterator_nvt_solution_type (iterator_t *);

const char*
result_iterator_nvt_family (iterator_t *);

Expand Down Expand Up @@ -2008,6 +2014,9 @@ nvt_iterator_qod (iterator_t*);
const char*
nvt_iterator_qod_type ( iterator_t *iterator );

const char*
nvt_iterator_solution (iterator_t*);

const char*
nvt_iterator_solution_type (iterator_t*);

Expand Down
39 changes: 36 additions & 3 deletions src/manage_migrators.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2013-2018 Greenbone Networks GmbH
/* Copyright (C) 2013-2019 Greenbone Networks GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
Expand Down Expand Up @@ -1162,8 +1162,6 @@ migrate_213_to_214 ()
return 0;
}

#undef UPDATE_DASHBOARD_SETTINGS

/**
* @brief Migrate the database from version 214 to version 215.
*
Expand Down Expand Up @@ -1196,6 +1194,40 @@ migrate_214_to_215 ()
return 0;
}

/**
* @brief Migrate the database from version 215 to version 216.
*
* @return 0 success, -1 error.
*/
int
migrate_215_to_216 ()
{
sql_begin_immediate ();

/* Ensure that the database is currently version 215. */

if (manage_db_version () != 215)
{
sql_rollback ();
return -1;
}

/* Update the database. */

/* Extend table "nvts" with additional column "solution" */
sql ("ALTER TABLE IF EXISTS nvts ADD COLUMN solution text;");

/* Set the database version to 216. */

set_db_version (216);

sql_commit ();

return 0;
}

#undef UPDATE_DASHBOARD_SETTINGS

/**
* @brief The oldest version for which migration is supported
*/
Expand All @@ -1215,6 +1247,7 @@ static migrator_t database_migrators[] = {
{213, migrate_212_to_213},
{214, migrate_213_to_214},
{215, migrate_214_to_215},
{216, migrate_215_to_216},
/* End marker. */
{-1, NULL}};

Expand Down
41 changes: 40 additions & 1 deletion src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -15550,7 +15550,7 @@ update_nvti_cache ()
nvti_cache = nvtis_new ();

init_iterator (&nvts,
"SELECT oid, name, family, cvss_base, tag FROM nvts;");
"SELECT oid, name, family, cvss_base, tag, solution, solution_type FROM nvts;");
while (next (&nvts))
{
iterator_t refs;
Expand All @@ -15561,6 +15561,8 @@ update_nvti_cache ()
nvti_set_family (nvti, iterator_string (&nvts, 2));
nvti_set_cvss_base (nvti, iterator_string (&nvts, 3));
nvti_set_tag (nvti, iterator_string (&nvts, 4));
nvti_set_solution (nvti, iterator_string (&nvts, 5));
nvti_set_solution_type (nvti, iterator_string (&nvts, 6));

init_iterator (&refs,
"SELECT type, ref_id, ref_text"
Expand Down Expand Up @@ -24300,6 +24302,43 @@ result_iterator_nvt_name (iterator_t *iterator)
return NULL;
}

/**
* @brief Get the NVT solution from a result iterator.
*
* @param[in] iterator Iterator.
*
* @return The solution of the NVT that produced the result, or NULL on error.
*/
const char*
result_iterator_nvt_solution (iterator_t *iterator)
{
nvti_t *nvti;
if (iterator->done) return NULL;
nvti = lookup_nvti (result_iterator_nvt_oid (iterator));
if (nvti)
return nvti_solution (nvti);
return NULL;
}

/**
* @brief Get the NVT solution_type from a result iterator.
*
* @param[in] iterator Iterator.
*
* @return The solution_type of the NVT that produced the result,
* or NULL on error.
*/
const char*
result_iterator_nvt_solution_type (iterator_t *iterator)
{
nvti_t *nvti;
if (iterator->done) return NULL;
nvti = lookup_nvti (result_iterator_nvt_oid (iterator));
if (nvti)
return nvti_solution_type (nvti);
return NULL;
}

/**
* @brief Get the NVT family from a result iterator.
*
Expand Down
Loading

0 comments on commit 79e39dd

Please sign in to comment.