diff --git a/src/gmp.c b/src/gmp.c index 492c39920..932382bb2 100644 --- a/src/gmp.c +++ b/src/gmp.c @@ -1974,6 +1974,7 @@ typedef struct { get_data_t get; ///< Get args with result filtering. get_data_t report_get; ///< Get args with report filtering. + char *config_id; ///< ID of report config. char *delta_report_id; ///< ID of report to compare single report to. char *format_id; ///< ID of report format. char *alert_id; ///< ID of alert. @@ -1995,6 +1996,7 @@ get_reports_data_reset (get_reports_data_t *data) { get_data_reset (&data->get); get_data_reset (&data->report_get); + free (data->config_id); free (data->delta_report_id); free (data->format_id); free (data->alert_id); @@ -5526,6 +5528,9 @@ gmp_xml_handle_start_element (/* unused */ GMarkupParseContext* context, append_attribute (attribute_names, attribute_values, "alert_id", &get_reports_data->alert_id); + append_attribute (attribute_names, attribute_values, "config_id", + &get_reports_data->config_id); + append_attribute (attribute_names, attribute_values, "format_id", &get_reports_data->format_id); @@ -14450,8 +14455,9 @@ static void handle_get_reports (gmp_parser_t *gmp_parser, GError **error) { report_t request_report = 0, delta_report = 0, report; - char no_report_format; - report_format_t report_format; + char no_report_config, no_report_format; + report_config_t report_config = 0; + report_format_t report_format = 0; iterator_t reports; int count, filtered, ret, first; @@ -14515,6 +14521,8 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error) return; } + no_report_config = (get_reports_data->config_id == NULL) + || (strcmp(get_reports_data->config_id, "") == 0); no_report_format = (get_reports_data->format_id == NULL) || (strcmp(get_reports_data->format_id, "") == 0); @@ -14543,6 +14551,44 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error) return; } + if (!no_report_config) + { + if (find_report_config_with_permission (get_reports_data->config_id, + &report_config, + "get_report_configs")) + { + get_reports_data_reset (get_reports_data); + SEND_TO_CLIENT_OR_FAIL (XML_INTERNAL_ERROR ("get_reports")); + set_client_state (CLIENT_AUTHENTIC); + return; + } + + if (report_config == 0) + { + if (send_find_error_to_client ("get_reports", "report config", + get_reports_data->config_id, + gmp_parser)) + { + error_send_to_client (error); + return; + } + get_reports_data_reset (get_reports_data); + set_client_state (CLIENT_AUTHENTIC); + return; + } + + if (report_config_report_format (report_config) != report_format) + { + get_reports_data_reset (get_reports_data); + SEND_TO_CLIENT_OR_FAIL + (XML_ERROR_SYNTAX ("get_reports", + "Report config is not compatible with" + " selected report format")); + set_client_state (CLIENT_AUTHENTIC); + return; + } + } + if (get_reports_data->get.filt_id && strcmp (get_reports_data->get.filt_id, FILT_ID_NONE) && strcmp (get_reports_data->get.filt_id, FILT_ID_USER_SETTING)) @@ -14748,12 +14794,16 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error) "", report_iterator_uuid (&reports), no_report_format ? "" : get_reports_data->format_id, + no_report_config + ? "" + : get_reports_data->config_id, extension, content_type); @@ -14822,7 +14872,7 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error) g_free (report_task_uuid); } - if (get_reports_data->format_id) + if (!no_report_format) { gchar *format_name = NULL; format_name = report_format_name (report_format); @@ -14834,7 +14884,22 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error) "", get_reports_data->format_id, format_name ? format_name : ""); - // g_free (report_format_name); + free (format_name); + } + + if (!no_report_config) + { + gchar *config_name = NULL; + config_name = report_config_name (report_config); + + buffer_xml_append_printf + (prefix, + "" + "%s" + "", + get_reports_data->config_id, + config_name ? config_name : ""); + free (config_name); } } @@ -14850,6 +14915,7 @@ handle_get_reports (gmp_parser_t *gmp_parser, GError **error) ret = manage_send_report (report, delta_report, no_report_format ? -1 : report_format, + report_config, &get_reports_data->get, get_reports_data->notes_details, get_reports_data->overrides_details, diff --git a/src/manage.h b/src/manage.h index 30e0642bb..8cd630f57 100644 --- a/src/manage.h +++ b/src/manage.h @@ -1634,12 +1634,14 @@ int report_progress (report_t); gchar * -manage_report (report_t, report_t, const get_data_t *, report_format_t, +manage_report (report_t, report_t, const get_data_t *, + report_format_t, report_config_t, int, int, gsize *, gchar **, gchar **, gchar **, gchar **, gchar **); int -manage_send_report (report_t, report_t, report_format_t, const get_data_t *, +manage_send_report (report_t, report_t, report_format_t, report_config_t, + const get_data_t *, int, int, int, int, int, int, gboolean (*) (const char *, int (*) (const char*, void*), diff --git a/src/manage_report_configs.h b/src/manage_report_configs.h index 3a59604a3..43944f8f1 100644 --- a/src/manage_report_configs.h +++ b/src/manage_report_configs.h @@ -63,6 +63,9 @@ modify_report_config (const char *, const char *, const char *, array_t *, int delete_report_config (const char *, int); +char * +report_config_name (report_format_t); + char * report_config_uuid (report_config_t); diff --git a/src/manage_sql.c b/src/manage_sql.c index 7f5182670..06c968042 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -12254,6 +12254,8 @@ report_content_for_alert (alert_t alert, report_t report, task_t task, { int ret; report_format_t report_format; + gboolean report_format_is_fallback = FALSE; + report_config_t report_config; get_data_t *alert_filter_get; gchar *report_content; filter_t filter; @@ -12362,6 +12364,8 @@ report_content_for_alert (alert_t alert, report_t report, task_t task, return -1; } + report_format_is_fallback = TRUE; + if (find_report_format_with_permission (fallback_format_id, &report_format, @@ -12380,12 +12384,26 @@ report_content_for_alert (alert_t alert, report_t report, task_t task, } } + // Get report config + + if (report_format_is_fallback) + { + // Config would only be valid for the original report format + report_config = 0; + } + else + { + // TODO: Get report config from alert + report_config = 0; + } + // Generate report content report_content = manage_report (report, get_delta_report (alert, task, report), alert_filter_get ? alert_filter_get : get, report_format, + report_config, notes_details, overrides_details, content_length, @@ -12592,12 +12610,16 @@ escalate_to_vfire (alert_t alert, task_t task, report_t report, event_t event, { gchar *report_format_id; report_format_t report_format; + report_config_t report_config; report_format_id = g_strstrip (*point); find_report_format_with_permission (report_format_id, &report_format, "get_report_formats"); + // TODO: Add option to set report configs + report_config = 0; + if (report_format) { alert_report_data_t *alert_report_item; @@ -12614,6 +12636,7 @@ escalate_to_vfire (alert_t alert, task_t task, report_t report, event_t event, ? alert_filter_get : get, report_format, + report_config, notes_details, overrides_details, &content_length, @@ -30206,6 +30229,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task, * @param[in] delta_report Report to compare with. * @param[in] get GET data for report. * @param[in] report_format Report format. + * @param[in] report_config Report config. * @param[in] notes_details If notes, Whether to include details. * @param[in] overrides_details If overrides, Whether to include details. * @param[out] output_length NULL or location for length of return. @@ -30222,6 +30246,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task, gchar * manage_report (report_t report, report_t delta_report, const get_data_t *get, const report_format_t report_format, + const report_config_t report_config, int notes_details, int overrides_details, gsize *output_length, gchar **extension, gchar **content_type, gchar **filter_term_return, gchar **zone_return, @@ -30274,14 +30299,14 @@ manage_report (report_t report, report_t delta_report, const get_data_t *get, /* Apply report format(s) */ gchar* report_format_id = report_format_uuid (report_format); - output_file = apply_report_format (report_format_id, + output_file = apply_report_format (report_format_id, report_config, xml_start, xml_file, xml_dir, &used_rfps); g_free (report_format_id); } else { - print_report_xml_end(xml_start, xml_file, -1); + print_report_xml_end (xml_start, xml_file, -1, 0); output_file = g_strdup(xml_file); } @@ -30374,6 +30399,7 @@ manage_report (report_t report, report_t delta_report, const get_data_t *get, * @param[in] report Report. * @param[in] delta_report Report to compare with. * @param[in] report_format Report format. + * @param[in] report_config Report config. * @param[in] get GET command data. * @param[in] notes_details If notes, Whether to include details. * @param[in] overrides_details If overrides, Whether to include details. @@ -30395,7 +30421,9 @@ manage_report (report_t report, report_t delta_report, const get_data_t *get, */ int manage_send_report (report_t report, report_t delta_report, - report_format_t report_format, const get_data_t *get, + report_format_t report_format, + report_config_t report_config, + const get_data_t *get, int notes_details, int overrides_details, int result_tags, int ignore_pagination, int lean, int base64, gboolean (*send) (const char *, @@ -30455,6 +30483,14 @@ manage_send_report (report_t report, report_t delta_report, && (report_format_trust (report_format) != TRUST_YES)) return -1; + if (report_config + && report_config_report_format (report_config) != report_format) + { + g_warning ("%s: report config is not compatible with report_format", + __func__); + return -1; + } + if (mkdtemp (xml_dir) == NULL) { g_warning ("%s: mkdtemp failed", __func__); @@ -30481,14 +30517,14 @@ manage_send_report (report_t report, report_t delta_report, /* Apply report format(s). */ gchar* report_format_id = report_format_uuid (report_format); - output_file = apply_report_format (report_format_id, + output_file = apply_report_format (report_format_id, report_config, xml_start, xml_file, xml_dir, &used_rfps); g_free (report_format_id); } else { - print_report_xml_end(xml_start, xml_file, -1); + print_report_xml_end (xml_start, xml_file, -1, 0); output_file = g_strdup(xml_file); } diff --git a/src/manage_sql_report_configs.c b/src/manage_sql_report_configs.c index 6d4b44c15..f9550ecee 100644 --- a/src/manage_sql_report_configs.c +++ b/src/manage_sql_report_configs.c @@ -1032,6 +1032,20 @@ report_config_param_iterator_using_default (iterator_t *iterator) /* Misc. functions */ +/** + * @brief Return the name of a config config. + * + * @param[in] report_config Report config. + * + * @return Newly allocated UUID. + */ +char * +report_config_name (report_config_t report_config) +{ + return sql_string ("SELECT name FROM report_configs WHERE id = %llu;", + report_config); +} + /** * @brief Return the UUID of a config config. * diff --git a/src/manage_sql_report_formats.c b/src/manage_sql_report_formats.c index 3f6df153f..34ee86b3a 100644 --- a/src/manage_sql_report_formats.c +++ b/src/manage_sql_report_formats.c @@ -26,6 +26,7 @@ #include "debug_utils.h" #include "manage_sql_report_formats.h" #include "manage_acl.h" +#include "manage_report_configs.h" #include "manage_report_formats.h" #include "sql.h" #include "utils.h" @@ -3746,12 +3747,14 @@ run_report_format_script (gchar *report_format_id, * @param[in] xml_start Path of file containing start of report. * @param[in] xml_full Path to file to print full report to. * @param[in] report_format Format of report that will be created from XML. + * @param[in] report_config Report config for report format param. * * @return 0 success, -1 error. */ int print_report_xml_end (gchar *xml_start, gchar *xml_full, - report_format_t report_format) + report_format_t report_format, + report_config_t report_config) { FILE *out; @@ -3774,15 +3777,41 @@ print_report_xml_end (gchar *xml_start, gchar *xml_full, if (report_format > 0) { - iterator_t params; + gboolean can_use_config = FALSE; PRINT (out, ""); - init_report_format_param_iterator (¶ms, report_format, 0, 1, NULL); - while (next (¶ms)) - PRINT (out, - "%s%s", - report_format_param_iterator_name (¶ms), - report_format_param_iterator_value (¶ms)); - cleanup_iterator (¶ms); + + if (report_config) + { + if (report_config_report_format (report_config) == report_format) + can_use_config = TRUE; + else + g_warning ("%s: report config not compatible with report format," + " using default params", + __func__); + } + + if (can_use_config) + { + iterator_t params; + init_report_config_param_iterator (¶ms, report_config, 0); + while (next (¶ms)) + PRINT (out, + "%s%s", + report_config_param_iterator_name (¶ms), + report_config_param_iterator_value (¶ms)); + cleanup_iterator (¶ms); + } + else + { + iterator_t params; + init_report_format_param_iterator (¶ms, report_format, 0, 1, NULL); + while (next (¶ms)) + PRINT (out, + "%s%s", + report_format_param_iterator_name (¶ms), + report_format_param_iterator_value (¶ms)); + cleanup_iterator (¶ms); + } PRINT (out, ""); } @@ -3804,6 +3833,7 @@ print_report_xml_end (gchar *xml_start, gchar *xml_full, * @brief Applies a report format to an XML report. * * @param[in] report_format_id Report format to apply. + * @param[in] report_config Optional report config to apply. * @param[in] xml_start Path to the main part of the report XML. * @param[in] xml_file Path to the report XML file. * @param[in] xml_dir Path to the temporary dir. @@ -3813,6 +3843,7 @@ print_report_xml_end (gchar *xml_start, gchar *xml_full, */ gchar* apply_report_format (gchar *report_format_id, + report_config_t report_config, gchar *xml_start, gchar *xml_file, gchar *xml_dir, @@ -3905,7 +3936,9 @@ apply_report_format (gchar *report_format_id, if (g_hash_table_contains (subreports, *current_rf_dependency) == FALSE) { + // TODO: Handle report configs for subreports subreport_file = apply_report_format (*current_rf_dependency, + 0, xml_start, subreport_xml, subreport_dir, @@ -3998,7 +4031,7 @@ apply_report_format (gchar *report_format_id, /* Add second half of input XML */ - if (print_report_xml_end (xml_start, xml_file, report_format)) + if (print_report_xml_end (xml_start, xml_file, report_format, report_config)) { g_free (output_file); output_file = NULL; diff --git a/src/manage_sql_report_formats.h b/src/manage_sql_report_formats.h index 632c22089..1ea556752 100644 --- a/src/manage_sql_report_formats.h +++ b/src/manage_sql_report_formats.h @@ -49,7 +49,7 @@ int restore_report_format (const char *); gchar * -apply_report_format (gchar *, gchar *, gchar *, gchar *, +apply_report_format (gchar *, report_config_t, gchar *, gchar *, gchar *, GList **); gboolean @@ -89,6 +89,6 @@ int check_db_report_formats_trash (); int -print_report_xml_end (gchar *, gchar *, report_format_t); +print_report_xml_end (gchar *, gchar *, report_format_t, report_config_t); #endif /* not _GVMD_MANAGE_SQL_REPORT_FORMATS_H */ diff --git a/src/schema_formats/XML/GMP.xml.in b/src/schema_formats/XML/GMP.xml.in index 40998645a..7f60c5d05 100644 --- a/src/schema_formats/XML/GMP.xml.in +++ b/src/schema_formats/XML/GMP.xml.in @@ -1806,6 +1806,13 @@ along with this program. If not, see . format_id uuid + UUID of the report format used + 1 + + + config_id + uuid + UUID of the report config used 1 @@ -1836,7 +1843,8 @@ along with this program. If not, see . writable in_use task - report_format + report_format + report_config report @@ -1915,6 +1923,23 @@ along with this program. If not, see . name + + report_config + The report config used + + + id + uuid + 1 + + name + + + name + The name of the report config + name + + report @@ -17412,7 +17437,12 @@ END:VCALENDAR format_id - ID of required report format + ID of requested report format + uuid + + + format_id + ID of requested report config uuid