Greenbone Vulnerability Manager  22.4.0~dev1
manage_sql.h
1 /* Copyright (C) 2010-2022 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: AGPL-3.0-or-later
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as
7  * published by the Free Software Foundation, either version 3 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /*
20  * @file manage_sql.h
21  * @brief Manager Manage library: SQL backend headers.
22  */
23 
24 #ifndef _GVMD_MANAGE_SQL_H
25 #define _GVMD_MANAGE_SQL_H
26 
27 #include <gvm/util/xmlutils.h>
28 
29 #include "manage.h"
30 #include "manage_utils.h"
31 
32 ␌
33 /* Internal types and preprocessor definitions. */
34 
38 #define LOCATION_TABLE 0
39 
43 #define LOCATION_TRASH 1
44 
48 #define MANAGE_NVT_SELECTOR_UUID_ALL "54b45713-d4f4-4435-b20d-304c175ed8c5"
49 
53 #define PERMISSION_UUID_ADMIN_EVERYTHING "b3b56a8c-c2fd-11e2-a135-406186ea4fc5"
54 
58 #define PERMISSION_UUID_SUPER_ADMIN_EVERYTHING "a9801074-6fe2-11e4-9d81-406186ea4fc5"
59 
63 #define ROLE_UUID_ADMIN "7a8cb5b4-b74d-11e2-8187-406186ea4fc5"
64 
68 #define ROLE_UUID_GUEST "cc9cac5e-39a3-11e4-abae-406186ea4fc5"
69 
73 #define ROLE_UUID_INFO "5f8fd16c-c550-11e3-b6ab-406186ea4fc5"
74 
78 #define ROLE_UUID_MONITOR "12cdb536-480b-11e4-8552-406186ea4fc5"
79 
83 #define ROLE_UUID_USER "8d453140-b74d-11e2-b0be-406186ea4fc5"
84 
88 #define ROLE_UUID_SUPER_ADMIN "9c5a6ec6-6fe2-11e4-8cb6-406186ea4fc5"
89 
93 #define ROLE_UUID_OBSERVER "87a7ebce-b74d-11e2-a81f-406186ea4fc5"
94 
98 #define SCANNER_UUID_DEFAULT "08b69003-5fc2-4037-a479-93b440211c73"
99 
103 #define SCANNER_UUID_CVE "6acd0832-df90-11e4-b9d5-28d24461215b"
104 
108 #define SETTING_UUID_ROWS_PER_PAGE "5f5a8712-8017-11e1-8556-406186ea4fc5"
109 
113 #define SETTING_UUID_MAX_ROWS_PER_PAGE "76374a7a-0569-11e6-b6da-28d24461215b"
114 
118 #define SETTING_UUID_DEFAULT_CA_CERT "9ac801ea-39f8-11e6-bbaa-28d24461215b"
119 
123 #define SETTING_UUID_LSC_DEB_MAINTAINER "2fcbeac8-4237-438f-b52a-540a23e7af97"
124 
128 #define SETTING_UUID_FEED_IMPORT_OWNER "78eceaec-3385-11ea-b237-28d24461215b"
129 
133 #define SETTING_UUID_FEED_IMPORT_ROLES "ff000362-338f-11ea-9051-28d24461215b"
134 
138 #define TRUST_ERROR 0
139 
143 #define TRUST_YES 1
144 
148 #define TRUST_NO 2
149 
153 #define TRUST_UNKNOWN 3
154 
158 #define TIMEVAL_SUBTRACT_MS(a,b) ((((a).tv_sec - (b).tv_sec) * 1000) + \
159  ((a).tv_usec - (b).tv_usec) / 1000)
160 
164 #define DB_SUPERUSER_ROLE "dba"
165 
166 ␌
167 /* Macros. */
168 
178 #define DEF_ACCESS(name, col) \
179 const char* \
180 name (iterator_t* iterator) \
181 { \
182  const char *ret; \
183  if (iterator->done) return NULL; \
184  ret = iterator_string (iterator, col); \
185  return ret; \
186 }
187 
195 #define PRINT(stream, format, args...) \
196  do \
197  { \
198  gchar *msg; \
199  msg = g_markup_printf_escaped (format, ## args); \
200  if (fprintf (stream, "%s", msg) < 0) \
201  { \
202  g_free (msg); \
203  fclose (stream); \
204  return -1; \
205  } \
206  g_free (msg); \
207  } \
208  while (0)
209 
210 ␌
211 /* Iterator definitions. */
212 
216 typedef struct
217 {
218  gchar *select;
219  gchar *filter;
220  keyword_type_t type;
221 } column_t;
222 
226 #define ANON_GET_ITERATOR_FILTER_COLUMNS "uuid", \
227  "created", "modified", "_owner"
228 
232 #define GET_ITERATOR_FILTER_COLUMNS "uuid", "name", "comment", \
233  "created", "modified", "_owner"
234 
240 #define GET_ITERATOR_COLUMNS_STRING \
241  "id, uuid, name, comment, iso_time (creation_time)," \
242  " iso_time (modification_time), creation_time AS created," \
243  " modification_time AS modified"
244 
250 #define GET_ITERATOR_COLUMNS_PREFIX(prefix) \
251  { prefix "id", NULL, KEYWORD_TYPE_INTEGER }, \
252  { prefix "uuid", NULL, KEYWORD_TYPE_STRING }, \
253  { prefix "name", NULL, KEYWORD_TYPE_STRING }, \
254  { prefix "comment", NULL, KEYWORD_TYPE_STRING }, \
255  { " iso_time (" prefix "creation_time)", NULL, KEYWORD_TYPE_STRING }, \
256  { " iso_time (" prefix "modification_time)", NULL, KEYWORD_TYPE_STRING }, \
257  { prefix "creation_time", "created", KEYWORD_TYPE_INTEGER }, \
258  { prefix "modification_time", "modified", KEYWORD_TYPE_INTEGER }
259 
265 #define GET_ITERATOR_COLUMNS(table) \
266  GET_ITERATOR_COLUMNS_PREFIX(""), \
267  { \
268  "(SELECT name FROM users AS inner_users" \
269  " WHERE inner_users.id = " G_STRINGIFY (table) ".owner)", \
270  "_owner", \
271  KEYWORD_TYPE_STRING \
272  }, \
273  { "owner", NULL, KEYWORD_TYPE_INTEGER }
274 
278 #define GET_ITERATOR_COLUMN_COUNT 10
279 
280 ␌
281 /* Variables */
282 
284 
285 ␌
286 /* Function prototypes */
287 
288 typedef long long int rowid_t;
289 
290 int manage_db_empty ();
291 
292 gboolean
293 host_nthlast_report_host (const char *, report_host_t *, int);
294 
295 char*
296 report_host_ip (const char *);
297 
298 void trim_report (report_t);
299 
300 int delete_report_internal (report_t);
301 
302 int set_report_scan_run_status (report_t, task_status_t);
303 
304 int update_report_modification_time (report_t);
305 
306 int set_report_slave_progress (report_t, int);
307 
308 void init_task_file_iterator (iterator_t *, task_t, const char *);
309 const char *task_file_iterator_name (iterator_t *);
310 const char *task_file_iterator_content (iterator_t *);
311 
312 void set_task_schedule_next_time (task_t, time_t);
313 
314 void set_task_schedule_next_time_uuid (const gchar *, time_t);
315 
316 void init_preference_iterator (iterator_t *, config_t, const char *);
317 const char *preference_iterator_name (iterator_t *);
318 const char *preference_iterator_value (iterator_t *);
319 
320 port_list_t target_port_list (target_t);
321 credential_t target_ssh_credential (target_t);
322 credential_t target_smb_credential (target_t);
323 credential_t target_esxi_credential (target_t);
324 credential_t target_ssh_elevate_credential (target_t);
325 
326 int create_current_report (task_t, char **, task_status_t);
327 
328 char *alert_data (alert_t, const char *, const char *);
329 
331 
333 
335 
336 const char *task_schedule_iterator_task_uuid (iterator_t *);
337 
338 schedule_t task_schedule_iterator_schedule (iterator_t *);
339 
340 const char *task_schedule_iterator_icalendar (iterator_t *);
341 
342 const char *task_schedule_iterator_timezone (iterator_t *);
343 
344 const char *task_schedule_iterator_owner_uuid (iterator_t *);
345 
346 const char *task_schedule_iterator_owner_name (iterator_t *);
347 
349 
351 
353 
354 time_t task_schedule_iterator_initial_offset (iterator_t *);
355 
356 int set_task_schedule_uuid (const gchar*, schedule_t, int);
357 
358 void reinit_manage_process ();
359 
361 
362 int manage_report_host_details (report_t, const char *, entity_t);
363 
364 const char *run_status_name_internal (task_status_t);
365 
366 void update_config_cache_init (const char *);
367 
368 alive_test_t target_alive_tests (target_t);
369 
370 void manage_session_init (const char *);
371 
372 int valid_gmp_command (const char *);
373 
374 void check_generate_scripts ();
375 
376 void auto_delete_reports ();
377 
378 int parse_iso_time (const char *);
379 
380 void set_report_scheduled (report_t);
381 
382 gchar *resource_uuid (const gchar *, resource_t);
383 
384 gboolean find_resource_with_permission (const char *, const char *,
385  resource_t *, const char *, int);
386 
387 int
388 resource_predefined (const gchar *, resource_t);
389 
390 void parse_osp_report (task_t, report_t, const char *);
391 
392 void reschedule_task (const gchar *);
393 
394 void insert_port_range (port_list_t, port_protocol_t, int, int);
395 
396 int manage_cert_db_exists ();
397 
398 int manage_scap_db_exists ();
399 
400 int
401 count (const char *, const get_data_t *, column_t *, column_t *, const char **,
402  int, const char *, const char *, int);
403 
404 int
405 init_get_iterator (iterator_t*, const char *, const get_data_t *, column_t *,
406  column_t *, const char **, int, const char *, const char *,
407  int);
408 
409 gchar *
411 
412 gchar *
413 filter_clause (const char*, const char*, const char **, column_t *,
414  column_t *, int, gchar **, int *, int *, array_t **, gchar **);
415 
416 void
417 check_alerts ();
418 
419 int
420 manage_option_setup (GSList *, const db_conn_info_t *);
421 
422 void
424 
425 void
427 
428 void
429 event (event_t, void *, resource_t, resource_t);
430 
431 gboolean
432 find_trash (const char *, const char *, resource_t *);
433 
434 void
435 tags_remove_resource (const char *, resource_t, int);
436 
437 void
438 tags_set_locations (const char *, resource_t, resource_t, int);
439 
440 void
441 permissions_set_locations (const char *, resource_t, resource_t, int);
442 
443 void
444 permissions_set_orphans (const char *, resource_t, int);
445 
446 int
447 copy_resource (const char *, const char *, const char *, const char *,
448  const char *, int, resource_t *, resource_t *);
449 
450 gboolean
451 resource_with_name_exists (const char *, const char *, resource_t);
452 
453 int
454 create_permission_internal (int, const char *, const char *, const char *,
455  const char *, const char *, const char *,
456  permission_t *);
457 
458 int
459 create_permission_no_acl (const char *, const char *, const char *, const char *,
460  const char *, const char *, permission_t *);
461 
462 int
463 copy_resource_lock (const char *, const char *, const char *, const char *,
464  const char *, int, resource_t *, resource_t *);
465 
466 nvti_t *
467 lookup_nvti (const gchar *);
468 
469 int
470 setting_value (const char *, char **);
471 
472 int
473 valid_type (const char *);
474 
475 void
476 add_role_permission_resource (const gchar *, const gchar *, const gchar *,
477  const gchar *);
478 
479 void
481 
482 int
483 config_family_entire_and_growing (config_t, const char*);
484 
485 void
487 
488 int
490 
491 int
493 
494 int
496 
497 int
498 cleanup_ids_for_table (const char *);
499 
500 #endif /* not _GVMD_MANAGE_SQL_H */
long long int resource_t
A resource, like a task or target.
Definition: iterator.h:39
const char * run_status_name_internal(task_status_t status)
Get the unique name of a run status.
Definition: manage.c:1552
void create_view_vulns()
Create or replace the vulns view.
Definition: manage_pg.c:1689
int manage_cert_db_exists()
Check if CERT db exists.
Definition: manage_pg.c:104
int manage_scap_db_exists()
Check if SCAP db exists.
Definition: manage_pg.c:119
int manage_db_empty()
Check whether database is empty.
Definition: manage_pg.c:84
void manage_session_init(const char *uuid)
Setup session.
Definition: manage_pg.c:55
int init_get_iterator(iterator_t *iterator, const char *type, const get_data_t *get, column_t *select_columns, column_t *trash_select_columns, const char **filter_columns, int distinct, const char *extra_tables, const char *extra_where, int owned)
Initialise a GET iterator, including observed resources.
Definition: manage_sql.c:5067
int manage_report_host_details(report_t report, const char *ip, entity_t entity)
Add host details to a report host.
Definition: manage_sql.c:47755
void set_task_schedule_next_time(task_t task, time_t time)
Set the next time a scheduled task will be due.
Definition: manage_sql.c:18484
alive_test_t target_alive_tests(target_t target)
Return a target's alive tests.
Definition: manage_sql.c:32612
credential_t target_ssh_elevate_credential(target_t target)
Return the ELEVATE credential associated with a target, if any.
Definition: manage_sql.c:32508
gchar * resource_uuid(const gchar *type, resource_t resource)
Get the UUID of a resource.
Definition: manage_sql.c:4717
void set_task_schedule_next_time_uuid(const gchar *task_id, time_t time)
Set the next time a scheduled task will be due.
Definition: manage_sql.c:18497
int valid_type(const char *type)
Check whether a resource type name is valid.
Definition: manage_sql.c:3900
void auto_delete_reports()
Auto delete reports.
Definition: manage_sql.c:18779
int create_permission_internal(int check_access, const char *name_arg, const char *comment, const char *resource_type_arg, const char *resource_id_arg, const char *subject_type, const char *subject_id, permission_t *permission)
Create a permission.
Definition: manage_sql.c:42497
void manage_option_cleanup()
Cleanup for an option process.
Definition: manage_sql.c:963
void check_alerts()
Check if any SecInfo alerts are due.
Definition: manage_sql.c:6340
void set_report_scheduled(report_t report)
Set a report's scheduled flag.
Definition: manage_sql.c:17651
void reschedule_task(const gchar *task_id)
Set a task's schedule so that it runs again next scheduling round.
Definition: manage_sql.c:18888
gboolean task_schedule_iterator_stop_due(iterator_t *iterator)
Get the stop due state from a task schedule iterator.
Definition: manage_sql.c:41040
credential_t target_esxi_credential(target_t target)
Return the ESXi credential associated with a target, if any.
Definition: manage_sql.c:32495
int copy_resource_lock(const char *type, const char *name, const char *comment, const char *resource_id, const char *columns, int make_name_unique, resource_t *new_resource, resource_t *old_resource)
Create a resource from an existing resource.
Definition: manage_sql.c:4346
void event(event_t event, void *event_data, resource_t resource_1, resource_t resource_2)
Produce an event.
Definition: manage_sql.c:14398
void reports_clear_count_cache_dynamic()
Clear all report counts for all dynamic severity users.
Definition: manage_sql.c:19904
gboolean task_schedule_iterator_timed_out(iterator_t *iterator)
Get if schedule of task in iterator is timed out.
Definition: manage_sql.c:41088
int delete_report_internal(report_t report)
Delete a report.
Definition: manage_sql.c:24363
int manage_option_setup(GSList *log_config, const db_conn_info_t *database)
Setup for an option process.
Definition: manage_sql.c:926
int count(const char *type, const get_data_t *get, column_t *select_columns, column_t *trash_select_columns, const char **filter_columns, int distinct, const char *extra_tables, const char *extra_where, int owned)
Count number of a particular resource.
Definition: manage_sql.c:5813
int set_report_scan_run_status(report_t report, task_status_t status)
Return the run status of the scan associated with a report.
Definition: manage_sql.c:23805
void reinit_manage_process()
Reinitialize the manage library for a process.
Definition: manage_sql.c:15284
char * alert_data(alert_t alert, const char *type, const char *name)
Return data associated with an alert.
Definition: manage_sql.c:8453
int set_report_slave_progress(report_t report, int progress)
Set slave progress of a report.
Definition: manage_sql.c:24557
int create_permission_no_acl(const char *name_arg, const char *comment, const char *resource_type_arg, const char *resource_id_arg, const char *subject_type, const char *subject_id, permission_t *permission)
Create a permission.
Definition: manage_sql.c:42659
gboolean find_resource_with_permission(const char *type, const char *uuid, resource_t *resource, const char *permission, int trash)
Find a resource given a UUID and a permission.
Definition: manage_sql.c:4176
void permissions_set_orphans(const char *type, resource_t resource, int location)
Set permissions to orphan.
Definition: manage_sql.c:42136
void cleanup_task_schedule_iterator(iterator_t *iterator)
Cleanup a task schedule iterator.
Definition: manage_sql.c:40903
void permissions_set_locations(const char *type, resource_t old, resource_t new, int to)
Adjust location of resource in permissions.
Definition: manage_sql.c:42107
gchar * report_host_ip(const char *host)
Get the IP of a host, using the 'hostname' report host details.
Definition: manage_sql.c:28769
void init_task_file_iterator(iterator_t *iterator, task_t task, const char *file)
Initialise a task file iterator.
Definition: manage_sql.c:29980
int init_task_schedule_iterator(iterator_t *iterator)
Initialise a task schedule iterator.
Definition: manage_sql.c:40871
gchar * filter_clause(const char *type, const char *filter, const char **filter_columns, column_t *select_columns, column_t *where_columns, int trash, gchar **order_return, int *first_return, int *max_return, array_t **permissions, gchar **owner_filter)
Return SQL WHERE clause for restricting a SELECT to a filter term.
Definition: manage_sql.c:2846
gboolean resource_with_name_exists(const char *name, const char *type, resource_t resource)
Check if a resource with a certain name exists already.
Definition: manage_sql.c:649
gboolean host_nthlast_report_host(const char *host, report_host_t *report_host, int position)
Get N'th last report_host given a host.
Definition: manage_sql.c:25640
int create_current_report(task_t task, char **report_id, task_status_t status)
Create the current report for a task.
Definition: manage_sql.c:20215
void add_role_permission_resource(const gchar *role_id, const gchar *permission, const gchar *type, const gchar *resource_id)
Add resource permission to role.
Definition: manage_sql.c:15596
credential_t target_smb_credential(target_t target)
Return the SMB credential associated with a target, if any.
Definition: manage_sql.c:32482
nvti_t * lookup_nvti(const gchar *nvt)
Update the memory cache of NVTs.
Definition: manage_sql.c:15298
int parse_iso_time(const char *text_time)
Convert an ISO time into seconds since epoch.
Definition: manage_sql.c:790
void trim_report(report_t report)
Prepare a partial report for restarting the scan from the beginning.
Definition: manage_sql.c:24571
int copy_resource(const char *type, const char *name, const char *comment, const char *resource_id, const char *columns, int make_name_unique, resource_t *new_resource, resource_t *old_resource)
Create a resource from an existing resource.
Definition: manage_sql.c:4548
port_list_t target_port_list(target_t target)
Return the port list associated with a target, if any.
Definition: manage_sql.c:32521
gchar * columns_build_select(column_t *select_columns)
Return column list for SELECT statement.
Definition: manage_sql.c:2522
int update_report_modification_time(report_t report)
Update modification_time of a report to current time.
Definition: manage_sql.c:23824
gboolean find_trash(const char *type, const char *uuid, resource_t *resource)
Find a resource in the trashcan given a UUID.
Definition: manage_sql.c:744
gboolean task_schedule_iterator_start_due(iterator_t *iterator)
Get the start due state from a task schedule iterator.
Definition: manage_sql.c:41008
void parse_osp_report(task_t task, report_t report, const char *report_xml)
Parse an OSP report.
Definition: manage_sql.c:28815
credential_t target_ssh_credential(target_t target)
Return the SSH credential associated with a target, if any.
Definition: manage_sql.c:32469
int valid_gmp_command(const char *name)
Check whether a command name is valid.
Definition: manage_sql.c:573
int setting_value(const char *uuid, char **value)
Get the value of a setting as a string.
Definition: manage_sql.c:49500
db_conn_info_t gvmd_db_conn_info
Name of the database file.
Definition: manage_sql.c:426
int set_task_schedule_uuid(const gchar *task_id, schedule_t schedule, int periods)
Set the schedule of a task.
Definition: manage_sql.c:18253
task_t task_schedule_iterator_task(iterator_t *iterator)
Get the task from a task schedule iterator.
Definition: manage_sql.c:40917
int manage_update_nvti_cache()
Update the memory cache of NVTs, if this has been requested.
Definition: manage_sql.c:15362
int config_family_entire_and_growing(config_t config, const char *family)
Get whether a config selects every NVT in a given family.
Definition: manage_sql_configs.c:3743
void init_preference_iterator(iterator_t *iterator, config_t config, const char *section)
Initialise a config preference iterator, with defaults.
Definition: manage_sql_configs.c:3425
int cleanup_config_sequences()
Cleans up scan config related id sequences likely to run out.
Definition: manage_sql_configs.c:4535
void update_all_config_caches()
Update count and growing info in every config across all users.
Definition: manage_sql_configs.c:4267
void update_config_cache_init(const char *uuid)
Update count and growing info in config, without checking user.
Definition: manage_sql_configs.c:4291
int cleanup_nvt_sequences()
Cleans up NVT related id sequences likely to run out.
Definition: manage_sql_nvts.c:2279
int cleanup_port_list_sequences()
Cleans up port list related id sequences likely to run out.
Definition: manage_sql_port_lists.c:2626
void insert_port_range(port_list_t port_list, port_protocol_t type, int start, int end)
Create a port range.
Definition: manage_sql_port_lists.c:1761
int resource_predefined(const gchar *type, resource_t resource)
Return whether a resource is predefined.
Definition: manage_sql_report_formats.c:143
Iterator column.
Definition: manage_sql.h:217
gchar * select
Column for SELECT.
Definition: manage_sql.h:218
gchar * filter
Filter column name. NULL to use select_column.
Definition: manage_sql.h:219
keyword_type_t type
Type of column.
Definition: manage_sql.h:220
Data structure for info used to connect to the database.
Definition: manage.h:47
Command data for a get command.
Definition: manage_get.h:35
A generic SQL iterator structure.
Definition: iterator.h:50