/* Copyright (C) 2010-2022 Greenbone AG
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

/*
 * @file
 * @brief Manager Manage library: SQL backend headers.
 */

#ifndef _GVMD_MANAGE_SQL_H
#define _GVMD_MANAGE_SQL_H

#include <gvm/util/xmlutils.h>
#include <time.h>

#include "manage.h"
#include "manage_utils.h"


/* Internal types and preprocessor definitions. */

/**
 * @brief Location of a constituent of a trashcan resource.
 */
#define LOCATION_TABLE 0

/**
 * @brief Location of a constituent of a trashcan resource.
 */
#define LOCATION_TRASH 1

/**
 * @brief UUID of 'All' NVT selector.
 */
#define MANAGE_NVT_SELECTOR_UUID_ALL "54b45713-d4f4-4435-b20d-304c175ed8c5"

/**
 * @brief Predefined role UUID.
 */
#define ROLE_UUID_ADMIN "7a8cb5b4-b74d-11e2-8187-406186ea4fc5"

/**
 * @brief Predefined role UUID.
 */
#define ROLE_UUID_GUEST "cc9cac5e-39a3-11e4-abae-406186ea4fc5"

/**
 * @brief Predefined role UUID.
 */
#define ROLE_UUID_INFO "5f8fd16c-c550-11e3-b6ab-406186ea4fc5"

/**
 * @brief Predefined role UUID.
 */
#define ROLE_UUID_MONITOR "12cdb536-480b-11e4-8552-406186ea4fc5"

/**
 * @brief Predefined role UUID.
 */
#define ROLE_UUID_USER "8d453140-b74d-11e2-b0be-406186ea4fc5"

/**
 * @brief Predefined role UUID.
 */
#define ROLE_UUID_SUPER_ADMIN "9c5a6ec6-6fe2-11e4-8cb6-406186ea4fc5"

/**
 * @brief Predefined role UUID.
 */
#define ROLE_UUID_OBSERVER "87a7ebce-b74d-11e2-a81f-406186ea4fc5"

/**
 * @brief UUID of 'OpenVAS Default' scanner.
 */
#define SCANNER_UUID_DEFAULT "08b69003-5fc2-4037-a479-93b440211c73"

/**
 * @brief UUID of 'openvasd Default' scanner.
 */
#define SCANNER_UUID_OPENVASD_DEFAULT "8154d8e3-30ee-4959-9151-1863c89a8e62"

#define SCANNER_UUID_CONTAINER_IMAGE_DEFAULT "1facb485-10e8-4520-9110-66f929d9ac2e"

/**
 * @brief UUID of 'CVE' scanner.
 */
#define SCANNER_UUID_CVE "6acd0832-df90-11e4-b9d5-28d24461215b"

/**
 * @brief Trust constant for error.
 */
#define TRUST_ERROR 0

/**
 * @brief Trust constant for yes.
 */
#define TRUST_YES 1

/**
 * @brief Trust constant for no.
 */
#define TRUST_NO 2

/**
 * @brief Trust constant for unknown.
 */
#define TRUST_UNKNOWN 3

/**
 * @brief Number of milliseconds between timevals a and b (performs a-b).
 */
#define TIMEVAL_SUBTRACT_MS(a,b) ((((a).tv_sec - (b).tv_sec) * 1000) + \
                                  ((a).tv_usec - (b).tv_usec) / 1000)

/**
 * @brief Database superuser role
 */
#define DB_SUPERUSER_ROLE "dba"


/* Macros. */

/**
 * @brief Write to a file or close stream and exit.
 *
 * @param[in]   stream    Stream to write to.
 * @param[in]   format    Format specification.
 * @param[in]   args      Arguments.
 */
#define PRINT(stream, format, args...)                                       \
  do                                                                         \
    {                                                                        \
      gchar *msg;                                                            \
      msg = g_markup_printf_escaped (format, ## args);                       \
      if (fprintf (stream, "%s", msg) < 0)                                   \
        {                                                                    \
          g_free (msg);                                                      \
          fclose (stream);                                                   \
          return -1;                                                         \
        }                                                                    \
      g_free (msg);                                                          \
    }                                                                        \
  while (0)


/* Iterator definitions. */

/**
 * @brief Delta results columns offset for result iterator.
 */
#define RESULT_ITERATOR_DELTA_COLUMN_OFFSET GET_ITERATOR_COLUMN_COUNT + 51

/**
 * @brief Struct to be sent as user data to the GFunc for adding results
 */
struct report_aux {
  GArray *results_array;         ///< Results.
  report_t report;               ///< Report.
  task_t task;                   ///< Task.
  GHashTable *hash_results;      ///< Hash.
  GHashTable *hash_hostdetails;  ///< Hash.
};


/* Variables */



/**
 * @brief Size of base64 chunk in manage_send_report.
 */
#define MANAGE_SEND_REPORT_CHUNK64_SIZE 262144

/**
 * @brief Size of file chunk in manage_send_report.
 */
#define MANAGE_SEND_REPORT_CHUNK_SIZE (MANAGE_SEND_REPORT_CHUNK64_SIZE * 3 / 4)

/**
 * @brief Context info for print_report_xml_start.
 */
struct print_report_context
{
 gchar *compliance_levels;   ///< Compliance levels.
 int count_filtered;         ///< Whether to count filtered results.
 report_t delta;             ///< Report to compare with.
 int filtered_result_count;  ///< Filtered result count.
 const get_data_t *get;      ///< GET command data.
 gchar *tz;                  ///< TZ.
 gchar *zone;                ///< Zone.
 char *old_tz_override;      ///< Old TZ.
 report_t report;            ///< Report.
 gchar *tsk_usage_type;      ///< Usage type of task, like "audit"
 // Counts.
 int criticals;              ///< Number of criticals.
 int holes;                  ///< Number of holes.
 int infos;                  ///< Number of infos.
 int logs;                   ///< Number of logs.
 int warnings;               ///< Number of warnings.
 int false_positives;        ///< Number of false positives.
 int total_result_count;     ///< Total number of results.
 // Filtered counts.
 GHashTable *f_host_criticals;       ///< Criticals per host.
 GHashTable *f_host_false_positives; ///< False positives per host.
 GHashTable *f_host_holes;           ///< Holes per host.
 GHashTable *f_host_infos;           ///< Infos per host.
 GHashTable *f_host_logs;            ///< Logs per hosts.
 GHashTable *f_host_ports;           ///< Ports per host.
 GHashTable *f_host_warnings;        ///< Warnings per hosts.
 GHashTable *f_host_max_severity;    ///< Max severity per host.
 // Filtered counts: audit.
 GHashTable *f_host_compliant;       ///< Compliants per host.
 GHashTable *f_host_incomplete;      ///< Incompletes per host.
 GHashTable *f_host_notcompliant;    ///< Notcompliants per host.
 GHashTable *f_host_undefined;       ///< Undefineds per host.
};

/**
 * @brief Context type for print_report_xml_start.
 */
typedef struct print_report_context print_report_context_t;

extern db_conn_info_t gvmd_db_conn_info;

/**
 * @brief Function to fork a connection that will accept GMP requests.
 */
extern manage_connection_forker_t manage_fork_connection;


/* Function prototypes */

typedef long long int rowid_t;

int
manage_db_empty ();

gboolean
db_table_has_column (const gchar *, const gchar *, const gchar *);

gboolean
host_nthlast_report_host (const char *, report_host_t *, int);

report_host_t
host_iterator_report_host (iterator_t *);

void
init_report_host_details_iterator (iterator_t *, report_host_t);

const char *
report_host_details_iterator_name (iterator_t *);

const char *
report_host_details_iterator_value (iterator_t *);

const char *
report_host_details_iterator_source_name (iterator_t *);

gchar *
report_creation_time (report_t);

gchar *
report_modification_time (report_t);

gchar *
report_start_time (report_t);

gchar *
report_end_time (report_t);

int
delete_report_internal (report_t);

int
set_report_scan_run_status (report_t, task_status_t);

int
update_report_modification_time (report_t);

int
set_report_slave_progress (report_t, int);

void
set_task_schedule_next_time (task_t, time_t);

void
set_task_schedule_next_time_uuid (const gchar *, time_t);

void
init_preference_iterator (iterator_t *, config_t, const char *);

const char *
preference_iterator_name (iterator_t *);

const char *
preference_iterator_value (iterator_t *);

int
create_current_report (task_t, char **, task_status_t);

int
create_agent_task_current_report (task_t, char *, task_status_t);

int
init_task_schedule_iterator (iterator_t *);

void
cleanup_task_schedule_iterator (iterator_t *);

task_t
task_schedule_iterator_task (iterator_t *);

const char *
task_schedule_iterator_task_uuid (iterator_t *);

schedule_t task_schedule_iterator_schedule (iterator_t *);

const char *
task_schedule_iterator_icalendar (iterator_t *);

const char *
task_schedule_iterator_timezone (iterator_t *);

const char *
task_schedule_iterator_owner_uuid (iterator_t *);

const char *
task_schedule_iterator_owner_name (iterator_t *);

gboolean
task_schedule_iterator_timed_out (iterator_t *);

gboolean
task_schedule_iterator_start_due (iterator_t *);

gboolean
task_schedule_iterator_stop_due (iterator_t *);

time_t
task_schedule_iterator_initial_offset (iterator_t *);

int
set_task_schedule_uuid (const gchar*, schedule_t, int);

int
manage_update_nvti_cache ();

const char *
run_status_name_internal (task_status_t);

void
check_generate_scripts ();

void
auto_delete_reports ();

int
parse_iso_time (const char *);

void
set_report_scheduled (report_t);

void
parse_osp_report (task_t, report_t, const char *);

void
reschedule_task (const gchar *);

void
insert_port_range (port_list_t, port_protocol_t, int, int);

int
manage_cert_db_exists ();

int
cert_check_time ();

int
scap_check_time ();

int
nvts_check_time ();

char *
nvt_severity (const char *, const char *);

int
count (const char *, const get_data_t *, column_t *, column_t *, const char **,
       int, const char *, const char *, int);

int
count2 (const char *, const get_data_t *, column_t *, column_t *, column_t *,
        column_t *, const char **, int, const char *, const char *,
        const char *, int);

int
init_get_iterator (iterator_t*, const char *, const get_data_t *, column_t *,
                   column_t *, const char **, int, const char *, const char *,
                   int);

int
init_get_iterator2 (iterator_t *, const char *, const get_data_t *, column_t *,
                    column_t *, column_t *, column_t *, const char **, int,
                    const char *, const char *, const char *, int, int,
                    const char *);

int
init_get_iterator2_with (iterator_t *, const char *, const get_data_t *,
                         column_t *, column_t *, column_t *, column_t *,
                         const char **, int, const char *, const char *,
                         const char *, int, int, const char *, const char *,
                         int, int);

int
openvasd_get_details_from_iterator (iterator_t *, char **, GSList **);

gchar *
columns_build_select (column_t *);

gchar*
filter_term_sql (const char *);

gchar *
filter_clause (const char*, const char*, const char **, column_t *,
               column_t *, int, int, gchar **, int *, int *, array_t **,
               gchar **);

void
check_alerts ();

int
manage_option_setup (GSList *, const db_conn_info_t *, int);

void
manage_option_cleanup ();

void
update_all_config_caches ();

int
task_report_previous (task_t, report_t, report_t *);

int
task_last_report_any_status (task_t, report_t *);

int
task_second_last_report (task_t, report_t *);

double
task_severity_double (task_t, int, int, int);

gchar *
tasks_extra_where (int, const char *);

gchar *
reports_extra_where (int, const gchar *, const char *);

gchar*
vulns_extra_where (int);

gchar *
vuln_iterator_extra_with_from_filter (const gchar *);

gchar*
vuln_iterator_opts_from_filter (const gchar *);

gboolean
find_trash (const char *, const char *, resource_t *);

void
init_user_task_iterator (iterator_t *, int, int);

gboolean
resource_with_id_exists (const char *, resource_t);

gboolean
resource_with_name_exists (const char *, const char *, resource_t);

gboolean
resource_with_name_exists_global (const char *, const char *, resource_t);

nvti_t *
lookup_nvti (const gchar *);

void
add_role_permission_resource (const gchar *, const gchar *, const gchar *,
                              const gchar *);

void
create_view_vulns ();

void
create_indexes_nvt ();

void
create_view_result_vt_epss ();

int
config_family_entire_and_growing (config_t, const char*);

gchar *
new_severity_clause (int, int);

void
reports_clear_count_cache (const gchar *);

void
reports_clear_count_cache_dynamic ();

GHashTable *
reports_for_override (override_t);

void
reports_add_all (GHashTable *);

void
reports_add_for_override (GHashTable *, override_t);

GHashTable *
reports_hashtable ();

void
report_cache_counts (report_t, int, int, const char *);

void
report_clear_count_cache (report_t, int, int, const char *);

GHashTable *
new_resources_hashtable ();

int
cleanup_config_sequences ();

int
cleanup_port_list_sequences ();

int
cleanup_nvt_sequences ();

int
cleanup_ids_for_table (const char *);

void

create_indexes_cpe ();

void
drop_indexes_cpe ();

void
create_indexes_cve ();

void
drop_indexes_cve ();

void
report_set_processing_required (report_t, int, int);

void
report_set_discovery (report_t, gboolean);

gboolean
check_report_discovery (report_t);

int
process_report_import (report_t);

int
check_host_detail_exists (report_t, const char *, const char *, const char *,
                          const char *, const char *, const char *, char **,
                          GHashTable *);

#if ENABLE_HTTP_SCANNER
void
parse_http_scanner_report (task_t, report_t, GSList *, time_t, time_t);

int
check_http_scanner_result_exists (report_t, task_t, http_scanner_result_t,
                                  char **, GHashTable *);

int
get_http_scanner_nvti_qod (const char *);

char *
convert_http_scanner_type_to_osp_type (const char *);
#endif

int
vector_find_filter (const gchar **, const gchar *);

int
ldap_auth_enabled ();

int
radius_auth_enabled ();

void
init_pg_timezones_iterator (iterator_t *);

const char *
pg_timezones_iterator_name (iterator_t *);

gboolean
pg_timezone_supported (const char *);

int
type_build_select (const char *, const char *, const get_data_t *, gboolean,
                   gboolean, const char *, const char *, const char *,
                   gchar **);

void
print_report_init_f_hosts (print_report_context_t *);

void
print_report_context_cleanup (print_report_context_t *);

const char *
report_compliance_from_counts (const int *,
                               const int *,
                               const int *,
                               const int *);

gboolean
nvt_exists (const char *);

int
validate_results_port (const char *);

void
result_nvt_notice (const gchar *);

#endif /* not _GVMD_MANAGE_SQL_H */
