Greenbone Vulnerability Manager  8.0.0~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_LSC_DEB_MAINTAINER "2fcbeac8-4237-438f-b52a-540a23e7af97"
234 
238 #define TRUST_ERROR 0
239 
243 #define TRUST_YES 1
244 
248 #define TRUST_NO 2
249 
253 #define TRUST_UNKNOWN 3
254 
258 #define TIMEVAL_SUBTRACT_MS(a, b) \
259  ((((a).tv_sec - (b).tv_sec) * 1000) + ((a).tv_usec - (b).tv_usec) / 1000)
260 
261 /* Macros. */
262 
272 #define DEF_ACCESS(name, col) \
273  const char *name (iterator_t *iterator) \
274  { \
275  const char *ret; \
276  if (iterator->done) \
277  return NULL; \
278  ret = iterator_string (iterator, col); \
279  return ret; \
280  }
281 
282 /* Iterator definitions. */
283 
287 typedef struct
288 {
289  gchar *select;
290  gchar *filter;
291  keyword_type_t type;
292 } column_t;
293 
297 #define ANON_GET_ITERATOR_FILTER_COLUMNS "uuid", "created", "modified", "_owner"
298 
302 #define GET_ITERATOR_FILTER_COLUMNS \
303  "uuid", "name", "comment", "created", "modified", "_owner"
304 
310 #define GET_ITERATOR_COLUMNS_STRING \
311  "id, uuid, name, comment, iso_time (creation_time)," \
312  " iso_time (modification_time), creation_time AS created," \
313  " modification_time AS modified"
314 
320 #define GET_ITERATOR_COLUMNS_PREFIX(prefix) \
321  {prefix "id", NULL, KEYWORD_TYPE_INTEGER}, \
322  {prefix "uuid", NULL, KEYWORD_TYPE_STRING}, \
323  {prefix "name", NULL, KEYWORD_TYPE_STRING}, \
324  {prefix "comment", NULL, KEYWORD_TYPE_STRING}, \
325  {" iso_time (" prefix "creation_time)", NULL, KEYWORD_TYPE_STRING}, \
326  {" iso_time (" prefix "modification_time)", NULL, KEYWORD_TYPE_STRING}, \
327  {prefix "creation_time", "created", KEYWORD_TYPE_INTEGER}, \
328  { \
329  prefix "modification_time", "modified", KEYWORD_TYPE_INTEGER \
330  }
331 
337 #define GET_ITERATOR_COLUMNS(table) \
338  GET_ITERATOR_COLUMNS_PREFIX (""), \
339  {"(SELECT name FROM users AS inner_users" \
340  " WHERE inner_users.id = " G_STRINGIFY (table) ".owner)", \
341  "_owner", \
342  KEYWORD_TYPE_STRING}, \
343  { \
344  "owner", NULL, KEYWORD_TYPE_INTEGER \
345  }
346 
350 #define GET_ITERATOR_COLUMN_COUNT 10
351 
352 /* Variables */
353 
354 extern gchar *gvmd_db_name;
355 
356 /* Function prototypes */
357 
358 typedef long long int rowid_t;
359 
360 int
361 manage_db_empty ();
362 
363 gboolean
364 host_nthlast_report_host (const char *, report_host_t *, int);
365 
366 char *
367 report_host_ip (const char *);
368 
369 gchar *
370 tag_value (const gchar *, const gchar *);
371 
372 void trim_report (report_t);
373 
374 int delete_report_internal (report_t);
375 
376 int set_report_scan_run_status (report_t, task_status_t);
377 
378 int
379 set_report_slave_progress (report_t, int);
380 
381 int
382 update_from_slave (task_t, entity_t, entity_t *, int *);
383 
384 void
385 set_report_slave_task_uuid (report_t, const char *);
386 
387 int
388 set_task_requested (task_t, task_status_t *);
389 
390 void
391 init_task_file_iterator (iterator_t *, task_t, const char *);
392 const char *
393 task_file_iterator_name (iterator_t *);
394 const char *
395 task_file_iterator_content (iterator_t *);
396 
397 void set_task_schedule_next_time (task_t, time_t);
398 
399 void
400 set_task_schedule_next_time_uuid (const gchar *, time_t);
401 
402 void
403 init_otp_pref_iterator (iterator_t *, config_t, const char *);
404 const char *
405 otp_pref_iterator_name (iterator_t *);
406 const char *
407 otp_pref_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
415 create_current_report (task_t, char **, task_status_t);
416 
417 char *
418 alert_data (alert_t, const char *, const char *);
419 
420 int
422 
423 void
425 
426 task_t
428 
429 const char *
430 task_schedule_iterator_task_uuid (iterator_t *);
431 
432 schedule_t
433 task_schedule_iterator_schedule (iterator_t *);
434 
435 const char *
436 task_schedule_iterator_icalendar (iterator_t *);
437 
438 const char *
439 task_schedule_iterator_timezone (iterator_t *);
440 
441 const char *
442 task_schedule_iterator_owner_uuid (iterator_t *);
443 
444 const char *
445 task_schedule_iterator_owner_name (iterator_t *);
446 
447 gboolean
449 
450 gboolean
452 
453 gboolean
455 
456 time_t
457 task_schedule_iterator_initial_offset (iterator_t *);
458 
459 int
460 set_task_schedule_uuid (const gchar *, schedule_t, int);
461 
462 void
464 
465 int
467 
468 int
469 manage_report_host_details (report_t, const char *, entity_t);
470 
471 const char *run_status_name_internal (task_status_t);
472 
473 gchar *
475 
476 void
477 update_config_cache_init (const char *);
478 
479 alive_test_t target_alive_tests (target_t);
480 
481 void
482 manage_session_init (const char *);
483 
484 int
485 valid_gmp_command (const char *);
486 
487 void
489 
490 void
492 
493 int
494 parse_iso_time (const char *);
495 
496 void set_report_scheduled (report_t);
497 
498 gchar *
499 resource_uuid (const gchar *, resource_t);
500 
501 gboolean
502 find_resource_with_permission (const char *,
503  const char *,
504  resource_t *,
505  const char *,
506  int);
507 
508 void
509 parse_osp_report (task_t, report_t, const char *);
510 
511 void
512 reschedule_task (const gchar *);
513 
514 void
515 insert_port_range (port_list_t, port_protocol_t, int, int);
516 
517 int
519 
520 void
522 
523 int
525 
526 void
528 
529 int
531 
532 int
534 
535 void
536 manage_db_check_mode (const gchar *);
537 
538 int
539 manage_db_check (const gchar *);
540 
541 int
542 count (const char *,
543  const get_data_t *,
544  column_t *,
545  column_t *,
546  const char **,
547  int,
548  const char *,
549  const char *,
550  int);
551 
552 int
554  const char *,
555  const get_data_t *,
556  column_t *,
557  column_t *,
558  const char **,
559  int,
560  const char *,
561  const char *,
562  int);
563 
564 gchar *
566 
567 gchar *
568 filter_clause (const char *,
569  const char *,
570  const char **,
571  column_t *,
572  column_t *,
573  int,
574  gchar **,
575  int *,
576  int *,
577  array_t **,
578  gchar **);
579 
580 void
581 check_alerts ();
582 
583 int
584 manage_option_setup (GSList *, const gchar *);
585 
586 void
588 
589 void
591 
592 void
593 event (event_t, void *, resource_t, resource_t);
594 
595 gboolean
596 find_trash (const char *, const char *, resource_t *);
597 
598 void
599 tags_remove_resource (const char *, resource_t, int);
600 
601 void
602 tags_set_locations (const char *, resource_t, resource_t, int);
603 
604 void
605 permissions_set_locations (const char *, resource_t, resource_t, int);
606 
607 void
608 permissions_set_orphans (const char *, resource_t, int);
609 
610 int
611 copy_resource (const char *,
612  const char *,
613  const char *,
614  const char *,
615  const char *,
616  int,
617  resource_t *,
618  resource_t *);
619 
620 gboolean
621 resource_with_name_exists (const char *, const char *, resource_t);
622 
623 int
624 create_permission_internal (const char *,
625  const char *,
626  const char *,
627  const char *,
628  const char *,
629  const char *,
630  permission_t *);
631 
632 #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:26996
char * alert_data(alert_t alert, const char *type, const char *name)
Return data associated with an alert.
Definition: manage_sql.c:8854
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:3239
gchar * report_host_ip(const char *host)
Get the IP of a host, using the &#39;hostname&#39; report host details.
Definition: manage_sql.c:32488
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:4857
gchar * columns_build_select(column_t *select_columns)
Return column list for SELECT statement.
Definition: manage_sql.c:2811
void reinit_manage_process()
Reinitialize the manage library for a process.
Definition: manage_sql.c:15330
gchar * gvmd_db_name
Name of the database file.
Definition: manage_sql.c:547
gchar * select
Column for SELECT.
Definition: manage_sql.h:289
void update_all_config_caches()
Update count and growing info in every config across all users.
Definition: manage_sql.c:38716
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&#39;th last report_host given a host.
Definition: manage_sql.c:28043
int manage_db_check(const gchar *name)
Dummy function.
Definition: manage_pg.c:3645
port_list_t target_port_list(target_t target)
Return the port list associated with a target, if any.
Definition: manage_sql.c:36197
credential_t target_ssh_credential(target_t target)
Return the SSH credential associated with a target, if any.
Definition: manage_sql.c:36158
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:33730
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:19398
void cleanup_task_schedule_iterator(iterator_t *iterator)
Cleanup a task schedule iterator.
Definition: manage_sql.c:50207
int manage_update_nvti_cache()
Update the memory cache of NVTs, if this has been requested.
Definition: manage_sql.c:15459
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&#39;s schedule so that it runs again next scheduling round.
Definition: manage_sql.c:20671
credential_t target_esxi_credential(target_t target)
Return the ESXi credential associated with a target, if any.
Definition: manage_sql.c:36184
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:20244
keyword_type_t type
Type of column.
Definition: manage_sql.h:291
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:5360
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:22120
gboolean task_schedule_iterator_stop_due(iterator_t *iterator)
Get the stop due state from a task schedule iterator.
Definition: manage_sql.c:50344
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:287
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:26981
gchar * filter
Filter column name. NULL to use select_column.
Definition: manage_sql.h:290
task_t task_schedule_iterator_task(iterator_t *iterator)
Get the task from a task schedule iterator.
Definition: manage_sql.c:50221
void parse_osp_report(task_t task, report_t report, const char *report_xml)
Parse an OSP report.
Definition: manage_sql.c:32534
void init_otp_pref_iterator(iterator_t *iterator, config_t config, const char *section)
Initialise an "OTP" preference iterator.
Definition: manage_sql.c:37853
gboolean task_schedule_iterator_start_due(iterator_t *iterator)
Get the start due state from a task schedule iterator.
Definition: manage_sql.c:50312
void check_alerts()
Check if any SecInfo alerts are due.
Definition: manage_sql.c:6737
gchar * resource_uuid(const gchar *type, resource_t resource)
Get the UUID of a resource.
Definition: manage_sql.c:5026
int delete_report_internal(report_t report)
Delete a report.
Definition: manage_sql.c:26661
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:26084
int set_task_schedule_uuid(const gchar *task_id, schedule_t schedule, int periods)
Set the schedule of a task.
Definition: manage_sql.c:20006
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:6105
int init_task_schedule_iterator(iterator_t *iterator)
Initialise a task schedule iterator.
Definition: manage_sql.c:50175
int set_report_slave_progress(report_t report, int progress)
Set slave progress of a report.
Definition: manage_sql.c:26946
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:3632
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:20231
void auto_delete_reports()
Auto delete reports.
Definition: manage_sql.c:20554
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:4516
void set_report_scheduled(report_t report)
Set a report&#39;s scheduled flag.
Definition: manage_sql.c:19309
credential_t target_smb_credential(target_t target)
Return the SMB credential associated with a target, if any.
Definition: manage_sql.c:36171
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:17813
void event(event_t event, void *event_data, resource_t resource_1, resource_t resource_2)
Produce an event.
Definition: manage_sql.c:14508
alive_test_t target_alive_tests(target_t target)
Return a target&#39;s alive tests.
Definition: manage_sql.c:36288
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:38740
gboolean task_schedule_iterator_timed_out(iterator_t *iterator)
Get if schedule of task in iterator is timed out.
Definition: manage_sql.c:50390
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