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

Added reading of gzip files for CPEs. #2314

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 55 additions & 13 deletions src/manage_sql_secinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2522,21 +2522,35 @@
inserts_t inserts, deprecated_by_inserts;
gvm_json_pull_parser_t parser;
gvm_json_pull_event_t event;
FILE *json_stream = fopen (path, "r");
if (json_stream == NULL)
FILE *cpe_file;

int fd = open (path, O_RDONLY);

Check warning on line 2527 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2527

Added line #L2527 was not covered by tests

if (fd < 0)
{
g_warning ("%s: Could not open file '%s': %s",
g_warning ("%s: Failed to open CPE file '%s': %s",

Check warning on line 2531 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2531

Added line #L2531 was not covered by tests
__func__, path, strerror(errno));
return -1;
}

gvm_json_pull_parser_init (&parser, json_stream);
g_info ("Updating %s", path);

Check warning on line 2536 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2536

Added line #L2536 was not covered by tests

cpe_file = gvm_gzip_open_file_reader_fd (fd);

Check warning on line 2538 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2538

Added line #L2538 was not covered by tests
if (cpe_file == NULL)
{
g_warning ("%s: Failed to open CPE file: %s",

Check warning on line 2541 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2541

Added line #L2541 was not covered by tests
__func__,
strerror (errno));
return -1;

Check warning on line 2544 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2544

Added line #L2544 was not covered by tests
}

gvm_json_pull_parser_init (&parser, cpe_file);

Check warning on line 2547 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2547

Added line #L2547 was not covered by tests
gvm_json_pull_event_init (&event);
if (scap_cpes_json_skip_to_products (&parser, &event))
{
gvm_json_pull_event_cleanup (&event);
gvm_json_pull_parser_cleanup (&parser);
fclose (json_stream);
fclose (cpe_file);

Check warning on line 2553 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2553

Added line #L2553 was not covered by tests
return -1;
}

Expand Down Expand Up @@ -2573,7 +2587,7 @@
gvm_json_pull_event_cleanup (&event);
gvm_json_pull_parser_cleanup (&parser);
cJSON_Delete (entry);
fclose (json_stream);
fclose (cpe_file);

Check warning on line 2590 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2590

Added line #L2590 was not covered by tests
sql_commit ();
return -1;
}
Expand All @@ -2582,7 +2596,7 @@
gvm_json_pull_event_cleanup (&event);
gvm_json_pull_parser_cleanup (&parser);
cJSON_Delete (entry);
fclose (json_stream);
fclose (cpe_file);

Check warning on line 2599 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2599

Added line #L2599 was not covered by tests
sql_commit ();
return -1;
}
Expand All @@ -2595,14 +2609,31 @@
gvm_json_pull_parser_cleanup (&parser);

// Reset and insert refs
fseek (json_stream, 0, SEEK_SET);
gvm_json_pull_parser_init (&parser, json_stream);
fclose (cpe_file);
fd = open (path, O_RDONLY);

Check warning on line 2613 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2612-L2613

Added lines #L2612 - L2613 were not covered by tests

if (fd < 0)
{
g_warning ("%s: Failed to open CPE file '%s': %s",

Check warning on line 2617 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2617

Added line #L2617 was not covered by tests
__func__, path, strerror(errno));
return -1;

Check warning on line 2619 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2619

Added line #L2619 was not covered by tests
}

cpe_file = gvm_gzip_open_file_reader_fd (fd);

Check warning on line 2622 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2622

Added line #L2622 was not covered by tests
if (cpe_file == NULL)
{
g_warning ("%s: Failed to open CPE file: %s",

Check warning on line 2625 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2625

Added line #L2625 was not covered by tests
__func__,
strerror (errno));
return -1;

Check warning on line 2628 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2628

Added line #L2628 was not covered by tests
}
gvm_json_pull_parser_init (&parser, cpe_file);

Check warning on line 2630 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2630

Added line #L2630 was not covered by tests

if (scap_cpes_json_skip_to_products (&parser, &event))
{
gvm_json_pull_event_cleanup (&event);
gvm_json_pull_parser_cleanup (&parser);
fclose (json_stream);
fclose (cpe_file);

Check warning on line 2636 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2636

Added line #L2636 was not covered by tests
return -1;
}

Expand All @@ -2625,7 +2656,7 @@
gvm_json_pull_event_cleanup (&event);
gvm_json_pull_parser_cleanup (&parser);
cJSON_Delete (entry);
fclose (json_stream);
fclose (cpe_file);

Check warning on line 2659 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2659

Added line #L2659 was not covered by tests
sql_commit ();
return -1;
}
Expand All @@ -2634,7 +2665,7 @@
gvm_json_pull_event_cleanup (&event);
gvm_json_pull_parser_cleanup (&parser);
cJSON_Delete (entry);
fclose (json_stream);
fclose (cpe_file);

Check warning on line 2668 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2668

Added line #L2668 was not covered by tests
sql_commit ();
return -1;
}
Expand All @@ -2645,6 +2676,7 @@
sql_commit ();
gvm_json_pull_parser_cleanup (&parser);

fclose (cpe_file);

Check warning on line 2679 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2679

Added line #L2679 was not covered by tests
return 0;
}

Expand Down Expand Up @@ -2854,8 +2886,15 @@
int ret;

full_path = g_build_filename (GVM_SCAP_DATA_DIR,
"nvd-cpes.json",
"nvd-cpes.json.gz",
NULL);
if (g_stat (full_path, &state))
{
g_free (full_path);
full_path = g_build_filename (GVM_SCAP_DATA_DIR,

Check warning on line 2894 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2893-L2894

Added lines #L2893 - L2894 were not covered by tests
"nvd-cpes.json",
mattmundell marked this conversation as resolved.
Show resolved Hide resolved
NULL);
}

if (g_stat (full_path, &state))
{
Expand Down Expand Up @@ -2887,6 +2926,9 @@
g_info ("Updating CPEs");

ret = update_scap_cpes_from_json_file (full_path);

g_free (full_path);

Check warning on line 2930 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2930

Added line #L2930 was not covered by tests

if (ret)
return -1;

Expand Down
Loading