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 
35 /**
36  * @brief Location of a constituent of a trashcan resource.
37  */
38 #define LOCATION_TABLE 0
39 
40 /**
41  * @brief Location of a constituent of a trashcan resource.
42  */
43 #define LOCATION_TRASH 1
44 
45 /**
46  * @brief UUID of 'All' NVT selector.
47  */
48 #define MANAGE_NVT_SELECTOR_UUID_ALL "54b45713-d4f4-4435-b20d-304c175ed8c5"
49 
50 /**
51  * @brief Predefined role UUID.
52  */
53 #define PERMISSION_UUID_ADMIN_EVERYTHING "b3b56a8c-c2fd-11e2-a135-406186ea4fc5"
54 
55 /**
56  * @brief Predefined role UUID.
57  */
58 #define PERMISSION_UUID_SUPER_ADMIN_EVERYTHING "a9801074-6fe2-11e4-9d81-406186ea4fc5"
59 
60 /**
61  * @brief Predefined role UUID.
62  */
63 #define ROLE_UUID_ADMIN "7a8cb5b4-b74d-11e2-8187-406186ea4fc5"
64 
65 /**
66  * @brief Predefined role UUID.
67  */
68 #define ROLE_UUID_GUEST "cc9cac5e-39a3-11e4-abae-406186ea4fc5"
69 
70 /**
71  * @brief Predefined role UUID.
72  */
73 #define ROLE_UUID_INFO "5f8fd16c-c550-11e3-b6ab-406186ea4fc5"
74 
75 /**
76  * @brief Predefined role UUID.
77  */
78 #define ROLE_UUID_MONITOR "12cdb536-480b-11e4-8552-406186ea4fc5"
79 
80 /**
81  * @brief Predefined role UUID.
82  */
83 #define ROLE_UUID_USER "8d453140-b74d-11e2-b0be-406186ea4fc5"
84 
85 /**
86  * @brief Predefined role UUID.
87  */
88 #define ROLE_UUID_SUPER_ADMIN "9c5a6ec6-6fe2-11e4-8cb6-406186ea4fc5"
89 
90 /**
91  * @brief Predefined role UUID.
92  */
93 #define ROLE_UUID_OBSERVER "87a7ebce-b74d-11e2-a81f-406186ea4fc5"
94 
95 /**
96  * @brief UUID of 'OpenVAS Default' scanner.
97  */
98 #define SCANNER_UUID_DEFAULT "08b69003-5fc2-4037-a479-93b440211c73"
99 
100 /**
101  * @brief UUID of 'CVE' scanner.
102  */
103 #define SCANNER_UUID_CVE "6acd0832-df90-11e4-b9d5-28d24461215b"
104 
105 /**
106  * @brief UUID of 'Rows Per Page' setting.
107  */
108 #define SETTING_UUID_ROWS_PER_PAGE "5f5a8712-8017-11e1-8556-406186ea4fc5"
109 
110 /**
111  * @brief UUID of 'Max Rows Per Page' setting.
112  */
113 #define SETTING_UUID_MAX_ROWS_PER_PAGE "76374a7a-0569-11e6-b6da-28d24461215b"
114 
115 /**
116  * @brief UUID of 'Default CA Cert' setting.
117  */
118 #define SETTING_UUID_DEFAULT_CA_CERT "9ac801ea-39f8-11e6-bbaa-28d24461215b"
119 
120 /**
121  * @brief UUID of 'Debian LSC Package Maintainer' setting.
122  */
123 #define SETTING_UUID_LSC_DEB_MAINTAINER "2fcbeac8-4237-438f-b52a-540a23e7af97"
124 
125 /**
126  * @brief UUID of 'Feed Import Owner' setting.
127  */
128 #define SETTING_UUID_FEED_IMPORT_OWNER "78eceaec-3385-11ea-b237-28d24461215b"
129 
130 /**
131  * @brief UUID of 'Feed Import Roles' setting.
132  */
133 #define SETTING_UUID_FEED_IMPORT_ROLES "ff000362-338f-11ea-9051-28d24461215b"
134 
135 /**
136  * @brief Trust constant for error.
137  */
138 #define TRUST_ERROR 0
139 
140 /**
141  * @brief Trust constant for yes.
142  */
143 #define TRUST_YES 1
144 
145 /**
146  * @brief Trust constant for no.
147  */
148 #define TRUST_NO 2
149 
150 /**
151  * @brief Trust constant for unknown.
152  */
153 #define TRUST_UNKNOWN 3
154 
155 /**
156  * @brief Number of milliseconds between timevals a and b (performs a-b).
157  */
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 
164 /**
165  * @brief Generate accessor for an SQL iterator.
166  *
167  * This convenience macro is used to generate an accessor returning a
168  * const string pointer.
169  *
170  * @param[in]  name  Name of accessor.
171  * @param[in]  col   Column number to access.
172  */
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 
183 /**
184  * @brief Write to a file or close stream and exit.
185  *
186  * @param[in]   stream    Stream to write to.
187  * @param[in]   format    Format specification.
188  * @param[in]   args      Arguments.
189  */
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 
208 /**
209  * @brief Iterator column.
210  */
211 typedef struct
212 {
213   gchar *select;       ///< Column for SELECT.
214   gchar *filter;       ///< Filter column name.  NULL to use select_column.
215   keyword_type_t type; ///< Type of column.
216 } column_t;
217 
218 /**
219  * @brief Filter columns for GET iterator.
220  */
221 #define ANON_GET_ITERATOR_FILTER_COLUMNS "uuid", \
222  "created", "modified", "_owner"
223 
224 /**
225  * @brief Filter columns for GET iterator.
226  */
227 #define GET_ITERATOR_FILTER_COLUMNS "uuid", "name", "comment", \
228  "created", "modified", "_owner"
229 
230 /**
231  * @brief Columns for GET iterator, as a single string.
232  *
233  * @param[in]  prefix  Column prefix.
234  */
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 
240 /**
241  * @brief Columns for GET iterator.
242  *
243  * @param[in]  prefix  Column prefix.
244  */
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 
255 /**
256  * @brief Columns for GET iterator.
257  *
258  * @param[in]  table  Table.
259  */
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 
270 /**
271  * @brief Number of columns for GET iterator.
272  */
273 #define GET_ITERATOR_COLUMN_COUNT 10
274 
275 
276 /* Variables */
277 
278 extern db_conn_info_t gvmd_db_conn_info;
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 
323 int init_task_schedule_iterator (iterator_t *);
324 
325 void cleanup_task_schedule_iterator (iterator_t *);
326 
327 task_t task_schedule_iterator_task (iterator_t *);
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 
341 gboolean task_schedule_iterator_timed_out (iterator_t *);
342 
343 gboolean task_schedule_iterator_start_due (iterator_t *);
344 
345 gboolean task_schedule_iterator_stop_due (iterator_t *);
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 
353 int manage_update_nvti_cache ();
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 *
405 columns_build_select (column_t *);
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
418 manage_option_cleanup ();
419 
420 void
421 update_all_config_caches ();
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
475 create_view_vulns ();
476 
477 int
478 config_family_entire_and_growing (config_t, const char*);
479 
480 void
481 reports_clear_count_cache_dynamic ();
482 
483 #endif /* not _GVMD_MANAGE_SQL_H */
484