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

Resolve tags: solution and solution_type #681

Merged
merged 8 commits into from
Aug 11, 2019
6 changes: 6 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
34 changes: 33 additions & 1 deletion 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,6 +1162,38 @@ migrate_213_to_214 ()
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

/**
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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really related ;)

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
76 changes: 44 additions & 32 deletions src/manage_sql_nvts.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ insert_nvt (const nvti_t *nvti)
gchar *qod_str, *qod_type, *cve;
gchar *quoted_name, *quoted_cve, *quoted_tag;
gchar *quoted_cvss_base, *quoted_qod_type, *quoted_family, *value;
gchar *quoted_solution_type;
gchar *quoted_solution, *quoted_solution_type;
int creation_time, modification_time, qod, i;

cve = nvti_refs (nvti, "cve", "", 0);
Expand All @@ -196,6 +196,11 @@ insert_nvt (const nvti_t *nvti)

g_free (cve);

quoted_solution = sql_quote (nvti_solution (nvti) ?
nvti_solution (nvti) : "");
quoted_solution_type = sql_quote (nvti_solution_type (nvti) ?
nvti_solution_type (nvti) : "");

if (nvti_tag (nvti))
{
gchar **split, **point;
Expand Down Expand Up @@ -243,6 +248,25 @@ insert_nvt (const nvti_t *nvti)
}
g_strfreev (split);

/* 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 (nvti_solution (nvti))
{
if (tag->str)
g_string_append_printf (tag, "|solution=%s", nvti_solution (nvti));
else
g_string_append_printf (tag, "solution=%s", nvti_solution (nvti));
}
if (nvti_solution_type (nvti))
{
if (tag->str)
g_string_append_printf (tag, "|solution_type=%s", nvti_solution_type (nvti));
else
g_string_append_printf (tag, "solution_type=%s", nvti_solution_type (nvti));
}

quoted_tag = sql_quote (tag->str);
g_string_free (tag, TRUE);
}
Expand Down Expand Up @@ -305,28 +329,19 @@ insert_nvt (const nvti_t *nvti)
}
g_free (value);

value = tag_value (nvti_tag (nvti), "solution_type");
if (value)
{
quoted_solution_type = sql_quote (value);
g_free (value);
}
else
quoted_solution_type = g_strdup ("");

if (sql_int ("SELECT EXISTS (SELECT * FROM nvts WHERE oid = '%s');",
nvti_oid (nvti)))
sql ("DELETE FROM nvts WHERE oid = '%s';", nvti_oid (nvti));

sql ("INSERT into nvts (oid, name,"
" cve, tag, category, family, cvss_base,"
" creation_time, modification_time, uuid, solution_type,"
" qod, qod_type)"
" solution, qod, qod_type)"
" VALUES ('%s', '%s', '%s',"
" '%s', %i, '%s', '%s', %i, %i, '%s', '%s', %d, '%s');",
" '%s', %i, '%s', '%s', %i, %i, '%s', '%s', '%s', %d, '%s');",
nvti_oid (nvti), quoted_name, quoted_cve, quoted_tag,
nvti_category (nvti), quoted_family, quoted_cvss_base, creation_time,
modification_time, nvti_oid (nvti), quoted_solution_type,
modification_time, nvti_oid (nvti), quoted_solution_type, quoted_solution,
qod, quoted_qod_type);

sql ("DELETE FROM vt_refs where vt_oid = '%s';", nvti_oid (nvti));
Expand All @@ -353,6 +368,7 @@ insert_nvt (const nvti_t *nvti)
g_free (quoted_tag);
g_free (quoted_cvss_base);
g_free (quoted_family);
g_free (quoted_solution);
g_free (quoted_solution_type);
g_free (quoted_qod_type);
}
Expand Down Expand Up @@ -1025,24 +1041,6 @@ get_tag (entity_t vt)
first = 0;
}

child = entity_child (vt, "solution");
if (child)
{
const gchar *type;

g_string_append_printf (tag,
"%ssolution=%s",
first ? "" : "|",
entity_text (child));
first = 0;

type = entity_attribute (child, "type");
if (type == NULL)
g_debug ("%s: SOLUTION missing type", __FUNCTION__);
else
g_string_append_printf (tag, "|solution_type=%s", type);
}

child = entity_child (vt, "severities");
if (child)
{
Expand Down Expand Up @@ -1208,7 +1206,7 @@ nvti_from_vt (entity_t vt)
{
nvti_t *nvti = nvti_new ();
const char *id;
entity_t name, detection, refs, ref, custom, family, category;
entity_t name, detection, solution, refs, ref, custom, family, category;
entities_t children;
gchar *tag, *cvss_base, *parsed_tags;

Expand Down Expand Up @@ -1236,6 +1234,20 @@ nvti_from_vt (entity_t vt)
nvti_set_qod_type (nvti, entity_attribute (detection, "qod_type"));
}

solution = entity_child (vt, "solution");
if (solution)
{
const gchar *type;

nvti_set_solution (nvti, entity_text (solution));

type = entity_attribute (solution, "type");
if (type == NULL)
g_debug ("%s: SOLUTION missing type", __FUNCTION__);
else
nvti_set_solution_type (nvti, type);
}

refs = entity_child (vt, "refs");
if (refs == NULL)
{
Expand Down