Skip to content

Commit

Permalink
Added reading of gzip files for CPEs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhelmold committed Oct 24, 2024
1 parent c3e6c2e commit 73f699b
Showing 1 changed file with 57 additions and 14 deletions.
71 changes: 57 additions & 14 deletions src/manage_sql_secinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2522,21 +2522,35 @@ update_scap_cpes_from_json_file (const gchar *path)
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 @@ update_scap_cpes_from_json_file (const gchar *path)
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 @@ update_scap_cpes_from_json_file (const gchar *path)
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 @@ update_scap_cpes_from_json_file (const gchar *path)
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 @@ update_scap_cpes_from_json_file (const gchar *path)
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 @@ update_scap_cpes_from_json_file (const gchar *path)
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 @@ update_scap_cpes_from_json_file (const gchar *path)
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 @@ update_scap_cpes ()
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",
NULL);
}

if (g_stat (full_path, &state))
{
Expand Down Expand Up @@ -2888,8 +2927,12 @@ update_scap_cpes ()

ret = update_scap_cpes_from_json_file (full_path);
if (ret)
return -1;
{
g_free (full_path);
return -1;

Check warning on line 2932 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2931-L2932

Added lines #L2931 - L2932 were not covered by tests
}

g_free (full_path);

Check warning on line 2935 in src/manage_sql_secinfo.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_secinfo.c#L2935

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

Expand Down

0 comments on commit 73f699b

Please sign in to comment.