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

Add report_id filter keyword for TLS certificates #728

Merged
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased]

### Added
- Added TLS certificates as a new resource type [#585](https://github.com/greenbone/gvmd/pull/585) [#663](https://github.com/greenbone/gvmd/pull/663) [#673](https://github.com/greenbone/gvmd/pull/673) [#695](https://github.com/greenbone/gvmd/pull/695) [#703](https://github.com/greenbone/gvmd/pull/703) [#732](https://github.com/greenbone/gvmd/pull/732)
- Added TLS certificates as a new resource type [#585](https://github.com/greenbone/gvmd/pull/585) [#663](https://github.com/greenbone/gvmd/pull/663) [#673](https://github.com/greenbone/gvmd/pull/673) [#695](https://github.com/greenbone/gvmd/pull/695) [#703](https://github.com/greenbone/gvmd/pull/703) [#728](https://github.com/greenbone/gvmd/pull/728) [#732](https://github.com/greenbone/gvmd/pull/732)
- Update NVTs via OSP [#392](https://github.com/greenbone/gvmd/pull/392) [#609](https://github.com/greenbone/gvmd/pull/609) [#626](https://github.com/greenbone/gvmd/pull/626)
- Handle addition of ID to NVT preferences. [#413](https://github.com/greenbone/gvmd/pull/413)
- Add setting 'OMP Slave Check Period' [#491](https://github.com/greenbone/gvmd/pull/491)
Expand Down
7 changes: 7 additions & 0 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -3214,6 +3214,13 @@ create_tables ()
" UNION SELECT 1 AS autofp_selection"
" UNION SELECT 2 AS autofp_selection) AS autofp_opts;");

sql ("CREATE OR REPLACE VIEW tls_certificate_source_origins AS"
" SELECT sources.id AS source_id, tls_certificate,"
" origin_id, origin_type, origin_data"
" FROM tls_certificate_sources AS sources"
" JOIN tls_certificate_origins AS origins"
" ON sources.origin = origins.id;");

sql ("DROP VIEW IF EXISTS vulns;");
if (sql_int ("SELECT EXISTS (SELECT * FROM information_schema.tables"
" WHERE table_catalog = '%s'"
Expand Down
4 changes: 4 additions & 0 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -63900,6 +63900,10 @@ type_extra_where (const char *type, int trash, const char *filter,
else
extra_where = g_strdup (" AND hidden = 0");
}
else if (strcasecmp (type, "TLS_CERTIFICATE") == 0)
{
extra_where = tls_certificate_extra_where (filter);
}
else if (strcasecmp (type, "REPORT") == 0)
{
if (trash)
Expand Down
110 changes: 98 additions & 12 deletions src/manage_sql_tls_certificates.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,44 @@ tls_certificate_select_columns ()
return columns;
}

/**
* @brief Get extra_where string for a TLS certificate iterator or count.
*
* @param[in] filter Filter string.
*
* @return Newly allocated extra_where string.
*/
gchar *
tls_certificate_extra_where (const char *filter)
{
GString *ret;
gchar *report_id;

ret = g_string_new ("");

report_id = filter_term_value (filter, "report_id");

if (report_id)
{
gchar *quoted_id;
quoted_id = sql_quote (report_id);
g_string_append_printf (ret,
" AND"
" (EXISTS"
" (SELECT * FROM"
" tls_certificate_source_origins AS src_orig"
" WHERE tls_certificate = tls_certificates.id"
" AND origin_type = 'Report'"
" AND origin_id = '%s'))",
quoted_id);
g_free (quoted_id);
}

g_free (report_id);

return g_string_free (ret, FALSE);
}

/**
* @brief Count number of tls_certificates.
*
Expand All @@ -192,9 +230,33 @@ tls_certificate_count (const get_data_t *get)
{
static const char *filter_columns[] = TLS_CERTIFICATE_ITERATOR_FILTER_COLUMNS;
static column_t columns[] = TLS_CERTIFICATE_ITERATOR_COLUMNS;
gchar *filter;
char *extra_where;
int ret;

return count ("tls_certificate", get, columns, NULL, filter_columns,
0, 0, 0, TRUE);
if (get->filt_id && strcmp (get->filt_id, FILT_ID_NONE))
mattmundell marked this conversation as resolved.
Show resolved Hide resolved
{
if (get->filter_replacement)
/* Replace the filter term with one given by the caller. This is
* used by GET_REPORTS to use the default filter with any task (when
* given the special value of -3 in filt_id). */
filter = g_strdup (get->filter_replacement);
else
filter = filter_term (get->filt_id);
if (filter == NULL)
return 2;
}
else
filter = NULL;

extra_where
= tls_certificate_extra_where (filter ? filter : get->filter);

ret = count ("tls_certificate", get, columns, NULL, filter_columns,
0, 0, extra_where, TRUE);

g_free (extra_where);
return ret;
}

/**
Expand All @@ -211,17 +273,41 @@ init_tls_certificate_iterator (iterator_t *iterator, const get_data_t *get)
{
static const char *filter_columns[] = TLS_CERTIFICATE_ITERATOR_FILTER_COLUMNS;
static column_t columns[] = TLS_CERTIFICATE_ITERATOR_COLUMNS;
gchar *filter;
char *extra_where;
int ret;

return init_get_iterator (iterator,
"tls_certificate",
get,
columns,
NULL,
filter_columns,
0,
NULL,
NULL,
TRUE);
if (get->filt_id && strcmp (get->filt_id, FILT_ID_NONE))
{
if (get->filter_replacement)
/* Replace the filter term with one given by the caller. This is
* used by GET_REPORTS to use the default filter with any task (when
* given the special value of -3 in filt_id). */
filter = g_strdup (get->filter_replacement);
else
filter = filter_term (get->filt_id);
if (filter == NULL)
return 2;
}
else
filter = NULL;

extra_where
= tls_certificate_extra_where (filter ? filter : get->filter);

ret = init_get_iterator (iterator,
"tls_certificate",
get,
columns,
NULL,
filter_columns,
0,
NULL,
extra_where,
TRUE);

g_free (extra_where);
return ret;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/manage_sql_tls_certificates.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ tls_certificate_filter_columns ();
column_t*
tls_certificate_select_columns ();

gchar *
tls_certificate_extra_where (const char *);

int
delete_tls_certificate (const char *, int);

Expand Down
5 changes: 5 additions & 0 deletions src/schema_formats/XML/GMP.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -22951,6 +22951,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
<type>iso_time</type>
<summary>Time the certificate was last collected</summary>
</column>
<option>
<name>report_id</name>
<type>uuid</type>
<summary>UUID of the report the cerificate must appear in</summary>
</option>
</filter_keywords>
</attrib>
<attrib>
Expand Down