Greenbone Vulnerability Manager  21.4.5
manage_sql.h
1 /* Copyright (C) 2010-2021 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 
161 ␌
162 /* Macros. */
163 
173 #define DEF_ACCESS(name, col) \
174 const char* \
175 name (iterator_t* iterator) \
176 { \
177  const char *ret; \
178  if (iterator->done) return NULL; \
179  ret = iterator_string (iterator, col); \
180  return ret; \
181 }
182 
190 #define PRINT(stream, format, args...) \
191  do \
192  { \
193  gchar *msg; \
194  msg = g_markup_printf_escaped (format, ## args); \
195  if (fprintf (stream, "%s", msg) < 0) \
196  { \
197  g_free (msg); \
198  fclose (stream); \
199  return -1; \
200  } \
201  g_free (msg); \
202  } \
203  while (0)
204 
205 ␌
206 /* Iterator definitions. */
207 
211 typedef struct
212 {
213  gchar *select;
214  gchar *filter;
215  keyword_type_t type;
216 } column_t;
217 
221 #define ANON_GET_ITERATOR_FILTER_COLUMNS "uuid", \
222  "created", "modified", "_owner"
223 
227 #define GET_ITERATOR_FILTER_COLUMNS "uuid", "name", "comment", \
228  "created", "modified", "_owner"
229 
235 #define GET_ITERATOR_COLUMNS_STRING \
236  "id, uuid, name, comment, iso_time (creation_time)," \
237  " iso_time (modification_time), creation_time AS created," \
238  " modification_time AS modified"
239 
245 #define GET_ITERATOR_COLUMNS_PREFIX(prefix) \
246  { prefix "id", NULL, KEYWORD_TYPE_INTEGER }, \
247  { prefix "uuid", NULL, KEYWORD_TYPE_STRING }, \
248  { prefix "name", NULL, KEYWORD_TYPE_STRING }, \
249  { prefix "comment", NULL, KEYWORD_TYPE_STRING }, \
250  { " iso_time (" prefix "creation_time)", NULL, KEYWORD_TYPE_STRING }, \
251  { " iso_time (" prefix "modification_time)", NULL, KEYWORD_TYPE_STRING }, \
252  { prefix "creation_time", "created", KEYWORD_TYPE_INTEGER }, \
253  { prefix "modification_time", "modified", KEYWORD_TYPE_INTEGER }
254 
260 #define GET_ITERATOR_COLUMNS(table) \
261  GET_ITERATOR_COLUMNS_PREFIX(""), \
262  { \
263  "(SELECT name FROM users AS inner_users" \
264  " WHERE inner_users.id = " G_STRINGIFY (table) ".owner)", \
265  "_owner", \
266  KEYWORD_TYPE_STRING \
267  }, \
268  { "owner", NULL, KEYWORD_TYPE_INTEGER }
269 
273 #define GET_ITERATOR_COLUMN_COUNT 10
274 
275 ␌
276 /* Variables */
277 
279 
280 ␌
281 /* Function prototypes */
282 
283 typedef long long int rowid_t;
284 
285 int manage_db_empty ();
286 
287 gboolean
288 host_nthlast_report_host (const char *, report_host_t *, int);
289 
290 char*
291 report_host_ip (const char *);
292 
293 void trim_report (report_t);
294 
295 int delete_report_internal (report_t);
296 
297 int set_report_scan_run_status (report_t, task_status_t);
298 
299 int set_report_slave_progress (report_t, int);
300 
301 void init_task_file_iterator (iterator_t *, task_t, const char *);
302 const char *task_file_iterator_name (iterator_t *);
303 const char *task_file_iterator_content (iterator_t *);
304 
305 void set_task_schedule_next_time (task_t, time_t);
306 
307 void set_task_schedule_next_time_uuid (const gchar *, time_t);
308 
309 void init_preference_iterator (iterator_t *, config_t, const char *);
310 const char *preference_iterator_name (iterator_t *);
311 const char *preference_iterator_value (iterator_t *);
312 
313 port_list_t target_port_list (target_t);
314 credential_t target_ssh_credential (target_t);
315 credential_t target_smb_credential (target_t);
316 credential_t target_esxi_credential (target_t);
317 credential_t target_ssh_elevate_credential (target_t);
318 
319 int create_current_report (task_t, char **, task_status_t);
320 
321 char *alert_data (alert_t, const char *, const char *);
322 
324 
326 
328 
329 const char *task_schedule_iterator_task_uuid (iterator_t *);
330 
331 schedule_t task_schedule_iterator_schedule (iterator_t *);
332 
333 const char *task_schedule_iterator_icalendar (iterator_t *);
334 
335 const char *task_schedule_iterator_timezone (iterator_t *);
336 
337 const char *task_schedule_iterator_owner_uuid (iterator_t *);
338 
339 const char *task_schedule_iterator_owner_name (iterator_t *);
340 
342 
344 
346 
347 time_t task_schedule_iterator_initial_offset (iterator_t *);
348 
349 int set_task_schedule_uuid (const gchar*, schedule_t, int);
350 
351 void reinit_manage_process ();
352 
354 
355 int manage_report_host_details (report_t, const char *, entity_t);
356 
357 const char *run_status_name_internal (task_status_t);
358 
359 gchar *get_ovaldef_short_filename (char*);
360 
361 void update_config_cache_init (const char *);
362 
363 alive_test_t target_alive_tests (target_t);
364 
365 void manage_session_init (const char *);
366 
367 int valid_gmp_command (const char *);
368 
369 void check_generate_scripts ();
370 
371 void auto_delete_reports ();
372 
373 int parse_iso_time (const char *);
374 
375 void set_report_scheduled (report_t);
376 
377 gchar *resource_uuid (const gchar *, resource_t);
378 
379 gboolean find_resource_with_permission (const char *, const char *,
380  resource_t *, const char *, int);
381 
382 int
383 resource_predefined (const gchar *, resource_t);
384 
385 void parse_osp_report (task_t, report_t, const char *);
386 
387 void reschedule_task (const gchar *);
388 
389 void insert_port_range (port_list_t, port_protocol_t, int, int);
390 
391 int manage_cert_db_exists ();
392 
393 int manage_scap_db_exists ();
394 
395 int
396 count (const char *, const get_data_t *, column_t *, column_t *, const char **,
397  int, const char *, const char *, int);
398 
399 int
400 init_get_iterator (iterator_t*, const char *, const get_data_t *, column_t *,
401  column_t *, const char **, int, const char *, const char *,
402  int);
403 
404 gchar *
406 
407 gchar *
408 filter_clause (const char*, const char*, const char **, column_t *,
409  column_t *, int, gchar **, int *, int *, array_t **, gchar **);
410 
411 void
412 check_alerts ();
413 
414 int
415 manage_option_setup (GSList *, const db_conn_info_t *);
416 
417 void
419 
420 void
422 
423 void
424 event (event_t, void *, resource_t, resource_t);
425 
426 gboolean
427 find_trash (const char *, const char *, resource_t *);
428 
429 void
430 tags_remove_resource (const char *, resource_t, int);
431 
432 void
433 tags_set_locations (const char *, resource_t, resource_t, int);
434 
435 void
436 permissions_set_locations (const char *, resource_t, resource_t, int);
437 
438 void
439 permissions_set_orphans (const char *, resource_t, int);
440 
441 int
442 copy_resource (const char *, const char *, const char *, const char *,
443  const char *, int, resource_t *, resource_t *);
444 
445 gboolean
446 resource_with_name_exists (const char *, const char *, resource_t);
447 
448 int
449 create_permission_internal (int, const char *, const char *, const char *,
450  const char *, const char *, const char *,
451  permission_t *);
452 
453 int
454 create_permission_no_acl (const char *, const char *, const char *, const char *,
455  const char *, const char *, permission_t *);
456 
457 int
458 copy_resource_lock (const char *, const char *, const char *, const char *,
459  const char *, int, resource_t *, resource_t *);
460 
461 nvti_t *
462 lookup_nvti (const gchar *);
463 
464 int
465 setting_value (const char *, char **);
466 
467 int
468 valid_type (const char *);
469 
470 void
471 add_role_permission_resource (const gchar *, const gchar *, const gchar *,
472  const gchar *);
473 
474 void
476 
477 int
478 config_family_entire_and_growing (config_t, const char*);
479 
480 void
482 
483 #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:1536
void create_view_vulns()
Create or replace the vulns view.
Definition: manage_pg.c:1712
int manage_cert_db_exists()
Check if CERT db exists.
Definition: manage_pg.c:109
int manage_scap_db_exists()
Check if SCAP db exists.
Definition: manage_pg.c:124
int manage_db_empty()
Check whether database is empty.
Definition: manage_pg.c:89
void manage_session_init(const char *uuid)
Setup session.
Definition: manage_pg.c:60
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:47991
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:18470
alive_test_t target_alive_tests(target_t target)
Return a target's alive tests.
Definition: manage_sql.c:32640
credential_t target_ssh_elevate_credential(target_t target)
Return the ELEVATE credential associated with a target, if any.
Definition: manage_sql.c:32536
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:18483
int valid_type(const char *type)
Check whether a resource type name is valid.
Definition: manage_sql.c:3899
void auto_delete_reports()
Auto delete reports.
Definition: manage_sql.c:18765
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:42726
void manage_option_cleanup()
Cleanup for an option process.
Definition: manage_sql.c:962
void check_alerts()
Check if any SecInfo alerts are due.
Definition: manage_sql.c:6341
void set_report_scheduled(report_t report)
Set a report's scheduled flag.
Definition: manage_sql.c:17641
void reschedule_task(const gchar *task_id)
Set a task's schedule so that it runs again next scheduling round.
Definition: manage_sql.c:18874
gboolean task_schedule_iterator_stop_due(iterator_t *iterator)
Get the stop due state from a task schedule iterator.
Definition: manage_sql.c:41271
credential_t target_esxi_credential(target_t target)
Return the ESXi credential associated with a target, if any.
Definition: manage_sql.c:32523
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:14389
void reports_clear_count_cache_dynamic()
Clear all report counts for all dynamic severity users.
Definition: manage_sql.c:19913
gboolean task_schedule_iterator_timed_out(iterator_t *iterator)
Get if schedule of task in iterator is timed out.
Definition: manage_sql.c:41318
int delete_report_internal(report_t report)
Delete a report.
Definition: manage_sql.c:24361
int manage_option_setup(GSList *log_config, const db_conn_info_t *database)
Setup for an option process.
Definition: manage_sql.c:925
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:5814
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:23820
void reinit_manage_process()
Reinitialize the manage library for a process.
Definition: manage_sql.c:15275
char * alert_data(alert_t alert, const char *type, const char *name)
Return data associated with an alert.
Definition: manage_sql.c:8457
int set_report_slave_progress(report_t report, int progress)
Set slave progress of a report.
Definition: manage_sql.c:24555
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:42888
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:42365
void cleanup_task_schedule_iterator(iterator_t *iterator)
Cleanup a task schedule iterator.
Definition: manage_sql.c:41134
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:42336
gchar * report_host_ip(const char *host)
Get the IP of a host, using the 'hostname' report host details.
Definition: manage_sql.c:28797
void init_task_file_iterator(iterator_t *iterator, task_t task, const char *file)
Initialise a task file iterator.
Definition: manage_sql.c:30008
int init_task_schedule_iterator(iterator_t *iterator)
Initialise a task schedule iterator.
Definition: manage_sql.c:41102
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:2845
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:648
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:25638
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:20224
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:15587
credential_t target_smb_credential(target_t target)
Return the SMB credential associated with a target, if any.
Definition: manage_sql.c:32510
nvti_t * lookup_nvti(const gchar *nvt)
Update the memory cache of NVTs.
Definition: manage_sql.c:15289
int parse_iso_time(const char *text_time)
Convert an ISO time into seconds since epoch.
Definition: manage_sql.c:789
void trim_report(report_t report)
Prepare a partial report for restarting the scan from the beginning.
Definition: manage_sql.c:24569
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:32549
gchar * columns_build_select(column_t *select_columns)
Return column list for SELECT statement.
Definition: manage_sql.c:2521
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:743
gboolean task_schedule_iterator_start_due(iterator_t *iterator)
Get the start due state from a task schedule iterator.
Definition: manage_sql.c:41239
void parse_osp_report(task_t task, report_t report, const char *report_xml)
Parse an OSP report.
Definition: manage_sql.c:28843
credential_t target_ssh_credential(target_t target)
Return the SSH credential associated with a target, if any.
Definition: manage_sql.c:32497
int valid_gmp_command(const char *name)
Check whether a command name is valid.
Definition: manage_sql.c:572
int setting_value(const char *uuid, char **value)
Get the value of a setting as a string.
Definition: manage_sql.c:49747
db_conn_info_t gvmd_db_conn_info
Name of the database file.
Definition: manage_sql.c:427
int set_task_schedule_uuid(const gchar *task_id, schedule_t schedule, int periods)
Set the schedule of a task.
Definition: manage_sql.c:18241
task_t task_schedule_iterator_task(iterator_t *iterator)
Get the task from a task schedule iterator.
Definition: manage_sql.c:41148
int manage_update_nvti_cache()
Update the memory cache of NVTs, if this has been requested.
Definition: manage_sql.c:15353
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:4217
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:3872
void update_all_config_caches()
Update count and growing info in every config across all users.
Definition: manage_sql_configs.c:4744
void update_config_cache_init(const char *uuid)
Update count and growing info in config, without checking user.
Definition: manage_sql_configs.c:4768
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:141
gchar * get_ovaldef_short_filename(char *item_id)
Get the short file name for an OVALDEF.
Definition: manage_sql_secinfo.c:991
Iterator column.
Definition: manage_sql.h:212
gchar * select
Column for SELECT.
Definition: manage_sql.h:213
gchar * filter
Filter column name. NULL to use select_column.
Definition: manage_sql.h:214
keyword_type_t type
Type of column.
Definition: manage_sql.h:215
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