Greenbone Vulnerability Manager 22.5.2
manage_sql.h
1/* Copyright (C) 2010-2022 Greenbone AG
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
298gchar *report_host_hostname (report_host_t);
299
300gchar *report_host_best_os_cpe (report_host_t);
301
302gchar *report_host_best_os_txt (report_host_t);
303
304void trim_report (report_t);
305
306int delete_report_internal (report_t);
307
308int set_report_scan_run_status (report_t, task_status_t);
309
311
312int set_report_slave_progress (report_t, int);
313
314void init_task_file_iterator (iterator_t *, task_t, const char *);
315const char *task_file_iterator_name (iterator_t *);
316const char *task_file_iterator_content (iterator_t *);
317
318void set_task_schedule_next_time (task_t, time_t);
319
320void set_task_schedule_next_time_uuid (const gchar *, time_t);
321
322void init_preference_iterator (iterator_t *, config_t, const char *);
323const char *preference_iterator_name (iterator_t *);
324const char *preference_iterator_value (iterator_t *);
325
326port_list_t target_port_list (target_t);
327credential_t target_ssh_credential (target_t);
328credential_t target_smb_credential (target_t);
329credential_t target_esxi_credential (target_t);
330credential_t target_ssh_elevate_credential (target_t);
331
332int create_current_report (task_t, char **, task_status_t);
333
334char *alert_data (alert_t, const char *, const char *);
335
337
339
341
342const char *task_schedule_iterator_task_uuid (iterator_t *);
343
344schedule_t task_schedule_iterator_schedule (iterator_t *);
345
346const char *task_schedule_iterator_icalendar (iterator_t *);
347
348const char *task_schedule_iterator_timezone (iterator_t *);
349
350const char *task_schedule_iterator_owner_uuid (iterator_t *);
351
352const char *task_schedule_iterator_owner_name (iterator_t *);
353
355
357
359
360time_t task_schedule_iterator_initial_offset (iterator_t *);
361
362int set_task_schedule_uuid (const gchar*, schedule_t, int);
363
365
367
368int manage_report_host_details (report_t, const char *, entity_t);
369
370const char *run_status_name_internal (task_status_t);
371
372void update_config_cache_init (const char *);
373
374alive_test_t target_alive_tests (target_t);
375
376void manage_session_init (const char *);
377
378int valid_gmp_command (const char *);
379
380void check_generate_scripts ();
381
382void auto_delete_reports ();
383
384int parse_iso_time (const char *);
385
386void set_report_scheduled (report_t);
387
388gchar *resource_uuid (const gchar *, resource_t);
389
390gboolean find_resource_with_permission (const char *, const char *,
391 resource_t *, const char *, int);
392
393int
394resource_predefined (const gchar *, resource_t);
395
396void parse_osp_report (task_t, report_t, const char *);
397
398void reschedule_task (const gchar *);
399
400void insert_port_range (port_list_t, port_protocol_t, int, int);
401
403
405
406int
407count (const char *, const get_data_t *, column_t *, column_t *, const char **,
408 int, const char *, const char *, int);
409
410int
411init_get_iterator (iterator_t*, const char *, const get_data_t *, column_t *,
412 column_t *, const char **, int, const char *, const char *,
413 int);
414
415gchar *
417
418gchar *
419filter_clause (const char*, const char*, const char **, column_t *,
420 column_t *, int, gchar **, int *, int *, array_t **, gchar **);
421
422void
423check_alerts ();
424
425int
426manage_option_setup (GSList *, const db_conn_info_t *);
427
428void
430
431void
432update_all_config_caches ();
433
434void
435event (event_t, void *, resource_t, resource_t);
436
437gboolean
438find_trash (const char *, const char *, resource_t *);
439
440void
441tags_remove_resource (const char *, resource_t, int);
442
443void
444tags_set_locations (const char *, resource_t, resource_t, int);
445
446void
448
449void
450permissions_set_orphans (const char *, resource_t, int);
451
452int
453copy_resource (const char *, const char *, const char *, const char *,
454 const char *, int, resource_t *, resource_t *);
455
456gboolean
457resource_with_name_exists (const char *, const char *, resource_t);
458
459int
460create_permission_internal (int, const char *, const char *, const char *,
461 const char *, const char *, const char *,
462 permission_t *);
463
464int
465create_permission_no_acl (const char *, const char *, const char *, const char *,
466 const char *, const char *, permission_t *);
467
468int
469copy_resource_lock (const char *, const char *, const char *, const char *,
470 const char *, int, resource_t *, resource_t *);
471
472nvti_t *
473lookup_nvti (const gchar *);
474
475int
476setting_value (const char *, char **);
477
478int
479valid_type (const char *);
480
481void
482add_role_permission_resource (const gchar *, const gchar *, const gchar *,
483 const gchar *);
484
485void
487
488int
489config_family_entire_and_growing (config_t, const char*);
490
491void
493
494int
495cleanup_config_sequences ();
496
497int
498cleanup_port_list_sequences ();
499
500int
501cleanup_nvt_sequences ();
502
503int
504cleanup_ids_for_table (const char *);
505
506#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:1558
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:5107
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:48199
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:18619
alive_test_t target_alive_tests(target_t target)
Return a target's alive tests.
Definition: manage_sql.c:33005
credential_t target_ssh_elevate_credential(target_t target)
Return the ELEVATE credential associated with a target, if any.
Definition: manage_sql.c:32901
gchar * resource_uuid(const gchar *type, resource_t resource)
Get the UUID of a resource.
Definition: manage_sql.c:4757
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:18632
gchar * columns_build_select(column_t *select_columns)
Return column list for SELECT statement.
Definition: manage_sql.c:2522
gchar * report_host_best_os_txt(report_host_t report_host)
Get the best_os_txt of a report_host.
Definition: manage_sql.c:28977
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:55834
void auto_delete_reports()
Auto delete reports.
Definition: manage_sql.c:18914
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:42937
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:6380
gchar * report_host_ip(const char *host)
Get the IP of a host, using the 'hostname' report host details.
Definition: manage_sql.c:28915
void set_report_scheduled(report_t report)
Set a report's scheduled flag.
Definition: manage_sql.c:17784
void reschedule_task(const gchar *task_id)
Set a task's schedule so that it runs again next scheduling round.
Definition: manage_sql.c:19023
gboolean task_schedule_iterator_stop_due(iterator_t *iterator)
Get the stop due state from a task schedule iterator.
Definition: manage_sql.c:41433
credential_t target_esxi_credential(target_t target)
Return the ESXi credential associated with a target, if any.
Definition: manage_sql.c:32888
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:56568
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:4386
void event(event_t event, void *event_data, resource_t resource_1, resource_t resource_2)
Produce an event.
Definition: manage_sql.c:14460
void reports_clear_count_cache_dynamic()
Clear all report counts for all dynamic severity users.
Definition: manage_sql.c:20047
gboolean task_schedule_iterator_timed_out(iterator_t *iterator)
Get if schedule of task in iterator is timed out.
Definition: manage_sql.c:41481
int delete_report_internal(report_t report)
Delete a report.
Definition: manage_sql.c:24509
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:5853
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:23951
void reinit_manage_process()
Reinitialize the manage library for a process.
Definition: manage_sql.c:15363
int set_report_slave_progress(report_t report, int progress)
Set slave progress of a report.
Definition: manage_sql.c:24703
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:43101
nvti_t * lookup_nvti(const gchar *nvt)
Update the memory cache of NVTs.
Definition: manage_sql.c:15377
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:4216
void permissions_set_orphans(const char *type, resource_t resource, int location)
Set permissions to orphan.
Definition: manage_sql.c:42529
gchar * report_host_best_os_cpe(report_host_t report_host)
Get the best_os_cpe of a report_host.
Definition: manage_sql.c:28958
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:55853
char * alert_data(alert_t alert, const char *type, const char *name)
Return data associated with an alert.
Definition: manage_sql.c:8493
void cleanup_task_schedule_iterator(iterator_t *iterator)
Cleanup a task schedule iterator.
Definition: manage_sql.c:41296
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:42500
void init_task_file_iterator(iterator_t *iterator, task_t task, const char *file)
Initialise a task file iterator.
Definition: manage_sql.c:30358
int init_task_schedule_iterator(iterator_t *iterator)
Initialise a task schedule iterator.
Definition: manage_sql.c:41264
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:25786
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:20358
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:15675
gchar * report_host_hostname(report_host_t report_host)
Get the hostname of a report_host.
Definition: manage_sql.c:28939
credential_t target_smb_credential(target_t target)
Return the SMB credential associated with a target, if any.
Definition: manage_sql.c:32875
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:24717
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:4588
port_list_t target_port_list(target_t target)
Return the port list associated with a target, if any.
Definition: manage_sql.c:32914
int update_report_modification_time(report_t report)
Update modification_time of a report to current time.
Definition: manage_sql.c:23970
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:41401
void parse_osp_report(task_t task, report_t report, const char *report_xml)
Parse an OSP report.
Definition: manage_sql.c:29182
credential_t target_ssh_credential(target_t target)
Return the SSH credential associated with a target, if any.
Definition: manage_sql.c:32862
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:50052
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:18388
task_t task_schedule_iterator_task(iterator_t *iterator)
Get the task from a task schedule iterator.
Definition: manage_sql.c:41310
int manage_update_nvti_cache()
Update the memory cache of NVTs, if this has been requested.
Definition: manage_sql.c:15441
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