Greenbone Vulnerability Manager  8.0.1~git
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 "manage.h"
29 #include "manage_utils.h"
30 
31 #include <gvm/util/xmlutils.h> /* for entity_t */
32 
33 /* Internal types and preprocessor definitions. */
34 
38 #define CONFIG_UUID_FULL_AND_FAST "daba56c8-73ec-11df-a475-002264764cea"
39 
43 #define CONFIG_UUID_FULL_AND_FAST_ULTIMATE \
44  "698f691e-7489-11df-9d8c-002264764cea"
45 
49 #define CONFIG_UUID_FULL_AND_VERY_DEEP "708f25c4-7489-11df-8094-002264764cea"
50 
54 #define CONFIG_UUID_FULL_AND_VERY_DEEP_ULTIMATE \
55  "74db13d6-7489-11df-91b9-002264764cea"
56 
60 #define CONFIG_UUID_EMPTY "085569ce-73ed-11df-83c3-002264764cea"
61 
65 #define CONFIG_UUID_DISCOVERY "8715c877-47a0-438d-98a3-27c7a6ab2196"
66 
70 #define CONFIG_UUID_HOST_DISCOVERY "2d3f051c-55ba-11e3-bf43-406186ea4fc5"
71 
75 #define CONFIG_UUID_SYSTEM_DISCOVERY "bbca7412-a950-11e3-9109-406186ea4fc5"
76 
80 #define LOCATION_TABLE 0
81 
85 #define LOCATION_TRASH 1
86 
90 #define MANAGE_NVT_SELECTOR_UUID_ALL "54b45713-d4f4-4435-b20d-304c175ed8c5"
91 
95 #define MANAGE_NVT_SELECTOR_UUID_DISCOVERY \
96  "0d9a2738-8fe2-4e22-8f26-bb886179e759"
97 
101 #define MANAGE_NVT_SELECTOR_UUID_HOST_DISCOVERY \
102  "f5f80744-55c7-11e3-8dc6-406186ea4fc5"
103 
107 #define MANAGE_NVT_SELECTOR_UUID_SYSTEM_DISCOVERY \
108  "07045d1c-a951-11e3-8da7-406186ea4fc5"
109 
113 #define PERMISSION_UUID_ADMIN_EVERYTHING "b3b56a8c-c2fd-11e2-a135-406186ea4fc5"
114 
118 #define PERMISSION_UUID_SUPER_ADMIN_EVERYTHING \
119  "a9801074-6fe2-11e4-9d81-406186ea4fc5"
120 
124 #define PORT_LIST_UUID_DEFAULT "c7e03b6c-3bbe-11e1-a057-406186ea4fc5"
125 
129 #define PORT_LIST_UUID_ALL_TCP "fd591a34-56fd-11e1-9f27-406186ea4fc5"
130 
134 #define PORT_LIST_UUID_ALL_TCP_NMAP_5_51_TOP_100 \
135  "730ef368-57e2-11e1-a90f-406186ea4fc5"
136 
140 #define PORT_LIST_UUID_ALL_TCP_NMAP_5_51_TOP_1000 \
141  "9ddce1ae-57e7-11e1-b13c-406186ea4fc5"
142 
146 #define PORT_LIST_UUID_ALL_PRIV_TCP "492b72f4-56fe-11e1-98a7-406186ea4fc5"
147 
151 #define PORT_LIST_UUID_ALL_PRIV_TCP_UDP "5f2029f6-56fe-11e1-bb94-406186ea4fc5"
152 
156 #define PORT_LIST_UUID_ALL_IANA_TCP_2012 "33d0cd82-57c6-11e1-8ed1-406186ea4fc5"
157 
161 #define PORT_LIST_UUID_ALL_IANA_TCP_UDP_2012 \
162  "4a4717fe-57d2-11e1-9a26-406186ea4fc5"
163 
167 #define PORT_LIST_UUID_NMAP_5_51_TOP_2000_TOP_100 \
168  "ab33f6b0-57f8-11e1-96f5-406186ea4fc5"
169 
173 #define ROLE_UUID_ADMIN "7a8cb5b4-b74d-11e2-8187-406186ea4fc5"
174 
178 #define ROLE_UUID_GUEST "cc9cac5e-39a3-11e4-abae-406186ea4fc5"
179 
183 #define ROLE_UUID_INFO "5f8fd16c-c550-11e3-b6ab-406186ea4fc5"
184 
188 #define ROLE_UUID_MONITOR "12cdb536-480b-11e4-8552-406186ea4fc5"
189 
193 #define ROLE_UUID_USER "8d453140-b74d-11e2-b0be-406186ea4fc5"
194 
198 #define ROLE_UUID_SUPER_ADMIN "9c5a6ec6-6fe2-11e4-8cb6-406186ea4fc5"
199 
203 #define ROLE_UUID_OBSERVER "87a7ebce-b74d-11e2-a81f-406186ea4fc5"
204 
208 #define SCANNER_UUID_DEFAULT "08b69003-5fc2-4037-a479-93b440211c73"
209 
213 #define SCANNER_UUID_CVE "6acd0832-df90-11e4-b9d5-28d24461215b"
214 
218 #define SETTING_UUID_ROWS_PER_PAGE "5f5a8712-8017-11e1-8556-406186ea4fc5"
219 
223 #define SETTING_UUID_MAX_ROWS_PER_PAGE "76374a7a-0569-11e6-b6da-28d24461215b"
224 
228 #define SETTING_UUID_DEFAULT_CA_CERT "9ac801ea-39f8-11e6-bbaa-28d24461215b"
229 
233 #define SETTING_UUID_SLAVE_CHECK_PERIOD "63adb79a-62ae-11e9-91ba-28d24461215b"
234 
238 #define SETTING_UUID_LSC_DEB_MAINTAINER "2fcbeac8-4237-438f-b52a-540a23e7af97"
239 
243 #define TRUST_ERROR 0
244 
248 #define TRUST_YES 1
249 
253 #define TRUST_NO 2
254 
258 #define TRUST_UNKNOWN 3
259 
263 #define TIMEVAL_SUBTRACT_MS(a, b) \
264  ((((a).tv_sec - (b).tv_sec) * 1000) + ((a).tv_usec - (b).tv_usec) / 1000)
265 
266 /* Macros. */
267 
277 #define DEF_ACCESS(name, col) \
278  const char *name (iterator_t *iterator) \
279  { \
280  const char *ret; \
281  if (iterator->done) \
282  return NULL; \
283  ret = iterator_string (iterator, col); \
284  return ret; \
285  }
286 
287 /* Iterator definitions. */
288 
292 typedef struct
293 {
294  gchar *select;
295  gchar *filter;
296  keyword_type_t type;
297 } column_t;
298 
302 #define ANON_GET_ITERATOR_FILTER_COLUMNS "uuid", "created", "modified", "_owner"
303 
307 #define GET_ITERATOR_FILTER_COLUMNS \
308  "uuid", "name", "comment", "created", "modified", "_owner"
309 
315 #define GET_ITERATOR_COLUMNS_STRING \
316  "id, uuid, name, comment, iso_time (creation_time)," \
317  " iso_time (modification_time), creation_time AS created," \
318  " modification_time AS modified"
319 
325 #define GET_ITERATOR_COLUMNS_PREFIX(prefix) \
326  {prefix "id", NULL, KEYWORD_TYPE_INTEGER}, \
327  {prefix "uuid", NULL, KEYWORD_TYPE_STRING}, \
328  {prefix "name", NULL, KEYWORD_TYPE_STRING}, \
329  {prefix "comment", NULL, KEYWORD_TYPE_STRING}, \
330  {" iso_time (" prefix "creation_time)", NULL, KEYWORD_TYPE_STRING}, \
331  {" iso_time (" prefix "modification_time)", NULL, KEYWORD_TYPE_STRING}, \
332  {prefix "creation_time", "created", KEYWORD_TYPE_INTEGER}, \
333  { \
334  prefix "modification_time", "modified", KEYWORD_TYPE_INTEGER \
335  }
336 
342 #define GET_ITERATOR_COLUMNS(table) \
343  GET_ITERATOR_COLUMNS_PREFIX (""), \
344  {"(SELECT name FROM users AS inner_users" \
345  " WHERE inner_users.id = " G_STRINGIFY (table) ".owner)", \
346  "_owner", \
347  KEYWORD_TYPE_STRING}, \
348  { \
349  "owner", NULL, KEYWORD_TYPE_INTEGER \
350  }
351 
355 #define GET_ITERATOR_COLUMN_COUNT 10
356 
357 /* Variables */
358 
359 extern gchar *gvmd_db_name;
360 
361 /* Function prototypes */
362 
363 typedef long long int rowid_t;
364 
365 int
366 manage_db_empty ();
367 
368 gboolean
369 host_nthlast_report_host (const char *, report_host_t *, int);
370 
371 char *
372 report_host_ip (const char *);
373 
374 gchar *
375 tag_value (const gchar *, const gchar *);
376 
377 void trim_report (report_t);
378 
379 int delete_report_internal (report_t);
380 
381 int set_report_scan_run_status (report_t, task_status_t);
382 
383 int
384 set_report_slave_progress (report_t, int);
385 
386 int
387 update_from_slave (task_t, entity_t, entity_t *, int *);
388 
389 void
390 set_report_slave_task_uuid (report_t, const char *);
391 
392 int
393 set_task_requested (task_t, task_status_t *);
394 
395 void
396 init_task_file_iterator (iterator_t *, task_t, const char *);
397 const char *
398 task_file_iterator_name (iterator_t *);
399 const char *
400 task_file_iterator_content (iterator_t *);
401 
402 void set_task_schedule_next_time (task_t, time_t);
403 
404 void
405 set_task_schedule_next_time_uuid (const gchar *, time_t);
406 
407 void
408 init_otp_pref_iterator (iterator_t *, config_t, const char *);
409 const char *
410 otp_pref_iterator_name (iterator_t *);
411 const char *
412 otp_pref_iterator_value (iterator_t *);
413 
414 port_list_t target_port_list (target_t);
415 credential_t target_ssh_credential (target_t);
416 credential_t target_smb_credential (target_t);
417 credential_t target_esxi_credential (target_t);
418 
419 int
420 create_current_report (task_t, char **, task_status_t);
421 
422 char *
423 alert_data (alert_t, const char *, const char *);
424 
425 int
427 
428 void
430 
431 task_t
433 
434 const char *
435 task_schedule_iterator_task_uuid (iterator_t *);
436 
437 schedule_t
438 task_schedule_iterator_schedule (iterator_t *);
439 
440 const char *
441 task_schedule_iterator_icalendar (iterator_t *);
442 
443 const char *
444 task_schedule_iterator_timezone (iterator_t *);
445 
446 const char *
447 task_schedule_iterator_owner_uuid (iterator_t *);
448 
449 const char *
450 task_schedule_iterator_owner_name (iterator_t *);
451 
452 gboolean
454 
455 gboolean
457 
458 gboolean
460 
461 time_t
462 task_schedule_iterator_initial_offset (iterator_t *);
463 
464 int
465 set_task_schedule_uuid (const gchar *, schedule_t, int);
466 
467 void
469 
470 int
472 
473 int
474 manage_report_host_details (report_t, const char *, entity_t);
475 
476 const char *run_status_name_internal (task_status_t);
477 
478 gchar *
480 
481 void
482 update_config_cache_init (const char *);
483 
484 alive_test_t target_alive_tests (target_t);
485 
486 void
487 manage_session_init (const char *);
488 
489 int
490 valid_gmp_command (const char *);
491 
492 void
494 
495 void
497 
498 int
499 parse_iso_time (const char *);
500 
501 void set_report_scheduled (report_t);
502 
503 gchar *
504 resource_uuid (const gchar *, resource_t);
505 
506 gboolean
507 find_resource_with_permission (const char *,
508  const char *,
509  resource_t *,
510  const char *,
511  int);
512 
513 void
514 parse_osp_report (task_t, report_t, const char *);
515 
516 void
517 reschedule_task (const gchar *);
518 
519 void
520 insert_port_range (port_list_t, port_protocol_t, int, int);
521 
522 int
524 
525 void
527 
528 int
530 
531 void
533 
534 int
536 
537 int
539 
540 void
541 manage_db_check_mode (const gchar *);
542 
543 int
544 manage_db_check (const gchar *);
545 
546 int
547 count (const char *,
548  const get_data_t *,
549  column_t *,
550  column_t *,
551  const char **,
552  int,
553  const char *,
554  const char *,
555  int);
556 
557 int
559  const char *,
560  const get_data_t *,
561  column_t *,
562  column_t *,
563  const char **,
564  int,
565  const char *,
566  const char *,
567  int);
568 
569 gchar *
571 
572 gchar *
573 filter_clause (const char *,
574  const char *,
575  const char **,
576  column_t *,
577  column_t *,
578  int,
579  gchar **,
580  int *,
581  int *,
582  array_t **,
583  gchar **);
584 
585 void
586 check_alerts ();
587 
588 int
589 manage_option_setup (GSList *, const gchar *);
590 
591 void
593 
594 void
596 
597 void
598 event (event_t, void *, resource_t, resource_t);
599 
600 gboolean
601 find_trash (const char *, const char *, resource_t *);
602 
603 void
604 tags_remove_resource (const char *, resource_t, int);
605 
606 void
607 tags_set_locations (const char *, resource_t, resource_t, int);
608 
609 void
610 permissions_set_locations (const char *, resource_t, resource_t, int);
611 
612 void
613 permissions_set_orphans (const char *, resource_t, int);
614 
615 int
616 copy_resource (const char *,
617  const char *,
618  const char *,
619  const char *,
620  const char *,
621  int,
622  resource_t *,
623  resource_t *);
624 
625 gboolean
626 resource_with_name_exists (const char *, const char *, resource_t);
627 
628 int
629 create_permission_internal (const char *,
630  const char *,
631  const char *,
632  const char *,
633  const char *,
634  const char *,
635  permission_t *);
636 
637 #endif /* not _GVMD_MANAGE_SQL_H */
void trim_report(report_t report)
Prepare a partial report for restarting the scan from the beginning.
Definition: manage_sql.c:27134
char * alert_data(alert_t alert, const char *type, const char *name)
Return data associated with an alert.
Definition: manage_sql.c:8956
void manage_update_scap_db_cleanup()
Database specific cleanup after SCAP update.
Definition: manage_pg.c:436
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:3339
gchar * report_host_ip(const char *host)
Get the IP of a host, using the 'hostname' report host details.
Definition: manage_sql.c:32628
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:4959
gchar * columns_build_select(column_t *select_columns)
Return column list for SELECT statement.
Definition: manage_sql.c:2911
void reinit_manage_process()
Reinitialize the manage library for a process.
Definition: manage_sql.c:15433
gchar * gvmd_db_name
Name of the database file.
Definition: manage_sql.c:547
gchar * select
Column for SELECT.
Definition: manage_sql.h:294
void update_all_config_caches()
Update count and growing info in every config across all users.
Definition: manage_sql.c:38863
int manage_db_empty()
Check whether database is empty.
Definition: manage_pg.c:81
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:28181
int manage_db_check(const gchar *name)
Dummy function.
Definition: manage_pg.c:3660
port_list_t target_port_list(target_t target)
Return the port list associated with a target, if any.
Definition: manage_sql.c:36344
credential_t target_ssh_credential(target_t target)
Return the SSH credential associated with a target, if any.
Definition: manage_sql.c:36305
int manage_cert_db_exists()
Check if CERT db exists.
Definition: manage_pg.c:100
int manage_update_scap_db_init()
Database specific setup for SCAP update.
Definition: manage_pg.c:236
A generic SQL iterator structure.
Definition: iterator.h:50
int manage_scap_db_exists()
Check if SCAP db exists.
Definition: manage_pg.c:115
void init_task_file_iterator(iterator_t *iterator, task_t task, const char *file)
Initialise a task file iterator.
Definition: manage_sql.c:33870
gchar * get_ovaldef_short_filename(char *item_id)
Get the short file name for an OVALDEF.
Definition: manage_sql_secinfo.c:641
int valid_gmp_command(const char *name)
Check whether a command name is valid.
Definition: manage_sql.c:691
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:19512
void cleanup_task_schedule_iterator(iterator_t *iterator)
Cleanup a task schedule iterator.
Definition: manage_sql.c:50403
int manage_update_nvti_cache()
Update the memory cache of NVTs, if this has been requested.
Definition: manage_sql.c:15562
void manage_option_cleanup()
Cleanup for an option process.
Definition: manage_sql.c:1338
int parse_iso_time(const char *text_time)
Convert an ISO time into seconds since epoch.
Definition: manage_sql.c:927
void reschedule_task(const gchar *task_id)
Set a task's schedule so that it runs again next scheduling round.
Definition: manage_sql.c:20785
credential_t target_esxi_credential(target_t target)
Return the ESXi credential associated with a target, if any.
Definition: manage_sql.c:36331
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:20358
keyword_type_t type
Type of column.
Definition: manage_sql.h:296
Command data for a get command.
Definition: manage.h:316
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:5462
void manage_session_init(const char *uuid)
Setup session.
Definition: manage_pg.c:50
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:22241
gboolean task_schedule_iterator_stop_due(iterator_t *iterator)
Get the stop due state from a task schedule iterator.
Definition: manage_sql.c:50540
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:768
Iterator column.
Definition: manage_sql.h:292
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:27119
gchar * filter
Filter column name. NULL to use select_column.
Definition: manage_sql.h:295
task_t task_schedule_iterator_task(iterator_t *iterator)
Get the task from a task schedule iterator.
Definition: manage_sql.c:50417
void parse_osp_report(task_t task, report_t report, const char *report_xml)
Parse an OSP report.
Definition: manage_sql.c:32674
void init_otp_pref_iterator(iterator_t *iterator, config_t config, const char *section)
Initialise an "OTP" preference iterator.
Definition: manage_sql.c:38000
gboolean task_schedule_iterator_start_due(iterator_t *iterator)
Get the start due state from a task schedule iterator.
Definition: manage_sql.c:50508
void check_alerts()
Check if any SecInfo alerts are due.
Definition: manage_sql.c:6839
gchar * resource_uuid(const gchar *type, resource_t resource)
Get the UUID of a resource.
Definition: manage_sql.c:5128
int delete_report_internal(report_t report)
Delete a report.
Definition: manage_sql.c:26799
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:26222
int set_task_schedule_uuid(const gchar *task_id, schedule_t schedule, int periods)
Set the schedule of a task.
Definition: manage_sql.c:20120
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:6207
int init_task_schedule_iterator(iterator_t *iterator)
Initialise a task schedule iterator.
Definition: manage_sql.c:50371
int set_report_slave_progress(report_t report, int progress)
Set slave progress of a report.
Definition: manage_sql.c:27084
gchar * tag_value(const gchar *tags, const gchar *tag)
Extract a tag from an OTP tag list.
Definition: manage_sql.c:1206
void manage_db_check_mode(const gchar *name)
Dummy function.
Definition: manage_pg.c:3647
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:20345
void auto_delete_reports()
Auto delete reports.
Definition: manage_sql.c:20668
const char * run_status_name_internal(task_status_t status)
Get the unique name of a run status.
Definition: manage.c:1527
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:4616
void set_report_scheduled(report_t report)
Set a report's scheduled flag.
Definition: manage_sql.c:19423
credential_t target_smb_credential(target_t target)
Return the SMB credential associated with a target, if any.
Definition: manage_sql.c:36318
void manage_update_cert_db_cleanup()
Database specific cleanup after CERT update.
Definition: manage_pg.c:213
void check_generate_scripts()
Ensure the generate scripts are all executable.
Definition: manage_sql.c:17927
void event(event_t event, void *event_data, resource_t resource_1, resource_t resource_2)
Produce an event.
Definition: manage_sql.c:14611
alive_test_t target_alive_tests(target_t target)
Return a target's alive tests.
Definition: manage_sql.c:36435
int manage_option_setup(GSList *log_config, const gchar *database)
Setup for an option process.
Definition: manage_sql.c:1299
int manage_update_cert_db_init()
Database specific setup for CERT update.
Definition: manage_pg.c:130
void update_config_cache_init(const char *uuid)
Update count and growing info in config, without checking user.
Definition: manage_sql.c:38887
gboolean task_schedule_iterator_timed_out(iterator_t *iterator)
Get if schedule of task in iterator is timed out.
Definition: manage_sql.c:50586
long long int resource_t
A resource, like a task or target.
Definition: iterator.h:40
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:881