From d18e6c1c8dd3c31692cf28d0536840a5188ac2a0 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 20 Nov 2023 14:06:59 +0200 Subject: [PATCH 01/14] Change: move GET iterator time conversion out of SQL --- src/manage_get.c | 36 ++++++++++++++++++++++++++++++++---- src/manage_get.h | 4 ++-- src/manage_sql.h | 10 +++++----- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/manage_get.c b/src/manage_get.c index 2b81ac2f8..3c9e151b9 100644 --- a/src/manage_get.c +++ b/src/manage_get.c @@ -154,18 +154,46 @@ get_iterator_comment (iterator_t* iterator) * * @param[in] iterator Iterator. * - * @return Creation time of the resource or NULL if iteration is complete. + * @return Creation time, or NULL if iteration is complete. Caller must free. */ -DEF_ACCESS (get_iterator_creation_time, 4); +gchar * +get_iterator_creation_time (iterator_t* iterator) +{ + time_t epoch; + char *iso; + + if (iterator->done) return NULL; + + epoch = iterator_int64 (iterator, 4); + iso = iso_time (&epoch); + if (iso) + // iso points to static memory. + return g_strdup (iso); + return g_strdup("ERR"); +} /** * @brief Get the modification time of the resource from a GET iterator. * * @param[in] iterator Iterator. * - * @return Modification time of the resource or NULL if iteration is complete. + * @return Modification time, or NULL if iteration is complete. Caller must free. */ -DEF_ACCESS (get_iterator_modification_time, 5); +gchar * +get_iterator_modification_time (iterator_t* iterator) +{ + time_t epoch; + char *iso; + + if (iterator->done) return NULL; + + epoch = iterator_int64 (iterator, 5); + iso = iso_time (&epoch); + if (iso) + // iso points to static memory. + return g_strdup (iso); + return g_strdup("ERR"); +} /** * @brief Get the owner name of the resource from a GET iterator. diff --git a/src/manage_get.h b/src/manage_get.h index 5c44e667c..a990825f1 100644 --- a/src/manage_get.h +++ b/src/manage_get.h @@ -66,10 +66,10 @@ get_iterator_name (iterator_t*); const char* get_iterator_comment (iterator_t*); -const char* +gchar* get_iterator_creation_time (iterator_t*); -const char* +gchar* get_iterator_modification_time (iterator_t*); const char* diff --git a/src/manage_sql.h b/src/manage_sql.h index 6ea3ac24c..e897075c1 100644 --- a/src/manage_sql.h +++ b/src/manage_sql.h @@ -248,9 +248,9 @@ typedef struct * * @param[in] prefix Column prefix. */ -#define GET_ITERATOR_COLUMNS_STRING \ - "id, uuid, name, comment, iso_time (creation_time)," \ - " iso_time (modification_time), creation_time AS created," \ +#define GET_ITERATOR_COLUMNS_STRING \ + "id, uuid, name, comment, creation_time," \ + " modification_time, creation_time AS created," \ " modification_time AS modified" /** @@ -263,8 +263,8 @@ typedef struct { prefix "uuid", NULL, KEYWORD_TYPE_STRING }, \ { prefix "name", NULL, KEYWORD_TYPE_STRING }, \ { prefix "comment", NULL, KEYWORD_TYPE_STRING }, \ - { " iso_time (" prefix "creation_time)", NULL, KEYWORD_TYPE_STRING }, \ - { " iso_time (" prefix "modification_time)", NULL, KEYWORD_TYPE_STRING }, \ + { prefix "creation_time", NULL, KEYWORD_TYPE_INTEGER }, \ + { prefix "modification_time", NULL, KEYWORD_TYPE_INTEGER }, \ { prefix "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { prefix "modification_time", "modified", KEYWORD_TYPE_INTEGER } From 82b6b6d8e9c83c63ba4c775f549d0c86c110b08a Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 20 Nov 2023 15:46:29 +0200 Subject: [PATCH 02/14] Free returns from time accessors --- src/gmp.c | 105 +++++++++++++++++++++++++++++++++++--------------- src/gmp_get.c | 15 +++++--- src/manage.c | 15 +++++--- 3 files changed, 93 insertions(+), 42 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index 4918287eb..ab77633d6 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -8040,9 +8040,14 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, if (include_notes_details == 0) { - const char *text = note_iterator_text (notes); - gchar *excerpt = utf8_substring (text, 0, - setting_excerpt_size_int ()); + gchar *excerpt, *creation, *modification; + const char *text; + + text = note_iterator_text (notes); + excerpt = utf8_substring (text, 0, setting_excerpt_size_int ()); + creation = get_iterator_creation_time (notes); + modification = get_iterator_modification_time (notes); + /* This must match send_get_common. */ buffer_xml_append_printf (buffer, "%s" @@ -8063,8 +8068,8 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, note_iterator_nvt_oid (notes), note_iterator_nvt_name (notes), note_iterator_nvt_type (notes), - get_iterator_creation_time (notes), - get_iterator_modification_time (notes), + creation, + modification, note_iterator_active (notes), strlen (excerpt) < strlen (text), excerpt, @@ -8072,6 +8077,8 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, && (uuid_task == NULL)) || (note_iterator_result (notes) && (uuid_result == NULL)))); + g_free (creation); + g_free (modification); if (tag_count) { @@ -8092,6 +8099,7 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, int trash_task; time_t end_time; iterator_t tags; + gchar *creation, *modification; if (uuid_task) { @@ -8105,6 +8113,8 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, } end_time = note_iterator_end_time (notes); + creation = get_iterator_creation_time (notes); + modification = get_iterator_modification_time (notes); /* This must match send_get_common. */ buffer_xml_append_printf @@ -8132,8 +8142,8 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, note_iterator_nvt_oid (notes), note_iterator_nvt_name (notes), note_iterator_nvt_type (notes), - get_iterator_creation_time (notes), - get_iterator_modification_time (notes), + creation, + modification, note_iterator_active (notes), end_time > 1 ? iso_time (&end_time) : "", note_iterator_text (notes), @@ -8150,6 +8160,8 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, || (note_iterator_result (notes) && (uuid_result == NULL)))); free (name_task); + g_free (creation); + g_free (modification); if (include_result && uuid_result && note_iterator_result (notes)) { @@ -8304,9 +8316,14 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, if (include_overrides_details == 0) { - const char *text = override_iterator_text (overrides); - gchar *excerpt = utf8_substring (text, 0, - setting_excerpt_size_int ()); + gchar *excerpt, *creation, *modification; + const char *text; + + text = override_iterator_text (overrides); + excerpt = utf8_substring (text, 0, setting_excerpt_size_int ()); + creation = get_iterator_creation_time (overrides); + modification = get_iterator_modification_time (overrides); + /* This must match send_get_common. */ buffer_xml_append_printf (buffer, "%s" @@ -8331,8 +8348,8 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, override_iterator_nvt_oid (overrides), override_iterator_nvt_name (overrides), override_iterator_nvt_type (overrides), - get_iterator_creation_time (overrides), - get_iterator_modification_time (overrides), + creation, + modification, override_iterator_active (overrides), strlen (excerpt) < strlen (text), excerpt, @@ -8349,6 +8366,9 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, || (override_iterator_result (overrides) && (uuid_result == NULL)))); + g_free (creation); + g_free (modification); + if (tag_count) { buffer_xml_append_printf (buffer, @@ -8368,6 +8388,7 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, int trash_task; time_t end_time; iterator_t tags; + gchar *creation, *modification; if (uuid_task) { @@ -8381,6 +8402,8 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, } end_time = override_iterator_end_time (overrides); + creation = get_iterator_creation_time (overrides); + modification = get_iterator_modification_time (overrides); /* This must match send_get_common. */ buffer_xml_append_printf @@ -8411,8 +8434,8 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, override_iterator_nvt_oid (overrides), override_iterator_nvt_name (overrides), override_iterator_nvt_type (overrides), - get_iterator_creation_time (overrides), - get_iterator_modification_time (overrides), + creation, + modification, override_iterator_active (overrides), end_time > 1 ? iso_time (&end_time) : "", override_iterator_text (overrides), @@ -8433,6 +8456,8 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, || (override_iterator_result (overrides) && (uuid_result == NULL)))); free (name_task); + g_free (creation); + g_free (modification); if (include_result && uuid_result && override_iterator_result (overrides)) @@ -9212,7 +9237,7 @@ buffer_results_xml (GString *buffer, iterator_t *results, task_t task, int changed, int cert_loaded, int lean, int use_delta_fields) { - const char *descr, *name, *comment, *creation_time; + const char *descr, *name, *comment; const char *severity, *original_severity, *original_level; const char *host, *hostname, *result_id, *port, *path, *asset_id, *qod, *qod_type; char *detect_oid, *detect_ref, *detect_cpe, *detect_loc, *detect_name; @@ -9221,6 +9246,7 @@ buffer_results_xml (GString *buffer, iterator_t *results, task_t task, result_t result; report_t report; task_t selected_task; + gchar *creation_time; comment = get_iterator_comment (results); name = get_iterator_name (results); @@ -9238,7 +9264,7 @@ buffer_results_xml (GString *buffer, iterator_t *results, task_t task, qod = result_iterator_delta_qod (results); qod_type = result_iterator_delta_qod_type (results); result = result_iterator_delta_result (results); - creation_time = result_iterator_delta_creation_time (results); + creation_time = (gchar *) result_iterator_delta_creation_time (results); // FIX result_id = result_iterator_delta_uuid (results); path = result_iterator_delta_path (results); report = result_iterator_delta_report (results); @@ -9323,9 +9349,12 @@ buffer_results_xml (GString *buffer, iterator_t *results, task_t task, comment); if (creation_time) - buffer_xml_append_printf (buffer, - "%s", - creation_time); + { + buffer_xml_append_printf (buffer, + "%s", + creation_time); + g_free (creation_time); + } if (include_details) { @@ -11536,7 +11565,7 @@ handle_get_assets (gmp_parser_t *gmp_parser, GError **error) while (next (&identifiers)) { const char *source_type; - gchar *name; + gchar *name, *creation, *modification; source_type = host_identifier_iterator_source_type (&identifiers); @@ -11546,6 +11575,9 @@ handle_get_assets (gmp_parser_t *gmp_parser, GError **error) else name = NULL; + creation = get_iterator_creation_time (&identifiers); + modification = get_iterator_modification_time (&identifiers); + xml_string_append (result, "" "%s" @@ -11561,8 +11593,8 @@ handle_get_assets (gmp_parser_t *gmp_parser, GError **error) get_iterator_uuid (&identifiers), get_iterator_name (&identifiers), host_identifier_iterator_value (&identifiers), - get_iterator_creation_time (&identifiers), - get_iterator_modification_time (&identifiers), + creation, + modification, host_identifier_iterator_source_id (&identifiers), source_type, @@ -11573,6 +11605,8 @@ handle_get_assets (gmp_parser_t *gmp_parser, GError **error) name ? name : ""); g_free (name); + g_free (creation); + g_free (modification); if (strcmp (get_iterator_name (&identifiers), "OS") == 0) xml_string_append (result, @@ -14593,6 +14627,10 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error) if (get_reports_data->alert_id == NULL) { task_t task; + gchar *creation, *modification; + + creation = get_iterator_creation_time (&reports); + modification = get_iterator_modification_time (&reports); /* Send the standard elements. Should match send_get_common. */ buffer_xml_append_printf @@ -14615,12 +14653,12 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error) get_iterator_comment (&reports) ? get_iterator_comment (&reports) : "", - get_iterator_creation_time (&reports) - ? get_iterator_creation_time (&reports) - : "", - get_iterator_modification_time (&reports) - ? get_iterator_modification_time (&reports) - : ""); + creation ? creation : "", + modification ? creation : ""); + + g_free (creation); + g_free (modification); + /* Send short task and report format info */ report_task (report, &task); if (task) @@ -17892,6 +17930,10 @@ handle_get_vulns (gmp_parser_t *gmp_parser, GError **error) while (next (&vulns)) { time_t oldest, newest; + gchar *creation, *modification; + + creation = get_iterator_creation_time (&vulns); + modification = get_iterator_modification_time (&vulns); count ++; SENDF_TO_CLIENT_OR_FAIL ("" @@ -17904,11 +17946,14 @@ handle_get_vulns (gmp_parser_t *gmp_parser, GError **error) get_iterator_uuid (&vulns), get_iterator_name (&vulns), vuln_iterator_type (&vulns), - get_iterator_creation_time (&vulns), - get_iterator_modification_time (&vulns), + creation, + modification, vuln_iterator_severity (&vulns), vuln_iterator_qod (&vulns)); + g_free (creation); + g_free (modification); + // results for the vulnerability oldest = vuln_iterator_oldest (&vulns); SENDF_TO_CLIENT_OR_FAIL ("" diff --git a/src/gmp_get.c b/src/gmp_get.c index b13e73192..32419aef1 100644 --- a/src/gmp_get.c +++ b/src/gmp_get.c @@ -316,9 +316,13 @@ send_get_common (const char *type, get_data_t *get, iterator_t *iterator, const char *tag_type; iterator_t tags; int tag_count; + gchar *creation, *modification; buffer = g_string_new (""); + creation = get_iterator_creation_time (iterator); + modification = get_iterator_modification_time (iterator); + buffer_xml_append_printf (buffer, "<%s id=\"%s\">" "%s" @@ -342,15 +346,14 @@ send_get_common (const char *type, get_data_t *get, iterator_t *iterator, get_iterator_comment (iterator) ? get_iterator_comment (iterator) : "", - get_iterator_creation_time (iterator) - ? get_iterator_creation_time (iterator) - : "", - get_iterator_modification_time (iterator) - ? get_iterator_modification_time (iterator) - : "", + creation ? creation : "", + modification ? modification : "", writable, in_use); + g_free (creation); + g_free (modification); + if (/* The user is the owner. */ (current_credentials.username && get_iterator_owner_name (iterator) diff --git a/src/manage.c b/src/manage.c index 5c8357459..5e94ec03b 100644 --- a/src/manage.c +++ b/src/manage.c @@ -5771,6 +5771,7 @@ get_nvt_xml (iterator_t *nvts, int details, int pref_count, { char *default_timeout; GString *nvt_tags; + gchar *creation, *modification; DEF (tag); @@ -5830,6 +5831,9 @@ get_nvt_xml (iterator_t *nvts, int details, int pref_count, } default_timeout = nvt_default_timeout (oid); + creation = get_iterator_creation_time (nvts); + modification = get_iterator_modification_time (nvts); + g_string_append_printf (buffer, "%s" "%s" @@ -5843,19 +5847,18 @@ get_nvt_xml (iterator_t *nvts, int details, int pref_count, "%s" "%s", default_timeout ? default_timeout : "", - get_iterator_creation_time (nvts) - ? get_iterator_creation_time (nvts) - : "", - get_iterator_modification_time (nvts) - ? get_iterator_modification_time (nvts) - : "", + creation ? creation : "", + modification ? modification : "", nvt_iterator_category (nvts), family_text, nvt_iterator_qod (nvts), nvt_iterator_qod_type (nvts), refs_str->str, nvt_tags->str); + free (default_timeout); + g_free (creation); + g_free (modification); g_string_free (nvt_tags, 1); } From ab20369077ab141d270eb60b0040329420636d65 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 20 Nov 2023 22:17:32 +0200 Subject: [PATCH 03/14] Remove iso_time from iterators that use the GET accessors --- src/manage_sql.c | 24 ++++++++++++------------ src/manage_sql_report_formats.c | 8 ++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index b18752a75..c2a7206ff 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -21658,8 +21658,8 @@ report_add_results_array (report_t report, GArray *results) { "uuid", NULL, KEYWORD_TYPE_STRING }, \ { "iso_time (creation_time)", "name", KEYWORD_TYPE_STRING }, \ { "''", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (creation_time)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (modification_time)", NULL, KEYWORD_TYPE_STRING }, \ + { "creation_time", NULL, KEYWORD_TYPE_STRING }, \ + { "modification_time", NULL, KEYWORD_TYPE_STRING }, \ { "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { "(SELECT name FROM users WHERE users.id = reports.owner)", \ @@ -50006,8 +50006,8 @@ init_host_identifier_iterator (iterator_t* iterator, host_t host, if (host) init_iterator (iterator, - "SELECT id, uuid, name, comment, iso_time (creation_time)," - " iso_time (modification_time), creation_time," + "SELECT id, uuid, name, comment, creation_time," + " modification_time, creation_time," " modification_time, owner, owner, value," " source_type, source_id, source_data," " (CASE WHEN source_type LIKE 'Report%%'" @@ -50019,8 +50019,8 @@ init_host_identifier_iterator (iterator_t* iterator, host_t host, " FROM host_identifiers" " WHERE host = %llu" " UNION" - " SELECT id, uuid, name, comment, iso_time (creation_time)," - " iso_time (modification_time), creation_time," + " SELECT id, uuid, name, comment, creation_time," + " modification_time, creation_time," " modification_time, owner, owner," " (SELECT name FROM oss WHERE id = os)," " source_type, source_id, source_data," @@ -50040,8 +50040,8 @@ init_host_identifier_iterator (iterator_t* iterator, host_t host, ascending ? "ASC" : "DESC"); else init_iterator (iterator, - "SELECT id, uuid, name, comment, iso_time (creation_time)," - " iso_time (modification_time), creation_time," + "SELECT id, uuid, name, comment, creation_time," + " modification_time, creation_time," " modification_time, owner, owner, value," " source_type, source_id, source_data, 0, '', ''" " FROM host_identifiers" @@ -50691,8 +50691,8 @@ init_os_host_iterator (iterator_t* iterator, resource_t os) { assert (os); init_iterator (iterator, - "SELECT id, uuid, name, comment, iso_time (creation_time)," - " iso_time (modification_time), creation_time," + "SELECT id, uuid, name, comment, creation_time," + " modification_time, creation_time," " modification_time, owner, owner," " (SELECT round (CAST (severity AS numeric), 1)" " FROM host_max_severities" @@ -55046,8 +55046,8 @@ user_resources_in_use (user_t user, { "uuid", "uuid", KEYWORD_TYPE_STRING }, \ { "name", "name", KEYWORD_TYPE_STRING }, \ { "''", "comment", KEYWORD_TYPE_STRING }, \ - { "iso_time (creation_time)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (modification_time)", NULL, KEYWORD_TYPE_STRING }, \ + { "creation_time", NULL, KEYWORD_TYPE_STRING }, \ + { "modification_time", NULL, KEYWORD_TYPE_STRING }, \ { "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { "cast (null AS text)", "_owner", KEYWORD_TYPE_INTEGER }, \ diff --git a/src/manage_sql_report_formats.c b/src/manage_sql_report_formats.c index 89b23c4ca..14f2d4e33 100644 --- a/src/manage_sql_report_formats.c +++ b/src/manage_sql_report_formats.c @@ -2599,8 +2599,8 @@ report_format_trust (report_format_t report_format) { "uuid", NULL, KEYWORD_TYPE_STRING }, \ { "name", NULL, KEYWORD_TYPE_STRING }, \ { "''", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (creation_time)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (modification_time)", NULL, KEYWORD_TYPE_STRING }, \ + { "creation_time", NULL, KEYWORD_TYPE_STRING }, \ + { "modification_time", NULL, KEYWORD_TYPE_STRING }, \ { "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { \ @@ -2630,8 +2630,8 @@ report_format_trust (report_format_t report_format) { "uuid", NULL, KEYWORD_TYPE_STRING }, \ { "name", NULL, KEYWORD_TYPE_STRING }, \ { "''", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (creation_time)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (modification_time)", NULL, KEYWORD_TYPE_STRING }, \ + { "creation_time", NULL, KEYWORD_TYPE_STRING }, \ + { "modification_time", NULL, KEYWORD_TYPE_STRING }, \ { "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { \ From 8f52e13aeb822197761bf87c78b5d29f311244fc Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Wed, 22 Nov 2023 14:06:48 +0200 Subject: [PATCH 04/14] Move iso_time to C for result_iterator_delta_creation_time --- src/gmp.c | 2 +- src/manage.h | 2 +- src/manage_sql.c | 19 ++++++++++++++----- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index ab77633d6..b4f65a221 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -9264,7 +9264,7 @@ buffer_results_xml (GString *buffer, iterator_t *results, task_t task, qod = result_iterator_delta_qod (results); qod_type = result_iterator_delta_qod_type (results); result = result_iterator_delta_result (results); - creation_time = (gchar *) result_iterator_delta_creation_time (results); // FIX + creation_time = result_iterator_delta_creation_time (results); result_id = result_iterator_delta_uuid (results); path = result_iterator_delta_path (results); report = result_iterator_delta_report (results); diff --git a/src/manage.h b/src/manage.h index a4936ea7c..05b72b536 100644 --- a/src/manage.h +++ b/src/manage.h @@ -1560,7 +1560,7 @@ result_iterator_delta_uuid (iterator_t*); const char * result_iterator_delta_qod_type (iterator_t*); -const char * +gchar * result_iterator_delta_creation_time (iterator_t*); const char * diff --git a/src/manage_sql.c b/src/manage_sql.c index c2a7206ff..d93fe7865 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -22381,7 +22381,7 @@ where_qod (int min_qod) { "comparison.delta_qod", NULL, KEYWORD_TYPE_INTEGER }, \ { "comparison.delta_uuid", NULL, KEYWORD_TYPE_STRING }, \ { "delta_qod_type", NULL, KEYWORD_TYPE_STRING }, \ - { " iso_time (delta_date, opts.user_zone)", \ + { "delta_date", \ "delta_creation_time", \ KEYWORD_TYPE_STRING }, \ { " iso_time (delta_date, opts.user_zone)", \ @@ -23798,13 +23798,22 @@ result_iterator_delta_qod_type (iterator_t* iterator) * * @param[in] iterator Iterator. * - * @return delta creation time if any, else NULL. + * @return Time, or NULL if iteration is complete. Caller must free. */ -const char * +gchar * result_iterator_delta_creation_time (iterator_t* iterator) { - if (iterator->done) return 0; - return iterator_string (iterator, RESULT_ITERATOR_DELTA_COLUMN_OFFSET + 6); + time_t epoch; + char *iso; + + if (iterator->done) return NULL; + + epoch = iterator_int64 (iterator, RESULT_ITERATOR_DELTA_COLUMN_OFFSET + 6); + iso = iso_time (&epoch); + if (iso) + // iso points to static memory. + return g_strdup (iso); + return g_strdup("ERR"); } /** From b10666a2afa54315d67cea535dfbd443a57c246e Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Wed, 22 Nov 2023 14:27:51 +0200 Subject: [PATCH 05/14] Move iso_time to C for result_iterator_delta_modification_time --- src/manage.h | 2 +- src/manage_sql.c | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/manage.h b/src/manage.h index 05b72b536..f0257416a 100644 --- a/src/manage.h +++ b/src/manage.h @@ -1563,7 +1563,7 @@ result_iterator_delta_qod_type (iterator_t*); gchar * result_iterator_delta_creation_time (iterator_t*); -const char * +gchar * result_iterator_delta_modification_time (iterator_t*); task_t diff --git a/src/manage_sql.c b/src/manage_sql.c index d93fe7865..9dfe2c7d9 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -22384,7 +22384,7 @@ where_qod (int min_qod) { "delta_date", \ "delta_creation_time", \ KEYWORD_TYPE_STRING }, \ - { " iso_time (delta_date, opts.user_zone)", \ + { "delta_date", \ "delta_modification_time", \ KEYWORD_TYPE_STRING }, \ { "delta_task", NULL, KEYWORD_TYPE_INTEGER }, \ @@ -23821,13 +23821,22 @@ result_iterator_delta_creation_time (iterator_t* iterator) * * @param[in] iterator Iterator. * - * @return delta modification time if any, else NULL. + * @return Time, or NULL if iteration is complete. Caller must free. */ -const char * +gchar * result_iterator_delta_modification_time (iterator_t* iterator) { - if (iterator->done) return 0; - return iterator_string (iterator, RESULT_ITERATOR_DELTA_COLUMN_OFFSET + 7); + time_t epoch; + char *iso; + + if (iterator->done) return NULL; + + epoch = iterator_int64 (iterator, RESULT_ITERATOR_DELTA_COLUMN_OFFSET + 7); + iso = iso_time (&epoch); + if (iso) + // iso points to static memory. + return g_strdup (iso); + return g_strdup("ERR"); } /** From f06311c29e67f069107b41410e67484c9edbb9e2 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Wed, 22 Nov 2023 16:17:07 +0200 Subject: [PATCH 06/14] Fix column keyword types --- src/manage_sql.c | 12 ++++++------ src/manage_sql_report_formats.c | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 9dfe2c7d9..97230d2b5 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -21658,8 +21658,8 @@ report_add_results_array (report_t report, GArray *results) { "uuid", NULL, KEYWORD_TYPE_STRING }, \ { "iso_time (creation_time)", "name", KEYWORD_TYPE_STRING }, \ { "''", NULL, KEYWORD_TYPE_STRING }, \ - { "creation_time", NULL, KEYWORD_TYPE_STRING }, \ - { "modification_time", NULL, KEYWORD_TYPE_STRING }, \ + { "creation_time", NULL, KEYWORD_TYPE_INTEGER }, \ + { "modification_time", NULL, KEYWORD_TYPE_INTEGER }, \ { "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { "(SELECT name FROM users WHERE users.id = reports.owner)", \ @@ -22383,10 +22383,10 @@ where_qod (int min_qod) { "delta_qod_type", NULL, KEYWORD_TYPE_STRING }, \ { "delta_date", \ "delta_creation_time", \ - KEYWORD_TYPE_STRING }, \ + KEYWORD_TYPE_INTEGER }, \ { "delta_date", \ "delta_modification_time", \ - KEYWORD_TYPE_STRING }, \ + KEYWORD_TYPE_INTEGER }, \ { "delta_task", NULL, KEYWORD_TYPE_INTEGER }, \ { "delta_report", NULL, KEYWORD_TYPE_INTEGER }, \ { "(SELECT name FROM users WHERE users.id = results.owner)", \ @@ -55064,8 +55064,8 @@ user_resources_in_use (user_t user, { "uuid", "uuid", KEYWORD_TYPE_STRING }, \ { "name", "name", KEYWORD_TYPE_STRING }, \ { "''", "comment", KEYWORD_TYPE_STRING }, \ - { "creation_time", NULL, KEYWORD_TYPE_STRING }, \ - { "modification_time", NULL, KEYWORD_TYPE_STRING }, \ + { "creation_time", NULL, KEYWORD_TYPE_INTEGER }, \ + { "modification_time", NULL, KEYWORD_TYPE_INTEGER }, \ { "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { "cast (null AS text)", "_owner", KEYWORD_TYPE_INTEGER }, \ diff --git a/src/manage_sql_report_formats.c b/src/manage_sql_report_formats.c index 14f2d4e33..5aff23131 100644 --- a/src/manage_sql_report_formats.c +++ b/src/manage_sql_report_formats.c @@ -2599,8 +2599,8 @@ report_format_trust (report_format_t report_format) { "uuid", NULL, KEYWORD_TYPE_STRING }, \ { "name", NULL, KEYWORD_TYPE_STRING }, \ { "''", NULL, KEYWORD_TYPE_STRING }, \ - { "creation_time", NULL, KEYWORD_TYPE_STRING }, \ - { "modification_time", NULL, KEYWORD_TYPE_STRING }, \ + { "creation_time", NULL, KEYWORD_TYPE_INTEGER }, \ + { "modification_time", NULL, KEYWORD_TYPE_INTEGER }, \ { "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { \ @@ -2630,8 +2630,8 @@ report_format_trust (report_format_t report_format) { "uuid", NULL, KEYWORD_TYPE_STRING }, \ { "name", NULL, KEYWORD_TYPE_STRING }, \ { "''", NULL, KEYWORD_TYPE_STRING }, \ - { "creation_time", NULL, KEYWORD_TYPE_STRING }, \ - { "modification_time", NULL, KEYWORD_TYPE_STRING }, \ + { "creation_time", NULL, KEYWORD_TYPE_INTEGER }, \ + { "modification_time", NULL, KEYWORD_TYPE_INTEGER }, \ { "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { \ From 1e1e1475470c8816973df3fbf56d4e54fb09031d Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 12 Dec 2023 15:16:19 +0200 Subject: [PATCH 07/14] Return time from get_iterator_creation_time and get_iterator_modification_time --- src/gmp.c | 246 ++++++++++++++++++++++++----------------------- src/gmp_get.c | 29 +++--- src/manage.c | 17 ++-- src/manage.h | 4 +- src/manage_get.c | 34 ++----- src/manage_get.h | 4 +- src/manage_sql.c | 34 ++----- src/utils.c | 20 +++- src/utils.h | 3 + 9 files changed, 189 insertions(+), 202 deletions(-) diff --git a/src/gmp.c b/src/gmp.c index 25fc54183..17f2335e5 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -8083,36 +8083,41 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, if (include_notes_details == 0) { - gchar *excerpt, *creation, *modification; + gchar *excerpt; const char *text; text = note_iterator_text (notes); excerpt = utf8_substring (text, 0, setting_excerpt_size_int ()); - creation = get_iterator_creation_time (notes); - modification = get_iterator_modification_time (notes); /* This must match send_get_common. */ + buffer_xml_append_printf (buffer, "%s" "" "%s" "%s" - "" - "%s" - "%s" - "1" - "0" - "%i" - "%s" - "%i", + "", get_iterator_owner_name (notes) ? get_iterator_owner_name (notes) : "", note_iterator_nvt_oid (notes), note_iterator_nvt_name (notes), - note_iterator_nvt_type (notes), - creation, - modification, + note_iterator_nvt_type (notes)); + + buffer_xml_append_printf (buffer, + "%s", + iso_if_time (get_iterator_creation_time (notes))); + + buffer_xml_append_printf (buffer, + "%s", + iso_if_time (get_iterator_modification_time (notes))); + + buffer_xml_append_printf (buffer, + "1" + "0" + "%i" + "%s" + "%i", note_iterator_active (notes), strlen (excerpt) < strlen (text), excerpt, @@ -8120,8 +8125,6 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, && (uuid_task == NULL)) || (note_iterator_result (notes) && (uuid_result == NULL)))); - g_free (creation); - g_free (modification); if (tag_count) { @@ -8142,7 +8145,6 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, int trash_task; time_t end_time; iterator_t tags; - gchar *creation, *modification; if (uuid_task) { @@ -8156,19 +8158,35 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, } end_time = note_iterator_end_time (notes); - creation = get_iterator_creation_time (notes); - modification = get_iterator_modification_time (notes); /* This must match send_get_common. */ + buffer_xml_append_printf (buffer, "%s" "" "%s" "%s" - "" - "%s" - "%s" + "", + get_iterator_owner_name (notes) + ? get_iterator_owner_name (notes) + : "", + note_iterator_nvt_oid (notes), + note_iterator_nvt_name (notes), + note_iterator_nvt_type (notes)); + + buffer_xml_append_printf + (buffer, + "%s", + iso_if_time (get_iterator_creation_time (notes))); + + buffer_xml_append_printf + (buffer, + "%s", + iso_if_time (get_iterator_modification_time (notes))); + + buffer_xml_append_printf + (buffer, "1" "0" "%i" @@ -8179,14 +8197,6 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, "%s" "%s%i" "%i", - get_iterator_owner_name (notes) - ? get_iterator_owner_name (notes) - : "", - note_iterator_nvt_oid (notes), - note_iterator_nvt_name (notes), - note_iterator_nvt_type (notes), - creation, - modification, note_iterator_active (notes), end_time > 1 ? iso_time (&end_time) : "", note_iterator_text (notes), @@ -8203,8 +8213,6 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details, || (note_iterator_result (notes) && (uuid_result == NULL)))); free (name_task); - g_free (creation); - g_free (modification); if (include_result && uuid_result && note_iterator_result (notes)) { @@ -8359,23 +8367,36 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, if (include_overrides_details == 0) { - gchar *excerpt, *creation, *modification; + gchar *excerpt; const char *text; text = override_iterator_text (overrides); excerpt = utf8_substring (text, 0, setting_excerpt_size_int ()); - creation = get_iterator_creation_time (overrides); - modification = get_iterator_modification_time (overrides); /* This must match send_get_common. */ + buffer_xml_append_printf (buffer, "%s" "" "%s" "%s" - "" - "%s" - "%s" + "", + get_iterator_owner_name (overrides) + ? get_iterator_owner_name (overrides) + : "", + override_iterator_nvt_oid (overrides), + override_iterator_nvt_name (overrides), + override_iterator_nvt_type (overrides)); + + buffer_xml_append_printf (buffer, + "%s", + iso_if_time (get_iterator_creation_time (overrides))); + + buffer_xml_append_printf (buffer, + "%s", + iso_if_time (get_iterator_modification_time (overrides))); + + buffer_xml_append_printf (buffer, "1" "0" "%i" @@ -8385,14 +8406,6 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, "%s" "%s" "%i", - get_iterator_owner_name (overrides) - ? get_iterator_owner_name (overrides) - : "", - override_iterator_nvt_oid (overrides), - override_iterator_nvt_name (overrides), - override_iterator_nvt_type (overrides), - creation, - modification, override_iterator_active (overrides), strlen (excerpt) < strlen (text), excerpt, @@ -8409,9 +8422,6 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, || (override_iterator_result (overrides) && (uuid_result == NULL)))); - g_free (creation); - g_free (modification); - if (tag_count) { buffer_xml_append_printf (buffer, @@ -8431,7 +8441,6 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, int trash_task; time_t end_time; iterator_t tags; - gchar *creation, *modification; if (uuid_task) { @@ -8445,19 +8454,35 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, } end_time = override_iterator_end_time (overrides); - creation = get_iterator_creation_time (overrides); - modification = get_iterator_modification_time (overrides); /* This must match send_get_common. */ + buffer_xml_append_printf (buffer, "%s" "" "%s" "%s" - "" - "%s" - "%s" + "", + get_iterator_owner_name (overrides) + ? get_iterator_owner_name (overrides) + : "", + override_iterator_nvt_oid (overrides), + override_iterator_nvt_name (overrides), + override_iterator_nvt_type (overrides)); + + buffer_xml_append_printf + (buffer, + "%s", + iso_if_time (get_iterator_creation_time (overrides))); + + buffer_xml_append_printf + (buffer, + "%s", + iso_if_time (get_iterator_modification_time (overrides))); + + buffer_xml_append_printf + (buffer, "1" "0" "%i" @@ -8471,14 +8496,6 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, "%s" "%s%i" "%i", - get_iterator_owner_name (overrides) - ? get_iterator_owner_name (overrides) - : "", - override_iterator_nvt_oid (overrides), - override_iterator_nvt_name (overrides), - override_iterator_nvt_type (overrides), - creation, - modification, override_iterator_active (overrides), end_time > 1 ? iso_time (&end_time) : "", override_iterator_text (overrides), @@ -8499,8 +8516,6 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides, || (override_iterator_result (overrides) && (uuid_result == NULL)))); free (name_task); - g_free (creation); - g_free (modification); if (include_result && uuid_result && override_iterator_result (overrides)) @@ -9289,7 +9304,7 @@ buffer_results_xml (GString *buffer, iterator_t *results, task_t task, result_t result; report_t report; task_t selected_task; - gchar *creation_time; + time_t creation_time; comment = get_iterator_comment (results); name = get_iterator_name (results); @@ -9361,7 +9376,8 @@ buffer_results_xml (GString *buffer, iterator_t *results, task_t task, if (lean == 0) { - const char *owner_name, *modification_time; + const char *owner_name; + time_t modification_time; if (use_delta_fields) { @@ -9382,7 +9398,7 @@ buffer_results_xml (GString *buffer, iterator_t *results, task_t task, if (modification_time) buffer_xml_append_printf (buffer, "%s", - modification_time); + iso_time (&modification_time) ?: ""); } if (comment @@ -9392,12 +9408,9 @@ buffer_results_xml (GString *buffer, iterator_t *results, task_t task, comment); if (creation_time) - { - buffer_xml_append_printf (buffer, - "%s", - creation_time); - g_free (creation_time); - } + buffer_xml_append_printf (buffer, + "%s", + iso_time (&creation_time) ?: ""); if (include_details) { @@ -11608,7 +11621,7 @@ handle_get_assets (gmp_parser_t *gmp_parser, GError **error) while (next (&identifiers)) { const char *source_type; - gchar *name, *creation, *modification; + gchar *name; source_type = host_identifier_iterator_source_type (&identifiers); @@ -11618,26 +11631,29 @@ handle_get_assets (gmp_parser_t *gmp_parser, GError **error) else name = NULL; - creation = get_iterator_creation_time (&identifiers); - modification = get_iterator_modification_time (&identifiers); - xml_string_append (result, "" "%s" - "%s" - "%s" - "%s" + "%s", + get_iterator_uuid (&identifiers), + get_iterator_name (&identifiers), + host_identifier_iterator_value (&identifiers)); + + xml_string_append (result, + "%s", + iso_if_time (get_iterator_creation_time (&identifiers))); + + xml_string_append (result, + "%s", + iso_if_time (get_iterator_modification_time (&identifiers))); + + xml_string_append (result, "" "%s" "%s" "%i" "%s" "", - get_iterator_uuid (&identifiers), - get_iterator_name (&identifiers), - host_identifier_iterator_value (&identifiers), - creation, - modification, host_identifier_iterator_source_id (&identifiers), source_type, @@ -11648,8 +11664,6 @@ handle_get_assets (gmp_parser_t *gmp_parser, GError **error) name ? name : ""); g_free (name); - g_free (creation); - g_free (modification); if (strcmp (get_iterator_name (&identifiers), "OS") == 0) xml_string_append (result, @@ -14670,23 +14684,14 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error) if (get_reports_data->alert_id == NULL) { task_t task; - gchar *creation, *modification; - - creation = get_iterator_creation_time (&reports); - modification = get_iterator_modification_time (&reports); /* Send the standard elements. Should match send_get_common. */ + buffer_xml_append_printf (prefix, "%s" "%s" - "%s" - "%s" - "" - "%s" - "" - "0" - "0", + "%s", get_iterator_owner_name (&reports) ? get_iterator_owner_name (&reports) : "", @@ -14695,12 +14700,19 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error) : "", get_iterator_comment (&reports) ? get_iterator_comment (&reports) - : "", - creation ? creation : "", - modification ? creation : ""); + : ""); - g_free (creation); - g_free (modification); + buffer_xml_append_printf + (prefix, + "%s", + iso_if_time (get_iterator_creation_time (&reports))); + + buffer_xml_append_printf + (prefix, + "%s" + "0" + "0", + iso_if_time (get_iterator_modification_time (&reports))); /* Send short task and report format info */ report_task (report, &task); @@ -18276,30 +18288,26 @@ handle_get_vulns (gmp_parser_t *gmp_parser, GError **error) while (next (&vulns)) { time_t oldest, newest; - gchar *creation, *modification; - - creation = get_iterator_creation_time (&vulns); - modification = get_iterator_modification_time (&vulns); count ++; SENDF_TO_CLIENT_OR_FAIL ("" "%s" - "%s" - "%s" - "%s" - "%1.1f" - "%d", + "%s", get_iterator_uuid (&vulns), get_iterator_name (&vulns), - vuln_iterator_type (&vulns), - creation, - modification, + vuln_iterator_type (&vulns)); + + SENDF_TO_CLIENT_OR_FAIL ("%s", + iso_if_time (get_iterator_creation_time (&vulns))); + + SENDF_TO_CLIENT_OR_FAIL ("%s", + iso_if_time (get_iterator_modification_time (&vulns))); + + SENDF_TO_CLIENT_OR_FAIL ("%1.1f" + "%d", vuln_iterator_severity (&vulns), vuln_iterator_qod (&vulns)); - g_free (creation); - g_free (modification); - // results for the vulnerability oldest = vuln_iterator_oldest (&vulns); SENDF_TO_CLIENT_OR_FAIL ("" diff --git a/src/gmp_get.c b/src/gmp_get.c index 32419aef1..3793a49d6 100644 --- a/src/gmp_get.c +++ b/src/gmp_get.c @@ -316,23 +316,14 @@ send_get_common (const char *type, get_data_t *get, iterator_t *iterator, const char *tag_type; iterator_t tags; int tag_count; - gchar *creation, *modification; buffer = g_string_new (""); - creation = get_iterator_creation_time (iterator); - modification = get_iterator_modification_time (iterator); - buffer_xml_append_printf (buffer, "<%s id=\"%s\">" "%s" "%s" - "%s" - "%s" - "%s" - "%i" - "%i" - "", + "%s", type, get_iterator_uuid (iterator) ? get_iterator_uuid (iterator) @@ -345,15 +336,21 @@ send_get_common (const char *type, get_data_t *get, iterator_t *iterator, : "", get_iterator_comment (iterator) ? get_iterator_comment (iterator) - : "", - creation ? creation : "", - modification ? modification : "", + : ""); + + buffer_xml_append_printf (buffer, + "%s", + iso_if_time (get_iterator_creation_time (iterator))); + + buffer_xml_append_printf (buffer, + "%s" + "%i" + "%i" + "", + iso_if_time (get_iterator_modification_time (iterator)), writable, in_use); - g_free (creation); - g_free (modification); - if (/* The user is the owner. */ (current_credentials.username && get_iterator_owner_name (iterator) diff --git a/src/manage.c b/src/manage.c index 5e94ec03b..7d51e0b50 100644 --- a/src/manage.c +++ b/src/manage.c @@ -5771,7 +5771,6 @@ get_nvt_xml (iterator_t *nvts, int details, int pref_count, { char *default_timeout; GString *nvt_tags; - gchar *creation, *modification; DEF (tag); @@ -5830,14 +5829,18 @@ get_nvt_xml (iterator_t *nvts, int details, int pref_count, nvt_iterator_detection (nvts)); } + g_string_append_printf (buffer, + "%s", + iso_if_time (get_iterator_creation_time (nvts))); + + g_string_append_printf (buffer, + "%s", + iso_if_time (get_iterator_modification_time (nvts))); + default_timeout = nvt_default_timeout (oid); - creation = get_iterator_creation_time (nvts); - modification = get_iterator_modification_time (nvts); g_string_append_printf (buffer, "%s" - "%s" - "%s" "%d" "%s" "" @@ -5847,8 +5850,6 @@ get_nvt_xml (iterator_t *nvts, int details, int pref_count, "%s" "%s", default_timeout ? default_timeout : "", - creation ? creation : "", - modification ? modification : "", nvt_iterator_category (nvts), family_text, nvt_iterator_qod (nvts), @@ -5857,8 +5858,6 @@ get_nvt_xml (iterator_t *nvts, int details, int pref_count, nvt_tags->str); free (default_timeout); - g_free (creation); - g_free (modification); g_string_free (nvt_tags, 1); } diff --git a/src/manage.h b/src/manage.h index c78954f0a..77977728e 100644 --- a/src/manage.h +++ b/src/manage.h @@ -1562,10 +1562,10 @@ result_iterator_delta_uuid (iterator_t*); const char * result_iterator_delta_qod_type (iterator_t*); -gchar * +time_t result_iterator_delta_creation_time (iterator_t*); -gchar * +time_t result_iterator_delta_modification_time (iterator_t*); task_t diff --git a/src/manage_get.c b/src/manage_get.c index 3c9e151b9..26b32a12c 100644 --- a/src/manage_get.c +++ b/src/manage_get.c @@ -154,22 +154,13 @@ get_iterator_comment (iterator_t* iterator) * * @param[in] iterator Iterator. * - * @return Creation time, or NULL if iteration is complete. Caller must free. + * @return Creation time, or 0 if iteration is complete. */ -gchar * +time_t get_iterator_creation_time (iterator_t* iterator) { - time_t epoch; - char *iso; - - if (iterator->done) return NULL; - - epoch = iterator_int64 (iterator, 4); - iso = iso_time (&epoch); - if (iso) - // iso points to static memory. - return g_strdup (iso); - return g_strdup("ERR"); + if (iterator->done) return 0; + return iterator_int64 (iterator, 4); } /** @@ -177,22 +168,13 @@ get_iterator_creation_time (iterator_t* iterator) * * @param[in] iterator Iterator. * - * @return Modification time, or NULL if iteration is complete. Caller must free. + * @return Modification time, or 0 if iteration is complete. */ -gchar * +time_t get_iterator_modification_time (iterator_t* iterator) { - time_t epoch; - char *iso; - - if (iterator->done) return NULL; - - epoch = iterator_int64 (iterator, 5); - iso = iso_time (&epoch); - if (iso) - // iso points to static memory. - return g_strdup (iso); - return g_strdup("ERR"); + if (iterator->done) return 0; + return iterator_int64 (iterator, 5); } /** diff --git a/src/manage_get.h b/src/manage_get.h index a990825f1..2b7bfce24 100644 --- a/src/manage_get.h +++ b/src/manage_get.h @@ -66,10 +66,10 @@ get_iterator_name (iterator_t*); const char* get_iterator_comment (iterator_t*); -gchar* +time_t get_iterator_creation_time (iterator_t*); -gchar* +time_t get_iterator_modification_time (iterator_t*); const char* diff --git a/src/manage_sql.c b/src/manage_sql.c index 4a0f2f5ed..a0882dc01 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -23885,22 +23885,13 @@ result_iterator_delta_qod_type (iterator_t* iterator) * * @param[in] iterator Iterator. * - * @return Time, or NULL if iteration is complete. Caller must free. + * @return Time, or 0 if iteration is complete. */ -gchar * +time_t result_iterator_delta_creation_time (iterator_t* iterator) { - time_t epoch; - char *iso; - - if (iterator->done) return NULL; - - epoch = iterator_int64 (iterator, RESULT_ITERATOR_DELTA_COLUMN_OFFSET + 6); - iso = iso_time (&epoch); - if (iso) - // iso points to static memory. - return g_strdup (iso); - return g_strdup("ERR"); + if (iterator->done) return 0; + return iterator_int64 (iterator, RESULT_ITERATOR_DELTA_COLUMN_OFFSET + 6); } /** @@ -23908,22 +23899,13 @@ result_iterator_delta_creation_time (iterator_t* iterator) * * @param[in] iterator Iterator. * - * @return Time, or NULL if iteration is complete. Caller must free. + * @return Time, or 0 if iteration is complete. */ -gchar * +time_t result_iterator_delta_modification_time (iterator_t* iterator) { - time_t epoch; - char *iso; - - if (iterator->done) return NULL; - - epoch = iterator_int64 (iterator, RESULT_ITERATOR_DELTA_COLUMN_OFFSET + 7); - iso = iso_time (&epoch); - if (iso) - // iso points to static memory. - return g_strdup (iso); - return g_strdup("ERR"); + if (iterator->done) return 0; + return iterator_int64 (iterator, RESULT_ITERATOR_DELTA_COLUMN_OFFSET + 7); } /** diff --git a/src/utils.c b/src/utils.c index 83c55f7fd..1763b15e6 100644 --- a/src/utils.c +++ b/src/utils.c @@ -483,7 +483,7 @@ iso_time_internal (time_t *epoch_time, const char **abbrev) /** * @brief Create an ISO time from seconds since epoch. * - * @param[in] epoch_time Time in seconds from epoch. + * @param[in] epoch_time Pointer to time in seconds from epoch. * * @return Pointer to ISO time in static memory, or NULL on error. */ @@ -496,7 +496,7 @@ iso_time (time_t *epoch_time) /** * @brief Create an ISO time from seconds since epoch, given a timezone. * - * @param[in] epoch_time Time in seconds from epoch. + * @param[in] epoch_time Pointer to time in seconds from epoch. * @param[in] zone Timezone. * @param[out] abbrev Timezone abbreviation. * @@ -543,6 +543,22 @@ iso_time_tz (time_t *epoch_time, const char *zone, const char **abbrev) return ret; } +/** + * @brief Create an ISO time from seconds since epoch, with a 0 check. + * + * @param[in] epoch_time Time in seconds from epoch. + * + * @return ISO time string in static memory. If epoch_time is 0 then string is empty. + */ +char * +iso_if_time (time_t epoch_time) +{ + static char *empty = ""; + if (epoch_time) + return iso_time (&epoch_time); + return empty; +} + /* Locks. */ diff --git a/src/utils.h b/src/utils.h index 50333d71e..fe8d05433 100644 --- a/src/utils.h +++ b/src/utils.h @@ -55,6 +55,9 @@ iso_time (time_t *); char * iso_time_tz (time_t *, const char *, const char **); +char * +iso_if_time (time_t); + /** * @brief Lockfile. */ From 05ae3c90a0e5906d751289830ce214b4ecc18fb8 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 18 Dec 2023 11:55:36 +0200 Subject: [PATCH 08/14] Remove creation and modification iso_time from result iterator --- src/manage_sql.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index b203ffc41..3e5bc84e1 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -22143,10 +22143,10 @@ where_qod (int min_qod) "name", \ KEYWORD_TYPE_STRING }, \ { "''", "comment", KEYWORD_TYPE_STRING }, \ - { " iso_time (date, opts.user_zone)", \ + { "date", \ "creation_time", \ KEYWORD_TYPE_STRING }, \ - { " iso_time (date, opts.user_zone)", \ + { "date", \ "modification_time", \ KEYWORD_TYPE_STRING }, \ { "date", "created", KEYWORD_TYPE_INTEGER }, \ @@ -22296,10 +22296,10 @@ where_qod (int min_qod) "name", \ KEYWORD_TYPE_STRING }, \ { "''", "comment", KEYWORD_TYPE_STRING }, \ - { " iso_time (date, opts.user_zone)", \ + { "date", \ "creation_time", \ KEYWORD_TYPE_STRING }, \ - { " iso_time (date, opts.user_zone)", \ + { "date", \ "modification_time", \ KEYWORD_TYPE_STRING }, \ { "date", "created", KEYWORD_TYPE_INTEGER }, \ From 03a951da95202c07b6ffc663f999d0e1a6bc4f7c Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 18 Dec 2023 12:39:51 +0200 Subject: [PATCH 09/14] Remove creation and modification iso_time from note and override iterators --- src/manage_sql.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 3e5bc84e1..f0d0383f1 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -38521,8 +38521,8 @@ modify_note (const gchar *note_id, const char *active, const char *nvt, KEYWORD_TYPE_STRING \ }, \ { "CAST ('' AS TEXT)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (notes.creation_time)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (notes.modification_time)", NULL, KEYWORD_TYPE_STRING }, \ + { "notes.creation_time", NULL, KEYWORD_TYPE_STRING }, \ + { "notes.modification_time", NULL, KEYWORD_TYPE_STRING }, \ { "notes.creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "notes.modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { "(SELECT name FROM users WHERE users.id = notes.owner)", \ @@ -38576,8 +38576,8 @@ modify_note (const gchar *note_id, const char *active, const char *nvt, { "notes_trash.uuid", "uuid", KEYWORD_TYPE_STRING }, \ { "CAST ('' AS TEXT)", NULL, KEYWORD_TYPE_STRING }, \ { "CAST ('' AS TEXT)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (notes_trash.creation_time)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (notes_trash.modification_time)", NULL, KEYWORD_TYPE_STRING }, \ + { "notes_trash.creation_time", NULL, KEYWORD_TYPE_STRING }, \ + { "notes_trash.modification_time", NULL, KEYWORD_TYPE_STRING }, \ { "notes_trash.creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "notes_trash.modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { "(SELECT name FROM users WHERE users.id = notes_trash.owner)", \ @@ -39758,8 +39758,8 @@ modify_override (const gchar *override_id, const char *active, const char *nvt, KEYWORD_TYPE_STRING \ }, \ { "CAST ('' AS TEXT)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (overrides.creation_time)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (overrides.modification_time)", NULL, KEYWORD_TYPE_STRING }, \ + { "overrides.creation_time", NULL, KEYWORD_TYPE_STRING }, \ + { "overrides.modification_time", NULL, KEYWORD_TYPE_STRING }, \ { "overrides.creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "overrides.modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { \ @@ -39826,10 +39826,10 @@ modify_override (const gchar *override_id, const char *active, const char *nvt, { "overrides_trash.uuid", "uuid", KEYWORD_TYPE_STRING }, \ { "CAST ('' AS TEXT)", NULL, KEYWORD_TYPE_STRING }, \ { "CAST ('' AS TEXT)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (overrides_trash.creation_time)", \ + { "overrides_trash.creation_time", \ NULL, \ KEYWORD_TYPE_STRING }, \ - { "iso_time (overrides_trash.modification_time)", \ + { "overrides_trash.modification_time", \ NULL, \ KEYWORD_TYPE_STRING }, \ { "overrides_trash.creation_time", \ From 3472888f177e1fd4a17b576470699f7d2735ea68 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 18 Dec 2023 12:53:53 +0200 Subject: [PATCH 10/14] Use integer for creation and modification columns in iterators --- src/manage_sql.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index f0d0383f1..0d2412918 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -22145,10 +22145,10 @@ where_qod (int min_qod) { "''", "comment", KEYWORD_TYPE_STRING }, \ { "date", \ "creation_time", \ - KEYWORD_TYPE_STRING }, \ + KEYWORD_TYPE_INTEGER }, \ { "date", \ "modification_time", \ - KEYWORD_TYPE_STRING }, \ + KEYWORD_TYPE_INTEGER }, \ { "date", "created", KEYWORD_TYPE_INTEGER }, \ { "date", "modified", KEYWORD_TYPE_INTEGER }, \ { "(SELECT name FROM users WHERE users.id = results.owner)", \ @@ -22298,10 +22298,10 @@ where_qod (int min_qod) { "''", "comment", KEYWORD_TYPE_STRING }, \ { "date", \ "creation_time", \ - KEYWORD_TYPE_STRING }, \ + KEYWORD_TYPE_INTEGER }, \ { "date", \ "modification_time", \ - KEYWORD_TYPE_STRING }, \ + KEYWORD_TYPE_INTEGER }, \ { "date", "created", KEYWORD_TYPE_INTEGER }, \ { "date", "modified", KEYWORD_TYPE_INTEGER }, \ { "(SELECT name FROM users WHERE users.id = results.owner)", \ @@ -38521,8 +38521,8 @@ modify_note (const gchar *note_id, const char *active, const char *nvt, KEYWORD_TYPE_STRING \ }, \ { "CAST ('' AS TEXT)", NULL, KEYWORD_TYPE_STRING }, \ - { "notes.creation_time", NULL, KEYWORD_TYPE_STRING }, \ - { "notes.modification_time", NULL, KEYWORD_TYPE_STRING }, \ + { "notes.creation_time", NULL, KEYWORD_TYPE_INTEGER }, \ + { "notes.modification_time", NULL, KEYWORD_TYPE_INTEGER }, \ { "notes.creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "notes.modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { "(SELECT name FROM users WHERE users.id = notes.owner)", \ @@ -38576,8 +38576,8 @@ modify_note (const gchar *note_id, const char *active, const char *nvt, { "notes_trash.uuid", "uuid", KEYWORD_TYPE_STRING }, \ { "CAST ('' AS TEXT)", NULL, KEYWORD_TYPE_STRING }, \ { "CAST ('' AS TEXT)", NULL, KEYWORD_TYPE_STRING }, \ - { "notes_trash.creation_time", NULL, KEYWORD_TYPE_STRING }, \ - { "notes_trash.modification_time", NULL, KEYWORD_TYPE_STRING }, \ + { "notes_trash.creation_time", NULL, KEYWORD_TYPE_INTEGER }, \ + { "notes_trash.modification_time", NULL, KEYWORD_TYPE_INTEGER }, \ { "notes_trash.creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "notes_trash.modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { "(SELECT name FROM users WHERE users.id = notes_trash.owner)", \ @@ -39758,8 +39758,8 @@ modify_override (const gchar *override_id, const char *active, const char *nvt, KEYWORD_TYPE_STRING \ }, \ { "CAST ('' AS TEXT)", NULL, KEYWORD_TYPE_STRING }, \ - { "overrides.creation_time", NULL, KEYWORD_TYPE_STRING }, \ - { "overrides.modification_time", NULL, KEYWORD_TYPE_STRING }, \ + { "overrides.creation_time", NULL, KEYWORD_TYPE_INTEGER }, \ + { "overrides.modification_time", NULL, KEYWORD_TYPE_INTEGER }, \ { "overrides.creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "overrides.modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { \ @@ -39828,10 +39828,10 @@ modify_override (const gchar *override_id, const char *active, const char *nvt, { "CAST ('' AS TEXT)", NULL, KEYWORD_TYPE_STRING }, \ { "overrides_trash.creation_time", \ NULL, \ - KEYWORD_TYPE_STRING }, \ + KEYWORD_TYPE_INTEGER }, \ { "overrides_trash.modification_time", \ NULL, \ - KEYWORD_TYPE_STRING }, \ + KEYWORD_TYPE_INTEGER }, \ { "overrides_trash.creation_time", \ "created", \ KEYWORD_TYPE_INTEGER }, \ From f62db8927326c6c628dbae477385f69e03039b4b Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 18 Dec 2023 13:51:23 +0200 Subject: [PATCH 11/14] Keep columns unique --- src/manage_sql.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 0d2412918..0b3ce888c 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -50136,8 +50136,8 @@ init_host_identifier_iterator (iterator_t* iterator, host_t host, if (host) init_iterator (iterator, "SELECT id, uuid, name, comment, creation_time," - " modification_time, creation_time," - " modification_time, owner, owner, value," + " modification_time, creation_time AS created," + " modification_time AS modified, owner, owner, value," " source_type, source_id, source_data," " (CASE WHEN source_type LIKE 'Report%%'" " THEN NOT EXISTS (SELECT * FROM reports" @@ -50149,8 +50149,8 @@ init_host_identifier_iterator (iterator_t* iterator, host_t host, " WHERE host = %llu" " UNION" " SELECT id, uuid, name, comment, creation_time," - " modification_time, creation_time," - " modification_time, owner, owner," + " modification_time, creation_time AS created," + " modification_time AS modified, owner, owner," " (SELECT name FROM oss WHERE id = os)," " source_type, source_id, source_data," " (CASE WHEN source_type LIKE 'Report%%'" @@ -50170,8 +50170,8 @@ init_host_identifier_iterator (iterator_t* iterator, host_t host, else init_iterator (iterator, "SELECT id, uuid, name, comment, creation_time," - " modification_time, creation_time," - " modification_time, owner, owner, value," + " modification_time, creation_time AS created," + " modification_time AS modified, owner, owner, value," " source_type, source_id, source_data, 0, '', ''" " FROM host_identifiers" " ORDER BY %s %s;", From cb983dfada959f0f73bfed7a438e5c79898408d6 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 2 Jan 2024 15:18:12 +0200 Subject: [PATCH 12/14] Work around clang-format limitation --- src/utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.h b/src/utils.h index fe8d05433..dfeed44b6 100644 --- a/src/utils.h +++ b/src/utils.h @@ -56,7 +56,7 @@ char * iso_time_tz (time_t *, const char *, const char **); char * -iso_if_time (time_t); +iso_if_time (time_t epoch_time); /** * @brief Lockfile. From 469a03dc1b6b0eb012855099bd620d3689a86809 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 27 Feb 2024 14:36:44 +0200 Subject: [PATCH 13/14] In iso_if_time also return empty if iso_time returns NULL --- src/utils.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/utils.c b/src/utils.c index 1763b15e6..efe108b23 100644 --- a/src/utils.c +++ b/src/utils.c @@ -555,7 +555,13 @@ iso_if_time (time_t epoch_time) { static char *empty = ""; if (epoch_time) - return iso_time (&epoch_time); + { + char *ret; + + ret = iso_time (&epoch_time); + if (ret) + return ret; + } return empty; } From be66e2eb308e6d5c15b6ff944ca6da1881766c23 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 27 Feb 2024 16:52:28 +0200 Subject: [PATCH 14/14] Move iso_time to C for report_config iterator --- src/manage_sql_report_configs.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/manage_sql_report_configs.c b/src/manage_sql_report_configs.c index ed158db2f..6d4b44c15 100644 --- a/src/manage_sql_report_configs.c +++ b/src/manage_sql_report_configs.c @@ -661,8 +661,8 @@ restore_report_config (const char *report_config_id) { "uuid", NULL, KEYWORD_TYPE_STRING }, \ { "name", NULL, KEYWORD_TYPE_STRING }, \ { "comment", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (creation_time)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (modification_time)", NULL, KEYWORD_TYPE_STRING }, \ + { "creation_time", NULL, KEYWORD_TYPE_INTEGER }, \ + { "modification_time", NULL, KEYWORD_TYPE_INTEGER }, \ { "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { \ @@ -696,8 +696,8 @@ restore_report_config (const char *report_config_id) { "uuid", NULL, KEYWORD_TYPE_STRING }, \ { "name", NULL, KEYWORD_TYPE_STRING }, \ { "comment", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (creation_time)", NULL, KEYWORD_TYPE_STRING }, \ - { "iso_time (modification_time)", NULL, KEYWORD_TYPE_STRING }, \ + { "creation_time", NULL, KEYWORD_TYPE_INTEGER }, \ + { "modification_time", NULL, KEYWORD_TYPE_INTEGER }, \ { "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \ { \