Greenbone Vulnerability Manager  9.0.1
manage_sql.h
1 /* Copyright (C) 2010-2018 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the 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 General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 /*
21  * @file manage_sql.h
22  * @brief Manager Manage library: SQL backend headers.
23  */
24 
25 #ifndef _GVMD_MANAGE_SQL_H
26 #define _GVMD_MANAGE_SQL_H
27 
28 #include <gvm/util/xmlutils.h> /* for entity_t */
29 
30 #include "manage.h"
31 #include "manage_utils.h"
32 
33 ␌
34 /* Internal types and preprocessor definitions. */
35 
39 #define CONFIG_UUID_BASE "d21f6c81-2b88-4ac1-b7b4-a2a9f2ad4663"
40 
44 #define CONFIG_UUID_FULL_AND_FAST "daba56c8-73ec-11df-a475-002264764cea"
45 
49 #define CONFIG_UUID_FULL_AND_FAST_ULTIMATE \
50  "698f691e-7489-11df-9d8c-002264764cea"
51 
55 #define CONFIG_UUID_FULL_AND_VERY_DEEP "708f25c4-7489-11df-8094-002264764cea"
56 
60 #define CONFIG_UUID_FULL_AND_VERY_DEEP_ULTIMATE \
61  "74db13d6-7489-11df-91b9-002264764cea"
62 
66 #define CONFIG_UUID_EMPTY "085569ce-73ed-11df-83c3-002264764cea"
67 
71 #define CONFIG_UUID_DISCOVERY "8715c877-47a0-438d-98a3-27c7a6ab2196"
72 
76 #define CONFIG_UUID_HOST_DISCOVERY "2d3f051c-55ba-11e3-bf43-406186ea4fc5"
77 
81 #define CONFIG_UUID_SYSTEM_DISCOVERY "bbca7412-a950-11e3-9109-406186ea4fc5"
82 
86 #define LOCATION_TABLE 0
87 
91 #define LOCATION_TRASH 1
92 
96 #define MANAGE_NVT_SELECTOR_UUID_ALL "54b45713-d4f4-4435-b20d-304c175ed8c5"
97 
101 #define MANAGE_NVT_SELECTOR_UUID_BASE "dd4a4170-0b5e-43fb-9bae-6ce93c19e893"
102 
106 #define MANAGE_NVT_SELECTOR_UUID_DISCOVERY "0d9a2738-8fe2-4e22-8f26-bb886179e759"
107 
111 #define MANAGE_NVT_SELECTOR_UUID_HOST_DISCOVERY \
112  "f5f80744-55c7-11e3-8dc6-406186ea4fc5"
113 
117 #define MANAGE_NVT_SELECTOR_UUID_SYSTEM_DISCOVERY \
118  "07045d1c-a951-11e3-8da7-406186ea4fc5"
119 
123 #define PERMISSION_UUID_ADMIN_EVERYTHING "b3b56a8c-c2fd-11e2-a135-406186ea4fc5"
124 
128 #define PERMISSION_UUID_SUPER_ADMIN_EVERYTHING "a9801074-6fe2-11e4-9d81-406186ea4fc5"
129 
133 #define PORT_LIST_UUID_DEFAULT "c7e03b6c-3bbe-11e1-a057-406186ea4fc5"
134 
138 #define PORT_LIST_UUID_ALL_TCP "fd591a34-56fd-11e1-9f27-406186ea4fc5"
139 
143 #define PORT_LIST_UUID_ALL_TCP_NMAP_5_51_TOP_100 "730ef368-57e2-11e1-a90f-406186ea4fc5"
144 
148 #define PORT_LIST_UUID_ALL_TCP_NMAP_5_51_TOP_1000 "9ddce1ae-57e7-11e1-b13c-406186ea4fc5"
149 
153 #define PORT_LIST_UUID_ALL_PRIV_TCP "492b72f4-56fe-11e1-98a7-406186ea4fc5"
154 
158 #define PORT_LIST_UUID_ALL_PRIV_TCP_UDP "5f2029f6-56fe-11e1-bb94-406186ea4fc5"
159 
163 #define PORT_LIST_UUID_ALL_IANA_TCP_2012 "33d0cd82-57c6-11e1-8ed1-406186ea4fc5"
164 
168 #define PORT_LIST_UUID_ALL_IANA_TCP_UDP_2012 "4a4717fe-57d2-11e1-9a26-406186ea4fc5"
169 
173 #define PORT_LIST_UUID_NMAP_5_51_TOP_2000_TOP_100 "ab33f6b0-57f8-11e1-96f5-406186ea4fc5"
174 
178 #define ROLE_UUID_ADMIN "7a8cb5b4-b74d-11e2-8187-406186ea4fc5"
179 
183 #define ROLE_UUID_GUEST "cc9cac5e-39a3-11e4-abae-406186ea4fc5"
184 
188 #define ROLE_UUID_INFO "5f8fd16c-c550-11e3-b6ab-406186ea4fc5"
189 
193 #define ROLE_UUID_MONITOR "12cdb536-480b-11e4-8552-406186ea4fc5"
194 
198 #define ROLE_UUID_USER "8d453140-b74d-11e2-b0be-406186ea4fc5"
199 
203 #define ROLE_UUID_SUPER_ADMIN "9c5a6ec6-6fe2-11e4-8cb6-406186ea4fc5"
204 
208 #define ROLE_UUID_OBSERVER "87a7ebce-b74d-11e2-a81f-406186ea4fc5"
209 
213 #define SCANNER_UUID_DEFAULT "08b69003-5fc2-4037-a479-93b440211c73"
214 
218 #define SCANNER_UUID_CVE "6acd0832-df90-11e4-b9d5-28d24461215b"
219 
223 #define SETTING_UUID_ROWS_PER_PAGE "5f5a8712-8017-11e1-8556-406186ea4fc5"
224 
228 #define SETTING_UUID_MAX_ROWS_PER_PAGE "76374a7a-0569-11e6-b6da-28d24461215b"
229 
233 #define SETTING_UUID_DEFAULT_CA_CERT "9ac801ea-39f8-11e6-bbaa-28d24461215b"
234 
238 #define SETTING_UUID_SLAVE_CHECK_PERIOD "63adb79a-62ae-11e9-91ba-28d24461215b"
239 
243 #define SETTING_UUID_LSC_DEB_MAINTAINER "2fcbeac8-4237-438f-b52a-540a23e7af97"
244 
248 #define TRUST_ERROR 0
249 
253 #define TRUST_YES 1
254 
258 #define TRUST_NO 2
259 
263 #define TRUST_UNKNOWN 3
264 
268 #define TIMEVAL_SUBTRACT_MS(a,b) ((((a).tv_sec - (b).tv_sec) * 1000) + \
269  ((a).tv_usec - (b).tv_usec) / 1000)
270 
271 ␌
272 /* Macros. */
273 
283 #define DEF_ACCESS(name, col) \
284 const char* \
285 name (iterator_t* iterator) \
286 { \
287  const char *ret; \
288  if (iterator->done) return NULL; \
289  ret = iterator_string (iterator, col); \
290  return ret; \
291 }
292 
293 ␌
294 /* Iterator definitions. */
295 
299 typedef struct
300 {
301  gchar *select;
302  gchar *filter;
303  keyword_type_t type;
304 } column_t;
305 
309 #define ANON_GET_ITERATOR_FILTER_COLUMNS "uuid", \
310  "created", "modified", "_owner"
311 
315 #define GET_ITERATOR_FILTER_COLUMNS "uuid", "name", "comment", \
316  "created", "modified", "_owner"
317 
323 #define GET_ITERATOR_COLUMNS_STRING \
324  "id, uuid, name, comment, iso_time (creation_time)," \
325  " iso_time (modification_time), creation_time AS created," \
326  " modification_time AS modified"
327 
333 #define GET_ITERATOR_COLUMNS_PREFIX(prefix) \
334  { prefix "id", NULL, KEYWORD_TYPE_INTEGER }, \
335  { prefix "uuid", NULL, KEYWORD_TYPE_STRING }, \
336  { prefix "name", NULL, KEYWORD_TYPE_STRING }, \
337  { prefix "comment", NULL, KEYWORD_TYPE_STRING }, \
338  { " iso_time (" prefix "creation_time)", NULL, KEYWORD_TYPE_STRING }, \
339  { " iso_time (" prefix "modification_time)", NULL, KEYWORD_TYPE_STRING }, \
340  { prefix "creation_time", "created", KEYWORD_TYPE_INTEGER }, \
341  { prefix "modification_time", "modified", KEYWORD_TYPE_INTEGER }
342 
348 #define GET_ITERATOR_COLUMNS(table) \
349  GET_ITERATOR_COLUMNS_PREFIX(""), \
350  { \
351  "(SELECT name FROM users AS inner_users" \
352  " WHERE inner_users.id = " G_STRINGIFY (table) ".owner)", \
353  "_owner", \
354  KEYWORD_TYPE_STRING \
355  }, \
356  { "owner", NULL, KEYWORD_TYPE_INTEGER }
357 
361 #define GET_ITERATOR_COLUMN_COUNT 10
362 
363 ␌
364 /* Variables */
365 
366 extern gchar *gvmd_db_name;
367 
368 ␌
369 /* Function prototypes */
370 
371 typedef long long int rowid_t;
372 
373 int manage_db_empty ();
374 
375 gboolean
376 host_nthlast_report_host (const char *, report_host_t *, int);
377 
378 char*
379 report_host_ip (const char *);
380 
381 gchar *tag_value (const gchar *, const gchar *);
382 
383 void trim_report (report_t);
384 
385 int delete_report_internal (report_t);
386 
387 int set_report_scan_run_status (report_t, task_status_t);
388 
389 int set_report_slave_progress (report_t, int);
390 
391 int update_from_slave (task_t, entity_t, entity_t *, int *);
392 
393 void set_report_slave_task_uuid (report_t, const char *);
394 
395 int set_task_requested (task_t, task_status_t *);
396 
397 void init_task_file_iterator (iterator_t *, task_t, const char *);
398 const char *task_file_iterator_name (iterator_t *);
399 const char *task_file_iterator_content (iterator_t *);
400 
401 void set_task_schedule_next_time (task_t, time_t);
402 
403 void set_task_schedule_next_time_uuid (const gchar *, time_t);
404 
405 void init_preference_iterator (iterator_t *, config_t, const char *);
406 const char *preference_iterator_name (iterator_t *);
407 const char *preference_iterator_value (iterator_t *);
408 
409 port_list_t target_port_list (target_t);
410 credential_t target_ssh_credential (target_t);
411 credential_t target_smb_credential (target_t);
412 credential_t target_esxi_credential (target_t);
413 
414 int create_current_report (task_t, char **, task_status_t);
415 
416 char *alert_data (alert_t, const char *, const char *);
417 
419 
421 
423 
424 const char *task_schedule_iterator_task_uuid (iterator_t *);
425 
426 schedule_t task_schedule_iterator_schedule (iterator_t *);
427 
428 const char *task_schedule_iterator_icalendar (iterator_t *);
429 
430 const char *task_schedule_iterator_timezone (iterator_t *);
431 
432 const char *task_schedule_iterator_owner_uuid (iterator_t *);
433 
434 const char *task_schedule_iterator_owner_name (iterator_t *);
435 
437 
439 
441 
442 time_t task_schedule_iterator_initial_offset (iterator_t *);
443 
444 int set_task_schedule_uuid (const gchar*, schedule_t, int);
445 
446 void reinit_manage_process ();
447 
449 
450 int manage_report_host_details (report_t, const char *, entity_t);
451 
452 const char *run_status_name_internal (task_status_t);
453 
454 gchar *get_ovaldef_short_filename (char*);
455 
456 void update_config_cache_init (const char *);
457 
458 alive_test_t target_alive_tests (target_t);
459 
460 void manage_session_init (const char *);
461 
462 int valid_gmp_command (const char *);
463 
464 void check_generate_scripts ();
465 
466 void auto_delete_reports ();
467 
468 int parse_iso_time (const char *);
469 
470 void set_report_scheduled (report_t);
471 
472 gchar *resource_uuid (const gchar *, resource_t);
473 
474 gboolean find_resource_with_permission (const char *, const char *,
475  resource_t *, const char *, int);
476 
477 void parse_osp_report (task_t, report_t, const char *);
478 
479 void reschedule_task (const gchar *);
480 
481 void insert_port_range (port_list_t, port_protocol_t, int, int);
482 
484 
486 
488 
490 
491 int manage_cert_db_exists ();
492 
493 int manage_scap_db_exists ();
494 
495 void manage_db_check_mode (const gchar *);
496 
497 int manage_db_check (const gchar *);
498 
499 int
500 count (const char *, const get_data_t *, column_t *, column_t *, const char **,
501  int, const char *, const char *, int);
502 
503 int
504 init_get_iterator (iterator_t*, const char *, const get_data_t *, column_t *,
505  column_t *, const char **, int, const char *, const char *,
506  int);
507 
508 gchar *
510 
511 gchar *
512 filter_clause (const char*, const char*, const char **, column_t *,
513  column_t *, int, gchar **, int *, int *, array_t **, gchar **);
514 
515 void
516 check_alerts ();
517 
518 int
519 manage_option_setup (GSList *, const gchar *);
520 
521 void
523 
524 void
526 
527 void
528 event (event_t, void *, resource_t, resource_t);
529 
530 gboolean
531 find_trash (const char *, const char *, resource_t *);
532 
533 void
534 tags_remove_resource (const char *, resource_t, int);
535 
536 void
537 tags_set_locations (const char *, resource_t, resource_t, int);
538 
539 void
540 permissions_set_locations (const char *, resource_t, resource_t, int);
541 
542 void
543 permissions_set_orphans (const char *, resource_t, int);
544 
545 int
546 copy_resource (const char *, const char *, const char *, const char *,
547  const char *, int, resource_t *, resource_t *);
548 
549 gboolean
550 resource_with_name_exists (const char *, const char *, resource_t);
551 
552 int
553 create_permission_internal (const char *, const char *, const char *, const char *,
554  const char *, const char *, permission_t *);
555 
556 int
557 copy_resource_lock (const char *, const char *, const char *, const char *,
558  const char *, int, resource_t *, resource_t *);
559 
560 nvti_t *
561 lookup_nvti (const gchar *);
562 
563 #endif /* not _GVMD_MANAGE_SQL_H */
long long int resource_t
A resource, like a task or target.
Definition: iterator.h:40
const char * run_status_name_internal(task_status_t status)
Get the unique name of a run status.
Definition: manage.c:1589
void manage_update_cert_db_cleanup()
Database specific cleanup after CERT update.
Definition: manage_pg.c:216
int manage_cert_db_exists()
Check if CERT db exists.
Definition: manage_pg.c:103
int manage_scap_db_exists()
Check if SCAP db exists.
Definition: manage_pg.c:118
int manage_update_scap_db_init()
Database specific setup for SCAP update.
Definition: manage_pg.c:239
int manage_db_empty()
Check whether database is empty.
Definition: manage_pg.c:83
int manage_db_check(const gchar *name)
Dummy function.
Definition: manage_pg.c:3735
void manage_db_check_mode(const gchar *name)
Dummy function.
Definition: manage_pg.c:3722
int manage_update_cert_db_init()
Database specific setup for CERT update.
Definition: manage_pg.c:133
void manage_update_scap_db_cleanup()
Database specific cleanup after SCAP update.
Definition: manage_pg.c:439
void manage_session_init(const char *uuid)
Setup session.
Definition: manage_pg.c:51
gchar * tag_value(const gchar *tags, const gchar *tag)
Extract a tag from a pipe separated tag list.
Definition: manage_sql.c:1166
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:5503
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:20620
alive_test_t target_alive_tests(target_t target)
Return a target's alive tests.
Definition: manage_sql.c:35489
void check_generate_scripts()
Ensure the generate scripts are all executable.
Definition: manage_sql.c:18276
gchar * resource_uuid(const gchar *type, resource_t resource)
Get the UUID of a resource.
Definition: manage_sql.c:5169
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:20633
void auto_delete_reports()
Auto delete reports.
Definition: manage_sql.c:20915
int set_task_requested(task_t task, task_status_t *status)
Atomically set the run state of a task to requested.
Definition: manage_sql.c:19803
void manage_option_cleanup()
Cleanup for an option process.
Definition: manage_sql.c:1298
void check_alerts()
Check if any SecInfo alerts are due.
Definition: manage_sql.c:6879
void set_report_scheduled(report_t report)
Set a report's scheduled flag.
Definition: manage_sql.c:19714
void reschedule_task(const gchar *task_id)
Set a task's schedule so that it runs again next scheduling round.
Definition: manage_sql.c:21024
gboolean task_schedule_iterator_stop_due(iterator_t *iterator)
Get the stop due state from a task schedule iterator.
Definition: manage_sql.c:45709
credential_t target_esxi_credential(target_t target)
Return the ESXi credential associated with a target, if any.
Definition: manage_sql.c:35385
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:4798
void event(event_t event, void *event_data, resource_t resource_1, resource_t resource_2)
Produce an event.
Definition: manage_sql.c:14813
int manage_option_setup(GSList *log_config, const gchar *database)
Setup for an option process.
Definition: manage_sql.c:1259
gboolean task_schedule_iterator_timed_out(iterator_t *iterator)
Get if schedule of task in iterator is timed out.
Definition: manage_sql.c:45755
int delete_report_internal(report_t report)
Delete a report.
Definition: manage_sql.c:26603
gchar * gvmd_db_name
Name of the database file.
Definition: manage_sql.c:521
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:6246
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:26026
void set_report_slave_task_uuid(report_t report, const char *uuid)
Set the UUID of the slave task, on the local task.
Definition: manage_sql.c:26919
void reinit_manage_process()
Reinitialize the manage library for a process.
Definition: manage_sql.c:15672
char * alert_data(alert_t alert, const char *type, const char *name)
Return data associated with an alert.
Definition: manage_sql.c:8996
int set_report_slave_progress(report_t report, int progress)
Set slave progress of a report.
Definition: manage_sql.c:26884
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:4628
void cleanup_task_schedule_iterator(iterator_t *iterator)
Cleanup a task schedule iterator.
Definition: manage_sql.c:45572
gchar * report_host_ip(const char *host)
Get the IP of a host, using the 'hostname' report host details.
Definition: manage_sql.c:31875
void init_task_file_iterator(iterator_t *iterator, task_t task, const char *file)
Initialise a task file iterator.
Definition: manage_sql.c:33045
int init_task_schedule_iterator(iterator_t *iterator)
Initialise a task schedule iterator.
Definition: manage_sql.c:45540
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:3306
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:746
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:27985
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:22235
credential_t target_smb_credential(target_t target)
Return the SMB credential associated with a target, if any.
Definition: manage_sql.c:35372
nvti_t * lookup_nvti(const gchar *nvt)
Update the memory cache of NVTs.
Definition: manage_sql.c:15772
int parse_iso_time(const char *text_time)
Convert an ISO time into seconds since epoch.
Definition: manage_sql.c:887
void trim_report(report_t report)
Prepare a partial report for restarting the scan from the beginning.
Definition: manage_sql.c:26934
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:5000
port_list_t target_port_list(target_t target)
Return the port list associated with a target, if any.
Definition: manage_sql.c:35398
gchar * columns_build_select(column_t *select_columns)
Return column list for SELECT statement.
Definition: manage_sql.c:2878
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:841
gboolean task_schedule_iterator_start_due(iterator_t *iterator)
Get the start due state from a task schedule iterator.
Definition: manage_sql.c:45677
void parse_osp_report(task_t task, report_t report, const char *report_xml)
Parse an OSP report.
Definition: manage_sql.c:31921
credential_t target_ssh_credential(target_t target)
Return the SSH credential associated with a target, if any.
Definition: manage_sql.c:35359
int valid_gmp_command(const char *name)
Check whether a command name is valid.
Definition: manage_sql.c:669
int set_task_schedule_uuid(const gchar *task_id, schedule_t schedule, int periods)
Set the schedule of a task.
Definition: manage_sql.c:20399
task_t task_schedule_iterator_task(iterator_t *iterator)
Get the task from a task schedule iterator.
Definition: manage_sql.c:45586
int manage_update_nvti_cache()
Update the memory cache of NVTs, if this has been requested.
Definition: manage_sql.c:15839
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:3557
void update_all_config_caches()
Update count and growing info in every config across all users.
Definition: manage_sql_configs.c:4419
void update_config_cache_init(const char *uuid)
Update count and growing info in config, without checking user.
Definition: manage_sql_configs.c:4443
gchar * get_ovaldef_short_filename(char *item_id)
Get the short file name for an OVALDEF.
Definition: manage_sql_secinfo.c:948
Iterator column.
Definition: manage_sql.h:300
gchar * select
Column for SELECT.
Definition: manage_sql.h:301
gchar * filter
Filter column name. NULL to use select_column.
Definition: manage_sql.h:302
keyword_type_t type
Type of column.
Definition: manage_sql.h:303
Command data for a get command.
Definition: manage.h:355
A generic SQL iterator structure.
Definition: iterator.h:51