OpenVAS Manager  7.0.3~git
sql.c File Reference
#include "sql.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for sql.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "md manage"
 GLib log domain. More...
 

Functions

int sql_prepare_internal (int, int, const char *, va_list, sql_stmt_t **)
 Prepare a statement. More...
 
int sql_exec_internal (int, sql_stmt_t *)
 Execute a prepared statement. More...
 
int sql_explain_internal (const char *, va_list)
 Write debug messages with the query plan for an SQL query to the log. More...
 
int sql_explain (const char *sql,...)
 Write debug messages with the query plan for an SQL query to the log. More...
 
gchar * sql_nquote (const char *string, size_t length)
 Quotes a string of a known length to be passed to sql statements. More...
 
gchar * sql_quote (const char *string)
 Quotes a string to be passed to sql statements. More...
 
gchar * sql_insert (const char *string)
 Get the SQL insert expression for a string. More...
 
int sqlv (int retry, char *sql, va_list args)
 Perform an SQL statement. More...
 
void sql (char *sql,...)
 Perform an SQL statement, retrying if database is busy or locked. More...
 
int sql_error (char *sql,...)
 Perform an SQL statement, retrying if database is busy or locked. More...
 
int sql_giveup (char *sql,...)
 Perform an SQL statement, giving up if database is busy or locked. More...
 
void sql_quiet (char *sql,...)
 Perform an SQL statement, without logging. More...
 
int sql_x (char *sql, va_list args, sql_stmt_t **stmt_return)
 Get a particular cell from a SQL query. More...
 
double sql_double (char *sql,...)
 Get the first value from a SQL query, as a double. More...
 
int sql_int (char *sql,...)
 Get a particular cell from a SQL query, as an int. More...
 
char * sql_string (char *sql,...)
 Get a particular cell from a SQL query, as an string. More...
 
int sql_int64 (long long int *ret, char *sql,...)
 Get a particular cell from a SQL query, as an int64. More...
 
void init_prepared_iterator (iterator_t *iterator, sql_stmt_t *stmt)
 Initialise an iterator. More...
 
void init_iterator (iterator_t *iterator, const char *sql,...)
 Initialise an iterator. More...
 
double iterator_double (iterator_t *iterator, int col)
 Get a double column from an iterator. More...
 
int iterator_int (iterator_t *iterator, int col)
 Get a int column from an iterator. More...
 
long long int iterator_int64 (iterator_t *iterator, int col)
 Get an integer column from an iterator. More...
 
const char * iterator_string (iterator_t *iterator, int col)
 Get a string column from an iterator. More...
 
void cleanup_iterator (iterator_t *iterator)
 Cleanup an iterator. More...
 
gboolean next (iterator_t *iterator)
 Increment an iterator. More...
 
sql_stmt_tsql_prepare (const char *sql,...)
 Prepare a statement. More...
 
int sql_exec (sql_stmt_t *stmt)
 Execute a prepared statement. More...
 

Variables

int log_errors = 1
 Whether to log errors. More...
 

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "md manage"

GLib log domain.

Definition at line 37 of file sql.c.

Function Documentation

◆ cleanup_iterator()

void cleanup_iterator ( iterator_t iterator)

Cleanup an iterator.

Parameters
[in]iteratorIterator.

Definition at line 664 of file sql.c.

References iterator::crypt_ctx, lsc_crypt_release(), iterator::prepared, sql_finalize(), and iterator::stmt.

Referenced by acl_users_with_access_sql(), auto_delete_reports(), check_db_sequences(), check_generate_scripts(), cleanup_task_schedule_iterator(), delete_reports(), get_nvti_xml(), get_ovaldi_files(), host_routes_xml(), manage_get_scanners(), migrate_122_to_123(), migrate_12_to_13(), migrate_143_to_144(), migrate_153_to_154(), migrate_156_to_157(), migrate_158_to_159(), migrate_160_to_161(), migrate_165_to_166(), migrate_16_to_17(), migrate_19_to_20(), migrate_1_to_2(), migrate_21_to_22(), migrate_35_to_36(), migrate_3_to_4(), migrate_4_to_5_copy_data(), migrate_55_to_56(), migrate_9_to_10(), ovaldef_cves(), refresh_nvt_cves(), report_add_result(), report_cache_counts(), report_severity(), reports_add_all(), reports_add_for_override(), reports_build_count_cache(), schedule_info(), sql_rename_column(), target_port_range(), task_observers(), and user_resources_in_use().

665 {
666  if (iterator == NULL)
667  {
668  g_warning ("%s: null iterator pointer.\n", __FUNCTION__);
669  return;
670  }
671 
672  if (iterator->prepared == 0)
673  sql_finalize (iterator->stmt);
674  if (iterator->crypt_ctx)
675  {
676  lsc_crypt_release (iterator->crypt_ctx);
677  iterator->crypt_ctx = NULL;
678  }
679 }
lsc_crypt_ctx_t crypt_ctx
Encryption context.
Definition: iterator.h:57
sql_stmt_t * stmt
SQL statement.
Definition: iterator.h:54
int prepared
Prepared flag.
Definition: iterator.h:56
void lsc_crypt_release(lsc_crypt_ctx_t ctx)
Release an LSC encryption context.
Definition: lsc_crypt.c:528
void sql_finalize(sql_stmt_t *)
Free a prepared statement.
Definition: sql_pg.c:824
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_iterator()

void init_iterator ( iterator_t iterator,
const char *  sql,
  ... 
)

Initialise an iterator.

Parameters
[in]iteratorIterator.
[in]sqlFormat string for SQL.

Definition at line 577 of file sql.c.

References iterator::crypt_ctx, iterator::done, iterator::prepared, sql_prepare_internal(), and iterator::stmt.

Referenced by acl_users_with_access_sql(), auto_delete_reports(), check_db_sequences(), check_generate_scripts(), host_routes_xml(), init_aggregate_iterator(), init_alert_data_iterator(), init_alert_task_iterator(), init_config_task_iterator(), init_config_timeout_iterator(), init_cpe_cve_iterator(), init_credential_scanner_iterator(), init_credential_target_iterator(), init_cve_nvt_iterator(), init_family_iterator(), init_filter_alert_iterator(), init_host_detail_iterator(), init_host_identifier_iterator(), init_nvt_preference_iterator(), init_nvt_selector_iterator(), init_os_host_iterator(), init_otp_pref_iterator(), init_ovaldi_file_iterator(), init_param_option_iterator(), init_port_list_target_iterator(), init_port_range_iterator(), init_preference_iterator(), init_report_counts_build_iterator(), init_report_errors_iterator(), init_report_format_alert_iterator(), init_report_format_param_iterator(), init_report_host_iterator(), init_report_iterator_task(), init_resource_tag_iterator(), init_scanner_task_iterator(), init_schedule_task_iterator(), init_target_task_iterator(), init_task_file_iterator(), init_task_group_iterator(), init_task_role_iterator(), init_task_schedule_iterator(), init_task_user_iterator(), init_user_group_iterator(), init_user_role_iterator(), manage_get_scanners(), migrate_122_to_123(), migrate_12_to_13(), migrate_138_to_139(), migrate_143_to_144(), migrate_153_to_154(), migrate_156_to_157(), migrate_158_to_159(), migrate_160_to_161(), migrate_165_to_166(), migrate_168_to_169(), migrate_16_to_17(), migrate_19_to_20(), migrate_1_to_2(), migrate_21_to_22(), migrate_24_to_25(), migrate_35_to_36(), migrate_4_to_5_copy_data(), migrate_55_to_56(), migrate_9_to_10(), ovaldef_cves(), refresh_nvt_cves(), report_severity(), reports_add_all(), reports_add_for_override(), reports_build_count_cache(), schedule_info(), sql_rename_column(), and user_resources_in_use().

578 {
579  int ret;
580  sql_stmt_t* stmt;
581  va_list args;
582 
583  iterator->done = FALSE;
584  iterator->prepared = 0;
585  iterator->crypt_ctx = NULL;
586 
587  va_start (args, sql);
588  ret = sql_prepare_internal (1, 1, sql, args, &stmt);
589  va_end (args);
590  if (ret)
591  {
592  g_warning ("%s: sql_prepare failed\n", __FUNCTION__);
593  abort ();
594  }
595  iterator->stmt = stmt;
596 }
int sql_prepare_internal(int, int, const char *, va_list, sql_stmt_t **)
Prepare a statement.
Definition: sql_pg.c:458
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
lsc_crypt_ctx_t crypt_ctx
Encryption context.
Definition: iterator.h:57
gboolean done
End flag.
Definition: iterator.h:55
sql_stmt_t * stmt
SQL statement.
Definition: iterator.h:54
int prepared
Prepared flag.
Definition: iterator.h:56
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_prepared_iterator()

void init_prepared_iterator ( iterator_t iterator,
sql_stmt_t stmt 
)

Initialise an iterator.

Parameters
[in]iteratorIterator.
[in]stmtStatement.

Definition at line 561 of file sql.c.

References iterator::crypt_ctx, iterator::done, iterator::prepared, and iterator::stmt.

Referenced by init_prognosis_iterator().

562 {
563  iterator->done = FALSE;
564  iterator->stmt = stmt;
565  iterator->prepared = 1;
566  iterator->crypt_ctx = NULL;
567  g_debug (" sql: init prepared %p\n", stmt);
568 }
lsc_crypt_ctx_t crypt_ctx
Encryption context.
Definition: iterator.h:57
gboolean done
End flag.
Definition: iterator.h:55
sql_stmt_t * stmt
SQL statement.
Definition: iterator.h:54
int prepared
Prepared flag.
Definition: iterator.h:56
Here is the caller graph for this function:

◆ iterator_double()

double iterator_double ( iterator_t iterator,
int  col 
)

Get a double column from an iterator.

Parameters
[in]iteratorIterator.
[in]colColumn offset.
Returns
Value of given column.

Definition at line 607 of file sql.c.

References iterator::done, sql_column_double(), and iterator::stmt.

Referenced by prognosis_iterator_cvss_double(), report_severity(), result_iterator_level(), result_iterator_original_level(), and result_iterator_severity_double().

608 {
609  if (iterator->done) abort ();
610  return sql_column_double (iterator->stmt, col);
611 }
double sql_column_double(sql_stmt_t *, int)
Return a column as a double from a prepared statement.
Definition: sql_pg.c:868
gboolean done
End flag.
Definition: iterator.h:55
sql_stmt_t * stmt
SQL statement.
Definition: iterator.h:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ iterator_int()

int iterator_int ( iterator_t iterator,
int  col 
)

Get a int column from an iterator.

Parameters
[in]iteratorIterator.
[in]colColumn offset.
Returns
Value of given column.

Definition at line 622 of file sql.c.

References iterator::done, sql_column_int(), and iterator::stmt.

Referenced by agent_iterator_trust(), agent_iterator_trust_time(), alert_iterator_condition(), alert_iterator_event(), alert_iterator_filter_name(), alert_iterator_filter_readable(), alert_iterator_filter_trash(), alert_iterator_filter_uuid(), alert_iterator_method(), alert_task_iterator_readable(), asset_os_iterator_installs(), config_iterator_families_growing(), config_iterator_family_count(), config_iterator_nvt_count(), config_iterator_nvts_growing(), config_iterator_type(), config_task_iterator_readable(), credential_iterator_allow_insecure(), credential_scanner_iterator_readable(), credential_target_iterator_readable(), filter_alert_iterator_readable(), host_identifier_iterator_source_orphan(), host_iterator_current_port(), host_iterator_max_port(), host_routes_xml(), migrate_153_to_154(), migrate_160_to_161(), migrate_165_to_166(), note_iterator_active(), note_iterator_end_time(), nvt_iterator_category(), nvt_selector_iterator_include(), nvt_selector_iterator_type(), override_iterator_active(), override_iterator_end_time(), port_list_iterator_count_all(), port_list_iterator_count_tcp(), port_list_iterator_count_udp(), port_list_target_iterator_readable(), port_range_iterator_type(), port_range_iterator_type_int(), report_counts_build_iterator_min_qod(), report_counts_build_iterator_override(), report_format_alert_iterator_readable(), report_format_iterator_trust(), report_format_iterator_trust_time(), report_format_param_iterator_type(), report_format_param_iterator_type_name(), result_iterator_level(), result_iterator_severity(), result_iterator_severity_double(), result_iterator_type(), scanner_iterator_credential_trash(), scanner_iterator_port(), scanner_iterator_type(), scanner_task_iterator_readable(), schedule_info(), schedule_iterator_duration(), schedule_iterator_first_time(), schedule_iterator_initial_offset(), schedule_iterator_next_time(), schedule_iterator_period(), schedule_iterator_period_months(), schedule_task_iterator_readable(), tag_iterator_active(), tag_iterator_orphan(), tag_iterator_resource_location(), target_iterator_alive_tests(), target_iterator_esxi_credential(), target_iterator_esxi_trash(), target_iterator_port_list_trash(), target_iterator_smb_credential(), target_iterator_smb_trash(), target_iterator_snmp_credential(), target_iterator_snmp_trash(), target_iterator_ssh_credential(), target_iterator_ssh_trash(), target_task_iterator_readable(), task_iterator_finished_reports(), task_iterator_run_status(), task_iterator_total_reports(), user_group_iterator_readable(), user_iterator_hosts_allow(), user_iterator_ifaces_allow(), and user_role_iterator_readable().

623 {
624  if (iterator->done) abort ();
625  return sql_column_int (iterator->stmt, col);
626 }
gboolean done
End flag.
Definition: iterator.h:55
int sql_column_int(sql_stmt_t *, int)
Return a column as an integer from a prepared statement.
Definition: sql_pg.c:906
sql_stmt_t * stmt
SQL statement.
Definition: iterator.h:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ iterator_int64()

long long int iterator_int64 ( iterator_t iterator,
int  col 
)

Get an integer column from an iterator.

Parameters
[in]iteratorIterator.
[in]colColumn offset.
Returns
Value of given column.

Definition at line 637 of file sql.c.

References iterator::done, sql_column_int64(), and iterator::stmt.

Referenced by acl_users_with_access_sql(), alert_iterator_filter(), asset_iterator_in_use(), asset_iterator_writable(), auto_delete_reports(), check_generate_scripts(), get_iterator_owner(), get_iterator_resource(), host_routes_xml(), migrate_138_to_139(), migrate_143_to_144(), migrate_153_to_154(), migrate_156_to_157(), migrate_158_to_159(), migrate_160_to_161(), migrate_165_to_166(), migrate_168_to_169(), migrate_16_to_17(), migrate_19_to_20(), migrate_1_to_2(), migrate_21_to_22(), migrate_35_to_36(), migrate_4_to_5_copy_data(), migrate_55_to_56(), migrate_9_to_10(), next_report(), note_iterator_result(), note_iterator_task(), override_iterator_result(), override_iterator_task(), permission_iterator_resource_in_trash(), permission_iterator_resource_orphan(), permission_iterator_subject_in_trash(), refresh_nvt_cves(), report_counts_build_iterator_user(), report_errors_iterator_result(), report_format_iterator_active(), report_format_param_iterator_param(), report_format_param_iterator_type_max(), report_format_param_iterator_type_min(), reports_add_all(), reports_add_for_override(), reports_build_count_cache(), result_iterator_report(), result_iterator_result(), result_iterator_task(), scanner_iterator_credential(), task_alert_iterator_alert(), task_iterator_scanner(), task_schedule_iterator_duration(), task_schedule_iterator_first_time(), task_schedule_iterator_initial_offset(), task_schedule_iterator_next_time(), task_schedule_iterator_period(), task_schedule_iterator_period_months(), task_schedule_iterator_schedule(), task_schedule_iterator_task(), and user_resources_in_use().

638 {
639  if (iterator->done) abort ();
640  return sql_column_int64 (iterator->stmt, col);
641 }
gboolean done
End flag.
Definition: iterator.h:55
long long int sql_column_int64(sql_stmt_t *, int)
Return a column as an int64 from a prepared statement.
Definition: sql_pg.c:936
sql_stmt_t * stmt
SQL statement.
Definition: iterator.h:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ iterator_string()

const char* iterator_string ( iterator_t iterator,
int  col 
)

Get a string column from an iterator.

Parameters
[in]iteratorIterator.
[in]colColumn offset.
Returns
Value of given column.

Definition at line 652 of file sql.c.

References iterator::done, sql_column_text(), and iterator::stmt.

Referenced by acl_users_with_access_sql(), alert_data_iterator_data(), alert_data_iterator_name(), alert_task_iterator_name(), alert_task_iterator_uuid(), auto_delete_reports(), check_db_sequences(), check_generate_scripts(), filter_iterator_type(), get_iterator_comment(), host_routes_xml(), manage_get_scanners(), migrate_122_to_123(), migrate_12_to_13(), migrate_138_to_139(), migrate_143_to_144(), migrate_153_to_154(), migrate_156_to_157(), migrate_158_to_159(), migrate_160_to_161(), migrate_165_to_166(), migrate_16_to_17(), migrate_19_to_20(), migrate_1_to_2(), migrate_21_to_22(), migrate_4_to_5_copy_data(), migrate_55_to_56(), migrate_9_to_10(), note_iterator_threat(), nvt_preference_iterator_config_value(), nvt_preference_iterator_nvt(), nvt_preference_iterator_real_name(), nvt_preference_iterator_type(), ovaldef_cves(), override_iterator_new_threat(), override_iterator_threat(), port_range_iterator_comment(), refresh_nvt_cves(), report_severity(), result_iterator_original_severity(), result_iterator_scan_nvt_version(), result_iterator_severity(), result_iterator_type(), scanner_iterator_key_priv(), task_iterator_first_report(), task_iterator_hosts_ordering(), task_iterator_last_report(), and task_iterator_run_status_name().

653 {
654  if (iterator->done) abort ();
655  return sql_column_text (iterator->stmt, col);
656 }
gboolean done
End flag.
Definition: iterator.h:55
const char * sql_column_text(sql_stmt_t *, int)
Return a column as text from a prepared statement.
Definition: sql_pg.c:887
sql_stmt_t * stmt
SQL statement.
Definition: iterator.h:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ next()

gboolean next ( iterator_t iterator)

Increment an iterator.

Parameters
[in]iteratorIterator.
Returns
TRUE if there was a next item, else FALSE.

Definition at line 689 of file sql.c.

References iterator::crypt_ctx, iterator::done, log_errors, lsc_crypt_flush(), sql_exec_internal(), and iterator::stmt.

Referenced by acl_users_with_access_sql(), auto_delete_reports(), buffer_aggregate_xml(), check_db_sequences(), check_generate_scripts(), create_asset_report(), get_nvti_xml(), get_ovaldi_files(), host_routes_xml(), manage_get_scanners(), manage_schedule(), manage_set_config_nvts(), migrate_122_to_123(), migrate_12_to_13(), migrate_138_to_139(), migrate_143_to_144(), migrate_153_to_154(), migrate_156_to_157(), migrate_158_to_159(), migrate_160_to_161(), migrate_165_to_166(), migrate_168_to_169(), migrate_16_to_17(), migrate_19_to_20(), migrate_1_to_2(), migrate_21_to_22(), migrate_24_to_25(), migrate_35_to_36(), migrate_3_to_4(), migrate_4_to_5_copy_data(), migrate_55_to_56(), migrate_9_to_10(), next_report(), ovaldef_cves(), refresh_nvt_cves(), report_add_result(), report_cache_counts(), report_severity(), reports_add_all(), reports_add_for_override(), reports_build_count_cache(), schedule_info(), sql_rename_column(), stop_active_tasks(), target_port_range(), task_observers(), user_resources_in_use(), verify_agent(), and verify_scanner().

690 {
691  int ret;
692 
693  if (iterator->done) return FALSE;
694 
695  lsc_crypt_flush (iterator->crypt_ctx);
696  while (1)
697  {
698  ret = sql_exec_internal (1, iterator->stmt);
699  if (ret == 0)
700  {
701  iterator->done = TRUE;
702  return FALSE;
703  }
704  if (ret == -1)
705  {
706  if (log_errors)
707  g_warning ("%s: sql_exec_internal failed\n", __FUNCTION__);
708  abort ();
709  }
710  if (ret == -2)
711  {
712  /* Busy or locked, with statement reset. Just try step again like
713  * we used to do in sql_exec_internal. We're not supposed to do this
714  * for SQLite, but it would mean quite a bit of reworking in the
715  * callers to be able to handle this case. */
716  g_warning ("%s: stepping after reset\n", __FUNCTION__);
717  continue;
718  }
719  if (ret == 2)
720  {
721  /* Schema changed, for example an internal change due to a VACUUM.
722  * Retrying will result in the same error, so abort. We lock
723  * exclusively around the VACUUM in --optimize, so hopefully when
724  * using --optimize the schema error will happen earlier, in the
725  * the init function for the iterator.
726  *
727  * This only applies to SQLite3. */
728  g_warning ("%s: schema error.\n"
729  " This is possibly due to running VACUUM while Manager\n"
730  " is running. Restart Manager. In future use\n"
731  " --optimize=vacuum instead of running VACUUM"
732  " directly.\n",
733  __FUNCTION__);
734  abort ();
735  }
736  break;
737  }
738  assert (ret == 1);
739  return TRUE;
740 }
int log_errors
Whether to log errors.
Definition: sql.c:62
lsc_crypt_ctx_t crypt_ctx
Encryption context.
Definition: iterator.h:57
gboolean done
End flag.
Definition: iterator.h:55
int sql_exec_internal(int, sql_stmt_t *)
Execute a prepared statement.
Definition: sql_pg.c:482
void lsc_crypt_flush(lsc_crypt_ctx_t ctx)
Flush an LSC encryption context.
Definition: lsc_crypt.c:586
sql_stmt_t * stmt
SQL statement.
Definition: iterator.h:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql()

void sql ( char *  sql,
  ... 
)

Perform an SQL statement, retrying if database is busy or locked.

Parameters
[in]sqlFormat string for SQL statement.
[in]...Arguments for format string.

Definition at line 199 of file sql.c.

References sqlv().

Referenced by add_role_permission(), add_task_alert(), check_config_discovery(), check_config_host_discovery(), check_config_system_discovery(), check_db_sequences(), cleanup_schedule_times(), cleanup_tables(), clear_duration_schedules(), copy_credential(), copy_target(), create_asset_host(), create_asset_report(), create_port_list_lock(), create_port_range(), create_tables(), create_tag(), delete_asset(), delete_port_range(), delete_report_assets(), delete_report_internal(), delete_task(), host_notice(), hosts_set_details(), hosts_set_identifiers(), hosts_set_max_severity(), init_nvt_iterator(), init_nvt_selector_iterator(), init_preference_iterator(), init_task_file_iterator(), init_user_config_iterator(), insert_port_range(), insert_report_host_detail(), make_config_discovery(), make_config_host_discovery(), make_config_system_discovery(), make_cve_result(), make_nvt_from_nvti(), make_osp_result(), make_report(), make_result(), manage_attach_databases(), manage_complete_nvt_cache_update(), manage_create_scanner(), manage_create_sql_functions(), manage_empty_trashcan(), manage_modify_scanner(), manage_nvt_preference_add(), manage_nvt_preferences_enable(), manage_report_host_add(), manage_session_init(), manage_session_set_timezone(), manage_set_config_comment(), manage_set_config_nvts(), manage_set_ldap_info(), manage_set_radius_info(), manage_task_remove_file(), manage_task_update_file(), migrate_0_to_1(), migrate_100_to_101(), migrate_101_to_102(), migrate_102_to_103(), migrate_103_to_104(), migrate_104_to_105(), migrate_105_to_106(), migrate_106_to_107(), migrate_107_to_108(), migrate_108_to_109(), migrate_109_to_110(), migrate_10_to_11(), migrate_110_to_111(), migrate_111_to_112(), migrate_112_to_113(), migrate_113_to_114(), migrate_114_to_115(), migrate_115_to_116(), migrate_116_to_117(), migrate_117_to_118(), migrate_118_to_119(), migrate_119_to_120(), migrate_11_to_12(), migrate_120_to_121(), migrate_121_to_122(), migrate_122_to_123(), migrate_123_to_124(), migrate_124_to_125(), migrate_125_to_126(), migrate_126_to_127(), migrate_127_to_128(), migrate_128_to_129(), migrate_129_to_130(), migrate_12_to_13(), migrate_130_to_131(), migrate_131_to_132(), migrate_132_to_133(), migrate_133_to_134(), migrate_134_to_135(), migrate_135_to_136(), migrate_136_to_137(), migrate_137_to_138(), migrate_138_to_139(), migrate_139_to_140(), migrate_13_to_14(), migrate_140_to_141(), migrate_141_to_142(), migrate_142_to_143(), migrate_143_to_144(), migrate_144_to_145(), migrate_145_to_146(), migrate_146_to_147(), migrate_148_to_149(), migrate_149_to_150(), migrate_14_to_15(), migrate_153_to_154(), migrate_154_to_155(), migrate_155_to_156(), migrate_156_to_157(), migrate_157_to_158(), migrate_158_to_159(), migrate_159_to_160(), migrate_15_to_16(), migrate_160_to_161(), migrate_161_to_162(), migrate_162_to_163(), migrate_163_to_164(), migrate_164_to_165(), migrate_165_to_166(), migrate_166_to_167(), migrate_167_to_168(), migrate_16_to_17(), migrate_172_to_173(), migrate_173_to_174(), migrate_175_to_176(), migrate_176_to_177(), migrate_177_to_178(), migrate_178_to_179(), migrate_179_to_180(), migrate_179_to_180_update_ref(), migrate_17_to_18(), migrate_17_to_18_set_pref(), migrate_180_to_181(), migrate_182_to_183(), migrate_183_to_184(), migrate_18_to_19(), migrate_19_to_20(), migrate_1_to_2(), migrate_20_to_21(), migrate_21_to_22(), migrate_22_to_23(), migrate_23_to_24(), migrate_25_to_26(), migrate_26_to_27(), migrate_27_to_28(), migrate_28_to_29(), migrate_29_to_30(), migrate_2_to_3(), migrate_30_to_31(), migrate_31_to_32(), migrate_32_to_33(), migrate_33_to_34_set_pref(), migrate_34_to_35(), migrate_35_to_36(), migrate_35_to_36_duplicate_target(), migrate_36_to_37(), migrate_38_to_39(), migrate_39_to_40_set_pref(), migrate_3_to_4(), migrate_40_to_41(), migrate_41_to_42(), migrate_42_to_43(), migrate_44_to_45(), migrate_45_to_46(), migrate_46_to_47(), migrate_47_to_48(), migrate_48_to_49(), migrate_49_to_50(), migrate_4_to_5(), migrate_4_to_5_copy_data(), migrate_50_to_51(), migrate_51_to_52(), migrate_52_to_53(), migrate_53_to_54(), migrate_54_to_55_format(), migrate_55_to_56(), migrate_55_to_56_ensure_predefined_port_lists_exist(), migrate_56_to_57(), migrate_57_to_58(), migrate_58_to_59(), migrate_59_to_60(), migrate_5_to_6_move_other_config(), migrate_60_to_61(), migrate_61_to_62(), migrate_62_to_63(), migrate_63_to_64(), migrate_64_to_65(), migrate_65_to_66(), migrate_66_to_67(), migrate_67_to_68(), migrate_68_to_69(), migrate_69_to_70(), migrate_6_to_7(), migrate_70_to_71(), migrate_71_to_72(), migrate_72_to_73(), migrate_73_to_74(), migrate_74_to_75(), migrate_75_to_76(), migrate_76_to_77(), migrate_77_to_78(), migrate_78_to_79(), migrate_79_to_80_remove_users(), migrate_7_to_8(), migrate_80_to_81(), migrate_82_to_83(), migrate_83_to_84(), migrate_84_to_85(), migrate_85_to_86(), migrate_86_to_87(), migrate_87_to_88(), migrate_88_to_89(), migrate_89_to_90(), migrate_8_to_9(), migrate_90_to_91(), migrate_91_to_92(), migrate_92_to_93(), migrate_93_to_94(), migrate_94_to_95(), migrate_95_to_96(), migrate_96_to_97(), migrate_97_to_98(), migrate_98_to_99(), migrate_99_to_100(), migrate_9_to_10(), modify_asset(), modify_report(), modify_tag(), permissions_set_locations(), permissions_set_orphans(), permissions_set_subjects(), refresh_nvt_cves(), report_add_result(), report_cache_counts(), report_clear_count_cache(), report_host_set_end_time(), report_set_slave_host(), report_set_slave_name(), report_set_slave_port(), report_set_slave_uuid(), report_set_source_iface(), reports_clear_count_cache(), request_delete_task_uuid(), reset_task(), set_credential_auth_algorithm(), set_credential_certificate(), set_credential_comment(), set_credential_data(), set_credential_login(), set_credential_name(), set_credential_password(), set_credential_privacy_algorithm(), set_credential_private_key(), set_credential_snmp_secret(), set_db_version(), set_nvts_feed_version(), set_password(), set_report_format_active(), set_report_format_name(), set_report_format_summary(), set_report_scan_run_status(), set_report_scheduled(), set_report_slave_progress(), set_report_slave_task_uuid(), set_scan_end_time(), set_scan_end_time_epoch(), set_scan_end_time_otp(), set_scan_host_end_time(), set_scan_host_end_time_otp(), set_scan_host_start_time(), set_scan_host_start_time_otp(), set_scan_ports(), set_scan_start_time(), set_scan_start_time_epoch(), set_scan_start_time_otp(), set_task_alerts(), set_task_alterable(), set_task_config(), set_task_end_time(), set_task_end_time_epoch(), set_task_groups(), set_task_hosts_ordering(), set_task_observers(), set_task_parameter(), set_task_preferences(), set_task_scanner(), set_task_schedule(), set_task_schedule_next_time(), set_task_schedule_next_time_uuid(), set_task_schedule_periods(), set_task_schedule_periods_id(), set_task_schedule_uuid(), set_task_start_time(), set_task_start_time_epoch(), set_task_start_time_otp(), set_task_target(), sql_rename_column(), tags_set_locations(), tags_set_orphans(), trim_partial_report(), trim_report(), update_duration_schedule_periods(), and user_ensure_in_db().

200 {
201  while (1)
202  {
203  va_list args;
204  int ret;
205 
206  va_start (args, sql);
207  ret = sqlv (1, sql, args);
208  va_end (args);
209  if (ret == -1)
210  abort ();
211  if (ret == 1)
212  /* Gave up with statement reset. */
213  continue;
214  break;
215  }
216 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
int sqlv(int retry, char *sql, va_list args)
Perform an SQL statement.
Definition: sql.c:158
Here is the call graph for this function:

◆ sql_double()

double sql_double ( char *  sql,
  ... 
)

Get the first value from a SQL query, as a double.

Warning
Aborts on invalid queries.
Aborts when the query returns fewer rows than row. The caller must ensure that the query will return sufficient rows.
Parameters
[in]sqlFormat string for SQL query.
[in]...Arguments for format string.
Returns
Result of the query as an integer.

Definition at line 404 of file sql.c.

References sql_column_double(), sql_finalize(), and sql_x().

Referenced by cpe_highest_cvss(), and report_add_result().

405 {
406  sql_stmt_t* stmt;
407  va_list args;
408  double ret;
409 
410  int sql_x_ret;
411  va_start (args, sql);
412  sql_x_ret = sql_x (sql, args, &stmt);
413  va_end (args);
414  if (sql_x_ret)
415  {
416  sql_finalize (stmt);
417  abort ();
418  }
419  ret = sql_column_double (stmt, 0);
420  sql_finalize (stmt);
421  return ret;
422 }
double sql_column_double(sql_stmt_t *, int)
Return a column as a double from a prepared statement.
Definition: sql_pg.c:868
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
int sql_x(char *sql, va_list args, sql_stmt_t **stmt_return)
Get a particular cell from a SQL query.
Definition: sql.c:385
void sql_finalize(sql_stmt_t *)
Free a prepared statement.
Definition: sql_pg.c:824
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql_error()

int sql_error ( char *  sql,
  ... 
)

Perform an SQL statement, retrying if database is busy or locked.

Return on error, instead of aborting.

Parameters
[in]sqlFormat string for SQL statement.
[in]...Arguments for format string.
Returns
0 success, -1 error.

Definition at line 229 of file sql.c.

References sqlv().

Referenced by manage_attach_databases(), manage_cert_loaded(), and manage_scap_loaded().

230 {
231  int ret;
232 
233  while (1)
234  {
235  va_list args;
236  va_start (args, sql);
237  ret = sqlv (1, sql, args);
238  va_end (args);
239  if (ret == 1)
240  /* Gave up with statement reset. */
241  continue;
242  break;
243  }
244 
245  return ret;
246 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
int sqlv(int retry, char *sql, va_list args)
Perform an SQL statement.
Definition: sql.c:158
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql_exec()

int sql_exec ( sql_stmt_t stmt)

Execute a prepared statement.

Parameters
[in]stmtStatement.
Returns
0 complete, 1 row available in results, 2 rerun prepare, -1 error, -2 gave up with statement reset.

Definition at line 776 of file sql.c.

References sql_exec_internal().

Referenced by migrate_19_to_20().

777 {
778  return sql_exec_internal (1, stmt);
779 }
int sql_exec_internal(int, sql_stmt_t *)
Execute a prepared statement.
Definition: sql_pg.c:482
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql_exec_internal()

int sql_exec_internal ( int  retry,
sql_stmt_t stmt 
)

Execute a prepared statement.

Parameters
[in]retryWhether to keep retrying while database is busy or locked.
[in]stmtStatement.
Returns
0 complete, 1 row available in results, -1 error, -2 gave up.
Parameters
[in]retryWhether to keep retrying while database is busy or locked.
[in]stmtStatement.
Returns
0 complete, 1 row available in results, 2 condition where caller must rerun prepare (for example schema changed internally after VACUUM), -1 error, -2 gave up.

Definition at line 482 of file sql_pg.c.

References BUSY_TIMEOUT, conn, sql_stmt::current_row, sql_stmt::executed, log_errors, openvas_usleep(), sql_stmt::param_formats, sql_stmt::param_lengths, sql_stmt::param_values, sql_stmt::result, sql_stmt::sql, sql_stmt::stmt, and task_db.

Referenced by next(), sql_exec(), sql_explain_internal(), sql_quiet(), and sqlv().

483 {
484  PGresult *result;
485 
486  assert (stmt->sql);
487 
488  if (stmt->executed == 0)
489  {
490  // FIX retry?
491 
492  result = PQexecParams (conn,
493  stmt->sql,
494  stmt->param_values->len,
495  NULL, /* Default param types. */
496  (const char* const*) stmt->param_values->pdata,
497  (const int*) stmt->param_lengths->data,
498  (const int*) stmt->param_formats->data,
499  0); /* Results as text. */
500  if (PQresultStatus (result) != PGRES_TUPLES_OK
501  && PQresultStatus (result) != PGRES_COMMAND_OK)
502  {
503  char *sqlstate;
504 
505  sqlstate = PQresultErrorField (result, PG_DIAG_SQLSTATE);
506  g_debug ("%s: sqlstate: %s\n", __FUNCTION__, sqlstate);
507  if (sqlstate && (strcmp (sqlstate, "57014") == 0)) /* query_canceled */
508  {
509  log_errors = 0;
510  g_debug ("%s: canceled SQL: %s\n", __FUNCTION__, stmt->sql);
511  }
512 
513  if (log_errors)
514  {
515  g_warning ("%s: PQexec failed: %s (%i)\n",
516  __FUNCTION__,
517  PQresultErrorMessage (result),
518  PQresultStatus (result));
519  g_warning ("%s: SQL: %s\n", __FUNCTION__, stmt->sql);
520  }
521 #if 0
522  // FIX ?
523  PQclear (result);
524  PQfinish (conn);
525 #endif
526  return -1;
527  }
528 
529  stmt->result = result;
530  stmt->executed = 1;
531  }
532 
533  if (stmt->current_row < (PQntuples (stmt->result) - 1))
534  {
535  stmt->current_row++;
536  return 1;
537  }
538 
539  return 0;
540 }
int executed
Whether statement has been executed.
Definition: sql_pg.c:61
array_t * param_values
Parameter values.
Definition: sql_pg.c:62
int log_errors
Whether to log errors.
Definition: sql.c:62
GArray * param_lengths
Parameter lengths (int&#39;s).
Definition: sql_pg.c:63
gchar * sql
SQL statement.
Definition: sql_pg.c:58
int current_row
Row position in results.
Definition: sql_pg.c:60
GArray * param_formats
Parameter formats (int&#39;s).
Definition: sql_pg.c:64
PGconn * conn
Handle on the database.
Definition: sql_pg.c:81
PGresult * result
Result set.
Definition: sql_pg.c:59
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql_explain()

int sql_explain ( const char *  sql,
  ... 
)

Write debug messages with the query plan for an SQL query to the log.

Parameters
[in]sqlFormat string for the SQL query.
[in]...Format string arguments.
Returns
0 success, -1 error.

Definition at line 540 of file sql.c.

References sql_explain_internal().

541 {
542  int ret;
543  va_list args;
544  va_start (args, sql);
545  ret = sql_explain_internal (sql, args);
546  va_end (args);
547 
548  return ret;
549 }
int sql_explain_internal(const char *, va_list)
Write debug messages with the query plan for an SQL query to the log.
Definition: sql_pg.c:551
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
Here is the call graph for this function:

◆ sql_explain_internal()

int sql_explain_internal ( const char *  sql,
va_list  args 
)

Write debug messages with the query plan for an SQL query to the log.

Parameters
[in]sqlFormat string for the SQL query.
[in]argsFormat string arguments in a va_list.
Returns
0 success, -1 error.

Definition at line 551 of file sql_pg.c.

References sql_stmt::current_row, log_errors, sql_stmt::result, sql_exec_internal(), sql_finalize(), sql_prepare_internal(), and sql_stmt::stmt.

Referenced by sql_explain().

552 {
553  char *explain_sql;
554  sql_stmt_t *explain_stmt;
555  int explain_ret;
556 
557  explain_sql = g_strconcat ("EXPLAIN ", sql, NULL);
558  if (sql_prepare_internal (1, 1, explain_sql, args, &explain_stmt))
559  {
560  if (log_errors)
561  g_warning ("%s : Failed to prepare EXPLAIN statement", __FUNCTION__);
562  g_free (explain_sql);
563  return -1;
564  }
565 
566  while (1)
567  {
568  explain_ret = sql_exec_internal (1, explain_stmt);
569  if (explain_ret == 1)
570  g_debug ("%s : %s",
571  __FUNCTION__,
572  PQgetvalue (explain_stmt->result,
573  explain_stmt->current_row,
574  0));
575  else if (explain_ret == 0)
576  break;
577  else
578  {
579  g_warning ("%s : Failed to get EXPLAIN row", __FUNCTION__);
580  sql_finalize (explain_stmt);
581  g_free (explain_sql);
582  return -1;
583  }
584  }
585 
586  sql_finalize (explain_stmt);
587  g_free (explain_sql);
588  return 0;
589 }
int sql_prepare_internal(int retry, int log, const char *sql, va_list args, sql_stmt_t **stmt)
Prepare a statement.
Definition: sql_pg.c:458
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
int log_errors
Whether to log errors.
Definition: sql.c:62
void sql_finalize(sql_stmt_t *stmt)
Free a prepared statement.
Definition: sql_pg.c:824
int sql_exec_internal(int retry, sql_stmt_t *stmt)
Execute a prepared statement.
Definition: sql_pg.c:482
int current_row
Row position in results.
Definition: sql_pg.c:60
PGresult * result
Result set.
Definition: sql_pg.c:59
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql_giveup()

int sql_giveup ( char *  sql,
  ... 
)

Perform an SQL statement, giving up if database is busy or locked.

Parameters
[in]sqlFormat string for SQL statement.
[in]...Arguments for format string.
Returns
0 success, 1 gave up, -1 error.

Definition at line 257 of file sql.c.

References sqlv().

Referenced by sql_begin_exclusive_giveup(), and sql_begin_immediate_giveup().

258 {
259  int ret;
260  va_list args;
261 
262  va_start (args, sql);
263  ret = sqlv (0, sql, args);
264  va_end (args);
265  return ret;
266 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
int sqlv(int retry, char *sql, va_list args)
Perform an SQL statement.
Definition: sql.c:158
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql_insert()

gchar* sql_insert ( const char *  string)

Get the SQL insert expression for a string.

Parameters
[in]stringThe string, which may be NULL.
Returns
Freshly allocated expression suitable for an INSERT statement, including SQL quotation marks.

Definition at line 136 of file sql.c.

References sql_quote().

Referenced by create_tag(), migrate_12_to_13(), migrate_4_to_5_copy_data(), migrate_9_to_10(), modify_tag(), and refresh_nvt_cves().

137 {
138  if (string)
139  {
140  gchar *quoted_value = sql_quote (string);
141  gchar *insert = g_strdup_printf ("'%s'", quoted_value);
142  g_free (quoted_value);
143  return insert;
144  }
145  return g_strdup ("NULL");
146 }
gchar * sql_quote(const char *string)
Quotes a string to be passed to sql statements.
Definition: sql.c:121
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql_int()

int sql_int ( char *  sql,
  ... 
)

Get a particular cell from a SQL query, as an int.

Warning
Aborts on invalid queries.
Aborts when the query returns fewer rows than row. The caller must ensure that the query will return sufficient rows.
Parameters
[in]sqlFormat string for SQL query.
[in]...Arguments for format string.
Returns
Result of the query as an integer.

Definition at line 438 of file sql.c.

References sql_column_int(), sql_finalize(), and sql_x().

Referenced by acl_role_can_super_everyone(), acl_user_can_everything(), acl_user_can_super_everyone(), acl_user_has_access_uuid(), acl_user_has_super(), acl_user_has_super_on(), acl_user_has_super_on_resource(), acl_user_is_admin(), acl_user_is_observer(), acl_user_is_owner(), acl_user_is_user(), acl_user_may(), acl_user_owns(), acl_user_owns_name(), acl_user_owns_trash_uuid(), acl_user_owns_uuid(), alert_in_use(), check_config_host_discovery(), check_config_system_discovery(), config_families_growing(), config_in_use(), config_nvts_growing(), create_note(), create_override(), create_port_list(), create_port_range(), credential_in_use(), delete_asset(), delete_port_range(), delete_report_internal(), delete_task_lock(), family_count(), family_nvt_count(), filter_in_use(), host_notice(), manage_cert_loaded(), manage_create_scanner(), manage_create_sql_functions(), manage_db_empty(), manage_modify_scanner(), manage_report_host_details(), manage_scap_loaded(), manage_set_config_families(), manage_set_config_nvts(), manage_task_remove_file(), manage_task_update_file(), manage_update_nvti_cache(), migrate_128_to_129(), migrate_12_to_13(), migrate_156_to_157(), migrate_158_to_159(), migrate_160_to_161(), migrate_165_to_166(), migrate_168_to_169(), migrate_177_to_178(), migrate_17_to_18(), migrate_17_to_18_set_pref(), migrate_21_to_22(), migrate_33_to_34_set_pref(), migrate_35_to_36(), migrate_55_to_56_ensure_predefined_port_lists_exist(), migrate_5_to_6(), migrate_5_to_6_move_other_config(), modify_scanner(), nvt_preference_count(), nvt_selector_family_count(), nvt_selector_family_growing(), nvt_selector_nvt_count(), permission_is_predefined(), port_list_in_use(), port_list_is_predefined(), port_range_port_list_uuid(), report_add_result(), report_format_in_use(), report_format_owner_uuid(), report_format_trust(), report_host_count(), report_host_dead(), report_host_result_count(), report_result_host_count(), report_scan_run_status(), report_slave_port_int(), report_slave_progress(), report_timestamp(), request_delete_task(), request_delete_task_uuid(), resource_exists(), resource_id_exists(), resource_tag_count(), role_is_predefined(), scan_host_end_time(), scan_start_time_epoch(), scanner_in_use(), schedule_duration(), schedule_in_use(), schedule_period(), set_credential_data(), set_scan_host_end_time(), set_scan_host_end_time_otp(), set_scan_host_start_time(), set_scan_host_start_time_otp(), set_task_preferences(), setting_auto_cache_rebuild_int(), setting_value_int(), sql_begin_exclusive_giveup(), sql_last_insert_id(), sql_uniquify(), target_alive_tests(), target_credential(), target_in_use(), target_login_port(), task_alterable(), task_average_scan_duration(), task_config_in_trash(), task_finished_report_count(), task_in_trash(), task_iterator_current_report(), task_result_count(), task_run_status(), task_running_report(), task_scanner_in_trash(), task_schedule_in_trash(), task_schedule_next_time(), task_schedule_next_time_uuid(), task_schedule_periods(), task_schedule_periods_uuid(), task_target_in_trash(), task_writable(), total_asset_count(), trash_alert_in_use(), trash_config_in_use(), trash_credential_in_use(), trash_filter_in_use(), trash_id_exists(), trash_port_list_in_use(), trash_report_format_in_use(), trash_scanner_in_use(), trash_schedule_in_use(), trash_target_credential(), trash_target_credential_location(), trash_target_in_use(), trash_target_login_port(), trash_task_writable(), user_ensure_in_db(), user_hosts_allow(), user_ifaces_allow(), and user_is_super_admin().

439 {
440  sql_stmt_t* stmt;
441  va_list args;
442  int ret;
443 
444  int sql_x_ret;
445  va_start (args, sql);
446  sql_x_ret = sql_x (sql, args, &stmt);
447  va_end (args);
448  if (sql_x_ret)
449  {
450  sql_finalize (stmt);
451  abort ();
452  }
453  ret = sql_column_int (stmt, 0);
454  sql_finalize (stmt);
455  return ret;
456 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
int sql_column_int(sql_stmt_t *, int)
Return a column as an integer from a prepared statement.
Definition: sql_pg.c:906
int sql_x(char *sql, va_list args, sql_stmt_t **stmt_return)
Get a particular cell from a SQL query.
Definition: sql.c:385
void sql_finalize(sql_stmt_t *)
Free a prepared statement.
Definition: sql_pg.c:824
Here is the call graph for this function:

◆ sql_int64()

int sql_int64 ( long long int *  ret,
char *  sql,
  ... 
)

Get a particular cell from a SQL query, as an int64.

Parameters
[in]retReturn value.
[in]sqlFormat string for SQL query.
[in]...Arguments for format string.
Returns
0 success, 1 too few rows, -1 error.

Definition at line 501 of file sql.c.

References sql_column_int64(), sql_finalize(), and sql_x().

Referenced by acl_user_has_access_uuid(), check_db_sequences(), config_scanner(), delete_asset(), delete_report_internal(), find_config(), find_nvt(), find_permission_with_permission(), find_port_list(), find_port_range_with_permission(), find_resource(), find_resource_by_name_with_permission(), find_resource_with_permission(), find_result_with_permission(), find_task(), host_nthlast_report_host(), hosts_set_identifiers(), lookup_report_format(), migrate_173_to_174(), migrate_177_to_178(), modify_asset(), permission_subject(), report_add_result(), report_format_active(), report_host_noticeable(), report_task(), reports_add_for_override(), reschedule_task(), set_report_format_param(), target_credential(), target_port_list(), task_config(), task_last_report(), task_last_stopped_report(), task_scanner(), task_schedule(), task_schedule_uuid(), task_severity_double(), task_target(), and trash_target_credential().

502 {
503  sql_stmt_t* stmt;
504  int sql_x_ret;
505  va_list args;
506 
507  va_start (args, sql);
508  sql_x_ret = sql_x (sql, args, &stmt);
509  va_end (args);
510  switch (sql_x_ret)
511  {
512  case 0:
513  break;
514  case 1:
515  sql_finalize (stmt);
516  return 1;
517  break;
518  default:
519  assert (0);
520  /* Fall through. */
521  case -1:
522  sql_finalize (stmt);
523  return -1;
524  break;
525  }
526  *ret = sql_column_int64 (stmt, 0);
527  sql_finalize (stmt);
528  return 0;
529 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
long long int sql_column_int64(sql_stmt_t *, int)
Return a column as an int64 from a prepared statement.
Definition: sql_pg.c:936
int sql_x(char *sql, va_list args, sql_stmt_t **stmt_return)
Get a particular cell from a SQL query.
Definition: sql.c:385
void sql_finalize(sql_stmt_t *)
Free a prepared statement.
Definition: sql_pg.c:824
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql_nquote()

gchar* sql_nquote ( const char *  string,
size_t  length 
)

Quotes a string of a known length to be passed to sql statements.

Parameters
[in]stringString to quote.
[in]lengthSize of string.
Returns
Freshly allocated, quoted string. Free with g_free.

Definition at line 76 of file sql.c.

Referenced by init_task_file_iterator(), set_task_parameter(), and sql_quote().

77 {
78  gchar *new, *new_start;
79  const gchar *start, *end;
80  int count = 0;
81 
82  assert (string);
83 
84  /* Count number of apostrophes. */
85 
86  start = string;
87  while ((start = strchr (start, '\''))) start++, count++;
88 
89  /* Allocate new string. */
90 
91  new = new_start = g_malloc0 (length + count + 1);
92 
93  /* Copy string, replacing apostrophes with double apostrophes. */
94 
95  start = string;
96  end = string + length;
97  for (; start < end; start++, new++)
98  {
99  char ch = *start;
100  if (ch == '\'')
101  {
102  *new = '\'';
103  new++;
104  *new = '\'';
105  }
106  else
107  *new = ch;
108  }
109 
110  return new_start;
111 }
Here is the caller graph for this function:

◆ sql_prepare()

sql_stmt_t* sql_prepare ( const char *  sql,
  ... 
)

Prepare a statement.

Parameters
[in]sqlFormat string for SQL.
Returns
Statement on success, NULL on error.

Definition at line 753 of file sql.c.

References sql_prepare_internal().

Referenced by init_prognosis_iterator(), migrate_19_to_20(), report_severity_data_prepare(), and report_severity_data_prepare_full().

754 {
755  int ret;
756  sql_stmt_t* stmt;
757  va_list args;
758 
759  va_start (args, sql);
760  ret = sql_prepare_internal (1, 1, sql, args, &stmt);
761  va_end (args);
762  if (ret)
763  return NULL;
764  return stmt;
765 }
int sql_prepare_internal(int, int, const char *, va_list, sql_stmt_t **)
Prepare a statement.
Definition: sql_pg.c:458
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql_prepare_internal()

int sql_prepare_internal ( int  retry,
int  log,
const char *  sql,
va_list  args,
sql_stmt_t **  stmt 
)

Prepare a statement.

Parameters
[in]retryWhether to keep retrying while database is busy or locked.
[in]logWhether to keep retrying while database is busy or locked.
[in]sqlFormat string for SQL statement.
[in]argsArguments for format string.
[out]stmtStatement return.
Returns
0 success, 1 gave up, -1 error.

Definition at line 458 of file sql_pg.c.

References BUSY_TIMEOUT, openvas_usleep(), and task_db.

Referenced by init_iterator(), sql_explain_internal(), sql_prepare(), sql_quiet(), and sqlv().

460 {
461  assert (stmt);
462 
463  *stmt = (sql_stmt_t*) g_malloc (sizeof (sql_stmt_t));
464  sql_stmt_init (*stmt);
465  (*stmt)->sql = g_strdup_vprintf (sql, args);
466 
467  if (log)
468  g_debug (" sql: %s\n", (*stmt)->sql);
469 
470  return 0;
471 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sql_quiet()

void sql_quiet ( char *  sql,
  ... 
)

Perform an SQL statement, without logging.

Parameters
[in]sqlFormat string for SQL statement.
[in]...Arguments for format string.

Definition at line 275 of file sql.c.

References log_errors, sql_exec_internal(), sql_finalize(), and sql_prepare_internal().

276 {
277  int ret;
278  sql_stmt_t *stmt;
279  va_list args;
280 
281  /* Prepare statement. */
282 
283  while (1)
284  {
285  va_start (args, sql);
286  ret = sql_prepare_internal (1, 0, sql, args, &stmt);
287  va_end (args);
288  if (ret)
289  {
290  g_warning ("%s: sql_prepare failed\n", __FUNCTION__);
291  abort ();
292  }
293 
294  /* Run statement. */
295 
296  while ((ret = sql_exec_internal (1, stmt)) == 1);
297  if (ret == -1)
298  {
299  if (log_errors)
300  g_warning ("%s: sql_exec_internal failed\n", __FUNCTION__);
301  abort ();
302  }
303  if (ret == -2 || ret == 2)
304  {
305  /* Busy or locked, with statement reset. Or schema changed. */
306  sql_finalize (stmt);
307  continue;
308  }
309  break;
310  }
311  sql_finalize (stmt);
312 }
int sql_prepare_internal(int, int, const char *, va_list, sql_stmt_t **)
Prepare a statement.
Definition: sql_pg.c:458
int log_errors
Whether to log errors.
Definition: sql.c:62
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
int sql_exec_internal(int, sql_stmt_t *)
Execute a prepared statement.
Definition: sql_pg.c:482
void sql_finalize(sql_stmt_t *)
Free a prepared statement.
Definition: sql_pg.c:824
Here is the call graph for this function:

◆ sql_quote()

gchar* sql_quote ( const char *  string)

Quotes a string to be passed to sql statements.

Parameters
[in]stringString to quote, has to be \0 terminated.
Returns
Freshly allocated, quoted string. Free with g_free.

Definition at line 121 of file sql.c.

References sql_nquote().

Referenced by acl_role_can_super_everyone(), acl_user_can_everything(), acl_user_can_super_everyone(), acl_user_has_access_uuid(), acl_user_has_super(), acl_user_has_super_on(), acl_user_is_admin(), acl_user_is_observer(), acl_user_is_user(), acl_user_may(), acl_user_owns_name(), acl_user_owns_trash_uuid(), acl_user_owns_uuid(), acl_where_owned_user(), alert_data(), app_location(), cpe_highest_cvss(), create_asset_host(), create_asset_report(), create_config(), create_filter(), create_note(), create_override(), create_port_list(), create_port_list_lock(), create_port_list_unique(), create_port_range(), cve_cvss_base(), delete_asset(), delete_report_assets(), family_nvt_count(), filter_term(), find_config(), find_permission_with_permission(), find_port_list(), find_port_range_with_permission(), find_resource(), find_resource_by_name_with_permission(), find_resource_with_permission(), find_result_with_permission(), host_notice(), host_nthlast_report_host(), hosts_set_identifiers(), init_cert_bund_adv_info_iterator(), init_cpe_cve_iterator(), init_cpe_info_iterator(), init_cve_info_iterator(), init_dfn_cert_adv_info_iterator(), init_family_iterator(), init_nvt_info_iterator(), init_nvt_preference_iterator(), init_nvt_selector_iterator(), init_otp_pref_iterator(), init_ovaldef_info_iterator(), insert_report_host_detail(), lookup_report_format(), make_cve_result(), make_nvt_from_nvti(), make_osp_result(), make_result(), manage_create_scanner(), manage_modify_scanner(), manage_nvt_preference_add(), manage_report_host_add(), manage_set_config_comment(), manage_set_config_nvts(), manage_set_ldap_info(), manage_set_radius_info(), manage_task_remove_file(), manage_task_update_file(), manage_user_hash(), migrate_129_to_130(), migrate_138_to_139(), migrate_143_to_144(), migrate_156_to_157(), migrate_158_to_159(), migrate_160_to_161(), migrate_165_to_166(), migrate_35_to_36(), migrate_35_to_36_duplicate_target(), migrate_3_to_4(), migrate_55_to_56(), migrate_5_to_6_move_other_config(), modify_asset(), modify_report(), nvt_oid(), nvt_preference_count(), nvt_preference_iterator_config_value(), nvt_selector_family_growing(), nvt_selector_nvt_count(), ovaldef_cves(), ovaldef_severity(), ovaldef_uuid(), ovaldef_version(), port_range_port_list_uuid(), report_host_ip(), report_set_slave_host(), report_set_slave_name(), report_set_slave_uuid(), report_set_source_iface(), result_detection_reference(), result_host_asset_id(), scan_end_time_uuid(), scan_host_end_time(), scan_start_time_uuid(), set_credential_comment(), set_credential_data(), set_credential_name(), set_nvts_feed_version(), set_report_format_name(), set_report_format_param(), set_report_format_summary(), set_report_slave_task_uuid(), set_scan_host_end_time(), set_scan_host_end_time_otp(), set_scan_host_start_time(), set_scan_host_start_time_otp(), set_task_hosts_ordering(), set_task_parameter(), set_task_preferences(), set_task_schedule_next_time_uuid(), set_task_schedule_periods(), set_task_schedule_uuid(), setting_value_int(), sql_cpe_title(), sql_credential_value(), sql_insert(), sql_uniquify(), target_credential(), target_login_port(), task_preference_value(), task_schedule_next_time_uuid(), task_schedule_periods_uuid(), task_schedule_uuid(), trash_target_credential(), trash_target_credential_location(), trash_target_login_port(), user_ensure_in_db(), user_hosts(), user_hosts_allow(), user_ifaces(), user_ifaces_allow(), user_is_super_admin(), user_name(), and user_uuid_method().

122 {
123  assert (string);
124  return sql_nquote (string, strlen (string));
125 }
gchar * sql_nquote(const char *string, size_t length)
Quotes a string of a known length to be passed to sql statements.
Definition: sql.c:76
Here is the call graph for this function:

◆ sql_string()

char* sql_string ( char *  sql,
  ... 
)

Get a particular cell from a SQL query, as an string.

Parameters
[in]sqlFormat string for SQL query.
[in]...Arguments for format string.
Returns
Freshly allocated string containing the result, NULL otherwise. NULL means that either the selected value was NULL or there were no rows in the result.

Definition at line 469 of file sql.c.

References sql_column_text(), sql_finalize(), and sql_x().

Referenced by agent_uuid(), alert_data(), alert_filter_id(), alert_name(), alert_owner_uuid(), alert_uuid(), app_location(), check_db_versions(), config_nvt_selector(), config_nvt_timeout(), config_type(), config_uuid(), credential_encrypted_value(), credential_name(), credential_type(), credential_uuid(), credential_value(), cve_cvss_base(), delete_asset(), delete_user(), filter_name(), filter_term(), filter_uuid(), find_resource_by_name_with_permission(), get_ovaldef_short_filename(), group_users(), group_uuid(), host_uuid(), init_report_counts_build_iterator(), make_result(), manage_cert_db_version(), manage_db_version(), manage_default_ca_cert(), manage_nvt_name(), manage_port_name(), manage_scap_db_version(), manage_user_hash(), migrate_165_to_166(), migrate_168_to_169(), migrate_21_to_22(), migrate_35_to_36(), migrate_5_to_6_move_other_config(), modify_user(), note_uuid(), nvt_oid(), nvt_preference_iterator_config_value(), nvt_severity(), nvts_feed_version(), ovaldef_severity(), ovaldef_uuid(), ovaldef_version(), override_uuid(), permission_name(), permission_resource_id(), permission_resource_type(), permission_subject_id(), permission_subject_type(), permission_uuid(), port_list_uuid(), port_range_port_list_uuid(), port_range_uuid(), report_add_result(), report_cache_counts(), report_format_content_type(), report_format_extension(), report_format_name(), report_format_owner_uuid(), report_format_uuid(), report_host_ip(), report_slave_host(), report_slave_name(), report_slave_port(), report_slave_task_uuid(), report_slave_uuid(), report_source_iface(), report_uuid(), reports_add_for_override(), resource_name(), resource_uuid(), result_detection_reference(), result_host_asset_id(), result_uuid(), role_users(), role_uuid(), scan_end_time(), scan_end_time_uuid(), scan_start_time(), scan_start_time_uuid(), scanner_ca_pub(), scanner_host(), scanner_key_priv(), scanner_key_pub(), scanner_login(), scanner_name(), scanner_password(), scanner_port(), scanner_type(), scanner_uuid(), schedule_name(), schedule_uuid(), setting_filter(), setting_timezone(), sql_cpe_title(), sql_credential_value(), tag_uuid(), target_exclude_hosts(), target_hosts(), target_name(), target_reverse_lookup_only(), target_reverse_lookup_unify(), target_uuid(), task_comment(), task_config_name(), task_config_uuid(), task_definitions_file(), task_hosts_ordering(), task_name(), task_owner_name(), task_preference_value(), task_second_last_report_id(), task_uuid(), trash_credential_name(), trash_credential_uuid(), trash_filter_name(), trash_filter_uuid(), trash_target_exclude_hosts(), trash_target_hosts(), trash_target_name(), trash_target_uuid(), user_hosts(), user_ifaces(), user_name(), user_uuid(), and user_uuid_method().

470 {
471  sql_stmt_t* stmt;
472  const char* ret2;
473  char* ret;
474  int sql_x_ret;
475 
476  va_list args;
477  va_start (args, sql);
478  sql_x_ret = sql_x (sql, args, &stmt);
479  va_end (args);
480  if (sql_x_ret)
481  {
482  sql_finalize (stmt);
483  return NULL;
484  }
485  ret2 = sql_column_text (stmt, 0);
486  ret = g_strdup (ret2);
487  sql_finalize (stmt);
488  return ret;
489 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
const char * sql_column_text(sql_stmt_t *, int)
Return a column as text from a prepared statement.
Definition: sql_pg.c:887
int sql_x(char *sql, va_list args, sql_stmt_t **stmt_return)
Get a particular cell from a SQL query.
Definition: sql.c:385
void sql_finalize(sql_stmt_t *)
Free a prepared statement.
Definition: sql_pg.c:824
Here is the call graph for this function:

◆ sql_x()

int sql_x ( char *  sql,
va_list  args,
sql_stmt_t **  stmt_return 
)

Get a particular cell from a SQL query.

Do logging as usual.

Parameters
[in]sqlFormat string for SQL query.
[in]argsArguments for format string.
[out]stmt_returnReturn from statement.
Returns
0 success, 1 too few rows, -1 error.

Definition at line 385 of file sql.c.

Referenced by sql_double(), sql_int(), sql_int64(), sql_string(), and sqli().

386 {
387  return sql_x_internal (1, sql, args, stmt_return);
388 }
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
Here is the caller graph for this function:

◆ sqlv()

int sqlv ( int  retry,
char *  sql,
va_list  args 
)

Perform an SQL statement.

Parameters
[in]retryWhether to keep retrying while database is busy or locked.
[in]sqlFormat string for SQL statement.
[in]argsArguments for format string.
Returns
0 success, 1 gave up (even when retry given), -1 error.

Definition at line 158 of file sql.c.

References log_errors, sql_exec_internal(), sql_finalize(), and sql_prepare_internal().

Referenced by sql(), sql_error(), sql_giveup(), and sqli().

159 {
160  while (1)
161  {
162  int ret;
163  sql_stmt_t* stmt;
164  va_list args_copy;
165 
166  /* Prepare statement.
167  * Copy args for this because a va_list can only be used once.
168  */
169  va_copy (args_copy, args);
170  ret = sql_prepare_internal (retry, 1, sql, args_copy, &stmt);
171  va_end (args_copy);
172  if (ret == -1)
173  g_warning ("%s: sql_prepare_internal failed\n", __FUNCTION__);
174  if (ret)
175  return ret;
176 
177  /* Run statement. */
178 
179  while ((ret = sql_exec_internal (retry, stmt)) == 1);
180  if ((ret == -1) && log_errors)
181  g_warning ("%s: sql_exec_internal failed\n", __FUNCTION__);
182  sql_finalize (stmt);
183  if (ret == 2)
184  continue;
185  if (ret == -2)
186  return 1;
187  assert (ret == -1 || ret == 0);
188  return ret;
189  }
190 }
int sql_prepare_internal(int, int, const char *, va_list, sql_stmt_t **)
Prepare a statement.
Definition: sql_pg.c:458
int log_errors
Whether to log errors.
Definition: sql.c:62
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
int sql_exec_internal(int, sql_stmt_t *)
Execute a prepared statement.
Definition: sql_pg.c:482
void sql_finalize(sql_stmt_t *)
Free a prepared statement.
Definition: sql_pg.c:824
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ log_errors

int log_errors = 1

Whether to log errors.

Used to turn off logging when cancelling statements on exit.

Definition at line 62 of file sql.c.

Referenced by next(), sql_cancel_internal(), sql_exec_internal(), sql_explain_internal(), sql_quiet(), and sqlv().