Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
- Rename scap.cpe_match_range to scap.cpe_match_strings
- Rename cpe in cpe_match_range to criteria
- Add cpe_name to scap.cpe_matches
- Add status to the configuration nodes in the response of
get_info command
  • Loading branch information
a-h-abdelsalam committed Nov 5, 2024
1 parent 64983ca commit 6ff3d18
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 96 deletions.
44 changes: 23 additions & 21 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -13282,29 +13282,31 @@ print_cpe_match_nodes_xml (resource_t node, GString *buffer)
xml_string_append (buffer, "<operator>%s</operator>", operator?: "");
xml_string_append (buffer, "<negate>%s</negate>", negate? "1" : "0");

Check warning on line 13283 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13282-L13283

Added lines #L13282 - L13283 were not covered by tests

init_cpe_match_range_iterator (&cpe_match_ranges, node);
init_cpe_match_string_iterator (&cpe_match_ranges, node);

Check warning on line 13285 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13285

Added line #L13285 was not covered by tests
while (next (&cpe_match_ranges))
{
const gchar *vsi, *vse, *vei, *vee, *match_criteria_id, *match_string;
const gchar *vsi, *vse, *vei, *vee, *match_criteria_id, *criteria, *status;

xml_string_append (buffer, "<match_criteria>");
xml_string_append (buffer, "<match_string>");

Check warning on line 13290 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13290

Added line #L13290 was not covered by tests
match_criteria_id
= cpe_match_range_iterator_match_criteria_id (&cpe_match_ranges);
match_string = cpe_match_range_iterator_cpe (&cpe_match_ranges);
= cpe_match_string_iterator_match_criteria_id (&cpe_match_ranges);
criteria = cpe_match_string_iterator_criteria (&cpe_match_ranges);
status = cpe_match_string_iterator_status (&cpe_match_ranges);

Check warning on line 13294 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13292-L13294

Added lines #L13292 - L13294 were not covered by tests

xml_string_append (buffer,

Check warning on line 13296 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13296

Added line #L13296 was not covered by tests
"<match_string>%s</match_string>",
match_string?: "");
xml_string_append (buffer,
"<vulnerable>%s</vulnerable>",
cpe_match_range_iterator_vulnerable (&cpe_match_ranges) != 0
"<criteria>%s</criteria>"
"<vulnerable>%s</vulnerable>"
"<status>%s</status>",
criteria?: "",
cpe_match_string_iterator_vulnerable (&cpe_match_ranges) != 0

Check warning on line 13301 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13300-L13301

Added lines #L13300 - L13301 were not covered by tests
? "1"
: "0");
: "0",
status?: "");

Check warning on line 13304 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13304

Added line #L13304 was not covered by tests

vsi = cpe_match_range_iterator_version_start_incl (&cpe_match_ranges);
vse = cpe_match_range_iterator_version_start_excl (&cpe_match_ranges);
vei = cpe_match_range_iterator_version_end_incl (&cpe_match_ranges);
vee = cpe_match_range_iterator_version_end_excl (&cpe_match_ranges);
vsi = cpe_match_string_iterator_version_start_incl (&cpe_match_ranges);
vse = cpe_match_string_iterator_version_start_excl (&cpe_match_ranges);
vei = cpe_match_string_iterator_version_end_incl (&cpe_match_ranges);
vee = cpe_match_string_iterator_version_end_excl (&cpe_match_ranges);

Check warning on line 13309 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13306-L13309

Added lines #L13306 - L13309 were not covered by tests

xml_string_append (buffer,

Check warning on line 13311 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13311

Added line #L13311 was not covered by tests
"<version_start_including>%s</version_start_including>",
Expand All @@ -13320,24 +13322,24 @@ print_cpe_match_nodes_xml (resource_t node, GString *buffer)
vee ?: "");

Check warning on line 13322 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13322

Added line #L13322 was not covered by tests

iterator_t cpe_matches;
init_cpe_match_range_matches_iterator (&cpe_matches, match_criteria_id);
init_cpe_match_string_matches_iterator (&cpe_matches, match_criteria_id);
xml_string_append (buffer, "<matched_cpes>");

Check warning on line 13326 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13325-L13326

Added lines #L13325 - L13326 were not covered by tests

while (next (&cpe_matches))
{
iterator_t cpes;

init_iterator (&cpes,

Check warning on line 13332 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13332

Added line #L13332 was not covered by tests
"SELECT name, deprecated FROM scap.cpes"
"SELECT deprecated FROM scap.cpes"
" WHERE cpe_name_id = '%s';",
cpe_matches_cpe_name_id(&cpe_matches));

const char* cpe = NULL;
const char* cpe = cpe_matches_cpe_name (&cpe_matches);

Check warning on line 13337 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13337

Added line #L13337 was not covered by tests

int deprecated = 0;

Check warning on line 13339 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13339

Added line #L13339 was not covered by tests
while (next (&cpes))
{
cpe = iterator_string (&cpes, 0);
deprecated = iterator_int (&cpes, 1);
deprecated = iterator_int (&cpes, 0);

Check warning on line 13342 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13342

Added line #L13342 was not covered by tests
}
cleanup_iterator (&cpes);

Check warning on line 13344 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13344

Added line #L13344 was not covered by tests

Expand All @@ -13361,7 +13363,7 @@ print_cpe_match_nodes_xml (resource_t node, GString *buffer)
xml_string_append (buffer, "</cpe>");

Check warning on line 13363 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13363

Added line #L13363 was not covered by tests
}
xml_string_append (buffer, "</matched_cpes>");
xml_string_append (buffer, "</match_criteria>");
xml_string_append (buffer, "</match_string>");
cleanup_iterator (&cpe_matches);

Check warning on line 13367 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13365-L13367

Added lines #L13365 - L13367 were not covered by tests
}
cleanup_iterator (&cpe_match_ranges);

Check warning on line 13369 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13369

Added line #L13369 was not covered by tests
Expand Down
14 changes: 7 additions & 7 deletions src/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -3180,19 +3180,19 @@ check_cpe_match_rule (long long int node, gboolean *match, gboolean *vulnerable,
return;
}

init_cpe_match_range_iterator (&cpe_match_ranges, node);
init_cpe_match_string_iterator (&cpe_match_ranges, node);

Check warning on line 3183 in src/manage.c

View check run for this annotation

Codecov / codecov/patch

src/manage.c#L3183

Added line #L3183 was not covered by tests
while (next (&cpe_match_ranges))
{
iterator_t cpe_host_details_products;
gchar *range_fs_cpe;
gchar *range_uri_product;
gchar *vsi, *vse, *vei, *vee;
range_fs_cpe = vsi = vse = vei = vee = NULL;
range_fs_cpe = g_strdup (cpe_match_range_iterator_cpe (&cpe_match_ranges));
vsi = g_strdup (cpe_match_range_iterator_version_start_incl (&cpe_match_ranges));
vse = g_strdup (cpe_match_range_iterator_version_start_excl (&cpe_match_ranges));
vei = g_strdup (cpe_match_range_iterator_version_end_incl (&cpe_match_ranges));
vee = g_strdup (cpe_match_range_iterator_version_end_excl (&cpe_match_ranges));
range_fs_cpe = g_strdup (cpe_match_string_iterator_criteria (&cpe_match_ranges));
vsi = g_strdup (cpe_match_string_iterator_version_start_incl (&cpe_match_ranges));
vse = g_strdup (cpe_match_string_iterator_version_start_excl (&cpe_match_ranges));
vei = g_strdup (cpe_match_string_iterator_version_end_incl (&cpe_match_ranges));
vee = g_strdup (cpe_match_string_iterator_version_end_excl (&cpe_match_ranges));

Check warning on line 3195 in src/manage.c

View check run for this annotation

Codecov / codecov/patch

src/manage.c#L3191-L3195

Added lines #L3191 - L3195 were not covered by tests
range_uri_product = fs_cpe_to_uri_product (range_fs_cpe);
init_host_details_cpe_product_iterator (&cpe_host_details_products, range_uri_product, report_host);
while (next (&cpe_host_details_products))
Expand All @@ -3216,7 +3216,7 @@ check_cpe_match_rule (long long int node, gboolean *match, gboolean *vulnerable,
cpe_struct_free (&source);
cpe_struct_free (&target);
}
if (*match && cpe_match_range_iterator_vulnerable (&cpe_match_ranges) == 1)
if (*match && cpe_match_string_iterator_vulnerable (&cpe_match_ranges) == 1)
{
cpe_struct_t source, target;
cpe_struct_init (&source);
Expand Down
23 changes: 13 additions & 10 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1724,38 +1724,41 @@ long long int
cpe_match_node_childs_iterator_id (iterator_t*);

void
init_cpe_match_range_iterator (iterator_t*, long long int);
init_cpe_match_string_iterator (iterator_t*, long long int);

const char*
cpe_match_range_iterator_cpe (iterator_t*);
cpe_match_string_iterator_criteria (iterator_t*);

const char*
cpe_match_range_iterator_match_criteria_id (iterator_t*);
cpe_match_string_iterator_match_criteria_id (iterator_t*);

const char*
cpe_match_range_iterator_status (iterator_t*);
cpe_match_string_iterator_status (iterator_t*);

const char*
cpe_match_range_iterator_version_start_incl (iterator_t*);
cpe_match_string_iterator_version_start_incl (iterator_t*);

const char*
cpe_match_range_iterator_version_start_excl (iterator_t*);
cpe_match_string_iterator_version_start_excl (iterator_t*);

const char*
cpe_match_range_iterator_version_end_incl (iterator_t*);
cpe_match_string_iterator_version_end_incl (iterator_t*);

const char*
cpe_match_range_iterator_version_end_excl (iterator_t*);
cpe_match_string_iterator_version_end_excl (iterator_t*);

int
cpe_match_range_iterator_vulnerable (iterator_t*);
cpe_match_string_iterator_vulnerable (iterator_t*);

void
init_cpe_match_range_matches_iterator (iterator_t*, const char *);
init_cpe_match_string_matches_iterator (iterator_t*, const char *);

const char*
cpe_matches_cpe_name_id (iterator_t*);

const char*
cpe_matches_cpe_name (iterator_t*);

void
init_host_details_cpe_product_iterator (iterator_t*, const char *, report_host_t);

Expand Down
9 changes: 5 additions & 4 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -3557,10 +3557,10 @@ manage_db_init (const gchar *name)
" vulnerable integer DEFAULT 0,"
" match_criteria_id text);");

sql ("CREATE TABLE scap2.cpe_match_range"
sql ("CREATE TABLE scap2.cpe_match_strings"

Check warning on line 3560 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L3560

Added line #L3560 was not covered by tests
" (id SERIAL PRIMARY KEY,"
" match_criteria_id text,"
" cpe text DEFAULT NULL,"
" criteria text DEFAULT NULL,"
" version_start_incl text DEFAULT NULL,"
" version_start_excl text DEFAULT NULL,"
" version_end_incl text DEFAULT NULL,"
Expand All @@ -3570,7 +3570,8 @@ manage_db_init (const gchar *name)
sql ("CREATE TABLE scap2.cpe_matches"

Check warning on line 3570 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L3570

Added line #L3570 was not covered by tests
" (id SERIAL PRIMARY KEY,"
" match_criteria_id text,"
" cpe_name_id text);");
" cpe_name_id text,"
" cpe_name text);");

sql ("CREATE TABLE scap2.cpe_details"
" (id SERIAL PRIMARY KEY,"
Expand Down Expand Up @@ -3646,7 +3647,7 @@ manage_db_add_constraints (const gchar *name)
" ALTER url SET NOT NULL,"
" ADD UNIQUE (cve_id, url);");

sql ("ALTER TABLE scap2.cpe_match_range"
sql ("ALTER TABLE scap2.cpe_match_strings"

Check warning on line 3650 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L3650

Added line #L3650 was not covered by tests
" ADD UNIQUE (match_criteria_id);");

sql ("ALTER TABLE scap2.cpe_matches"

Check warning on line 3653 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L3653

Added line #L3653 was not covered by tests
Expand Down
Loading

0 comments on commit 6ff3d18

Please sign in to comment.