Greenbone Vulnerability Manager 22.4.1
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) \
179const char* \
180name (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
216typedef 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
288typedef long long int rowid_t;
289
290int manage_db_empty ();
291
292gboolean
293host_nthlast_report_host (const char *, report_host_t *, int);
294
295char*
296report_host_ip (const char *);
297
298void trim_report (report_t);
299
300int delete_report_internal (report_t);
301
302int set_report_scan_run_status (report_t, task_status_t);
303
305
306int set_report_slave_progress (report_t, int);
307
308void init_task_file_iterator (iterator_t *, task_t, const char *);
309const char *task_file_iterator_name (iterator_t *);
310const char *task_file_iterator_content (iterator_t *);
311
312void set_task_schedule_next_time (task_t, time_t);
313
314void set_task_schedule_next_time_uuid (const gchar *, time_t);
315
316void init_preference_iterator (iterator_t *, config_t, const char *);
317const char *preference_iterator_name (iterator_t *);
318const char *preference_iterator_value (iterator_t *);
319
320port_list_t target_port_list (target_t);
321credential_t target_ssh_credential (target_t);
322credential_t target_smb_credential (target_t);
323credential_t target_esxi_credential (target_t);
324credential_t target_ssh_elevate_credential (target_t);
325
326int create_current_report (task_t, char **, task_status_t);
327
328char *alert_data (alert_t, const char *, const char *);
329
331
333
335
336const char *task_schedule_iterator_task_uuid (iterator_t *);
337
338schedule_t task_schedule_iterator_schedule (iterator_t *);
339
340const char *task_schedule_iterator_icalendar (iterator_t *);
341
342const char *task_schedule_iterator_timezone (iterator_t *);
343
344const char *task_schedule_iterator_owner_uuid (iterator_t *);
345
346const char *task_schedule_iterator_owner_name (iterator_t *);
347
349
351
353
354time_t task_schedule_iterator_initial_offset (iterator_t *);
355
356int set_task_schedule_uuid (const gchar*, schedule_t, int);
357
359
361
362int manage_report_host_details (report_t, const char *, entity_t);
363
364const char *run_status_name_internal (task_status_t);
365
366void update_config_cache_init (const char *);
367
368alive_test_t target_alive_tests (target_t);
369
370void manage_session_init (const char *);
371
372int valid_gmp_command (const char *);
373
374void check_generate_scripts ();
375
376void auto_delete_reports ();
377
378int parse_iso_time (const char *);
379
380void set_report_scheduled (report_t);
381
382gchar *resource_uuid (const gchar *, resource_t);
383
384gboolean find_resource_with_permission (const char *, const char *,
385 resource_t *, const char *, int);
386
387int
388resource_predefined (const gchar *, resource_t);
389
390void parse_osp_report (task_t, report_t, const char *);
391
392void reschedule_task (const gchar *);
393
394void insert_port_range (port_list_t, port_protocol_t, int, int);
395
397
399
400int
401count (const char *, const get_data_t *, column_t *, column_t *, const char **,
402 int, const char *, const char *, int);
403
404int
405init_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
409gchar *
411
412gchar *
413filter_clause (const char*, const char*, const char **, column_t *,
414 column_t *, int, gchar **, int *, int *, array_t **, gchar **);
415
416void
417check_alerts ();
418
419int
420manage_option_setup (GSList *, const db_conn_info_t *);
421
422void
424
425void
426update_all_config_caches ();
427
428void
429event (event_t, void *, resource_t, resource_t);
430
431gboolean
432find_trash (const char *, const char *, resource_t *);
433
434void
435tags_remove_resource (const char *, resource_t, int);
436
437void
438tags_set_locations (const char *, resource_t, resource_t, int);
439
440void
442
443void
444permissions_set_orphans (const char *, resource_t, int);
445
446int
447copy_resource (const char *, const char *, const char *, const char *,
448 const char *, int, resource_t *, resource_t *);
449
450gboolean
451resource_with_name_exists (const char *, const char *, resource_t);
452
453int
454create_permission_internal (int, const char *, const char *, const char *,
455 const char *, const char *, const char *,
456 permission_t *);
457
458int
459create_permission_no_acl (const char *, const char *, const char *, const char *,
460 const char *, const char *, permission_t *);
461
462int
463copy_resource_lock (const char *, const char *, const char *, const char *,
464 const char *, int, resource_t *, resource_t *);
465
466nvti_t *
467lookup_nvti (const gchar *);
468
469int
470setting_value (const char *, char **);
471
472int
473valid_type (const char *);
474
475void
476add_role_permission_resource (const gchar *, const gchar *, const gchar *,
477 const gchar *);
478
479void
481
482int
483config_family_entire_and_growing (config_t, const char*);
484
485void
487
488int
489cleanup_config_sequences ();
490
491int
492cleanup_port_list_sequences ();
493
494int
495cleanup_nvt_sequences ();
496
497int
498cleanup_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:1711
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:47772
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:18501
alive_test_t target_alive_tests(target_t target)
Return a target's alive tests.
Definition: manage_sql.c:32629
credential_t target_ssh_elevate_credential(target_t target)
Return the ELEVATE credential associated with a target, if any.
Definition: manage_sql.c:32525
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:18514
gchar * columns_build_select(column_t *select_columns)
Return column list for SELECT statement.
Definition: manage_sql.c:2522
int valid_type(const char *type)
Check whether a resource type name is valid.
Definition: manage_sql.c:3900
void tags_remove_resource(const char *type, resource_t resource, int location)
Remove a resource from tags.
Definition: manage_sql.c:55253
void auto_delete_reports()
Auto delete reports.
Definition: manage_sql.c:18796
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:42514
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
gchar * report_host_ip(const char *host)
Get the IP of a host, using the 'hostname' report host details.
Definition: manage_sql.c:28786
void set_report_scheduled(report_t report)
Set a report's scheduled flag.
Definition: manage_sql.c:17668
void reschedule_task(const gchar *task_id)
Set a task's schedule so that it runs again next scheduling round.
Definition: manage_sql.c:18905
gboolean task_schedule_iterator_stop_due(iterator_t *iterator)
Get the stop due state from a task schedule iterator.
Definition: manage_sql.c:41057
credential_t target_esxi_credential(target_t target)
Return the ESXi credential associated with a target, if any.
Definition: manage_sql.c:32512
int cleanup_ids_for_table(const char *table)
Resets the id sequence for a table and assigns new ids to all rows.
Definition: manage_sql.c:55987
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:19921
gboolean task_schedule_iterator_timed_out(iterator_t *iterator)
Get if schedule of task in iterator is timed out.
Definition: manage_sql.c:41105
int delete_report_internal(report_t report)
Delete a report.
Definition: manage_sql.c:24380
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:23822
void reinit_manage_process()
Reinitialize the manage library for a process.
Definition: manage_sql.c:15301
int set_report_slave_progress(report_t report, int progress)
Set slave progress of a report.
Definition: manage_sql.c:24574
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:42676
nvti_t * lookup_nvti(const gchar *nvt)
Update the memory cache of NVTs.
Definition: manage_sql.c:15315
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:42153
void tags_set_locations(const char *type, resource_t old, resource_t new, int to)
Adjust location of resource in tags.
Definition: manage_sql.c:55272
char * alert_data(alert_t alert, const char *type, const char *name)
Return data associated with an alert.
Definition: manage_sql.c:8453
void cleanup_task_schedule_iterator(iterator_t *iterator)
Cleanup a task schedule iterator.
Definition: manage_sql.c:40920
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:42124
void init_task_file_iterator(iterator_t *iterator, task_t task, const char *file)
Initialise a task file iterator.
Definition: manage_sql.c:29997
int init_task_schedule_iterator(iterator_t *iterator)
Initialise a task schedule iterator.
Definition: manage_sql.c:40888
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:25657
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:20232
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:15613
credential_t target_smb_credential(target_t target)
Return the SMB credential associated with a target, if any.
Definition: manage_sql.c:32499
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:24588
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:32538
int update_report_modification_time(report_t report)
Update modification_time of a report to current time.
Definition: manage_sql.c:23841
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:41025
void parse_osp_report(task_t task, report_t report, const char *report_xml)
Parse an OSP report.
Definition: manage_sql.c:28832
credential_t target_ssh_credential(target_t target)
Return the SSH credential associated with a target, if any.
Definition: manage_sql.c:32486
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:49517
db_conn_info_t gvmd_db_conn_info
Name of the database file.
Definition: manage_sql.c:426
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
int set_task_schedule_uuid(const gchar *task_id, schedule_t schedule, int periods)
Set the schedule of a task.
Definition: manage_sql.c:18270
task_t task_schedule_iterator_task(iterator_t *iterator)
Get the task from a task schedule iterator.
Definition: manage_sql.c:40934
int manage_update_nvti_cache()
Update the memory cache of NVTs, if this has been requested.
Definition: manage_sql.c:15379
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