Skip to content

Commit

Permalink
Move modification and creation iso_time to C for result iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmundell committed Nov 27, 2023
1 parent e88153c commit 89d683f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 39 deletions.
23 changes: 11 additions & 12 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -8045,7 +8045,7 @@ buffer_notes_xml (GString *buffer, iterator_t *notes, int include_notes_details,

text = note_iterator_text (notes);
excerpt = utf8_substring (text, 0, setting_excerpt_size_int ());
creation = get_iterator_creation_time (notes);
creation = get_iterator_creation_time (notes, NULL);
modification = get_iterator_modification_time (notes);

/* This must match send_get_common. */
Expand Down Expand Up @@ -8113,7 +8113,7 @@ 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);
creation = get_iterator_creation_time (notes, NULL);
modification = get_iterator_modification_time (notes);

/* This must match send_get_common. */
Expand Down Expand Up @@ -8321,7 +8321,7 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides,

text = override_iterator_text (overrides);
excerpt = utf8_substring (text, 0, setting_excerpt_size_int ());
creation = get_iterator_creation_time (overrides);
creation = get_iterator_creation_time (overrides, NULL);
modification = get_iterator_modification_time (overrides);

/* This must match send_get_common. */
Expand Down Expand Up @@ -8402,7 +8402,7 @@ buffer_overrides_xml (GString *buffer, iterator_t *overrides,
}

end_time = override_iterator_end_time (overrides);
creation = get_iterator_creation_time (overrides);
creation = get_iterator_creation_time (overrides, NULL);
modification = get_iterator_modification_time (overrides);

/* This must match send_get_common. */
Expand Down Expand Up @@ -9236,24 +9236,23 @@ buffer_results_xml (GString *buffer, iterator_t *results, task_t task,
const char *delta_state, iterator_t *delta_results,
int changed, int cert_loaded, int lean, int use_delta_fields)
{

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;
double severity_double;
gchar *nl_descr, *nl_descr_escaped;
gchar *nl_descr, *nl_descr_escaped, *creation_time, *user_zone;
result_t result;
report_t report;
task_t selected_task;
gchar *creation_time;

comment = get_iterator_comment (results);
name = get_iterator_name (results);
host = result_iterator_host (results);
port = result_iterator_port (results);
asset_id = NULL;

manage_current_user_info (&user_zone);
if (use_delta_fields)
{
descr = result_iterator_delta_description (results);
Expand Down Expand Up @@ -9282,15 +9281,15 @@ buffer_results_xml (GString *buffer, iterator_t *results, task_t task,
qod = result_iterator_qod (results);
qod_type = result_iterator_qod_type (results);
result = result_iterator_result (results);
creation_time = get_iterator_creation_time (results);
creation_time = get_iterator_creation_time (results, user_zone);
result_id = get_iterator_uuid (results);
path = result_iterator_path (results);
report = result_iterator_report (results);
hostname = result_iterator_hostname (results);
if (host)
asset_id = result_iterator_asset_host_id (results);
}

g_free (user_zone);

if (descr)
{
Expand Down Expand Up @@ -11575,7 +11574,7 @@ handle_get_assets (gmp_parser_t *gmp_parser, GError **error)
else
name = NULL;

creation = get_iterator_creation_time (&identifiers);
creation = get_iterator_creation_time (&identifiers, NULL);
modification = get_iterator_modification_time (&identifiers);

xml_string_append (result,
Expand Down Expand Up @@ -14629,7 +14628,7 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error)
task_t task;
gchar *creation, *modification;

creation = get_iterator_creation_time (&reports);
creation = get_iterator_creation_time (&reports, NULL);
modification = get_iterator_modification_time (&reports);

/* Send the standard elements. Should match send_get_common. */
Expand Down Expand Up @@ -17932,7 +17931,7 @@ handle_get_vulns (gmp_parser_t *gmp_parser, GError **error)
time_t oldest, newest;
gchar *creation, *modification;

creation = get_iterator_creation_time (&vulns);
creation = get_iterator_creation_time (&vulns, NULL);
modification = get_iterator_modification_time (&vulns);

count ++;
Expand Down
2 changes: 1 addition & 1 deletion src/gmp_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ send_get_common (const char *type, get_data_t *get, iterator_t *iterator,

buffer = g_string_new ("");

creation = get_iterator_creation_time (iterator);
creation = get_iterator_creation_time (iterator, NULL);
modification = get_iterator_modification_time (iterator);

buffer_xml_append_printf (buffer,
Expand Down
2 changes: 1 addition & 1 deletion src/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -5831,7 +5831,7 @@ get_nvt_xml (iterator_t *nvts, int details, int pref_count,
}

default_timeout = nvt_default_timeout (oid);
creation = get_iterator_creation_time (nvts);
creation = get_iterator_creation_time (nvts, NULL);
modification = get_iterator_modification_time (nvts);

g_string_append_printf (buffer,
Expand Down
3 changes: 3 additions & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -3430,6 +3430,9 @@ manage_default_ca_cert ();

/* Users. */

user_t
manage_current_user_info (gchar **);

gboolean
find_user_by_name_with_permission (const char *, user_t *, const char *);

Expand Down
4 changes: 2 additions & 2 deletions src/manage_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ get_iterator_comment (iterator_t* iterator)
* @return Creation time, or NULL if iteration is complete. Caller must free.
*/
gchar *
get_iterator_creation_time (iterator_t* iterator)
get_iterator_creation_time (iterator_t* iterator, const gchar *zone)
{
time_t epoch;
char *iso;

if (iterator->done) return NULL;

epoch = iterator_int64 (iterator, 4);
iso = iso_time (&epoch);
iso = iso_time_tz (&epoch, zone, NULL);
if (iso)
// iso points to static memory.
return g_strdup (iso);
Expand Down
2 changes: 1 addition & 1 deletion src/manage_get.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const char*
get_iterator_comment (iterator_t*);

gchar*
get_iterator_creation_time (iterator_t*);
get_iterator_creation_time (iterator_t*, const gchar*);

gchar*
get_iterator_modification_time (iterator_t*);
Expand Down
60 changes: 38 additions & 22 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -22074,10 +22074,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 }, \
Expand Down Expand Up @@ -22223,10 +22223,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 }, \
Expand Down Expand Up @@ -22484,41 +22484,57 @@ where_qod (int min_qod)
}

/**
* @brief Generate the extra_tables string for a result iterator.
* @brief Get ID and zone of the current user.
*
* @param[in] override Whether to apply overrides.
* @param[in] dynamic Whether to use dynamic severity scores.
* @param[out] user_zone Location for newly allocated user zone.
*
* @return Newly allocated string with the extra_tables clause.
* @return User ID.
*/
static gchar*
result_iterator_opts_table (int override, int dynamic)
user_t
manage_current_user_info (gchar **user_zone)
{
user_t user_id;
gchar *user_zone, *quoted_user_zone, *ret;

if (current_credentials.uuid)
{
user_id = sql_int64_0 ("SELECT id FROM users WHERE uuid = '%s';",
current_credentials.uuid);
if (user_id > 0)
user_zone = sql_string ("SELECT"
" coalesce ((SELECT current_setting"
" ('gvmd.tz_override')),"
" (SELECT timezone FROM users"
" WHERE id = %llu));",
user_id);
*user_zone = sql_string ("SELECT"
" coalesce ((SELECT current_setting"
" ('gvmd.tz_override')),"
" (SELECT timezone FROM users"
" WHERE id = %llu));",
user_id);
else
user_zone = g_strdup ("UTC");
*user_zone = g_strdup ("UTC");
}
else
{
user_id = 0;
user_zone = sql_string ("SELECT"
" coalesce ((SELECT current_setting"
" ('gvmd.tz_override')),"
" 'UTC');");
*user_zone = sql_string ("SELECT"
" coalesce ((SELECT current_setting"
" ('gvmd.tz_override')),"
" 'UTC');");
}
return user_id;
}

/**
* @brief Generate the extra_tables string for a result iterator.
*
* @param[in] override Whether to apply overrides.
* @param[in] dynamic Whether to use dynamic severity scores.
*
* @return Newly allocated string with the extra_tables clause.
*/
static gchar*
result_iterator_opts_table (int override, int dynamic)
{
user_t user_id;
gchar *user_zone, *quoted_user_zone, *ret;

user_id = manage_current_user_info (&user_zone);

quoted_user_zone = sql_quote ("user_zone");
g_free (user_zone);
Expand Down

0 comments on commit 89d683f

Please sign in to comment.