OpenVAS Manager  7.0.3~git
manage_migrators.c
Go to the documentation of this file.
1 /* OpenVAS Manager
2  * $Id$
3  * Description: Module for OpenVAS Manager: the DB migrators.
4  *
5  * Authors:
6  * Hani Benhabiles <hani.benhabiles@greenbone.net>
7  *
8  * Copyright:
9  * Copyright (C) 2013 Greenbone Networks GmbH
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
122 /* time.h in glibc2 needs this for strptime. */
123 #define _XOPEN_SOURCE
124 
125 #include <time.h>
126 #include <stdlib.h>
127 #include <string.h>
128 #include <errno.h>
129 #include <glib/gstdio.h>
130 #include <assert.h>
131 #include <sys/stat.h>
132 #include <ctype.h>
133 #include <dirent.h>
134 
135 #include "manage_sql.h"
136 #include "sql.h"
137 
138 #include <openvas/misc/openvas_uuid.h>
139 #include <openvas/base/openvas_file.h>
140 #include <openvas/misc/openvas_logging.h>
141 
142 #undef G_LOG_DOMAIN
143 
146 #define G_LOG_DOMAIN "md main"
147 
148 
149 /* Old config IDs. */
150 
154 #define CONFIG_ID_FULL_AND_FAST 1
155 
159 #define CONFIG_ID_FULL_AND_FAST_ULTIMATE 2
160 
164 #define CONFIG_ID_FULL_AND_VERY_DEEP 3
165 
169 #define CONFIG_ID_FULL_AND_VERY_DEEP_ULTIMATE 4
170 
171 
172 /* Headers from backend specific manage_xxx.c file. */
173 
174 int
176 
177 
178 /* Types. */
179 
183 typedef struct
184 {
185  int version;
186  int (*function) ();
187 } migrator_t;
188 
189 
190 /* Functions. */
191 
195 static void
196 create_tables_version_4 ()
197 {
198  sql ("CREATE TABLE IF NOT EXISTS config_preferences"
199  " (id INTEGER PRIMARY KEY, config INTEGER, type, name, value);");
200  sql ("CREATE TABLE IF NOT EXISTS configs"
201  " (id INTEGER PRIMARY KEY, name UNIQUE, nvt_selector, comment,"
202  " family_count INTEGER, nvt_count INTEGER, families_growing INTEGER,"
203  " nvts_growing INTEGER);");
204  sql ("CREATE TABLE IF NOT EXISTS lsc_credentials"
205  " (id INTEGER PRIMARY KEY, name, password, comment, public_key TEXT,"
206  " private_key TEXT, rpm TEXT, deb TEXT, exe TEXT);");
207  sql ("CREATE TABLE IF NOT EXISTS meta"
208  " (id INTEGER PRIMARY KEY, name UNIQUE, value);");
209  sql ("CREATE TABLE IF NOT EXISTS nvt_preferences"
210  " (id INTEGER PRIMARY KEY, name, value);");
211  /* nvt_selectors types: 0 all, 1 family, 2 NVT (NVT_SELECTOR_TYPE_* above). */
212  sql ("CREATE TABLE IF NOT EXISTS nvt_selectors"
213  " (id INTEGER PRIMARY KEY, name, exclude INTEGER, type INTEGER,"
214  " family_or_nvt, family);");
215  sql ("CREATE TABLE IF NOT EXISTS nvts"
216  " (id INTEGER PRIMARY KEY, oid, version, name, summary, description,"
217  " copyright, cve, bid, xref, tag, sign_key_ids, category INTEGER,"
218  " family);");
219  sql ("CREATE TABLE IF NOT EXISTS report_hosts"
220  " (id INTEGER PRIMARY KEY, report INTEGER, host, start_time, end_time,"
221  " attack_state, current_port, max_port);");
222  sql ("CREATE INDEX IF NOT EXISTS report_hosts_by_report_and_host"
223  " ON report_hosts (report, host);");
224  sql ("CREATE TABLE IF NOT EXISTS report_results"
225  " (id INTEGER PRIMARY KEY, report INTEGER, result INTEGER);");
226  sql ("CREATE TABLE IF NOT EXISTS reports"
227  " (id INTEGER PRIMARY KEY, uuid, hidden INTEGER, task INTEGER,"
228  " date INTEGER, start_time, end_time, nbefile, comment,"
229  " scan_run_status INTEGER);");
230  sql ("CREATE TABLE IF NOT EXISTS results"
231  " (id INTEGER PRIMARY KEY, task INTEGER, subnet, host, port, nvt, type,"
232  " description)");
233  sql ("CREATE TABLE IF NOT EXISTS targets"
234  " (id INTEGER PRIMARY KEY, name, hosts, comment);");
235  sql ("CREATE TABLE IF NOT EXISTS task_files"
236  " (id INTEGER PRIMARY KEY, task INTEGER, name, content);");
237  sql ("CREATE TABLE IF NOT EXISTS tasks"
238  " (id INTEGER PRIMARY KEY, uuid, name, hidden INTEGER, time, comment,"
239  " description, owner" ", run_status INTEGER,"
240  " start_time, end_time, config, target);");
241  sql ("CREATE TABLE IF NOT EXISTS users"
242  " (id INTEGER PRIMARY KEY, name UNIQUE, password);");
243 }
244 
250 int
252 {
254 
255  /* Ensure that the database is currently version 0. */
256 
257  if (manage_db_version () != 0)
258  {
259  sql_rollback ();
260  return -1;
261  }
262 
263  /* Update the database. */
264 
265  /* In SVN the database version flag changed from 0 to 1 on 2009-09-30,
266  * while the database changed to the version 1 schema on 2009-08-29. This
267  * means the database could be flagged as version 0 while it has a version
268  * 1 schema. In this case the ADD COLUMN below would fail. A work around
269  * would be simply to update the version number to 1 in the database by
270  * hand. */
271 
272  sql ("ALTER TABLE reports ADD COLUMN scan_run_status INTEGER;");
273 
274  /* SQLite 3.1.3 and earlier requires a VACUUM before it can read
275  * from the new column. However, vacuuming might change the ROWIDs,
276  * which would screw up the data. Debian 5.0 (Lenny) is 3.5.9-6
277  * already. */
278 
279  sql ("UPDATE reports SET scan_run_status = '%u';",
281 
282  sql ("UPDATE reports SET scan_run_status = '%u'"
283  " WHERE start_time IS NULL OR end_time IS NULL;",
285 
286  sql ("UPDATE reports SET scan_run_status = '%u'"
287  " WHERE end_time IS NOT NULL;",
289 
290  /* Set the database version to 1. */
291 
292  set_db_version (1);
293 
294  sql_commit ();
295 
296  return 0;
297 }
298 
304 int
306 {
307  iterator_t nvts;
308 
310 
311  /* Ensure that the database is currently version 1. */
312 
313  if (manage_db_version () != 1)
314  {
315  sql_rollback ();
316  return -1;
317  }
318 
319  /* Update the database. */
320 
321  /* The category column in nvts changed type from string to int. This
322  * may be a redundant conversion, as SQLite may have converted these
323  * values automatically in each query anyway. */
324 
325  init_iterator (&nvts, "SELECT ROWID, category FROM nvts;");
326  while (next (&nvts))
327  {
328  int category;
329  const char *category_string;
330 
331  category_string = iterator_string (&nvts, 1);
332 
333  category = atoi (category_string);
334  sql ("UPDATE nvts SET category = %i WHERE ROWID = %llu;",
335  category,
336  iterator_int64 (&nvts, 0));
337  }
338  cleanup_iterator (&nvts);
339 
340  /* Set the database version to 2. */
341 
342  set_db_version (2);
343 
344  sql_commit ();
345 
346  return 0;
347 }
348 
354 int
356 {
358 
359  /* Ensure that the database is currently version 2. */
360 
361  if (manage_db_version () != 2)
362  {
363  sql_rollback ();
364  return -1;
365  }
366 
367  /* Update the database. */
368 
369  /* Add tables added since version 2 that are adjust later in the
370  * migration. */
371 
372  sql ("CREATE TABLE IF NOT EXISTS lsc_credentials (name, comment, rpm, deb, dog);");
373 
374  /* The lsc_credentials table changed: package columns changed type from BLOB
375  * to string, new columns "password", "public key" and "private key" appeared
376  * and the dog column changed name to exe.
377  *
378  * Just remove all the LSC credentials, as credential generation only
379  * started working after version 3. */
380 
381  sql ("DELETE from lsc_credentials;");
382  /* Before revision 5769 this could have caused problems, because these
383  * columns are added on the end of the table, so columns referenced by
384  * position in * queries may have been wrong (for example, with the iterator
385  * returned by init_lsc_credential_iterator). Since 5769 the queries
386  * name all columns explicitly. */
387  sql ("ALTER TABLE lsc_credentials ADD COLUMN password;");
388  sql ("ALTER TABLE lsc_credentials ADD COLUMN public_key TEXT;");
389  sql ("ALTER TABLE lsc_credentials ADD COLUMN private_key TEXT;");
390  sql ("ALTER TABLE lsc_credentials ADD COLUMN exe TEXT;");
391 
392  /* Set the database version to 3. */
393 
394  set_db_version (3);
395 
396  sql_commit ();
397 
398  return 0;
399 }
400 
406 int
408 {
409  iterator_t nvts;
410 
412 
413  /* Ensure that the database is currently version 3. */
414 
415  if (manage_db_version () != 3)
416  {
417  sql_rollback ();
418  return -1;
419  }
420 
421  /* Update the database. */
422 
423  /* The nvt_selectors table got a family column. */
424 
425  sql ("ALTER TABLE nvt_selectors ADD COLUMN family;");
426 
427  init_nvt_selector_iterator (&nvts, NULL, (config_t) 0, 2);
428  while (next (&nvts))
429  {
430  gchar *quoted_name = sql_quote (nvt_selector_iterator_name (&nvts));
431  gchar *quoted_nvt = sql_quote (nvt_selector_iterator_nvt (&nvts));
432  sql ("UPDATE nvt_selectors SET family ="
433  " (SELECT family FROM nvts where oid = '%s')"
434  " WHERE name = '%s';",
435  quoted_nvt, quoted_name);
436  g_free (quoted_name);
437  g_free (quoted_nvt);
438  }
439  cleanup_iterator (&nvts);
440 
441  /* Set the database version to 4. */
442 
443  set_db_version (4);
444 
445  sql_commit ();
446 
447  return 0;
448 }
449 
453 void
455 {
456  iterator_t rows;
457 
458  /* Table config_preferences. */
459  init_iterator (&rows,
460  "SELECT rowid, config, type, name, value"
461  " FROM config_preferences_4;");
462  while (next (&rows))
463  {
464  gchar *quoted_type = sql_insert (iterator_string (&rows, 2));
465  gchar *quoted_name = sql_insert (iterator_string (&rows, 3));
466  gchar *quoted_value = sql_insert (iterator_string (&rows, 4));
467  sql ("INSERT into config_preferences (id, config, type, name, value)"
468  " VALUES (%llu, %llu, %s, %s, %s);",
469  iterator_int64 (&rows, 0),
470  iterator_int64 (&rows, 1),
471  quoted_type,
472  quoted_name,
473  quoted_value);
474  g_free (quoted_type);
475  g_free (quoted_name);
476  g_free (quoted_value);
477  }
478  cleanup_iterator (&rows);
479  sql ("DROP TABLE config_preferences_4;");
480 
481  /* Table configs. */
482  init_iterator (&rows,
483  "SELECT rowid, name, nvt_selector, comment, family_count,"
484  " nvt_count, families_growing, nvts_growing"
485  " FROM configs_4;");
486  while (next (&rows))
487  {
488  gchar *quoted_name = sql_insert (iterator_string (&rows, 1));
489  gchar *quoted_nvt_selector = sql_insert (iterator_string (&rows, 2));
490  gchar *quoted_comment = sql_insert (iterator_string (&rows, 3));
491  sql ("INSERT into configs"
492  " (id, name, nvt_selector, comment, family_count, nvt_count,"
493  " families_growing, nvts_growing)"
494  " VALUES"
495  " (%llu, %s, %s, %s, %llu, %llu, %llu, %llu);",
496  iterator_int64 (&rows, 0),
497  quoted_name,
498  quoted_nvt_selector,
499  quoted_comment,
500  iterator_int64 (&rows, 4),
501  iterator_int64 (&rows, 5),
502  iterator_int64 (&rows, 6),
503  iterator_int64 (&rows, 7));
504  g_free (quoted_name);
505  g_free (quoted_nvt_selector);
506  g_free (quoted_comment);
507  }
508  cleanup_iterator (&rows);
509  sql ("DROP TABLE configs_4;");
510 
511  /* Table lsc_credentials. */
512  init_iterator (&rows,
513  "SELECT rowid, name, password, comment, public_key,"
514  " private_key, rpm, deb, exe"
515  " FROM lsc_credentials_4;");
516  while (next (&rows))
517  {
518  gchar *quoted_name = sql_insert (iterator_string (&rows, 1));
519  gchar *quoted_password = sql_insert (iterator_string (&rows, 2));
520  gchar *quoted_comment = sql_insert (iterator_string (&rows, 3));
521  gchar *quoted_public_key = sql_insert (iterator_string (&rows, 4));
522  gchar *quoted_private_key = sql_insert (iterator_string (&rows, 5));
523  gchar *quoted_rpm = sql_insert (iterator_string (&rows, 6));
524  gchar *quoted_deb = sql_insert (iterator_string (&rows, 7));
525  gchar *quoted_exe = sql_insert (iterator_string (&rows, 8));
526  sql ("INSERT into lsc_credentials"
527  " (id, name, password, comment, public_key, private_key, rpm, deb,"
528  " exe)"
529  " VALUES"
530  " (%llu, %s, %s, %s, %s, %s, %s, %s, %s);",
531  iterator_int64 (&rows, 0),
532  quoted_name,
533  quoted_password,
534  quoted_comment,
535  quoted_public_key,
536  quoted_private_key,
537  quoted_rpm,
538  quoted_deb,
539  quoted_exe);
540  g_free (quoted_name);
541  g_free (quoted_password);
542  g_free (quoted_comment);
543  g_free (quoted_public_key);
544  g_free (quoted_private_key);
545  g_free (quoted_rpm);
546  g_free (quoted_deb);
547  g_free (quoted_exe);
548  }
549  cleanup_iterator (&rows);
550  sql ("DROP TABLE lsc_credentials_4;");
551 
552  /* Table meta. */
553  init_iterator (&rows, "SELECT rowid, name, value FROM meta_4;");
554  while (next (&rows))
555  {
556  gchar *quoted_name = sql_insert (iterator_string (&rows, 1));
557  gchar *quoted_value = sql_insert (iterator_string (&rows, 2));
558  sql ("INSERT into meta (id, name, value)"
559  " VALUES (%llu, %s, %s);",
560  iterator_int64 (&rows, 0),
561  quoted_name,
562  quoted_value);
563  g_free (quoted_name);
564  g_free (quoted_value);
565  }
566  cleanup_iterator (&rows);
567  sql ("DROP TABLE meta_4;");
568 
569  /* Table nvt_preferences. */
570  init_iterator (&rows, "SELECT rowid, name, value FROM nvt_preferences_4;");
571  while (next (&rows))
572  {
573  gchar *quoted_name = sql_insert (iterator_string (&rows, 1));
574  gchar *quoted_value = sql_insert (iterator_string (&rows, 2));
575  sql ("INSERT into nvt_preferences (id, name, value)"
576  " VALUES (%llu, %s, %s);",
577  iterator_int64 (&rows, 0),
578  quoted_name,
579  quoted_value);
580  g_free (quoted_name);
581  g_free (quoted_value);
582  }
583  cleanup_iterator (&rows);
584  sql ("DROP TABLE nvt_preferences_4;");
585 
586  /* Table nvt_selectors. */
587  init_iterator (&rows,
588  "SELECT rowid, name, exclude, type, family_or_nvt, family"
589  " FROM nvt_selectors_4;");
590  while (next (&rows))
591  {
592  gchar *quoted_name = sql_insert (iterator_string (&rows, 1));
593  gchar *quoted_family_or_nvt = sql_insert (iterator_string (&rows, 4));
594  gchar *quoted_family = sql_insert (iterator_string (&rows, 5));
595  sql ("INSERT into nvt_selectors"
596  " (id, name, exclude, type, family_or_nvt, family)"
597  " VALUES"
598  " (%llu, %s, %llu, %llu, %s, %s);",
599  iterator_int64 (&rows, 0),
600  quoted_name,
601  iterator_int64 (&rows, 2),
602  iterator_int64 (&rows, 3),
603  quoted_family_or_nvt,
604  quoted_family);
605  g_free (quoted_name);
606  g_free (quoted_family_or_nvt);
607  g_free (quoted_family);
608  }
609  cleanup_iterator (&rows);
610  sql ("DROP TABLE nvt_selectors_4;");
611 
612  /* Table nvts. */
613  init_iterator (&rows,
614  "SELECT rowid, oid, version, name, summary, description,"
615  " copyright, cve, bid, xref, tag, sign_key_ids, category,"
616  " family"
617  " FROM nvts_4;");
618  while (next (&rows))
619  {
620  gchar *quoted_oid = sql_insert (iterator_string (&rows, 1));
621  gchar *quoted_version = sql_insert (iterator_string (&rows, 2));
622  gchar *quoted_name = sql_insert (iterator_string (&rows, 3));
623  gchar *quoted_summary = sql_insert (iterator_string (&rows, 4));
624  gchar *quoted_description = sql_insert (iterator_string (&rows, 5));
625  gchar *quoted_copyright = sql_insert (iterator_string (&rows, 6));
626  gchar *quoted_cve = sql_insert (iterator_string (&rows, 7));
627  gchar *quoted_bid = sql_insert (iterator_string (&rows, 8));
628  gchar *quoted_xref = sql_insert (iterator_string (&rows, 9));
629  gchar *quoted_tag = sql_insert (iterator_string (&rows, 10));
630  gchar *quoted_sign_key_ids = sql_insert (iterator_string (&rows, 11));
631  gchar *quoted_family = sql_insert (iterator_string (&rows, 13));
632 
633  {
634  /* Starting from revision 5726 on 2009-10-26 (just before 0.9.2),
635  * the Manager converts semicolons in OTP NVT descriptions to newlines
636  * before entering them in the database. Convert the existing
637  * semicolons here, because it is a convenient place to do it. */
638  gchar* pos = quoted_description;
639  while ((pos = strchr (pos, ';')))
640  pos[0] = '\n';
641  }
642 
643  sql ("INSERT into nvts"
644  " (id, oid, version, name, summary, description, copyright, cve,"
645  " bid, xref, tag, sign_key_ids, category, family)"
646  " VALUES"
647  " (%llu, %s, %s, %s, %s, %s, %s, %s, %s, %s,"
648  " %s, %s, %llu, %s);",
649  iterator_int64 (&rows, 0),
650  quoted_oid,
651  quoted_version,
652  quoted_name,
653  quoted_summary,
654  quoted_description,
655  quoted_copyright,
656  quoted_cve,
657  quoted_bid,
658  quoted_xref,
659  quoted_tag,
660  quoted_sign_key_ids,
661  iterator_int64 (&rows, 12),
662  quoted_family);
663  g_free (quoted_oid);
664  g_free (quoted_version);
665  g_free (quoted_name);
666  g_free (quoted_summary);
667  g_free (quoted_description);
668  g_free (quoted_copyright);
669  g_free (quoted_cve);
670  g_free (quoted_bid);
671  g_free (quoted_xref);
672  g_free (quoted_tag);
673  g_free (quoted_sign_key_ids);
674  g_free (quoted_family);
675  }
676  cleanup_iterator (&rows);
677  sql ("DROP TABLE nvts_4;");
678 
679  /* Table report_hosts. */
680  init_iterator (&rows,
681  "SELECT rowid, report, host, start_time, end_time,"
682  " attack_state, current_port, max_port"
683  " FROM report_hosts_4;");
684  while (next (&rows))
685  {
686  gchar *quoted_host = sql_insert (iterator_string (&rows, 2));
687  gchar *quoted_start_time = sql_insert (iterator_string (&rows, 3));
688  gchar *quoted_end_time = sql_insert (iterator_string (&rows, 4));
689  gchar *quoted_attack_state = sql_insert (iterator_string (&rows, 5));
690  gchar *quoted_current_port = sql_insert (iterator_string (&rows, 6));
691  gchar *quoted_max_port = sql_insert (iterator_string (&rows, 7));
692  sql ("INSERT into report_hosts"
693  " (id, report, host, start_time, end_time, attack_state,"
694  " current_port, max_port)"
695  " VALUES"
696  " (%llu, %llu, %s, %s, %s, %s, %s, %s);",
697  iterator_int64 (&rows, 0),
698  iterator_int64 (&rows, 1),
699  quoted_host,
700  quoted_start_time,
701  quoted_end_time,
702  quoted_attack_state,
703  quoted_current_port,
704  quoted_max_port);
705  g_free (quoted_host);
706  g_free (quoted_start_time);
707  g_free (quoted_end_time);
708  g_free (quoted_attack_state);
709  g_free (quoted_current_port);
710  g_free (quoted_max_port);
711  }
712  cleanup_iterator (&rows);
713  sql ("DROP TABLE report_hosts_4;");
714 
715  /* Table report_results. */
716  init_iterator (&rows, "SELECT rowid, report, result FROM report_results_4;");
717  while (next (&rows))
718  {
719  sql ("INSERT into report_results (id, report, result)"
720  " VALUES (%llu, %llu, %llu)",
721  iterator_int64 (&rows, 0),
722  iterator_int64 (&rows, 1),
723  iterator_int64 (&rows, 2));
724  }
725  cleanup_iterator (&rows);
726  sql ("DROP TABLE report_results_4;");
727 
728  /* Table reports. */
729  init_iterator (&rows,
730  "SELECT rowid, uuid, hidden, task, date, start_time, end_time,"
731  " nbefile, comment, scan_run_status"
732  " FROM reports_4;");
733  while (next (&rows))
734  {
735  gchar *quoted_uuid = sql_insert (iterator_string (&rows, 1));
736  gchar *quoted_start_time = sql_insert (iterator_string (&rows, 5));
737  gchar *quoted_end_time = sql_insert (iterator_string (&rows, 6));
738  gchar *quoted_nbefile = sql_insert (iterator_string (&rows, 7));
739  gchar *quoted_comment = sql_insert (iterator_string (&rows, 8));
740  sql ("INSERT into reports"
741  " (id, uuid, hidden, task, date, start_time, end_time, nbefile,"
742  " comment, scan_run_status)"
743  " VALUES"
744  " (%llu, %s, %llu, %llu, %llu, %s, %s, %s, %s, %llu);",
745  iterator_int64 (&rows, 0),
746  quoted_uuid,
747  iterator_int64 (&rows, 2),
748  iterator_int64 (&rows, 3),
749  iterator_int64 (&rows, 4),
750  quoted_start_time,
751  quoted_end_time,
752  quoted_nbefile,
753  quoted_comment,
754  iterator_int64 (&rows, 9));
755  g_free (quoted_uuid);
756  g_free (quoted_start_time);
757  g_free (quoted_end_time);
758  g_free (quoted_nbefile);
759  g_free (quoted_comment);
760  }
761  cleanup_iterator (&rows);
762  sql ("DROP TABLE reports_4;");
763 
764  /* Table results. */
765  init_iterator (&rows,
766  "SELECT rowid, task, subnet, host, port, nvt, type,"
767  " description"
768  " FROM results_4;");
769  while (next (&rows))
770  {
771  gchar *quoted_subnet = sql_insert (iterator_string (&rows, 2));
772  gchar *quoted_host = sql_insert (iterator_string (&rows, 3));
773  gchar *quoted_port = sql_insert (iterator_string (&rows, 4));
774  gchar *quoted_nvt = sql_insert (iterator_string (&rows, 5));
775  gchar *quoted_type = sql_insert (iterator_string (&rows, 6));
776  gchar *quoted_description = sql_insert (iterator_string (&rows, 7));
777  sql ("INSERT into results"
778  " (id, task, subnet, host, port, nvt, type, description)"
779  " VALUES"
780  " (%llu, %llu, %s, %s, %s, %s, %s, %s);",
781  iterator_int64 (&rows, 0),
782  iterator_int64 (&rows, 1),
783  quoted_subnet,
784  quoted_host,
785  quoted_port,
786  quoted_nvt,
787  quoted_type,
788  quoted_description);
789  g_free (quoted_subnet);
790  g_free (quoted_host);
791  g_free (quoted_port);
792  g_free (quoted_nvt);
793  g_free (quoted_type);
794  g_free (quoted_description);
795  }
796  cleanup_iterator (&rows);
797  sql ("DROP TABLE results_4;");
798 
799  /* Table targets. */
800  init_iterator (&rows, "SELECT rowid, name, hosts, comment FROM targets_4;");
801  while (next (&rows))
802  {
803  gchar *quoted_name = sql_insert (iterator_string (&rows, 1));
804  gchar *quoted_hosts = sql_insert (iterator_string (&rows, 2));
805  gchar *quoted_comment = sql_insert (iterator_string (&rows, 3));
806  sql ("INSERT into targets (id, name, hosts, comment)"
807  " VALUES (%llu, %s, %s, %s);",
808  iterator_int64 (&rows, 0),
809  quoted_name,
810  quoted_hosts,
811  quoted_comment);
812  g_free (quoted_name);
813  g_free (quoted_hosts);
814  g_free (quoted_comment);
815  }
816  cleanup_iterator (&rows);
817  sql ("DROP TABLE targets_4;");
818 
819  /* Table task_files. */
820  init_iterator (&rows, "SELECT rowid, task, name, content FROM task_files_4;");
821  while (next (&rows))
822  {
823  gchar *quoted_name = sql_insert (iterator_string (&rows, 2));
824  gchar *quoted_content = sql_insert (iterator_string (&rows, 3));
825  sql ("INSERT into task_files (id, task, name, content)"
826  " VALUES (%llu, %llu, %s, %s);",
827  iterator_int64 (&rows, 0),
828  iterator_int64 (&rows, 1),
829  quoted_name,
830  quoted_content);
831  g_free (quoted_name);
832  g_free (quoted_content);
833  }
834  cleanup_iterator (&rows);
835  sql ("DROP TABLE task_files_4;");
836 
837  /* Table tasks. */
838  init_iterator (&rows,
839  "SELECT rowid, uuid, name, hidden, time, comment, description,"
840  " owner, run_status, start_time, end_time, config, target"
841  " FROM tasks_4;");
842  while (next (&rows))
843  {
844  gchar *quoted_uuid = sql_insert (iterator_string (&rows, 1));
845  gchar *quoted_name = sql_insert (iterator_string (&rows, 2));
846  gchar *quoted_time = sql_insert (iterator_string (&rows, 4));
847  gchar *quoted_comment = sql_insert (iterator_string (&rows, 5));
848  gchar *quoted_description = sql_insert (iterator_string (&rows, 6));
849  gchar *quoted_start_time = sql_insert (iterator_string (&rows, 9));
850  gchar *quoted_end_time = sql_insert (iterator_string (&rows, 10));
851  gchar *quoted_config = sql_insert (iterator_string (&rows, 11));
852  gchar *quoted_target = sql_insert (iterator_string (&rows, 12));
853  sql ("INSERT into tasks"
854  " (id, uuid, name, hidden, time, comment, description, owner,"
855  " run_status, start_time, end_time, config, target)"
856  " VALUES"
857  " (%llu, %s, %s, %llu, %s, %s, %s, %llu, %llu, %s,"
858  " %s, %s, %s);",
859  iterator_int64 (&rows, 0),
860  quoted_uuid,
861  quoted_name,
862  iterator_int64 (&rows, 3),
863  quoted_time,
864  quoted_comment,
865  quoted_description,
866  iterator_int64 (&rows, 7),
867  iterator_int64 (&rows, 8),
868  quoted_start_time,
869  quoted_end_time,
870  quoted_config,
871  quoted_target);
872  g_free (quoted_uuid);
873  g_free (quoted_name);
874  g_free (quoted_time);
875  g_free (quoted_comment);
876  g_free (quoted_description);
877  g_free (quoted_start_time);
878  g_free (quoted_end_time);
879  g_free (quoted_config);
880  g_free (quoted_target);
881  }
882  cleanup_iterator (&rows);
883  sql ("DROP TABLE tasks_4;");
884 
885  /* Table users. */
886  init_iterator (&rows, "SELECT rowid, name, password FROM users_4;");
887  while (next (&rows))
888  {
889  gchar *quoted_name = sql_insert (iterator_string (&rows, 1));
890  gchar *quoted_password = sql_insert (iterator_string (&rows, 2));
891  sql ("INSERT into users (id, name, password)"
892  " VALUES (%llu, %s, %s);",
893  iterator_int64 (&rows, 0),
894  quoted_name,
895  quoted_password);
896  g_free (quoted_name);
897  g_free (quoted_password);
898  }
899  cleanup_iterator (&rows);
900  sql ("DROP TABLE users_4;");
901 }
902 
908 int
910 {
912 
913  /* Ensure that the database is currently version 4. */
914 
915  if (manage_db_version () != 4)
916  {
917  sql_rollback ();
918  return -1;
919  }
920 
921  /* Update the database. */
922 
923  /* Every table got an "id INTEGER PRIMARY KEY" column. As the column is a
924  * primary key, every table must be recreated and the data transfered.
925  *
926  * Also, starting from revision 5726 on 2009-10-26 (just before 0.9.2),
927  * the Manager converts semicolons in OTP NVT descriptions to newlines
928  * before entering them in the database. Convert the existing
929  * semicolons while transfering the data. This should have been an
930  * entirely separate version and migration between the current 4 and 5. */
931 
932  /* Ensure that all tables exist that will be adjusted below. */
933 
934  /* Both introduced between version 1 and 2. */
935  sql ("CREATE TABLE IF NOT EXISTS nvt_preferences (name, value);");
936  sql ("CREATE TABLE IF NOT EXISTS task_files (task INTEGER, name, content);");
937 
938  /* Move the tables away. */
939 
940  sql ("ALTER TABLE config_preferences RENAME TO config_preferences_4;");
941  sql ("ALTER TABLE configs RENAME TO configs_4;");
942  sql ("ALTER TABLE lsc_credentials RENAME TO lsc_credentials_4;");
943  sql ("ALTER TABLE meta RENAME TO meta_4;");
944  sql ("ALTER TABLE nvt_preferences RENAME TO nvt_preferences_4;");
945  sql ("ALTER TABLE nvt_selectors RENAME TO nvt_selectors_4;");
946  sql ("ALTER TABLE nvts RENAME TO nvts_4;");
947  sql ("ALTER TABLE report_hosts RENAME TO report_hosts_4;");
948  sql ("ALTER TABLE report_results RENAME TO report_results_4;");
949  sql ("ALTER TABLE reports RENAME TO reports_4;");
950  sql ("ALTER TABLE results RENAME TO results_4;");
951  sql ("ALTER TABLE targets RENAME TO targets_4;");
952  sql ("ALTER TABLE task_files RENAME TO task_files_4;");
953  sql ("ALTER TABLE tasks RENAME TO tasks_4;");
954  sql ("ALTER TABLE users RENAME TO users_4;");
955 
956  /* Create the new tables in version 4 format. */
957 
958  create_tables_version_4 ();
959 
960  /* Copy the data into the new tables, dropping the old tables. */
961 
963 
964  /* Set the database version to 5. */
965 
966  set_db_version (5);
967 
968  sql_commit ();
969 
970  /* All the moving may have left much empty space, so vacuum. */
971 
972  sql ("VACUUM;");
973 
974  return 0;
975 }
976 
983 void
984 migrate_5_to_6_move_other_config (const char *predefined_config_name,
985  config_t predefined_config_id)
986 {
987  if (sql_int ("SELECT COUNT(*) = 0 FROM configs"
988  " WHERE name = '%s';",
989  predefined_config_name)
990  && sql_int ("SELECT COUNT(*) = 1 FROM configs"
991  " WHERE id = %llu;",
992  predefined_config_id))
993  {
994  config_t config;
995  char *name;
996  gchar *quoted_name;
997 
998  sql ("INSERT into configs (nvt_selector, comment, family_count,"
999  " nvt_count, nvts_growing, families_growing)"
1000  " SELECT nvt_selector, comment, family_count,"
1001  " nvt_count, nvts_growing, families_growing"
1002  " FROM configs"
1003  " WHERE id = %llu;",
1004  predefined_config_id);
1005  /* This ID will be larger then predefined_config_id because
1006  * predefined_config_id exists already. At worst the ID will be one
1007  * larger. */
1008  config = sql_last_insert_id ();
1009  sql ("UPDATE config_preferences SET config = %llu WHERE config = %llu;",
1010  config,
1011  predefined_config_id);
1012  name = sql_string ("SELECT name FROM configs WHERE id = %llu;",
1013  predefined_config_id);
1014  if (name == NULL)
1015  {
1016  sql_rollback ();
1017  abort ();
1018  }
1019  quoted_name = sql_quote (name);
1020  free (name);
1021  /* Table tasks references config by name, so it stays the same. */
1022  sql ("DELETE FROM configs WHERE id = %llu;",
1023  predefined_config_id);
1024  sql ("UPDATE configs SET name = '%s' WHERE id = %llu;",
1025  quoted_name,
1026  config);
1027  g_free (quoted_name);
1028  }
1029 }
1030 
1036 int
1038 {
1040 
1041  /* Ensure that the database is currently version 5. */
1042 
1043  if (manage_db_version () != 5)
1044  {
1045  sql_rollback ();
1046  return -1;
1047  }
1048 
1049  /* Update the database. */
1050 
1051  /* The predefined configs got predefined ID's and the manager now also
1052  * caches counts for growing configs. */
1053 
1054  /* Fail with a message if the predefined configs have somehow got ID's
1055  * other than the usual ones. */
1056 
1057  if (sql_int ("SELECT COUNT(*) = 0 OR id == 1 FROM configs"
1058  " WHERE name = 'Full and fast';")
1059  && sql_int ("SELECT COUNT(*) = 0 OR id == 2 FROM configs"
1060  " WHERE name = 'Full and fast ultimate';")
1061  && sql_int ("SELECT COUNT(*) = 0 OR id == 3 FROM configs"
1062  " WHERE name = 'Full and very deep';")
1063  && sql_int ("SELECT COUNT(*) = 0 OR id == 4 FROM configs"
1064  " WHERE name = 'Full and very deep ultimate';"))
1065  {
1066  /* Any predefined configs are OK. Move any other configs that have the
1067  * predefined ID's. */
1068 
1069  /* The ID of the moved config may be only one larger, so these must
1070  * be done in ID order. */
1071  migrate_5_to_6_move_other_config ("Full and fast", 1);
1072  migrate_5_to_6_move_other_config ("Full and fast ultimate", 2);
1073  migrate_5_to_6_move_other_config ("Full and very deep", 3);
1074  migrate_5_to_6_move_other_config ("Full and very deep ultimate", 4);
1075  }
1076  else
1077  {
1078  g_warning ("%s: a predefined config has moved from the standard location,"
1079  " giving up\n",
1080  __FUNCTION__);
1081  sql_rollback ();
1082  return -1;
1083  }
1084 
1085  /* This would need a duplicate version of update_all_config_caches that
1086  * worked with the version 6 database. Just let the cache be wrong. This
1087  * is a very old version now. */
1088 #if 0
1089  /* Update cache counts for growing configs. */
1090 
1091  update_all_config_caches ();
1092 #endif
1093 
1094  /* Set the database version to 6. */
1095 
1096  set_db_version (6);
1097 
1098  sql_commit ();
1099 
1100  return 0;
1101 }
1102 
1108 int
1110 {
1112 
1113  /* Ensure that the database is currently version 6. */
1114 
1115  if (manage_db_version () != 6)
1116  {
1117  sql_rollback ();
1118  return -1;
1119  }
1120 
1121  /* Update the database. */
1122 
1123  /* Add lsc_credential column to targets table. */
1124  sql ("ALTER TABLE targets ADD COLUMN lsc_credential INTEGER;");
1125  sql ("UPDATE targets SET lsc_credential = 0;");
1126 
1127  /* Set the database version to 7. */
1128 
1129  set_db_version (7);
1130 
1131  sql_commit ();
1132 
1133  return 0;
1134 }
1135 
1141 int
1143 {
1145 
1146  /* Ensure that the database is currently version 7. */
1147 
1148  if (manage_db_version () != 7)
1149  {
1150  sql_rollback ();
1151  return -1;
1152  }
1153 
1154  /* Update the database. */
1155 
1156  /* The lsc_credentials table got a login column. */
1157 
1158  sql ("ALTER TABLE lsc_credentials ADD COLUMN login;");
1159  sql ("UPDATE lsc_credentials SET login = name;");
1160 
1161  /* Set the database version to 8. */
1162 
1163  set_db_version (8);
1164 
1165  sql_commit ();
1166 
1167  return 0;
1168 }
1169 
1175 int
1177 {
1179 
1180  /* Ensure that the database is currently version 8. */
1181 
1182  if (manage_db_version () != 8)
1183  {
1184  sql_rollback ();
1185  return -1;
1186  }
1187 
1188  /* Update the database. */
1189 
1192  /* Ensure that all tables that will be modified here exist. These were
1193  * all added after version 8 anyway. */
1194 
1195  sql ("CREATE TABLE IF NOT EXISTS escalators"
1196  " (id INTEGER PRIMARY KEY, name UNIQUE, comment, event INTEGER,"
1197  " condition INTEGER, method INTEGER);");
1198 
1199  sql ("CREATE TABLE IF NOT EXISTS agents"
1200  " (id INTEGER PRIMARY KEY, name UNIQUE, comment, installer TEXT,"
1201  " howto_install TEXT, howto_use TEXT);");
1202 
1203  /* Many tables got an owner column. */
1204 
1205  sql ("ALTER TABLE targets ADD COLUMN owner INTEGER;");
1206  sql ("UPDATE targets SET owner = NULL;");
1207 
1208  sql ("ALTER TABLE configs ADD COLUMN owner INTEGER;");
1209  sql ("UPDATE configs SET owner = NULL;");
1210 
1211  sql ("ALTER TABLE lsc_credentials ADD COLUMN owner INTEGER;");
1212  sql ("UPDATE lsc_credentials SET owner = NULL;");
1213 
1214  sql ("ALTER TABLE escalators ADD COLUMN owner INTEGER;");
1215  sql ("UPDATE escalators SET owner = NULL;");
1216 
1217  sql ("ALTER TABLE reports ADD COLUMN owner INTEGER;");
1218  sql ("UPDATE reports SET owner = NULL;");
1219 
1220  sql ("ALTER TABLE agents ADD COLUMN owner INTEGER;");
1221  sql ("UPDATE agents SET owner = NULL;");
1222 
1223  /* The owner column in tasks changed type from string to int. This
1224  * may be a redundant conversion, as SQLite may have converted these
1225  * values automatically in each query anyway. */
1226 
1227  sql ("UPDATE tasks SET owner = CAST (owner AS INTEGER);"),
1228 
1229  /* Set the database version to 9. */
1230 
1231  set_db_version (9);
1232 
1233  sql_commit ();
1234 
1235  return 0;
1236 }
1237 
1247 gchar *
1248 migrate_9_to_10_user_uuid (const char *name)
1249 {
1250  gchar *uuid_file;
1251 
1252  uuid_file = g_build_filename (OPENVAS_STATE_DIR, "users", name, "uuid", NULL);
1253  if (g_file_test (uuid_file, G_FILE_TEST_EXISTS))
1254  {
1255  gsize size;
1256  gchar *uuid;
1257  /* File exists, get its content (the uuid). */
1258  if (g_file_get_contents (uuid_file, &uuid, &size, NULL))
1259  {
1260  if (strlen (uuid) < 36)
1261  g_free (uuid);
1262  else
1263  {
1264  g_free (uuid_file);
1265  /* Drop any trailing characters. */
1266  uuid[36] = '\0';
1267  return uuid;
1268  }
1269  }
1270  }
1271  g_free (uuid_file);
1272  return NULL;
1273 }
1274 
1280 int
1282 {
1283  iterator_t rows;
1284 
1286 
1287  /* Ensure that the database is currently version 9. */
1288 
1289  if (manage_db_version () != 9)
1290  {
1291  sql_rollback ();
1292  return -1;
1293  }
1294 
1295  /* Update the database. */
1296 
1297  /* The user table got a unique "uuid" column and lost the
1298  * uniqueness of its "name" column. */
1299 
1302  sql ("ALTER TABLE users RENAME TO users_9;");
1303 
1304  sql ("CREATE TABLE users"
1305  " (id INTEGER PRIMARY KEY, uuid UNIQUE, name, password);");
1306 
1307  init_iterator (&rows, "SELECT id, name, password FROM users_9;");
1308  while (next (&rows))
1309  {
1310  gchar *quoted_name, *quoted_password, *uuid;
1311 
1312  uuid = migrate_9_to_10_user_uuid (iterator_string (&rows, 1));
1313  if (uuid == NULL)
1314  {
1315  uuid = openvas_uuid_make ();
1316  if (uuid == NULL)
1317  {
1318  cleanup_iterator (&rows);
1319  sql_rollback ();
1320  return -1;
1321  }
1322  }
1323 
1324  quoted_name = sql_insert (iterator_string (&rows, 1));
1325  quoted_password = sql_insert (iterator_string (&rows, 2));
1326  sql ("INSERT into users (id, uuid, name, password)"
1327  " VALUES (%llu, '%s', %s, %s);",
1328  iterator_int64 (&rows, 0),
1329  uuid,
1330  quoted_name,
1331  quoted_password);
1332  g_free (uuid);
1333  g_free (quoted_name);
1334  g_free (quoted_password);
1335  }
1336  cleanup_iterator (&rows);
1337  sql ("DROP TABLE users_9;");
1338 
1339  /* Set the database version to 10. */
1340 
1341  set_db_version (10);
1342 
1343  sql_commit ();
1344 
1345  return 0;
1346 }
1347 
1353 int
1355 {
1357 
1358  /* Ensure that the database is currently version 10. */
1359 
1360  if (manage_db_version () != 10)
1361  {
1362  sql_rollback ();
1363  return -1;
1364  }
1365 
1366  /* Update the database. */
1367 
1368  /* The config and target columns of the tasks table changed from the name
1369  * of the config/target to the id of the config/target.
1370  *
1371  * Recreate the table, in order to add INTEGER to the column definitions. */
1372 
1375  sql ("ALTER TABLE tasks RENAME TO tasks_10;");
1376 
1377  sql ("CREATE TABLE tasks"
1378  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, hidden INTEGER,"
1379  " time, comment, description, run_status INTEGER, start_time,"
1380  " end_time, config INTEGER, target INTEGER);");
1381 
1382  sql ("INSERT into tasks"
1383  " (id, uuid, owner, name, hidden, time, comment, description,"
1384  " run_status, start_time, end_time, config, target)"
1385  " SELECT"
1386  " id, uuid, owner, name, hidden, time, comment, description,"
1387  " run_status, start_time, end_time,"
1388  " (SELECT id FROM configs WHERE configs.name = tasks_10.config),"
1389  " (SELECT id FROM targets WHERE targets.name = tasks_10.target)"
1390  " FROM tasks_10;");
1391 
1392  sql ("DROP TABLE tasks_10;");
1393 
1394  /* Set the database version to 11. */
1395 
1396  set_db_version (11);
1397 
1398  sql_commit ();
1399 
1400  return 0;
1401 }
1402 
1408 int
1410 {
1412 
1413  /* Ensure that the database is currently version 11. */
1414 
1415  if (manage_db_version () != 11)
1416  {
1417  sql_rollback ();
1418  return -1;
1419  }
1420 
1421  /* Update the database. */
1422 
1423  /* Tables agents, configs and escalators were relieved of the UNIQUE
1424  * constraint on the name column.
1425  *
1426  * Recreate the tables, in order to remove the contraint. */
1427 
1430  sql ("ALTER TABLE agents RENAME TO agents_11;");
1431 
1432  sql ("CREATE TABLE agents"
1433  " (id INTEGER PRIMARY KEY, owner INTEGER, name, comment,"
1434  " installer TEXT, howto_install TEXT, howto_use TEXT);");
1435 
1436  sql ("INSERT into agents"
1437  " (id, owner, name, comment, installer, howto_install, howto_use)"
1438  " SELECT"
1439  " id, owner, name, comment, installer, howto_install, howto_use"
1440  " FROM agents_11;");
1441 
1442  sql ("DROP TABLE agents_11;");
1443 
1444  sql ("ALTER TABLE configs RENAME TO configs_11;");
1445 
1446  sql ("CREATE TABLE configs"
1447  " (id INTEGER PRIMARY KEY, owner INTEGER, name, nvt_selector, comment,"
1448  " family_count INTEGER, nvt_count INTEGER, families_growing INTEGER,"
1449  " nvts_growing INTEGER);");
1450 
1451  sql ("INSERT into configs"
1452  " (id, owner, name, nvt_selector, comment, family_count, nvt_count,"
1453  " families_growing, nvts_growing)"
1454  " SELECT"
1455  " id, owner, name, nvt_selector, comment, family_count, nvt_count,"
1456  " families_growing, nvts_growing"
1457  " FROM configs_11;");
1458 
1459  sql ("DROP TABLE configs_11;");
1460 
1461  sql ("ALTER TABLE escalators RENAME TO escalators_11;");
1462 
1463  sql ("CREATE TABLE escalators"
1464  " (id INTEGER PRIMARY KEY, owner INTEGER, name, comment, event INTEGER,"
1465  " condition INTEGER, method INTEGER);");
1466 
1467  sql ("INSERT into escalators"
1468  " (id, owner, name, comment, event, condition, method)"
1469  " SELECT"
1470  " id, owner, name, comment, event, condition, method"
1471  " FROM escalators_11;");
1472 
1473  sql ("DROP TABLE escalators_11;");
1474 
1475  /* Set the database version to 12. */
1476 
1477  set_db_version (12);
1478 
1479  sql_commit ();
1480 
1481  return 0;
1482 }
1483 
1489 int
1491 {
1492  iterator_t rows;
1493 
1495 
1496  /* Ensure that the database is currently version 12. */
1497 
1498  if (manage_db_version () != 12)
1499  {
1500  sql_rollback ();
1501  return -1;
1502  }
1503 
1504  /* Update the database. */
1505 
1506  /* Table nvt_selectors column name changed to a UUID.
1507  *
1508  * Replace names with UUIDs, ensuring that the 'All' selector gets the
1509  * predefined UUID. */
1510 
1513  init_iterator (&rows, "SELECT distinct name FROM nvt_selectors;");
1514  while (next (&rows))
1515  {
1516  gchar *quoted_name, *uuid;
1517 
1518  if (strcmp (iterator_string (&rows, 0), "All") == 0)
1519  continue;
1520 
1521  uuid = openvas_uuid_make ();
1522  if (uuid == NULL)
1523  {
1524  cleanup_iterator (&rows);
1525  sql_rollback ();
1526  return -1;
1527  }
1528 
1529  quoted_name = sql_insert (iterator_string (&rows, 0));
1530 
1531  sql ("UPDATE nvt_selectors SET name = '%s' WHERE name = %s;",
1532  uuid,
1533  quoted_name);
1534 
1535  sql ("UPDATE configs SET nvt_selector = '%s' WHERE nvt_selector = %s;",
1536  uuid,
1537  quoted_name);
1538 
1539  g_free (uuid);
1540  g_free (quoted_name);
1541  }
1542  cleanup_iterator (&rows);
1543 
1544  if (sql_int ("SELECT COUNT(*) FROM nvt_selectors WHERE name = '"
1546  sql ("DELETE FROM nvt_selectors WHERE name = 'All';");
1547  else
1548  sql ("UPDATE nvt_selectors"
1549  " SET name = '" MANAGE_NVT_SELECTOR_UUID_ALL "'"
1550  " WHERE name = 'All';");
1551 
1552  sql ("UPDATE configs"
1553  " SET nvt_selector = '" MANAGE_NVT_SELECTOR_UUID_ALL "'"
1554  " WHERE nvt_selector = 'All';");
1555 
1556  /* Set the database version to 13. */
1557 
1558  set_db_version (13);
1559 
1560  sql_commit ();
1561 
1562  return 0;
1563 }
1564 
1570 int
1572 {
1574 
1575  /* Ensure that the database is currently version 13. */
1576 
1577  if (manage_db_version () != 13)
1578  {
1579  sql_rollback ();
1580  return -1;
1581  }
1582 
1583  /* Update the database. */
1584 
1585  /* Table results got a UUID column. */
1586 
1589  sql ("ALTER TABLE results ADD COLUMN uuid;");
1590  sql ("UPDATE results SET uuid = make_uuid();");
1591 
1592  /* Set the database version to 14. */
1593 
1594  set_db_version (14);
1595 
1596  sql_commit ();
1597 
1598  return 0;
1599 }
1600 
1606 int
1608 {
1610 
1611  /* Ensure that the database is currently version 14. */
1612 
1613  if (manage_db_version () != 14)
1614  {
1615  sql_rollback ();
1616  return -1;
1617  }
1618 
1619  /* Update the database. */
1620 
1621  /* Table tasks got columns for scheduling info. */
1622 
1625  sql ("ALTER TABLE tasks ADD COLUMN schedule INTEGER;");
1626  sql ("ALTER TABLE tasks ADD COLUMN schedule_next_time;");
1627  sql ("UPDATE tasks SET schedule = 0, schedule_next_time = 0;");
1628 
1629  /* Set the database version to 15. */
1630 
1631  set_db_version (15);
1632 
1633  sql_commit ();
1634 
1635  return 0;
1636 }
1637 
1643 int
1645 {
1647 
1648  /* Ensure that the database is currently version 15. */
1649 
1650  if (manage_db_version () != 15)
1651  {
1652  sql_rollback ();
1653  return -1;
1654  }
1655 
1656  /* Update the database. */
1657 
1658  /* Table schedules got a period_months column. */
1659 
1662  sql ("CREATE TABLE IF NOT EXISTS schedules"
1663  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment,"
1664  " first_time, period, duration);");
1665 
1666  sql ("ALTER TABLE schedules ADD COLUMN period_months;");
1667  sql ("UPDATE schedules SET period_months = 0;");
1668 
1669  /* GSA was hardcoded to set the comment to "comment" before revision 7157,
1670  * so clear all task comments here. */
1671 
1672  sql ("UPDATE tasks SET comment = '';");
1673 
1674  /* Set the database version to 16. */
1675 
1676  set_db_version (16);
1677 
1678  sql_commit ();
1679 
1680  return 0;
1681 }
1682 
1688 int
1690 {
1691  iterator_t rows;
1692 
1694 
1695  /* Ensure that the database is currently version 16. */
1696 
1697  if (manage_db_version () != 16)
1698  {
1699  sql_rollback ();
1700  return -1;
1701  }
1702 
1703  /* Update the database. */
1704 
1705  /* Table nvts got columns for CVSS base and risk factor. */
1706 
1709  sql ("ALTER TABLE nvts ADD COLUMN cvss_base;");
1710  sql ("ALTER TABLE nvts ADD COLUMN risk_factor;");
1711 
1712  /* Move the CVSS and risk values out of any existing tags. */
1713 
1714  init_iterator (&rows, "SELECT id, tag FROM nvts;");
1715  while (next (&rows))
1716  {
1717  gchar *tags, *cvss_base;
1718 
1719  /* With db version 83, the risk_factor is dropped anyway, so
1720  * the value can be ignored already here.
1721  */
1722  parse_tags (iterator_string (&rows, 1), &tags, &cvss_base);
1723 
1724  sql ("UPDATE nvts SET cvss_base = '%s', risk_factor = '%s', tag = '%s'"
1725  " WHERE id = %llu;",
1726  cvss_base ? cvss_base : "",
1727  "",
1728  tags ? tags : "",
1729  iterator_int64 (&rows, 0));
1730 
1731  g_free (tags);
1732  g_free (cvss_base);
1733  }
1734  cleanup_iterator (&rows);
1735 
1736  /* Set the database version to 17. */
1737 
1738  set_db_version (17);
1739 
1740  sql_commit ();
1741 
1742  return 0;
1743 }
1744 
1750 void
1752 {
1753  if (sql_int ("SELECT count(*) FROM config_preferences"
1754  " WHERE config = %llu"
1755  " AND name ="
1756  " 'Ping Host[checkbox]:Mark unrechable Hosts as dead"
1757  " (not scanning)'",
1758  config)
1759  == 0)
1760  sql ("INSERT into config_preferences (config, type, name, value)"
1761  " VALUES (%llu, 'PLUGINS_PREFS',"
1762  " 'Ping Host[checkbox]:Mark unrechable Hosts as dead (not scanning)',"
1763  " 'yes');",
1764  config);
1765 }
1766 
1772 int
1774 {
1776 
1777  /* Ensure that the database is currently version 17. */
1778 
1779  if (manage_db_version () != 17)
1780  {
1781  sql_rollback ();
1782  return -1;
1783  }
1784 
1785  /* Update the database. */
1786 
1787  /* NVT "Ping Host" was added to the predefined configs, with the
1788  * "Mark unrechable..." preference set to "yes". */
1789 
1792  /* Add "Ping Host" to the "All" NVT selector. */
1793 
1794  if (sql_int ("SELECT count(*) FROM nvt_selectors WHERE name ="
1796  " AND family_or_nvt = '1.3.6.1.4.1.25623.1.0.100315';")
1797  == 0)
1798  {
1799  sql ("INSERT into nvt_selectors"
1800  " (name, exclude, type, family_or_nvt, family)"
1801  " VALUES ('" MANAGE_NVT_SELECTOR_UUID_ALL "', 0, "
1802  G_STRINGIFY (NVT_SELECTOR_TYPE_NVT) ","
1803  /* OID of the "Ping Host" NVT. */
1804  " '1.3.6.1.4.1.25623.1.0.100315', 'Port scanners');");
1805  }
1806 
1807  /* Ensure the preference is set on the predefined configs. */
1808 
1813 
1814  /* Set the database version to 18. */
1815 
1816  set_db_version (18);
1817 
1818  sql_commit ();
1819 
1820  return 0;
1821 }
1822 
1828 int
1830 {
1832 
1833  /* Ensure that the database is currently version 18. */
1834 
1835  if (manage_db_version () != 18)
1836  {
1837  sql_rollback ();
1838  return -1;
1839  }
1840 
1841  /* Update the database. */
1842 
1843  /* Many tables got a unique UUID column. As a result the predefined
1844  * configs and target got fixed UUIDs.
1845  *
1846  * Recreate the tables, in order to add the unique contraint. */
1847 
1850  sql ("ALTER TABLE agents RENAME TO agents_18;");
1851 
1852  sql ("CREATE TABLE agents"
1853  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
1854  " installer TEXT, howto_install TEXT, howto_use TEXT);");
1855 
1856  sql ("INSERT into agents"
1857  " (id, uuid, owner, name, comment, installer, howto_install, howto_use)"
1858  " SELECT"
1859  " id, make_uuid (), owner, name, comment, installer, howto_install, howto_use"
1860  " FROM agents_18;");
1861 
1862  sql ("DROP TABLE agents_18;");
1863 
1864  sql ("ALTER TABLE configs RENAME TO configs_18;");
1865 
1866  sql ("CREATE TABLE configs"
1867  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name,"
1868  " nvt_selector, comment, family_count INTEGER, nvt_count INTEGER,"
1869  " families_growing INTEGER, nvts_growing INTEGER);");
1870 
1871  sql ("INSERT into configs"
1872  " (id, uuid, owner, name, nvt_selector, comment, family_count,"
1873  " nvt_count, families_growing, nvts_growing)"
1874  " SELECT"
1875  " id, make_uuid (), owner, name, nvt_selector, comment, family_count,"
1876  " nvt_count, families_growing, nvts_growing"
1877  " FROM configs_18;");
1878 
1879  sql ("DROP TABLE configs_18;");
1880 
1881  sql ("ALTER TABLE escalators RENAME TO escalators_18;");
1882 
1883  sql ("CREATE TABLE escalators"
1884  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
1885  " event INTEGER, condition INTEGER, method INTEGER);");
1886 
1887  sql ("INSERT into escalators"
1888  " (id, uuid, owner, name, comment, event, condition, method)"
1889  " SELECT"
1890  " id, make_uuid (), owner, name, comment, event, condition, method"
1891  " FROM escalators_18;");
1892 
1893  sql ("DROP TABLE escalators_18;");
1894 
1895  sql ("ALTER TABLE lsc_credentials RENAME TO lsc_credentials_18;");
1896 
1897  sql ("CREATE TABLE lsc_credentials"
1898  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, login,"
1899  " password, comment, public_key TEXT, private_key TEXT, rpm TEXT,"
1900  " deb TEXT, exe TEXT);");
1901 
1902  sql ("INSERT into lsc_credentials"
1903  " (id, uuid, owner, name, login, password, comment, public_key,"
1904  " private_key, rpm, deb, exe)"
1905  " SELECT"
1906  " id, make_uuid (), owner, name, login, password, comment, public_key,"
1907  " private_key, rpm, deb, exe"
1908  " FROM lsc_credentials_18;");
1909 
1910  sql ("DROP TABLE lsc_credentials_18;");
1911 
1912  sql ("ALTER TABLE targets RENAME TO targets_18;");
1913 
1914  sql ("CREATE TABLE targets"
1915  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, hosts,"
1916  " comment, lsc_credential INTEGER);");
1917 
1918  sql ("INSERT into targets"
1919  " (id, uuid, owner, name, hosts, comment, lsc_credential)"
1920  " SELECT"
1921  " id, make_uuid (), owner, name, hosts, comment, lsc_credential"
1922  " FROM targets_18;");
1923 
1924  sql ("DROP TABLE targets_18;");
1925 
1926  /* Set the new predefined UUIDs. */
1927 
1928  sql ("UPDATE configs"
1929  " SET uuid = '" CONFIG_UUID_FULL_AND_FAST "'"
1930  " WHERE id = " G_STRINGIFY (CONFIG_ID_FULL_AND_FAST) ";");
1931 
1932  sql ("UPDATE configs"
1933  " SET uuid = '" CONFIG_UUID_FULL_AND_FAST_ULTIMATE "'"
1934  " WHERE id = " G_STRINGIFY (CONFIG_ID_FULL_AND_FAST_ULTIMATE) ";");
1935 
1936  sql ("UPDATE configs"
1937  " SET uuid = '" CONFIG_UUID_FULL_AND_VERY_DEEP "'"
1938  " WHERE id = " G_STRINGIFY (CONFIG_ID_FULL_AND_VERY_DEEP) ";");
1939 
1940  sql ("UPDATE configs"
1941  " SET uuid = '" CONFIG_UUID_FULL_AND_VERY_DEEP_ULTIMATE "'"
1942  " WHERE id = "
1943  G_STRINGIFY (CONFIG_ID_FULL_AND_VERY_DEEP_ULTIMATE) ";");
1944 
1945  sql ("UPDATE configs"
1946  " SET uuid = '" CONFIG_UUID_EMPTY "'"
1947  " WHERE name = 'empty';");
1948 
1949  sql ("UPDATE targets"
1950  " SET uuid = 'b493b7a8-7489-11df-a3ec-002264764cea'"
1951  " WHERE name = 'Localhost';");
1952 
1953  /* Set the database version to 19. */
1954 
1955  set_db_version (19);
1956 
1957  sql_commit ();
1958 
1959  return 0;
1960 }
1961 
1967 int
1969 {
1970  iterator_t rows;
1971 
1973 
1974  /* Ensure that the database is currently version 19. */
1975 
1976  if (manage_db_version () != 19)
1977  {
1978  sql_rollback ();
1979  return -1;
1980  }
1981 
1982  /* Update the database. */
1983 
1984  /* The agents table got new columns. In particular the installer column
1985  * moved to installer_64 and the table got a new installer column with the
1986  * plain installer. */
1987 
1990  sql ("ALTER TABLE agents ADD COLUMN installer_64 TEXT;");
1991  sql ("ALTER TABLE agents ADD COLUMN installer_signature_64 TEXT;");
1992  sql ("ALTER TABLE agents ADD COLUMN installer_trust INTEGER;");
1993 
1994  init_iterator (&rows, "SELECT id, installer FROM agents;");
1995  while (next (&rows))
1996  {
1997  const char *installer_64 = iterator_string (&rows, 1);
1998  gchar *installer;
1999  gsize installer_size;
2000  int ret;
2001  sql_stmt_t* stmt;
2002 
2003  sql ("UPDATE agents SET"
2004  " installer_trust = %i,"
2005  " installer_64 = installer,"
2006  " installer_signature_64 = ''"
2007  " WHERE id = %llu",
2008  TRUST_UNKNOWN,
2009  iterator_int64 (&rows, 0));
2010 
2011  stmt = sql_prepare ("UPDATE agents SET installer = $1"
2012  " WHERE id = %llu;",
2013  iterator_int64 (&rows, 0));
2014 
2015  /* Prepare statement. */
2016 
2017  if (stmt == NULL)
2018  {
2019  g_warning ("%s: sql_prepare failed\n", __FUNCTION__);
2020  cleanup_iterator (&rows);
2021  sql_rollback ();
2022  return -1;
2023  }
2024 
2025  if (strlen (installer_64) > 0)
2026  installer = (gchar*) g_base64_decode (installer_64, &installer_size);
2027  else
2028  {
2029  installer = g_strdup ("");
2030  installer_size = 0;
2031  }
2032 
2033  /* Bind the packages to the "$1" in the SQL statement. */
2034 
2035  if (sql_bind_text (stmt, 1, installer, installer_size))
2036  {
2037  g_warning ("%s: sql_bind_text failed\n", __FUNCTION__);
2038  cleanup_iterator (&rows);
2039  sql_rollback ();
2040  g_free (installer);
2041  return -1;
2042  }
2043  g_free (installer);
2044 
2045  /* Run the statement. */
2046 
2047  while ((ret = sql_exec (stmt)) > 0);
2048  if (ret < 0)
2049  {
2050  g_warning ("%s: sql_exec failed\n", __FUNCTION__);
2051  cleanup_iterator (&rows);
2052  sql_rollback ();
2053  return -1;
2054  }
2055 
2056  sql_finalize (stmt);
2057  }
2058  cleanup_iterator (&rows);
2059 
2060  /* Set the database version to 20. */
2061 
2062  set_db_version (20);
2063 
2064  sql_commit ();
2065 
2066  return 0;
2067 }
2068 
2074 int
2076 {
2078 
2079  /* Ensure that the database is currently version 20. */
2080 
2081  if (manage_db_version () != 20)
2082  {
2083  sql_rollback ();
2084  return -1;
2085  }
2086 
2087  /* Update the database. */
2088 
2089  /* The agents table got an installer_filename columns. */
2090 
2093  sql ("ALTER TABLE agents ADD COLUMN installer_filename TEXT;");
2094 
2095  /* Set the database version to 21. */
2096 
2097  set_db_version (21);
2098 
2099  sql_commit ();
2100 
2101  return 0;
2102 }
2103 
2109 int
2111 {
2112  iterator_t rows;
2113 
2115 
2116  /* Ensure that the database is currently version 21. */
2117 
2118  if (manage_db_version () != 21)
2119  {
2120  sql_rollback ();
2121  return -1;
2122  }
2123 
2124  /* Update the report formats.
2125  *
2126  * The name of the report format directories on disk changed from the report
2127  * format name to the report format UUID. */
2128 
2131  /* Ensure that the report_formats table exists. */
2132 
2133  sql ("CREATE TABLE IF NOT EXISTS report_formats"
2134  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, extension,"
2135  " content_type, summary, description);");
2136 
2137  /* Ensure that the predefined formats all exist in the database. */
2138 
2139  if (sql_int ("SELECT count(*) FROM report_formats WHERE name = 'CPE';")
2140  == 0)
2141  sql ("INSERT into report_formats (uuid, owner, name, summary, description,"
2142  " extension, content_type)"
2143  " VALUES (make_uuid (), NULL, 'CPE',"
2144  " 'Common Product Enumeration CSV table.',"
2145  " 'CPE stands for Common Product Enumeration. It is a structured naming scheme for\n"
2146  "information technology systems, platforms, and packages. In other words: CPE\n"
2147  "provides a unique identifier for virtually any software product that is known for\n"
2148  "a vulnerability.\n"
2149  "\n"
2150  "The CPE dictionary is maintained by MITRE and NIST. MITRE also maintains CVE\n"
2151  "(Common Vulnerability Enumeration) and other relevant security standards.\n"
2152  "\n"
2153  "The report selects all CPE tables from the results and forms a single table\n"
2154  "as a comma separated values file.\n',"
2155  " 'csv', 'text/csv');");
2156 
2157  if (sql_int ("SELECT count(*) FROM report_formats WHERE name = 'HTML';")
2158  == 0)
2159  sql ("INSERT into report_formats (uuid, owner, name, summary, description,"
2160  " extension, content_type)"
2161  " VALUES (make_uuid (), NULL, 'HTML', 'Single page HTML report.',"
2162  " 'A single HTML page listing results of a scan. Style information is embedded in\n"
2163  "the HTML, so the page is suitable for viewing in a browser as is.\n',"
2164  " 'html', 'text/html');");
2165 
2166  if (sql_int ("SELECT count(*) FROM report_formats WHERE name = 'ITG';")
2167  == 0)
2168  sql ("INSERT into report_formats (uuid, owner, name, summary, description,"
2169  " extension, content_type)"
2170  " VALUES (make_uuid (), NULL, 'ITG',"
2171  " 'German \"IT-Grundschutz-Kataloge\" report.',"
2172  " 'Tabular report on the German \"IT-Grundschutz-Kataloge\",\n"
2173  "as published and maintained by the German Federal Agency for IT-Security.\n',"
2174  " 'csv', 'text/csv');");
2175 
2176  if (sql_int ("SELECT count(*) FROM report_formats WHERE name = 'LaTeX';")
2177  == 0)
2178  sql ("INSERT into report_formats (uuid, owner, name, summary, description,"
2179  " extension, content_type)"
2180  " VALUES (make_uuid (), NULL, 'LaTeX',"
2181  " 'LaTeX source file.',"
2182  " 'Report as LaTeX source file for further processing.\n',"
2183  " 'tex', 'text/plain');");
2184 
2185  if (sql_int ("SELECT count(*) FROM report_formats WHERE name = 'NBE';")
2186  == 0)
2187  sql ("INSERT into report_formats (uuid, owner, name, summary, description,"
2188  " extension, content_type)"
2189  " VALUES (make_uuid (), NULL, 'NBE', 'Legacy OpenVAS report.',"
2190  " 'The traditional OpenVAS Scanner text based format.',"
2191  " 'nbe', 'text/plain');");
2192 
2193  if (sql_int ("SELECT count(*) FROM report_formats WHERE name = 'PDF';")
2194  == 0)
2195  sql ("INSERT into report_formats (uuid, owner, name, summary, description,"
2196  " extension, content_type)"
2197  " VALUES (make_uuid (), NULL, 'PDF',"
2198  " 'Portable Document Format report.',"
2199  " 'Scan results in Portable Document Format (PDF).',"
2200  "'pdf', 'application/pdf');");
2201 
2202  if (sql_int ("SELECT count(*) FROM report_formats WHERE name = 'TXT';")
2203  == 0)
2204  sql ("INSERT into report_formats (uuid, owner, name, summary, description,"
2205  " extension, content_type)"
2206  " VALUES (make_uuid (), NULL, 'TXT', 'Plain text report.',"
2207  " 'Plain text report, best viewed with fixed font size.',"
2208  " 'txt', 'text/plain');");
2209 
2210  if (sql_int ("SELECT count(*) FROM report_formats WHERE name = 'XML';")
2211  == 0)
2212  sql ("INSERT into report_formats (uuid, owner, name, summary, description,"
2213  " extension, content_type)"
2214  " VALUES (make_uuid (), NULL, 'XML',"
2215  " 'Raw XML report.',"
2216  " 'Complete scan report in OpenVAS Manager XML format.',"
2217  " 'xml', 'text/xml');");
2218 
2219  /* Update the UUIDs of the predefined formats to the new predefined UUIDs. */
2220 
2221  sql ("UPDATE report_formats SET uuid = 'a0704abb-2120-489f-959f-251c9f4ffebd'"
2222  " WHERE name = 'CPE'");
2223 
2224  sql ("UPDATE report_formats SET uuid = 'b993b6f5-f9fb-4e6e-9c94-dd46c00e058d'"
2225  " WHERE name = 'HTML'");
2226 
2227  sql ("UPDATE report_formats SET uuid = '929884c6-c2c4-41e7-befb-2f6aa163b458'"
2228  " WHERE name = 'ITG'");
2229 
2230  sql ("UPDATE report_formats SET uuid = '9f1ab17b-aaaa-411a-8c57-12df446f5588'"
2231  " WHERE name = 'LaTeX'");
2232 
2233  sql ("UPDATE report_formats SET uuid = 'f5c2a364-47d2-4700-b21d-0a7693daddab'"
2234  " WHERE name = 'NBE'");
2235 
2236  sql ("UPDATE report_formats SET uuid = '1a60a67e-97d0-4cbf-bc77-f71b08e7043d'"
2237  " WHERE name = 'PDF'");
2238 
2239  sql ("UPDATE report_formats SET uuid = '19f6f1b3-7128-4433-888c-ccc764fe6ed5'"
2240  " WHERE name = 'TXT'");
2241 
2242  sql ("UPDATE report_formats SET uuid = 'd5da9f67-8551-4e51-807b-b6a873d70e34'"
2243  " WHERE name = 'XML'");
2244 
2245  /* Rename the directories. */
2246 
2247  init_iterator (&rows, "SELECT id, uuid, owner, name FROM report_formats;");
2248  while (next (&rows))
2249  {
2250  const char *name, *uuid;
2251  gchar *old_dir, *new_dir;
2252  int user_format = 0;
2253 
2254  uuid = iterator_string (&rows, 1);
2255  name = iterator_string (&rows, 3);
2256 
2257  if (sql_int ("SELECT owner is NULL FROM report_formats"
2258  " WHERE id = %llu;",
2259  iterator_int64 (&rows, 0)))
2260  {
2261  /* Global. */
2262  old_dir = g_build_filename (OPENVAS_SYSCONF_DIR,
2263  "openvasmd",
2264  "global_report_formats",
2265  name,
2266  NULL);
2267  new_dir = g_build_filename (OPENVAS_SYSCONF_DIR,
2268  "openvasmd",
2269  "global_report_formats",
2270  uuid,
2271  NULL);
2272  }
2273  else
2274  {
2275  char *owner_uuid;
2276  owner_uuid = sql_string ("SELECT uuid FROM users"
2277  " WHERE id = %llu;",
2278  iterator_int64 (&rows, 2));
2279  if (owner_uuid == NULL)
2280  {
2281  g_warning ("%s: owner missing from users table\n", __FUNCTION__);
2282  cleanup_iterator (&rows);
2283  sql_rollback ();
2284  return -1;
2285  }
2286  old_dir = g_build_filename (OPENVAS_SYSCONF_DIR,
2287  "openvasmd",
2288  "report_formats",
2289  owner_uuid,
2290  name,
2291  NULL);
2292  new_dir = g_build_filename (OPENVAS_SYSCONF_DIR,
2293  "openvasmd",
2294  "report_formats",
2295  owner_uuid,
2296  uuid,
2297  NULL);
2298  free (owner_uuid);
2299  user_format = 1;
2300  }
2301  if (g_file_test (new_dir, G_FILE_TEST_EXISTS))
2302  {
2303  if (g_file_test (old_dir, G_FILE_TEST_EXISTS)
2304  && openvas_file_remove_recurse (old_dir))
2305  g_warning ("%s: failed to remove %s\n",
2306  __FUNCTION__,
2307  old_dir);
2308  }
2309  /* If the old dir of a predefined format is missing that's OK, the
2310  * Manager will create the dir when it starts proper. */
2311  else if ((g_file_test (old_dir, G_FILE_TEST_EXISTS)
2312  || user_format)
2313  && rename (old_dir, new_dir))
2314  {
2315  g_warning ("%s: renaming %s to %s failed: %s\n",
2316  __FUNCTION__,
2317  old_dir,
2318  new_dir,
2319  strerror (errno));
2320  g_free (old_dir);
2321  g_free (new_dir);
2322  cleanup_iterator (&rows);
2323  sql_rollback ();
2324  return -1;
2325  }
2326  g_free (old_dir);
2327  g_free (new_dir);
2328  }
2329 
2330  /* Set the database version to 22. */
2331 
2332  set_db_version (22);
2333 
2334  sql_commit ();
2335 
2336  return 0;
2337 }
2338 
2344 int
2346 {
2348 
2349  /* Ensure that the database is currently version 22. */
2350 
2351  if (manage_db_version () != 22)
2352  {
2353  sql_rollback ();
2354  return -1;
2355  }
2356 
2357  /* Update the report formats.
2358  *
2359  * The report_formats table got signature and trust columns. */
2360 
2363  sql ("ALTER TABLE report_formats ADD COLUMN signature;");
2364  sql ("UPDATE report_formats SET signature = '';");
2365 
2366  sql ("ALTER TABLE report_formats ADD COLUMN trust;");
2367  sql ("UPDATE report_formats SET trust = %i;", TRUST_UNKNOWN);
2368 
2369  /* Set the database version to 23. */
2370 
2371  set_db_version (23);
2372 
2373  sql_commit ();
2374 
2375  return 0;
2376 }
2377 
2383 int
2385 {
2387 
2388  /* Ensure that the database is currently version 23. */
2389 
2390  if (manage_db_version () != 23)
2391  {
2392  sql_rollback ();
2393  return -1;
2394  }
2395 
2396  /* Update the database. */
2397 
2398  /* The 8 to 9 migrator cast owner to an integer because owner had
2399  * changed from a string to an integer. This means empty strings would
2400  * be converted to 0 instead of NULL, so convert any 0's to NULL. */
2401 
2402  sql ("UPDATE tasks SET owner = NULL where owner = 0;"),
2403 
2404  /* Set the database version to 24. */
2405 
2406  set_db_version (24);
2407 
2408  sql_commit ();
2409 
2410  return 0;
2411 }
2412 
2418 int
2420 {
2421  iterator_t rows;
2422 
2424 
2425  /* Ensure that the database is currently version 24. */
2426 
2427  if (manage_db_version () != 24)
2428  {
2429  sql_rollback ();
2430  return -1;
2431  }
2432 
2433  /* Update the database. */
2434 
2435  /* Missing parameter chunking handling in the GSA may have resulted in
2436  * empty options in NVT radio preference values. */
2437 
2438  init_iterator (&rows, "SELECT id, name, value FROM nvt_preferences;");
2439  while (next (&rows))
2440  {
2441  const char *name;
2442  int type_start = -1, type_end = -1, count;
2443 
2444  name = iterator_string (&rows, 1);
2445 
2446  /* NVT[radio]:Preference */
2447  count = sscanf (name, "%*[^[][%nradio%n]:", &type_start, &type_end);
2448  if (count == 0 && type_start > 0 && type_end > 0)
2449  {
2450  const char *value;
2451  gchar **split, **point, *quoted_value;
2452  GString *string;
2453  gboolean first;
2454 
2455  /* Flush any empty options (";a;;b;" becomes "a;b"). */
2456  first = TRUE;
2457  value = iterator_string (&rows, 2);
2458  split = g_strsplit (value, ";", 0);
2459  string = g_string_new ("");
2460  point = split;
2461  while (*point)
2462  {
2463  if (strlen (*point))
2464  {
2465  if (first)
2466  first = FALSE;
2467  else
2468  g_string_append_c (string, ';');
2469  g_string_append (string, *point);
2470  }
2471  point++;
2472  }
2473  g_strfreev (split);
2474 
2475  quoted_value = sql_nquote (string->str, string->len);
2476  g_string_free (string, TRUE);
2477  sql ("UPDATE nvt_preferences SET value = '%s' WHERE id = %llu",
2478  quoted_value,
2479  iterator_int64 (&rows, 0));
2480  g_free (quoted_value);
2481  }
2482  }
2483  cleanup_iterator (&rows);
2484 
2485  init_iterator (&rows,
2486  "SELECT id, name, value FROM config_preferences"
2487  " WHERE type = 'PLUGINS_PREFS';");
2488  while (next (&rows))
2489  {
2490  const char *name;
2491  int type_start = -1, type_end = -1, count;
2492 
2493  name = iterator_string (&rows, 1);
2494 
2495  /* NVT[radio]:Preference */
2496  count = sscanf (name, "%*[^[][%nradio%n]:", &type_start, &type_end);
2497  if (count == 0 && type_start > 0 && type_end > 0)
2498  {
2499  const char *value;
2500  gchar **split, **point, *quoted_value;
2501  GString *string;
2502  gboolean first;
2503 
2504  /* Flush any empty options (";a;;b;" becomes "a;b"). */
2505  first = TRUE;
2506  value = iterator_string (&rows, 2);
2507  split = g_strsplit (value, ";", 0);
2508  string = g_string_new ("");
2509  point = split;
2510  while (*point)
2511  {
2512  if (strlen (*point))
2513  {
2514  if (first)
2515  first = FALSE;
2516  else
2517  g_string_append_c (string, ';');
2518  g_string_append (string, *point);
2519  }
2520  point++;
2521  }
2522  g_strfreev (split);
2523 
2524  quoted_value = sql_nquote (string->str, string->len);
2525  g_string_free (string, TRUE);
2526  sql ("UPDATE config_preferences SET value = '%s' WHERE id = %llu",
2527  quoted_value,
2528  iterator_int64 (&rows, 0));
2529  g_free (quoted_value);
2530  }
2531  }
2532  cleanup_iterator (&rows);
2533 
2534  /* Set the database version to 25. */
2535 
2536  set_db_version (25);
2537 
2538  sql_commit ();
2539 
2540  return 0;
2541 }
2542 
2548 int
2550 {
2552 
2553  /* Ensure that the database is currently version 25. */
2554 
2555  if (manage_db_version () != 25)
2556  {
2557  sql_rollback ();
2558  return -1;
2559  }
2560 
2561  /* Update the database. */
2562 
2563  /* The report_formats table got a trust_time column. */
2564 
2565  sql ("ALTER TABLE report_formats ADD column trust_time;");
2566  sql ("UPDATE report_formats SET trust_time = %i;", time (NULL));
2567 
2568  /* Set the database version to 26. */
2569 
2570  set_db_version (26);
2571 
2572  sql_commit ();
2573 
2574  return 0;
2575 }
2576 
2582 int
2584 {
2586 
2587  /* Ensure that the database is currently version 26. */
2588 
2589  if (manage_db_version () != 26)
2590  {
2591  sql_rollback ();
2592  return -1;
2593  }
2594 
2595  /* Update the database. */
2596 
2597  /* The reports table got a slave_progress column and the tasks table got a
2598  * slave column. */
2599 
2600  sql ("ALTER TABLE reports ADD column slave_progress;");
2601  sql ("UPDATE reports SET slave_progress = 0;");
2602 
2603  sql ("ALTER TABLE tasks ADD column slave;");
2604  sql ("UPDATE tasks SET slave = 0;");
2605 
2606  /* Set the database version to 27. */
2607 
2608  set_db_version (27);
2609 
2610  sql_commit ();
2611 
2612  return 0;
2613 }
2614 
2620 int
2622 {
2624 
2625  /* Ensure that the database is currently version 27. */
2626 
2627  if (manage_db_version () != 27)
2628  {
2629  sql_rollback ();
2630  return -1;
2631  }
2632 
2633  /* Update the database. */
2634 
2635  /* The report_formats table got a flags column. */
2636 
2637  sql ("ALTER TABLE report_formats ADD COLUMN flags INTEGER;");
2638  sql ("UPDATE report_formats SET flags = 1;");
2639 
2640  /* Set the database version to 28. */
2641 
2642  set_db_version (28);
2643 
2644  sql_commit ();
2645 
2646  return 0;
2647 }
2648 
2654 int
2656 {
2658 
2659  /* Ensure that the database is currently version 28. */
2660 
2661  if (manage_db_version () != 28)
2662  {
2663  sql_rollback ();
2664  return -1;
2665  }
2666 
2667  /* Update the database. */
2668 
2669  /* The reports table got a slave_task_uuid column. */
2670 
2671  sql ("ALTER TABLE reports ADD COLUMN slave_task_uuid;");
2672  sql ("UPDATE reports SET slave_task_uuid = ''");
2673 
2674  /* Set the database version to 29. */
2675 
2676  set_db_version (29);
2677 
2678  sql_commit ();
2679 
2680  return 0;
2681 }
2682 
2688 int
2690 {
2692 
2693  /* Ensure that the database is currently version 29. */
2694 
2695  if (manage_db_version () != 29)
2696  {
2697  sql_rollback ();
2698  return -1;
2699  }
2700 
2701  /* Update the database. */
2702 
2703  /* The agents table got an installer_trust_time column. */
2704 
2705  sql ("ALTER TABLE agents ADD column installer_trust_time;");
2706  sql ("UPDATE agents SET installer_trust_time = %i;", time (NULL));
2707 
2708  /* Set the database version to 30. */
2709 
2710  set_db_version (30);
2711 
2712  sql_commit ();
2713 
2714  return 0;
2715 }
2716 
2722 int
2724 {
2726 
2727  /* Ensure that the database is currently version 30. */
2728 
2729  if (manage_db_version () != 30)
2730  {
2731  sql_rollback ();
2732  return -1;
2733  }
2734 
2735  /* Update the database. */
2736 
2737  /* Slaves switched from being targets to being resources of their own.
2738  * Just clear any task slaves. */
2739 
2740  sql ("UPDATE tasks SET slave = 0;");
2741 
2742  /* Set the database version to 31. */
2743 
2744  set_db_version (31);
2745 
2746  sql_commit ();
2747 
2748  return 0;
2749 }
2750 
2756 int
2758 {
2760 
2761  /* Ensure that the database is currently version 31. */
2762 
2763  if (manage_db_version () != 31)
2764  {
2765  sql_rollback ();
2766  return -1;
2767  }
2768 
2769  /* Update the database. */
2770 
2771  /* Ensure that the report_format_params table exists. */
2772 
2773  sql ("CREATE TABLE IF NOT EXISTS report_format_params"
2774  " (id INTEGER PRIMARY KEY, report_format, name, value);");
2775 
2776  /* The report_format_params table got a type column. */
2777 
2778  sql ("ALTER TABLE report_format_params ADD column type INTEGER;");
2779  sql ("UPDATE report_format_params SET type = 3;");
2780 
2781  /* Set the database version to 32. */
2782 
2783  set_db_version (32);
2784 
2785  sql_commit ();
2786 
2787  return 0;
2788 }
2789 
2795 int
2797 {
2799 
2800  /* Ensure that the database is currently version 32. */
2801 
2802  if (manage_db_version () != 32)
2803  {
2804  sql_rollback ();
2805  return -1;
2806  }
2807 
2808  /* Update the database. */
2809 
2810  /* The report_format_params table got a few new columns. */
2811 
2812  sql ("ALTER TABLE report_format_params ADD column type_min;");
2813  sql ("UPDATE report_format_params SET type_min = %lli;", LLONG_MIN);
2814 
2815  sql ("ALTER TABLE report_format_params ADD column type_max;");
2816  sql ("UPDATE report_format_params SET type_max = %lli;", LLONG_MAX);
2817 
2818  sql ("ALTER TABLE report_format_params ADD column type_regex;");
2819  sql ("UPDATE report_format_params SET type_regex = '';");
2820 
2821  sql ("ALTER TABLE report_format_params ADD column fallback;");
2822  sql ("UPDATE report_format_params SET fallback = value;");
2823 
2824  /* Set the database version to 33. */
2825 
2826  set_db_version (33);
2827 
2828  sql_commit ();
2829 
2830  return 0;
2831 }
2832 
2838 void
2840 {
2841  if (sql_int ("SELECT count(*) FROM config_preferences"
2842  " WHERE config = %llu"
2843  " AND name ="
2844  " 'Login configurations[checkbox]:NTLMSSP';",
2845  config)
2846  == 0)
2847  sql ("INSERT into config_preferences (config, type, name, value)"
2848  " VALUES (%llu, 'PLUGINS_PREFS',"
2849  " 'Login configurations[checkbox]:NTLMSSP',"
2850  " 'yes');",
2851  config);
2852 }
2853 
2859 int
2861 {
2863 
2864  /* Ensure that the database is currently version 33. */
2865 
2866  if (manage_db_version () != 33)
2867  {
2868  sql_rollback ();
2869  return -1;
2870  }
2871 
2872  /* Update the database. */
2873 
2874  /* The preference "NTLMSSP" was set to yes in the predefined configs. */
2875 
2882 
2883  /* Set the database version to 34. */
2884 
2885  set_db_version (34);
2886 
2887  sql_commit ();
2888 
2889  return 0;
2890 }
2891 
2897 int
2899 {
2901 
2902  /* Ensure that the database is currently version 34. */
2903 
2904  if (manage_db_version () != 34)
2905  {
2906  sql_rollback ();
2907  return -1;
2908  }
2909 
2910  /* Update the database. */
2911 
2912  /* The LSC credential element of the target resource was split into two
2913  * elements, for SSH and SMB. */
2914 
2917  sql ("ALTER TABLE targets ADD column smb_lsc_credential;");
2918  sql ("UPDATE targets SET smb_lsc_credential = lsc_credential;");
2919 
2920  /* Set the database version to 35. */
2921 
2922  set_db_version (35);
2923 
2924  sql_commit ();
2925 
2926  return 0;
2927 }
2928 
2937 target_t
2939 {
2940  char *quoted_name = sql_quote (name);
2941  sql ("INSERT INTO targets"
2942  " (uuid, owner, name, hosts, comment, lsc_credential,"
2943  " smb_lsc_credential)"
2944  " SELECT make_uuid (), owner, uniquify ('target', '%s', owner, ''),"
2945  " hosts, comment, lsc_credential, smb_lsc_credential"
2946  " FROM targets WHERE id = %llu;",
2947  quoted_name,
2948  target);
2949  g_free (quoted_name);
2950  return sql_last_insert_id ();
2951 }
2952 
2958 int
2960 {
2961  iterator_t tasks;
2962  char *scanner_range, *quoted_scanner_range;
2963 
2965 
2966  /* Ensure that the database is currently version 35. */
2967 
2968  if (manage_db_version () != 35)
2969  {
2970  sql_rollback ();
2971  return -1;
2972  }
2973 
2974  /* Update the database. */
2975 
2976  /* For a time between 1.0.0 beta3 and 1.0.0 beta5 the Manager would create
2977  * the example task with name references to the target and config, instead
2978  * of ID references. Correct this now. */
2979 
2980  sql ("UPDATE tasks SET"
2981  " target = (SELECT id FROM targets WHERE name = 'Localhost'),"
2982  " config = (SELECT id FROM configs WHERE name = 'Full and fast')"
2983  " WHERE uuid = '343435d6-91b0-11de-9478-ffd71f4c6f29';");
2984 
2985  /* Scanner preference "port_range" moved from config into target. */
2986 
2989  sql ("ALTER TABLE targets ADD column port_range;");
2990  sql ("UPDATE targets SET port_range = NULL;");
2991 
2992  scanner_range = sql_string ("SELECT value FROM nvt_preferences"
2993  " WHERE name = 'port_range'");
2994  if (scanner_range)
2995  {
2996  quoted_scanner_range = sql_quote (scanner_range);
2997  free (scanner_range);
2998  }
2999  else
3000  quoted_scanner_range = NULL;
3001 
3002  init_iterator (&tasks, "SELECT id, target, config FROM tasks;");
3003  while (next (&tasks))
3004  {
3005  char *config_range, *quoted_config_range;
3006  target_t target;
3007 
3008  target = iterator_int64 (&tasks, 1);
3009 
3010  if (sql_int ("SELECT port_range IS NULL FROM targets WHERE id = %llu;",
3011  target)
3012  == 0)
3013  {
3014  gchar *name;
3015 
3016  /* Already used this target, use a copy of it. */
3017 
3018  name = sql_string ("SELECT name || ' Migration' FROM targets"
3019  " WHERE id = %llu;",
3020  target);
3021  assert (name);
3022  target = migrate_35_to_36_duplicate_target (target, name);
3023  free (name);
3024 
3025  sql ("UPDATE tasks SET target = %llu WHERE id = %llu",
3026  target,
3027  iterator_int64 (&tasks, 0));
3028  }
3029 
3030  config_range = sql_string ("SELECT value FROM config_preferences"
3031  " WHERE config = %llu"
3032  " AND name = 'port_range';",
3033  iterator_int64 (&tasks, 2));
3034 
3035  if (config_range)
3036  {
3037  quoted_config_range = sql_quote (config_range);
3038  free (config_range);
3039  }
3040  else
3041  quoted_config_range = NULL;
3042 
3043  sql ("UPDATE targets SET port_range = '%s'"
3044  " WHERE id = %llu;",
3045  quoted_config_range
3046  ? quoted_config_range
3047  : (quoted_scanner_range ? quoted_scanner_range : "default"),
3048  target);
3049 
3050  free (quoted_config_range);
3051  }
3052  cleanup_iterator (&tasks);
3053 
3054  sql ("UPDATE targets SET port_range = 'default' WHERE port_range IS NULL;");
3055 
3056  sql ("DELETE FROM config_preferences WHERE name = 'port_range';");
3057  sql ("DELETE FROM nvt_preferences WHERE name = 'port_range';");
3058 
3059  free (quoted_scanner_range);
3060 
3061  /* Set the database version to 36. */
3062 
3063  set_db_version (36);
3064 
3065  sql_commit ();
3066 
3067  return 0;
3068 }
3069 
3075 int
3077 {
3079 
3080  /* Ensure that the database is currently version 36. */
3081 
3082  if (manage_db_version () != 36)
3083  {
3084  sql_rollback ();
3085  return -1;
3086  }
3087 
3088  /* Update the database. */
3089 
3090  /* The target and config clauses were swapped in the example task statement
3091  in migrate_35_to_36 in SVN for some time. Run the statement again with
3092  the correct clauses. */
3093 
3094  sql ("UPDATE tasks SET"
3095  " target = (SELECT id FROM targets WHERE name = 'Localhost'),"
3096  " config = (SELECT id FROM configs WHERE name = 'Full and fast')"
3097  " WHERE uuid = '343435d6-91b0-11de-9478-ffd71f4c6f29';");
3098 
3099  /* Set the database version to 37. */
3100 
3101  set_db_version (37);
3102 
3103  sql_commit ();
3104 
3105  return 0;
3106 }
3107 
3113 int
3115 {
3116  gchar *old_dir, *new_dir;
3117 
3119 
3120  /* Ensure that the database is currently version 37. */
3121 
3122  if (manage_db_version () != 37)
3123  {
3124  sql_rollback ();
3125  return -1;
3126  }
3127 
3128  /* Update the database. */
3129 
3130  /* The report formats moved to FHS compliant locations. */
3131 
3132  /* Remove the global report format dirs, as they should have been
3133  * installed in the new location already. */
3134 
3135  old_dir = g_build_filename (OPENVAS_SYSCONF_DIR,
3136  "openvasmd",
3137  "global_report_formats",
3138  NULL);
3139 
3140  openvas_file_remove_recurse (old_dir);
3141  g_free (old_dir);
3142 
3143  /* Move user uploaded report formats. */
3144 
3145  new_dir = g_build_filename (OPENVAS_STATE_DIR,
3146  "openvasmd",
3147  NULL);
3148 
3149  if (g_mkdir_with_parents (new_dir, 0755 /* "rwxr-xr-x" */))
3150  {
3151  g_warning ("%s: failed to create dir %s", __FUNCTION__, new_dir);
3152  g_free (new_dir);
3153  sql_rollback ();
3154  return -1;
3155  }
3156 
3157  old_dir = g_build_filename (OPENVAS_SYSCONF_DIR,
3158  "openvasmd",
3159  "report_formats",
3160  NULL);
3161 
3162  /* Ensure the old dir exists. */
3163  g_mkdir_with_parents (old_dir, 0755 /* "rwxr-xr-x" */);
3164 
3165  {
3166  gchar **cmd;
3167  gchar *standard_out = NULL;
3168  gchar *standard_err = NULL;
3169  gint exit_status;
3170 
3171  cmd = (gchar **) g_malloc (4 * sizeof (gchar *));
3172  cmd[0] = g_strdup ("mv");
3173  cmd[1] = old_dir;
3174  cmd[2] = new_dir;
3175  cmd[3] = NULL;
3176  g_debug ("%s: Spawning in .: %s %s %s\n",
3177  __FUNCTION__, cmd[0], cmd[1], cmd[2]);
3178  if ((g_spawn_sync (".",
3179  cmd,
3180  NULL, /* Environment. */
3181  G_SPAWN_SEARCH_PATH,
3182  NULL, /* Setup function. */
3183  NULL,
3184  &standard_out,
3185  &standard_err,
3186  &exit_status,
3187  NULL)
3188  == FALSE)
3189  || (WIFEXITED (exit_status) == 0)
3190  || WEXITSTATUS (exit_status))
3191  {
3192  g_warning ("%s: failed rename: %d (WIF %i, WEX %i)",
3193  __FUNCTION__,
3194  exit_status,
3195  WIFEXITED (exit_status),
3196  WEXITSTATUS (exit_status));
3197  g_debug ("%s: stdout: %s\n", __FUNCTION__, standard_out);
3198  g_debug ("%s: stderr: %s\n", __FUNCTION__, standard_err);
3199  g_free (old_dir);
3200  g_free (new_dir);
3201  g_free (cmd[0]);
3202  g_free (cmd);
3203  sql_rollback ();
3204  return -1;
3205  }
3206 
3207  g_free (cmd[0]);
3208  g_free (cmd);
3209  }
3210 
3211  g_free (old_dir);
3212  g_free (new_dir);
3213 
3214  /* Set the database version to 38. */
3215 
3216  set_db_version (38);
3217 
3218  sql_commit ();
3219 
3220  return 0;
3221 }
3222 
3228 int
3230 {
3232 
3233  /* Ensure that the database is currently version 38. */
3234 
3235  if (manage_db_version () != 38)
3236  {
3237  sql_rollback ();
3238  return -1;
3239  }
3240 
3241  /* Update the database. */
3242 
3243  /* The w3af NVT (80109) was removed from the predefined configs. */
3244 
3245  /* Just update the config comments, because init_manage will add the new
3246  * selectors. */
3247 
3248  sql ("UPDATE configs SET comment ="
3249  " 'Most NVT''s; optimized by using previously collected information.'"
3250  " WHERE id = " G_STRINGIFY (CONFIG_ID_FULL_AND_FAST) ";");
3251 
3252  sql ("UPDATE configs SET comment ="
3253  " 'Most NVT''s including those that can stop services/hosts;"
3254  " optimized by using previously collected information.'"
3255  " WHERE id = " G_STRINGIFY (CONFIG_ID_FULL_AND_FAST_ULTIMATE) ";");
3256 
3257  sql ("UPDATE configs SET comment ="
3258  " 'Most NVT''s; don''t trust previously collected information; slow.'"
3259  " WHERE id = " G_STRINGIFY (CONFIG_ID_FULL_AND_VERY_DEEP) ";");
3260 
3261  sql ("UPDATE configs SET comment ="
3262  " 'Most NVT''s including those that can stop services/hosts;"
3263  " don''t trust previously collected information; slow.'"
3264  " WHERE id = " G_STRINGIFY (CONFIG_ID_FULL_AND_VERY_DEEP_ULTIMATE) ";");
3265 
3266  /* Set the database version to 39. */
3267 
3268  set_db_version (39);
3269 
3270  sql_commit ();
3271 
3272  return 0;
3273 }
3274 
3280 void
3282 {
3283  sql ("UPDATE config_preferences SET value = 'yes'"
3284  " WHERE config = %llu"
3285  " AND type = 'SERVER_PREFS'"
3286  " AND name = 'unscanned_closed';",
3287  config);
3288 }
3289 
3295 int
3297 {
3299 
3300  /* Ensure that the database is currently version 39. */
3301 
3302  if (manage_db_version () != 39)
3303  {
3304  sql_rollback ();
3305  return -1;
3306  }
3307 
3308  /* Update the database. */
3309 
3310  /* The preference "unscanned_closed" was set to yes in the predefined
3311  * configs. */
3312 
3319 
3320  /* Set the database version to 40. */
3321 
3322  set_db_version (40);
3323 
3324  sql_commit ();
3325 
3326  return 0;
3327 }
3328 
3334 int
3336 {
3338 
3339  /* Ensure that the database is currently version 40. */
3340 
3341  if (manage_db_version () != 40)
3342  {
3343  sql_rollback ();
3344  return -1;
3345  }
3346 
3347  /* Update the database. */
3348 
3349  /* For report formats, feed signatures were given priority over signatures
3350  * in imported XML. This includes only setting the db signature when it is
3351  * imported. So remove the db signatures for all predefined reports. */
3352 
3355  sql ("UPDATE report_formats SET signature = NULL"
3356  " WHERE uuid = 'a0704abb-2120-489f-959f-251c9f4ffebd';");
3357  sql ("UPDATE report_formats SET signature = NULL"
3358  " WHERE uuid = 'b993b6f5-f9fb-4e6e-9c94-dd46c00e058d';");
3359  sql ("UPDATE report_formats SET signature = NULL"
3360  " WHERE uuid = '929884c6-c2c4-41e7-befb-2f6aa163b458';");
3361  sql ("UPDATE report_formats SET signature = NULL"
3362  " WHERE uuid = '9f1ab17b-aaaa-411a-8c57-12df446f5588';");
3363  sql ("UPDATE report_formats SET signature = NULL"
3364  " WHERE uuid = 'f5c2a364-47d2-4700-b21d-0a7693daddab';");
3365  sql ("UPDATE report_formats SET signature = NULL"
3366  " WHERE uuid = '1a60a67e-97d0-4cbf-bc77-f71b08e7043d';");
3367  sql ("UPDATE report_formats SET signature = NULL"
3368  " WHERE uuid = '19f6f1b3-7128-4433-888c-ccc764fe6ed5';");
3369  sql ("UPDATE report_formats SET signature = NULL"
3370  " WHERE uuid = 'd5da9f67-8551-4e51-807b-b6a873d70e34';");
3371 
3372  /* Set the database version to 41. */
3373 
3374  set_db_version (41);
3375 
3376  sql_commit ();
3377 
3378  return 0;
3379 }
3380 
3386 int
3388 {
3390 
3391  /* Require that the database is currently version 41. */
3392 
3393  if (manage_db_version () != 41)
3394  {
3395  sql_rollback ();
3396  return -1;
3397  }
3398 
3399  /* Update the database. */
3400 
3401  /* Two task tables got trashcan location fields. */
3402 
3405  sql ("ALTER TABLE tasks ADD column config_location INTEGER;");
3406  sql ("ALTER TABLE tasks ADD column target_location INTEGER;");
3407  sql ("ALTER TABLE tasks ADD column schedule_location INTEGER;");
3408  sql ("ALTER TABLE tasks ADD column slave_location INTEGER;");
3409 
3410  sql ("UPDATE tasks SET"
3411  " config_location = " G_STRINGIFY (LOCATION_TABLE) ","
3412  " target_location = " G_STRINGIFY (LOCATION_TABLE) ","
3413  " schedule_location = " G_STRINGIFY (LOCATION_TABLE) ","
3414  " slave_location = " G_STRINGIFY (LOCATION_TABLE) ";");
3415 
3416  /* Ensure that the task_escalators table exists. */
3417  sql ("CREATE TABLE IF NOT EXISTS task_escalators"
3418  " (id INTEGER PRIMARY KEY, task INTEGER, escalator INTEGER);");
3419 
3420  sql ("ALTER TABLE task_escalators ADD column escalator_location INTEGER;");
3421 
3422  sql ("UPDATE task_escalators"
3423  " SET escalator_location = " G_STRINGIFY (LOCATION_TABLE) ";");
3424 
3425  /* Set the database version to 42. */
3426 
3427  set_db_version (42);
3428 
3429  sql_commit ();
3430 
3431  return 0;
3432 }
3433 
3439 int
3441 {
3443 
3444  /* Require that the database is currently version 42. */
3445 
3446  if (manage_db_version () != 42)
3447  {
3448  sql_rollback ();
3449  return -1;
3450  }
3451 
3452  /* Update the database. */
3453 
3454  /* The targets table got an ssh_port field. */
3455 
3458  /* Ensure that the targets_trash table exists. */
3459  sql ("CREATE TABLE IF NOT EXISTS targets_trash"
3460  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, hosts,"
3461  " comment, lsc_credential INTEGER, smb_lsc_credential INTEGER,"
3462  " port_range, ssh_location INTEGER, smb_location INTEGER);");
3463 
3464  sql ("ALTER TABLE targets ADD column ssh_port;");
3465  sql ("ALTER TABLE targets_trash ADD column ssh_port;");
3466 
3467  sql ("UPDATE targets SET ssh_port = 22"
3468  " WHERE lsc_credential > 0;");
3469  sql ("UPDATE targets_trash SET ssh_port = 22"
3470  " WHERE lsc_credential > 0;");
3471 
3472  /* Set the database version to 43. */
3473 
3474  set_db_version (43);
3475 
3476  sql_commit ();
3477 
3478  return 0;
3479 }
3480 
3486 int
3488 {
3490 
3491  /* Require that the database is currently version 43. */
3492 
3493  if (manage_db_version () != 43)
3494  {
3495  sql_rollback ();
3496  return -1;
3497  }
3498 
3499  /* Update the database. */
3500 
3501  /* The file permission got much tighter. */
3502 
3503  if (chmod (task_db_name ? task_db_name : OPENVAS_STATE_DIR "/mgr/tasks.db",
3504  S_IRUSR | S_IWUSR))
3505  {
3506  g_warning ("%s: failed to chmod %s: %s",
3507  __FUNCTION__,
3509  : OPENVAS_STATE_DIR "/mgr/tasks.db",
3510  strerror (errno));
3511  sql_rollback ();
3512  return -1;
3513  }
3514 
3515  /* Set the database version to 44. */
3516 
3517  set_db_version (44);
3518 
3519  sql_commit ();
3520 
3521  return 0;
3522 }
3523 
3529 int
3531 {
3533 
3534  /* Require that the database is currently version 44. */
3535 
3536  if (manage_db_version () != 44)
3537  {
3538  sql_rollback ();
3539  return -1;
3540  }
3541 
3542  /* Update the database. */
3543 
3544  /* The tasks table got a upload_result_count column. */
3545 
3546  sql ("ALTER TABLE tasks ADD column upload_result_count;");
3547  sql ("UPDATE tasks SET upload_result_count = -1;");
3548 
3549  /* Set the database version to 45. */
3550 
3551  set_db_version (45);
3552 
3553  sql_commit ();
3554 
3555  return 0;
3556 }
3557 
3563 int
3565 {
3567 
3568  /* Require that the database is currently version 45. */
3569 
3570  if (manage_db_version () != 45)
3571  {
3572  sql_rollback ();
3573  return -1;
3574  }
3575 
3576  /* Update the database. */
3577 
3578  /* CREATE_TARGET now cleans the hosts string. */
3579 
3580  sql ("UPDATE targets SET hosts = clean_hosts (hosts);");
3581  sql ("UPDATE targets_trash SET hosts = clean_hosts (hosts);");
3582 
3583  /* Set the database version to 46. */
3584 
3585  set_db_version (46);
3586 
3587  sql_commit ();
3588 
3589  return 0;
3590 }
3591 
3597 int
3599 {
3601 
3602  /* Require that the database is currently version 46. */
3603 
3604  if (manage_db_version () != 46)
3605  {
3606  sql_rollback ();
3607  return -1;
3608  }
3609 
3610  /* Update the database. */
3611 
3612  /* Performance prefs move from config to task. */
3613 
3614  /* Ensure that the table exists. */
3615  sql ("CREATE TABLE IF NOT EXISTS task_preferences"
3616  " (id INTEGER PRIMARY KEY, task INTEGER, name, value);");
3617 
3618  sql ("INSERT INTO task_preferences (task, name, value)"
3619  " SELECT tasks.id, config_preferences.name, config_preferences.value"
3620  " FROM tasks, config_preferences"
3621  " WHERE tasks.config = config_preferences.config"
3622  " AND (config_preferences.name = 'max_checks'"
3623  " OR config_preferences.name = 'max_hosts')");
3624 
3625  /* Set the database version to 47. */
3626 
3627  set_db_version (47);
3628 
3629  sql_commit ();
3630 
3631  return 0;
3632 }
3633 
3639 int
3641 {
3643 
3644  /* Require that the database is currently version 47. */
3645 
3646  if (manage_db_version () != 47)
3647  {
3648  sql_rollback ();
3649  return -1;
3650  }
3651 
3652  /* Update the database. */
3653 
3654  /* Scanner "app" host detail changed name to "App". */
3655 
3656  /* Ensure that the table exists. */
3657  sql ("CREATE TABLE IF NOT EXISTS report_host_details"
3658  " (id INTEGER PRIMARY KEY, report_host INTEGER, source_type,"
3659  " source_name, source_description, name, value);");
3660 
3661  sql ("UPDATE report_host_details SET name = 'App' WHERE name = 'app';");
3662 
3663  /* Set the database version to 48. */
3664 
3665  set_db_version (48);
3666 
3667  sql_commit ();
3668 
3669  return 0;
3670 }
3671 
3677 int
3679 {
3681 
3682  /* Require that the database is currently version 48. */
3683 
3684  if (manage_db_version () != 48)
3685  {
3686  sql_rollback ();
3687  return -1;
3688  }
3689 
3690  /* Update the database. */
3691 
3692  /* If the example task was created before version 14 then the 13 to 14
3693  * migrator would have given the example result an arbitrary UUID instead
3694  * of the predefined one.
3695  *
3696  * Also, the host of the example result has now changed to an IP. */
3697 
3698  sql ("UPDATE results SET uuid = 'cb291ec0-1b0d-11df-8aa1-002264764cea'"
3699  " WHERE host = 'localhost';");
3700 
3701  sql ("UPDATE results SET host = '127.0.0.1'"
3702  " WHERE uuid = 'cb291ec0-1b0d-11df-8aa1-002264764cea';");
3703 
3704  sql ("UPDATE report_hosts SET host = '127.0.0.1'"
3705  " WHERE host = 'localhost';");
3706 
3707  /* Set the database version to 49. */
3708 
3709  set_db_version (49);
3710 
3711  sql_commit ();
3712 
3713  return 0;
3714 }
3715 
3721 int
3723 {
3725 
3726  /* Ensure that the database is currently version 49. */
3727 
3728  if (manage_db_version () != 49)
3729  {
3730  sql_rollback ();
3731  return -1;
3732  }
3733 
3734  /* Update the database. */
3735 
3736  /* The UNIQUE constraint in task_preferences was removed. */
3737 
3738  /* Move the table away. */
3739 
3740  sql ("ALTER TABLE task_preferences RENAME TO task_preferences_49;");
3741 
3742  /* Create the table in the new format. */
3743 
3744  sql ("CREATE TABLE IF NOT EXISTS task_preferences"
3745  " (id INTEGER PRIMARY KEY, task INTEGER, name, value);");
3746 
3747  /* Copy the data into the new table. */
3748 
3749  sql ("INSERT into task_preferences"
3750  " (id, task, name, value)"
3751  " SELECT"
3752  " id, task, name, value"
3753  " FROM task_preferences_49;");
3754 
3755  /* Drop the old tables. */
3756 
3757  sql ("DROP TABLE task_preferences_49;");
3758 
3759  /* Set the database version to 50. */
3760 
3761  set_db_version (50);
3762 
3763  sql_commit ();
3764 
3765  return 0;
3766 }
3767 
3773 int
3775 {
3777 
3778  /* Ensure that the database is currently version 50. */
3779 
3780  if (manage_db_version () != 50)
3781  {
3782  sql_rollback ();
3783  return -1;
3784  }
3785 
3786  /* Update the database. */
3787 
3788  /* The user table got a timezone column. */
3789 
3790  sql ("ALTER TABLE users ADD column timezone;");
3791  sql ("UPDATE users SET timezone = NULL;");
3792 
3793  /* Set the database version to 51. */
3794 
3795  set_db_version (51);
3796 
3797  sql_commit ();
3798 
3799  return 0;
3800 }
3801 
3807 int
3809 {
3811 
3812  /* Ensure that the database is currently version 51. */
3813 
3814  if (manage_db_version () != 51)
3815  {
3816  sql_rollback ();
3817  return -1;
3818  }
3819 
3820  /* Add an SQL helper. */
3821 
3823  {
3824  sql_rollback ();
3825  g_critical ("%s: failed to create convert", __FUNCTION__);
3826  return -1;
3827  }
3828 
3829  /* Update the database. */
3830 
3831  /* Date storage switched from text format to seconds since the epoch. */
3832 
3833  sql ("UPDATE report_hosts SET start_time = convert (start_time);");
3834  sql ("UPDATE report_hosts SET end_time = convert (end_time);");
3835  sql ("UPDATE reports SET start_time = convert (start_time);");
3836  sql ("UPDATE reports SET end_time = convert (end_time);");
3837  sql ("UPDATE tasks SET start_time = convert (start_time);");
3838  sql ("UPDATE tasks SET end_time = convert (end_time);");
3839 
3840  /* Set the database version to 52. */
3841 
3842  set_db_version (52);
3843 
3844  sql_commit ();
3845 
3846  return 0;
3847 }
3848 
3854 int
3856 {
3858 
3859  /* Ensure that the database is currently version 52. */
3860 
3861  if (manage_db_version () != 52)
3862  {
3863  sql_rollback ();
3864  return -1;
3865  }
3866 
3867  /* Update the database. */
3868 
3869  /* The overrides table got a end_time column. */
3870 
3871  /* Ensure that the table exists. */
3872  sql ("CREATE TABLE IF NOT EXISTS overrides"
3873  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt,"
3874  " creation_time, modification_time, text, hosts, port, threat,"
3875  " new_threat, task INTEGER, result INTEGER);");
3876 
3877  sql ("ALTER TABLE overrides ADD column end_time;");
3878  sql ("UPDATE overrides SET end_time = 0;");
3879 
3880  /* Set the database version to 53. */
3881 
3882  set_db_version (53);
3883 
3884  sql_commit ();
3885 
3886  return 0;
3887 }
3888 
3894 int
3896 {
3898 
3899  /* Ensure that the database is currently version 53. */
3900 
3901  if (manage_db_version () != 53)
3902  {
3903  sql_rollback ();
3904  return -1;
3905  }
3906 
3907  /* Update the database. */
3908 
3909  /* The notes table got a end_time column. */
3910 
3911  /* Ensure that the table exists. */
3912  sql ("CREATE TABLE IF NOT EXISTS notes"
3913  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt,"
3914  " creation_time, modification_time, text, hosts, port, threat,"
3915  " task INTEGER, result INTEGER);");
3916 
3917  sql ("ALTER TABLE notes ADD column end_time;");
3918  sql ("UPDATE notes SET end_time = 0;");
3919 
3920  /* Set the database version to 54. */
3921 
3922  set_db_version (54);
3923 
3924  sql_commit ();
3925 
3926  return 0;
3927 }
3928 
3937 int
3938 migrate_54_to_55_format (const char *old_uuid, const char *new_uuid)
3939 {
3940  gchar *dir;
3941 
3942  dir = g_build_filename (OPENVAS_DATA_DIR,
3943  "openvasmd",
3944  "global_report_formats",
3945  old_uuid,
3946  NULL);
3947 
3948  if (g_file_test (dir, G_FILE_TEST_EXISTS) && openvas_file_remove_recurse (dir))
3949  {
3950  g_warning ("%s: failed to remove dir %s", __FUNCTION__, dir);
3951  g_free (dir);
3952  return -1;
3953  }
3954  g_free (dir);
3955 
3956  sql ("UPDATE report_formats"
3957  " SET uuid = '%s'"
3958  " WHERE uuid = '%s';",
3959  new_uuid,
3960  old_uuid);
3961 
3962  return 0;
3963 }
3964 
3970 int
3972 {
3974 
3975  /* Ensure that the database is currently version 54. */
3976 
3977  if (manage_db_version () != 54)
3978  {
3979  sql_rollback ();
3980  return -1;
3981  }
3982 
3983  /* Update the database. */
3984 
3985  /* For report formats, feed signatures were given priority over signatures
3986  * in imported XML. This includes only setting the db signature when it is
3987  * imported. So remove the db signatures for all predefined reports. */
3988 
3991  if (migrate_54_to_55_format ("a0704abb-2120-489f-959f-251c9f4ffebd",
3992  "5ceff8ba-1f62-11e1-ab9f-406186ea4fc5"))
3993  {
3994  sql_rollback ();
3995  return -1;
3996  }
3997 
3998  if (migrate_54_to_55_format ("b993b6f5-f9fb-4e6e-9c94-dd46c00e058d",
3999  "6c248850-1f62-11e1-b082-406186ea4fc5"))
4000  {
4001  sql_rollback ();
4002  return -1;
4003  }
4004 
4005  if (migrate_54_to_55_format ("929884c6-c2c4-41e7-befb-2f6aa163b458",
4006  "77bd6c4a-1f62-11e1-abf0-406186ea4fc5"))
4007  {
4008  sql_rollback ();
4009  return -1;
4010  }
4011 
4012  if (migrate_54_to_55_format ("9f1ab17b-aaaa-411a-8c57-12df446f5588",
4013  "7fcc3a1a-1f62-11e1-86bf-406186ea4fc5"))
4014  {
4015  sql_rollback ();
4016  return -1;
4017  }
4018 
4019  if (migrate_54_to_55_format ("f5c2a364-47d2-4700-b21d-0a7693daddab",
4020  "9ca6fe72-1f62-11e1-9e7c-406186ea4fc5"))
4021  {
4022  sql_rollback ();
4023  return -1;
4024  }
4025 
4026  if (migrate_54_to_55_format ("1a60a67e-97d0-4cbf-bc77-f71b08e7043d",
4027  "a0b5bfb2-1f62-11e1-85db-406186ea4fc5"))
4028  {
4029  sql_rollback ();
4030  return -1;
4031  }
4032 
4033  if (migrate_54_to_55_format ("19f6f1b3-7128-4433-888c-ccc764fe6ed5",
4034  "a3810a62-1f62-11e1-9219-406186ea4fc5"))
4035  {
4036  sql_rollback ();
4037  return -1;
4038  }
4039 
4040  if (migrate_54_to_55_format ("d5da9f67-8551-4e51-807b-b6a873d70e34",
4041  "a994b278-1f62-11e1-96ac-406186ea4fc5"))
4042  {
4043  sql_rollback ();
4044  return -1;
4045  }
4046 
4047  /* Set the database version to 55. */
4048 
4049  set_db_version (55);
4050 
4051  sql_commit ();
4052 
4053  return 0;
4054 }
4055 
4059 #define MIGRATE_55_TO_56_RANGE(type, start, end) \
4060  sql ("INSERT INTO port_ranges" \
4061  " (uuid, port_list, type, start, \"end\", comment, exclude)" \
4062  " VALUES" \
4063  " (make_uuid (), %llu, %i," \
4064  " '" G_STRINGIFY (start) "'," \
4065  " '" G_STRINGIFY (end) "'," \
4066  " '', 0)", \
4067  list, \
4068  type)
4069 
4073 void
4075 {
4076  if (sql_int ("SELECT count(*) FROM port_lists"
4077  " WHERE uuid = '" PORT_LIST_UUID_DEFAULT "';")
4078  == 0)
4079  {
4080  resource_t list;
4081  sql ("INSERT INTO port_lists (uuid, owner, name, comment)"
4082  " VALUES ('" PORT_LIST_UUID_DEFAULT "', NULL, 'OpenVAS Default',"
4083  " '')");
4084  list = sql_last_insert_id ();
4085 
4086  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1, 5);
4087  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7, 7);
4088  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9, 9);
4089  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11, 11);
4090  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 13, 13);
4091  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 15, 15);
4092  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 17, 25);
4093  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 27, 27);
4094  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 29, 29);
4095  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 31, 31);
4096  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 33, 33);
4097  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 35, 35);
4098  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 37, 39);
4099  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 41, 59);
4100  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 61, 224);
4101  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 242, 248);
4102  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 256, 268);
4103  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 280, 287);
4104  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 308, 322);
4105  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 333, 333);
4106  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 344, 700);
4107  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 702, 702);
4108  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 704, 707);
4109  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 709, 711);
4110  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 721, 721);
4111  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 723, 723);
4112  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 729, 731);
4113  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 740, 742);
4114  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 744, 744);
4115  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 747, 754);
4116  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 758, 765);
4117  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 767, 767);
4118  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 769, 777);
4119  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 780, 783);
4120  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 786, 787);
4121  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 799, 801);
4122  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 808, 808);
4123  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 810, 810);
4124  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 828, 829);
4125  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 847, 848);
4126  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 860, 860);
4127  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 871, 871);
4128  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 873, 873);
4129  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 886, 888);
4130  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 898, 898);
4131  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 900, 904);
4132  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 911, 913);
4133  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 927, 927);
4134  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 950, 950);
4135  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 953, 953);
4136  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 975, 975);
4137  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 989, 1002);
4138  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1005, 1005);
4139  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1008, 1008);
4140  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1010, 1010);
4141  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1023, 1027);
4142  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1029, 1036);
4143  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1040, 1040);
4144  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1042, 1042);
4145  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1045, 1045);
4146  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1047, 1112);
4147  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1114, 1117);
4148  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1119, 1120);
4149  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1122, 1127);
4150  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1139, 1139);
4151  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1154, 1155);
4152  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1161, 1162);
4153  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1168, 1170);
4154  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1178, 1178);
4155  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1180, 1181);
4156  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1183, 1188);
4157  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1194, 1194);
4158  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1199, 1231);
4159  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1233, 1286);
4160  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1288, 1774);
4161  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 1776, 2028);
4162  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2030, 2030);
4163  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2032, 2035);
4164  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2037, 2038);
4165  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2040, 2065);
4166  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2067, 2083);
4167  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2086, 2087);
4168  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2089, 2152);
4169  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2155, 2155);
4170  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2159, 2167);
4171  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2170, 2177);
4172  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2180, 2181);
4173  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2190, 2191);
4174  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2199, 2202);
4175  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2213, 2213);
4176  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2220, 2223);
4177  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2232, 2246);
4178  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2248, 2255);
4179  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2260, 2260);
4180  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2273, 2273);
4181  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2279, 2289);
4182  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2294, 2311);
4183  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2313, 2371);
4184  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2381, 2425);
4185  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2427, 2681);
4186  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2683, 2824);
4187  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2826, 2854);
4188  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2856, 2924);
4189  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 2926, 3096);
4190  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3098, 3299);
4191  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3302, 3321);
4192  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3326, 3366);
4193  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3372, 3403);
4194  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3405, 3545);
4195  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3547, 3707);
4196  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3709, 3765);
4197  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3767, 3770);
4198  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3772, 3800);
4199  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3802, 3802);
4200  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3845, 3871);
4201  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3875, 3876);
4202  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3885, 3885);
4203  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3900, 3900);
4204  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3928, 3929);
4205  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3939, 3939);
4206  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3959, 3959);
4207  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3970, 3971);
4208  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3984, 3987);
4209  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 3999, 4036);
4210  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4040, 4042);
4211  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4045, 4045);
4212  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4080, 4080);
4213  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4096, 4100);
4214  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4111, 4111);
4215  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4114, 4114);
4216  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4132, 4134);
4217  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4138, 4138);
4218  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4141, 4145);
4219  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4154, 4154);
4220  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4160, 4160);
4221  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4199, 4200);
4222  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4242, 4242);
4223  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4300, 4300);
4224  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4321, 4321);
4225  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4333, 4333);
4226  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4343, 4351);
4227  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4353, 4358);
4228  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4369, 4369);
4229  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4400, 4400);
4230  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4442, 4457);
4231  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4480, 4480);
4232  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4500, 4500);
4233  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4545, 4547);
4234  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4555, 4555);
4235  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4557, 4557);
4236  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4559, 4559);
4237  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4567, 4568);
4238  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4600, 4601);
4239  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4658, 4662);
4240  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4672, 4672);
4241  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4752, 4752);
4242  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4800, 4802);
4243  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4827, 4827);
4244  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4837, 4839);
4245  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4848, 4849);
4246  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4868, 4869);
4247  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4885, 4885);
4248  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4894, 4894);
4249  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4899, 4899);
4250  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4950, 4950);
4251  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4983, 4983);
4252  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4987, 4989);
4253  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 4998, 4998);
4254  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5000, 5011);
4255  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5020, 5025);
4256  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5031, 5031);
4257  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5042, 5042);
4258  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5050, 5057);
4259  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5060, 5061);
4260  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5064, 5066);
4261  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5069, 5069);
4262  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5071, 5071);
4263  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5081, 5081);
4264  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5093, 5093);
4265  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5099, 5102);
4266  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5137, 5137);
4267  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5145, 5145);
4268  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5150, 5152);
4269  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5154, 5154);
4270  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5165, 5165);
4271  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5190, 5193);
4272  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5200, 5203);
4273  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5222, 5222);
4274  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5225, 5226);
4275  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5232, 5232);
4276  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5236, 5236);
4277  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5250, 5251);
4278  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5264, 5265);
4279  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5269, 5269);
4280  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5272, 5272);
4281  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5282, 5282);
4282  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5300, 5311);
4283  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5314, 5315);
4284  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5351, 5355);
4285  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5400, 5432);
4286  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5435, 5435);
4287  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5454, 5456);
4288  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5461, 5463);
4289  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5465, 5465);
4290  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5500, 5504);
4291  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5510, 5510);
4292  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5520, 5521);
4293  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5530, 5530);
4294  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5540, 5540);
4295  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5550, 5550);
4296  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5553, 5556);
4297  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5566, 5566);
4298  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5569, 5569);
4299  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5595, 5605);
4300  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5631, 5632);
4301  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5666, 5666);
4302  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5673, 5680);
4303  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5688, 5688);
4304  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5690, 5690);
4305  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5713, 5717);
4306  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5720, 5720);
4307  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5729, 5730);
4308  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5741, 5742);
4309  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5745, 5746);
4310  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5755, 5755);
4311  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5757, 5757);
4312  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5766, 5768);
4313  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5771, 5771);
4314  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5800, 5803);
4315  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5813, 5813);
4316  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5858, 5859);
4317  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5882, 5882);
4318  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5888, 5889);
4319  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5900, 5903);
4320  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5968, 5969);
4321  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5977, 5979);
4322  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5987, 5991);
4323  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 5997, 6010);
4324  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6050, 6051);
4325  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6064, 6073);
4326  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6085, 6085);
4327  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6100, 6112);
4328  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6123, 6123);
4329  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6141, 6150);
4330  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6175, 6177);
4331  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6200, 6200);
4332  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6253, 6253);
4333  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6255, 6255);
4334  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6270, 6270);
4335  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6300, 6300);
4336  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6321, 6322);
4337  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6343, 6343);
4338  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6346, 6347);
4339  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6373, 6373);
4340  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6382, 6382);
4341  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6389, 6389);
4342  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6400, 6400);
4343  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6455, 6456);
4344  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6471, 6471);
4345  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6500, 6503);
4346  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6505, 6510);
4347  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6543, 6543);
4348  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6547, 6550);
4349  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6558, 6558);
4350  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6566, 6566);
4351  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6580, 6582);
4352  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6588, 6588);
4353  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6620, 6621);
4354  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6623, 6623);
4355  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6628, 6628);
4356  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6631, 6631);
4357  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6665, 6670);
4358  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6672, 6673);
4359  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6699, 6701);
4360  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6714, 6714);
4361  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6767, 6768);
4362  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6776, 6776);
4363  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6788, 6790);
4364  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6831, 6831);
4365  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6841, 6842);
4366  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6850, 6850);
4367  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6881, 6889);
4368  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6891, 6891);
4369  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6901, 6901);
4370  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6939, 6939);
4371  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6961, 6966);
4372  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6969, 6970);
4373  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 6998, 7015);
4374  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7020, 7021);
4375  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7030, 7030);
4376  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7070, 7070);
4377  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7099, 7100);
4378  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7121, 7121);
4379  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7161, 7161);
4380  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7170, 7170);
4381  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7174, 7174);
4382  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7200, 7201);
4383  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7210, 7210);
4384  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7269, 7269);
4385  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7273, 7273);
4386  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7280, 7281);
4387  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7283, 7283);
4388  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7300, 7300);
4389  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7320, 7320);
4390  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7326, 7326);
4391  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7391, 7392);
4392  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7395, 7395);
4393  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7426, 7431);
4394  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7437, 7437);
4395  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7464, 7464);
4396  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7491, 7491);
4397  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7501, 7501);
4398  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7510, 7511);
4399  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7544, 7545);
4400  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7560, 7560);
4401  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7566, 7566);
4402  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7570, 7570);
4403  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7575, 7575);
4404  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7588, 7588);
4405  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7597, 7597);
4406  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7624, 7624);
4407  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7626, 7627);
4408  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7633, 7634);
4409  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7648, 7649);
4410  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7666, 7666);
4411  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7674, 7676);
4412  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7743, 7743);
4413  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7775, 7779);
4414  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7781, 7781);
4415  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7786, 7786);
4416  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7797, 7798);
4417  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7800, 7801);
4418  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7845, 7846);
4419  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7875, 7875);
4420  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7902, 7902);
4421  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7913, 7913);
4422  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7932, 7933);
4423  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7967, 7967);
4424  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7979, 7980);
4425  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 7999, 8005);
4426  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8007, 8010);
4427  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8022, 8022);
4428  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8032, 8033);
4429  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8044, 8044);
4430  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8074, 8074);
4431  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8080, 8082);
4432  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8088, 8089);
4433  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8098, 8098);
4434  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8100, 8100);
4435  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8115, 8116);
4436  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8118, 8118);
4437  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8121, 8122);
4438  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8130, 8132);
4439  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8160, 8161);
4440  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8181, 8194);
4441  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8199, 8201);
4442  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8204, 8208);
4443  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8224, 8225);
4444  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8245, 8245);
4445  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8311, 8311);
4446  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8351, 8351);
4447  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8376, 8380);
4448  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8400, 8403);
4449  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8416, 8417);
4450  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8431, 8431);
4451  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8443, 8444);
4452  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8450, 8450);
4453  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8473, 8473);
4454  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8554, 8555);
4455  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8649, 8649);
4456  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8733, 8733);
4457  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8763, 8765);
4458  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8786, 8787);
4459  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8804, 8804);
4460  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8863, 8864);
4461  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8875, 8875);
4462  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8880, 8880);
4463  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8888, 8894);
4464  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8900, 8901);
4465  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8910, 8911);
4466  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8954, 8954);
4467  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8989, 8989);
4468  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 8999, 9002);
4469  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9006, 9006);
4470  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9009, 9009);
4471  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9020, 9026);
4472  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9080, 9080);
4473  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9090, 9091);
4474  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9100, 9103);
4475  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9110, 9111);
4476  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9131, 9131);
4477  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9152, 9152);
4478  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9160, 9164);
4479  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9200, 9207);
4480  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9210, 9211);
4481  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9217, 9217);
4482  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9281, 9285);
4483  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9287, 9287);
4484  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9292, 9292);
4485  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9321, 9321);
4486  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9343, 9344);
4487  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9346, 9346);
4488  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9374, 9374);
4489  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9390, 9390);
4490  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9396, 9397);
4491  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9400, 9400);
4492  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9418, 9418);
4493  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9495, 9495);
4494  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9500, 9500);
4495  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9535, 9537);
4496  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9593, 9595);
4497  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9600, 9600);
4498  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9612, 9612);
4499  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9704, 9704);
4500  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9747, 9747);
4501  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9753, 9753);
4502  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9797, 9797);
4503  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9800, 9802);
4504  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9872, 9872);
4505  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9875, 9876);
4506  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9888, 9889);
4507  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9898, 9901);
4508  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9909, 9909);
4509  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9911, 9911);
4510  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9950, 9952);
4511  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 9990, 10005);
4512  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10007, 10008);
4513  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10012, 10012);
4514  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10080, 10083);
4515  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10101, 10103);
4516  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10113, 10116);
4517  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10128, 10128);
4518  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10252, 10252);
4519  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10260, 10260);
4520  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10288, 10288);
4521  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10607, 10607);
4522  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10666, 10666);
4523  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10752, 10752);
4524  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 10990, 10990);
4525  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11000, 11001);
4526  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11111, 11111);
4527  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11201, 11201);
4528  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11223, 11223);
4529  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11319, 11321);
4530  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11367, 11367);
4531  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11371, 11371);
4532  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11600, 11600);
4533  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11720, 11720);
4534  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11751, 11751);
4535  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11965, 11965);
4536  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11967, 11967);
4537  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 11999, 12006);
4538  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 12076, 12076);
4539  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 12109, 12109);
4540  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 12168, 12168);
4541  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 12172, 12172);
4542  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 12223, 12223);
4543  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 12321, 12321);
4544  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 12345, 12346);
4545  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 12361, 12362);
4546  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 12468, 12468);
4547  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 12701, 12701);
4548  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 12753, 12753);
4549  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 13160, 13160);
4550  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 13223, 13224);
4551  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 13701, 13702);
4552  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 13705, 13706);
4553  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 13708, 13718);
4554  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 13720, 13722);
4555  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 13724, 13724);
4556  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 13782, 13783);
4557  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 13818, 13822);
4558  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 14001, 14001);
4559  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 14033, 14034);
4560  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 14141, 14141);
4561  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 14145, 14145);
4562  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 14149, 14149);
4563  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 14194, 14194);
4564  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 14237, 14237);
4565  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 14936, 14937);
4566  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 15000, 15000);
4567  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 15126, 15126);
4568  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 15345, 15345);
4569  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 15363, 15363);
4570  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 16360, 16361);
4571  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 16367, 16368);
4572  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 16384, 16384);
4573  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 16660, 16661);
4574  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 16959, 16959);
4575  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 16969, 16969);
4576  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 16991, 16991);
4577  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 17007, 17007);
4578  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 17185, 17185);
4579  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 17219, 17219);
4580  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 17300, 17300);
4581  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 17770, 17772);
4582  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 18000, 18000);
4583  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 18181, 18187);
4584  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 18190, 18190);
4585  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 18241, 18241);
4586  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 18463, 18463);
4587  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 18769, 18769);
4588  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 18888, 18888);
4589  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 19191, 19191);
4590  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 19194, 19194);
4591  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 19283, 19283);
4592  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 19315, 19315);
4593  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 19398, 19398);
4594  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 19410, 19412);
4595  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 19540, 19541);
4596  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 19638, 19638);
4597  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 19726, 19726);
4598  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 20000, 20001);
4599  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 20005, 20005);
4600  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 20011, 20012);
4601  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 20034, 20034);
4602  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 20200, 20200);
4603  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 20202, 20203);
4604  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 20222, 20222);
4605  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 20670, 20670);
4606  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 20999, 21000);
4607  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 21490, 21490);
4608  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 21544, 21544);
4609  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 21590, 21590);
4610  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 21800, 21800);
4611  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 21845, 21849);
4612  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 22000, 22001);
4613  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 22222, 22222);
4614  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 22273, 22273);
4615  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 22289, 22289);
4616  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 22305, 22305);
4617  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 22321, 22321);
4618  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 22370, 22370);
4619  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 22555, 22555);
4620  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 22800, 22800);
4621  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 22951, 22951);
4622  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 23456, 23456);
4623  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 24000, 24006);
4624  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 24242, 24242);
4625  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 24249, 24249);
4626  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 24345, 24347);
4627  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 24386, 24386);
4628  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 24554, 24554);
4629  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 24677, 24678);
4630  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 24922, 24922);
4631  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 25000, 25009);
4632  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 25378, 25378);
4633  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 25544, 25544);
4634  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 25793, 25793);
4635  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 25867, 25867);
4636  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 25901, 25901);
4637  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 25903, 25903);
4638  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 26000, 26000);
4639  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 26208, 26208);
4640  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 26260, 26264);
4641  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 27000, 27010);
4642  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 27345, 27345);
4643  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 27374, 27374);
4644  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 27504, 27504);
4645  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 27665, 27665);
4646  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 27999, 27999);
4647  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 28001, 28001);
4648  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 29559, 29559);
4649  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 29891, 29891);
4650  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 30001, 30002);
4651  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 30100, 30102);
4652  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 30303, 30303);
4653  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 30999, 30999);
4654  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 31337, 31337);
4655  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 31339, 31339);
4656  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 31416, 31416);
4657  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 31457, 31457);
4658  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 31554, 31554);
4659  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 31556, 31556);
4660  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 31620, 31620);
4661  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 31765, 31765);
4662  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 31785, 31787);
4663  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 32261, 32261);
4664  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 32666, 32666);
4665  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 32768, 32780);
4666  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 32786, 32787);
4667  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 32896, 32896);
4668  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 33270, 33270);
4669  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 33331, 33331);
4670  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 33434, 33434);
4671  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 33911, 33911);
4672  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 34249, 34249);
4673  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 34324, 34324);
4674  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 34952, 34952);
4675  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 36865, 36865);
4676  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 37475, 37475);
4677  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 37651, 37651);
4678  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 38037, 38037);
4679  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 38201, 38201);
4680  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 38292, 38293);
4681  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 39681, 39681);
4682  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 40412, 40412);
4683  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 40841, 40843);
4684  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 41111, 41111);
4685  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 41508, 41508);
4686  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 41794, 41795);
4687  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 42508, 42510);
4688  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 43118, 43118);
4689  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 43188, 43190);
4690  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 44321, 44322);
4691  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 44333, 44334);
4692  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 44442, 44443);
4693  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 44818, 44818);
4694  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 45000, 45000);
4695  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 45054, 45054);
4696  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 45678, 45678);
4697  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 45966, 45966);
4698  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 47000, 47000);
4699  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 47557, 47557);
4700  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 47624, 47624);
4701  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 47806, 47806);
4702  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 47808, 47808);
4703  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 47891, 47891);
4704  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 48000, 48003);
4705  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 48556, 48556);
4706  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 49400, 49400);
4707  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 50000, 50004);
4708  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 50505, 50505);
4709  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 50776, 50776);
4710  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 51210, 51210);
4711  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 53001, 53001);
4712  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 54320, 54321);
4713  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 57341, 57341);
4714  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 59595, 59595);
4715  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 60177, 60177);
4716  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 60179, 60179);
4717  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 61439, 61441);
4718  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 61446, 61446);
4719  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 65000, 65000);
4720  MIGRATE_55_TO_56_RANGE (PORT_PROTOCOL_TCP, 65301, 65301);
4721  }
4722 }
4723 
4729 int
4731 {
4732  iterator_t rows;
4733 
4735 
4736  /* Ensure that the database is currently version 55. */
4737 
4738  if (manage_db_version () != 55)
4739  {
4740  sql_rollback ();
4741  return -1;
4742  }
4743 
4744  /* Update the database. */
4745 
4746  /* The port_range in the targets and targets_trash tables changed to
4747  * refer to a port list. The targets_trash table got a port_list_location
4748  * column. */
4749 
4752  /* Add the new column. */
4753 
4754  sql ("ALTER TABLE targets_trash ADD COLUMN port_list_location;");
4755  sql ("UPDATE targets_trash"
4756  " SET port_list_location = " G_STRINGIFY (LOCATION_TRASH) ";");
4757 
4758  /* Ensure the new tables exist for the migrator. */
4759 
4760  sql ("CREATE TABLE IF NOT EXISTS port_lists"
4761  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment);");
4762  sql ("CREATE TABLE IF NOT EXISTS port_lists_trash"
4763  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment);");
4764  sql ("CREATE TABLE IF NOT EXISTS port_ranges"
4765  " (id INTEGER PRIMARY KEY, uuid UNIQUE, port_list INTEGER, type,"
4766  " start, end, comment, exclude);");
4767  sql ("CREATE TABLE IF NOT EXISTS port_ranges_trash"
4768  " (id INTEGER PRIMARY KEY, uuid UNIQUE, port_list INTEGER, type,"
4769  " start, end, comment, exclude);");
4770 
4772 
4773  /* Make a port list and port range(s) for each target. */
4774 
4775  init_iterator (&rows, "SELECT id, owner, name, port_range FROM targets;");
4776  while (next (&rows))
4777  {
4778  resource_t target;
4779  const gchar *range;
4780 
4781  target = iterator_int64 (&rows, 0);
4782  range = iterator_string (&rows, 3);
4783 
4784  if (range && strcmp (range, "default"))
4785  {
4786  resource_t owner, list;
4787  const gchar *name;
4788  gchar *quoted_name;
4789 
4790  owner = iterator_int64 (&rows, 1);
4791  name = iterator_string (&rows, 2);
4792  quoted_name = sql_quote (name);
4793 
4794  /* Make the port list. Store target in comment for modification
4795  * outside iteration. */
4796 
4797  sql ("INSERT INTO port_lists (uuid, owner, name, comment)"
4798  " VALUES (make_uuid (), %llu, '%s', %llu)",
4799  owner,
4800  quoted_name,
4801  target);
4802 
4803  g_free (quoted_name);
4804 
4805  list = sql_last_insert_id ();
4806 
4807  /* Convert old range (1-100,1649,210-214) to multiple new ranges. */
4808 
4809  {
4810  gchar **split, **point;
4811 
4812  while (*range && isblank (*range)) range++;
4813 
4814  split = g_strsplit (range, ",", 0);
4815  point = split;
4816 
4817  while (*point)
4818  {
4819  gchar *hyphen;
4820 
4821  hyphen = strchr (*point, '-');
4822  if (hyphen)
4823  {
4824  *hyphen = '\0';
4825  hyphen++;
4826 
4827  /* A range. */
4828 
4829  sql ("INSERT INTO port_ranges"
4830  " (uuid, port_list, type, start, \"end\", comment,"
4831  " exclude)"
4832  " VALUES"
4833  " (make_uuid (), %llu, %i, %s, %s, '', 0)",
4834  list,
4835  PORT_PROTOCOL_TCP,
4836  *point,
4837  hyphen);
4838  }
4839  else
4840  {
4841  /* A single port. */
4842 
4843  sql ("INSERT INTO port_ranges"
4844  " (uuid, port_list, type, start, \"end\", comment,"
4845  " exclude)"
4846  " VALUES"
4847  " (make_uuid (), %llu, %i, %s, NULL, '',"
4848  " 0)",
4849  list,
4850  PORT_PROTOCOL_TCP,
4851  *point);
4852  }
4853  point += 1;
4854  }
4855 
4856  g_strfreev (split);
4857  }
4858  }
4859  else
4860  sql ("UPDATE targets SET port_range"
4861  " = (SELECT id FROM port_lists"
4862  " WHERE uuid = '" PORT_LIST_UUID_DEFAULT "')"
4863  " WHERE id = %llu;",
4864  target);
4865  }
4866  cleanup_iterator (&rows);
4867 
4868  /* Set the port_ranges of the targets to the new port lists. */
4869 
4870  sql ("UPDATE targets SET"
4871  " port_range = (SELECT id FROM port_lists"
4872  " WHERE comment = targets.id)"
4873  " WHERE port_range"
4874  " != (SELECT id FROM port_lists"
4875  " WHERE uuid = '" PORT_LIST_UUID_DEFAULT "');");
4876 
4877  sql ("UPDATE port_lists SET"
4878  " comment = 'Migrated from target '"
4879  " || (SELECT targets.name FROM targets"
4880  " WHERE port_lists.id = targets.port_range)"
4881  " || '.'"
4882  " WHERE uuid != '" PORT_LIST_UUID_DEFAULT "';");
4883 
4884  /* Make a port list and port range(s) for each trash target. */
4885 
4886  init_iterator (&rows,
4887  "SELECT id, owner, name, port_range FROM targets_trash;");
4888  while (next (&rows))
4889  {
4890  resource_t target;
4891  gchar *range;
4892 
4893  target = iterator_int64 (&rows, 0);
4894  range = g_strdup (iterator_string (&rows, 3));
4895 
4896  if (range && strcmp (range, "default"))
4897  {
4898  resource_t owner, list;
4899  const gchar *name;
4900  gchar *quoted_name;
4901 
4902  owner = iterator_int64 (&rows, 1);
4903  name = iterator_string (&rows, 2);
4904  quoted_name = sql_quote (name);
4905 
4906  /* Make the port list. Store target in comment for modification
4907  * outside iteration. */
4908 
4909  sql ("INSERT INTO port_lists_trash (uuid, owner, name, comment)"
4910  " VALUES (make_uuid (), %llu, '%s', %llu)",
4911  owner,
4912  quoted_name,
4913  target);
4914 
4915  g_free (quoted_name);
4916 
4917  list = sql_last_insert_id ();
4918 
4919  /* Convert old range (1-100,1649,210-214) to multiple new ranges. */
4920 
4921  {
4922  gchar **split, **point;
4923 
4924  while (*range && isblank (*range)) range++;
4925 
4926  split = g_strsplit (range, ",", 0);
4927  point = split;
4928 
4929  while (*point)
4930  {
4931  gchar *hyphen;
4932 
4933  hyphen = strchr (*point, '-');
4934  if (hyphen)
4935  {
4936  *hyphen = '\0';
4937  hyphen++;
4938 
4939  /* A range. */
4940 
4941  sql ("INSERT INTO port_ranges_trash"
4942  " (uuid, port_list, type, start, \"end\", comment,"
4943  " exclude)"
4944  " VALUES"
4945  " (make_uuid (), %llu, %i, %s, %s, '', 0)",
4946  list,
4947  PORT_PROTOCOL_TCP,
4948  *point,
4949  hyphen);
4950  }
4951  else
4952  {
4953  /* A single port. */
4954 
4955  sql ("INSERT INTO port_ranges_trash"
4956  " (uuid, port_list, type, start, \"end\", comment,"
4957  " exclude)"
4958  " VALUES"
4959  " (make_uuid (), %llu, %i, %s, NULL, '',"
4960  " 0)",
4961  list,
4962  PORT_PROTOCOL_TCP,
4963  *point);
4964  }
4965  point += 1;
4966  }
4967 
4968  g_strfreev (split);
4969  }
4970  }
4971  else
4972  sql ("UPDATE targets_trash SET port_range"
4973  " = (SELECT id FROM port_lists"
4974  " WHERE uuid = '" PORT_LIST_UUID_DEFAULT "'),"
4975  " port_list_location = " G_STRINGIFY (LOCATION_TABLE)
4976  " WHERE id = %llu;",
4977  target);
4978 
4979  g_free (range);
4980  }
4981  cleanup_iterator (&rows);
4982 
4983  /* Set the port_ranges of the trash targets to the new port lists. */
4984 
4985  sql ("UPDATE targets_trash SET"
4986  " port_range = (SELECT id FROM port_lists_trash"
4987  " WHERE comment = targets_trash.id)"
4988  " WHERE port_range"
4989  " != (SELECT id FROM port_lists"
4990  " WHERE uuid = '" PORT_LIST_UUID_DEFAULT "');");
4991 
4992  sql ("UPDATE port_lists_trash SET"
4993  " comment = 'Migrated from trashcan target '"
4994  " || (SELECT targets_trash.name FROM targets_trash"
4995  " WHERE port_lists_trash.id = targets_trash.port_range)"
4996  " || '.'");
4997 
4998  /* Set the database version to 56. */
4999 
5000  set_db_version (56);
5001 
5002  sql_commit ();
5003 
5004  return 0;
5005 }
5006 
5012 int
5014 {
5016 
5017  /* Ensure that the database is currently version 56. */
5018 
5019  if (manage_db_version () != 56)
5020  {
5021  sql_rollback ();
5022  return -1;
5023  }
5024 
5025  /* Update the database. */
5026 
5029  /* Ensure the new tables exist for the migrator. */
5030 
5031  sql ("CREATE TABLE IF NOT EXISTS escalator_condition_data"
5032  " (id INTEGER PRIMARY KEY, escalator INTEGER, name, data);");
5033  sql ("CREATE TABLE IF NOT EXISTS escalator_condition_data_trash"
5034  " (id INTEGER PRIMARY KEY, escalator INTEGER, name, data);");
5035  sql ("CREATE TABLE IF NOT EXISTS escalator_event_data"
5036  " (id INTEGER PRIMARY KEY, escalator INTEGER, name, data);");
5037  sql ("CREATE TABLE IF NOT EXISTS escalator_event_data_trash"
5038  " (id INTEGER PRIMARY KEY, escalator INTEGER, name, data);");
5039  sql ("CREATE TABLE IF NOT EXISTS escalator_method_data"
5040  " (id INTEGER PRIMARY KEY, escalator INTEGER, name, data);");
5041  sql ("CREATE TABLE IF NOT EXISTS escalator_method_data_trash"
5042  " (id INTEGER PRIMARY KEY, escalator INTEGER, name, data);");
5043  sql ("CREATE TABLE IF NOT EXISTS escalators"
5044  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
5045  " event INTEGER, condition INTEGER, method INTEGER);");
5046  sql ("CREATE TABLE IF NOT EXISTS escalators_trash"
5047  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
5048  " event INTEGER, condition INTEGER, method INTEGER);");
5049  sql ("CREATE TABLE IF NOT EXISTS task_escalators"
5050  " (id INTEGER PRIMARY KEY, task INTEGER, escalator INTEGER,"
5051  " escalator_location INTEGER);");
5052 
5053  /* Escalators were renamed to alerts. */
5054 
5055  sql ("CREATE TABLE alert_condition_data"
5056  " (id INTEGER PRIMARY KEY, alert INTEGER, name, data);");
5057  sql_rename_column ("escalator_condition_data", "alert_condition_data",
5058  "escalator", "alert");
5059  sql ("DROP TABLE escalator_condition_data;");
5060 
5061  /* Note: This is missing the escalator_condition_data_trash case. It's so
5062  * long ago that anyone who was affected has probably worked through the
5063  * problem already, so I'm leaving it like this. */
5064 
5065  sql ("CREATE TABLE alert_event_data"
5066  " (id INTEGER PRIMARY KEY, alert INTEGER, name, data);");
5067  sql_rename_column ("escalator_event_data", "alert_event_data",
5068  "escalator", "alert");
5069  sql ("DROP TABLE escalator_event_data;");
5070 
5071  sql ("CREATE TABLE alert_event_data_trash"
5072  " (id INTEGER PRIMARY KEY, alert INTEGER, name, data);");
5073  sql_rename_column ("escalator_event_data_trash", "alert_event_data_trash",
5074  "escalator", "alert");
5075  sql ("DROP TABLE escalator_event_data_trash;");
5076 
5077  sql ("CREATE TABLE alert_method_data"
5078  " (id INTEGER PRIMARY KEY, alert INTEGER, name, data);");
5079  sql_rename_column ("escalator_method_data", "alert_method_data",
5080  "escalator", "alert");
5081  sql ("DROP TABLE escalator_method_data;");
5082 
5083  sql ("CREATE TABLE alert_method_data_trash"
5084  " (id INTEGER PRIMARY KEY, alert INTEGER, name, data);");
5085  sql_rename_column ("escalator_method_data_trash", "alert_method_data_trash",
5086  "escalator", "alert");
5087  sql ("DROP TABLE escalator_method_data_trash;");
5088 
5089  sql ("CREATE TABLE alerts"
5090  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
5091  " event INTEGER, condition INTEGER, method INTEGER);");
5092  sql_rename_column ("escalators", "alerts",
5093  "escalator", "alert");
5094  sql ("DROP TABLE escalators;");
5095 
5096  sql ("CREATE TABLE alerts_trash"
5097  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
5098  " event INTEGER, condition INTEGER, method INTEGER);");
5099  sql_rename_column ("escalators_trash", "alerts_trash",
5100  "escalator", "alert");
5101  sql ("DROP TABLE escalators_trash;");
5102 
5103  sql ("CREATE TABLE task_alerts_56"
5104  " (id INTEGER PRIMARY KEY, task INTEGER, alert INTEGER,"
5105  " escalator_location INTEGER);");
5106  sql_rename_column ("task_escalators", "task_alerts_56",
5107  "escalator", "alert");
5108  sql ("DROP TABLE task_escalators;");
5109 
5110  sql ("CREATE TABLE task_alerts"
5111  " (id INTEGER PRIMARY KEY, task INTEGER, alert INTEGER,"
5112  " alert_location INTEGER);");
5113  sql_rename_column ("task_alerts_56", "task_alerts",
5114  "escalator_location", "alert_location");
5115  sql ("DROP TABLE task_alerts_56;");
5116 
5117  /* Set the database version to 57. */
5118 
5119  set_db_version (57);
5120 
5121  sql_commit ();
5122 
5123  return 0;
5124 }
5125 
5131 int
5133 {
5135 
5136  /* Ensure that the database is currently version 57. */
5137 
5138  if (manage_db_version () != 57)
5139  {
5140  sql_rollback ();
5141  return -1;
5142  }
5143 
5144  /* Update the database. */
5145 
5148  /* Ensure the new tables exist for the migrator. */
5149 
5150  sql ("CREATE TABLE IF NOT EXISTS agents_trash"
5151  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
5152  " installer TEXT, installer_64 TEXT, installer_filename,"
5153  " installer_signature_64 TEXT, installer_trust INTEGER,"
5154  " installer_trust_time, howto_install TEXT, howto_use TEXT);");
5155 
5156  /* Targets and agents got creation and modification times. */
5157 
5158  sql ("ALTER TABLE targets ADD COLUMN creation_time;");
5159  sql ("ALTER TABLE targets ADD COLUMN modification_time;");
5160  sql ("UPDATE targets SET creation_time = 0, modification_time = 0;");
5161 
5162  sql ("ALTER TABLE targets_trash ADD COLUMN creation_time;");
5163  sql ("ALTER TABLE targets_trash ADD COLUMN modification_time;");
5164  sql ("UPDATE targets_trash SET creation_time = 0, modification_time = 0;");
5165 
5166  sql ("ALTER TABLE agents ADD COLUMN creation_time;");
5167  sql ("ALTER TABLE agents ADD COLUMN modification_time;");
5168  sql ("UPDATE agents SET creation_time = 0, modification_time = 0;");
5169 
5170  sql ("ALTER TABLE agents_trash ADD COLUMN creation_time;");
5171  sql ("ALTER TABLE agents_trash ADD COLUMN modification_time;");
5172  sql ("UPDATE agents_trash SET creation_time = 0, modification_time = 0;");
5173 
5174  /* Set the database version to 58. */
5175 
5176  set_db_version (58);
5177 
5178  sql_commit ();
5179 
5180  return 0;
5181 }
5182 
5188 int
5190 {
5192 
5193  /* Ensure that the database is currently version 58. */
5194 
5195  if (manage_db_version () != 58)
5196  {
5197  sql_rollback ();
5198  return -1;
5199  }
5200 
5201  /* Update the database. */
5202 
5205  /* Database version 55 introduced new UUIDs for the predefined report formats.
5206  Update the alert method data to use these new UUIDs. */
5207 
5208  sql ("UPDATE alert_method_data"
5209  " SET data = '5ceff8ba-1f62-11e1-ab9f-406186ea4fc5'"
5210  " WHERE data = 'a0704abb-2120-489f-959f-251c9f4ffebd';");
5211 
5212  sql ("UPDATE alert_method_data"
5213  " SET data = '6c248850-1f62-11e1-b082-406186ea4fc5'"
5214  " WHERE data = 'b993b6f5-f9fb-4e6e-9c94-dd46c00e058d';");
5215 
5216  sql ("UPDATE alert_method_data"
5217  " SET data = '77bd6c4a-1f62-11e1-abf0-406186ea4fc5'"
5218  " WHERE data = '929884c6-c2c4-41e7-befb-2f6aa163b458';");
5219 
5220  sql ("UPDATE alert_method_data"
5221  " SET data = '7fcc3a1a-1f62-11e1-86bf-406186ea4fc5'"
5222  " WHERE data = '9f1ab17b-aaaa-411a-8c57-12df446f5588';");
5223 
5224  sql ("UPDATE alert_method_data"
5225  " SET data = '9ca6fe72-1f62-11e1-9e7c-406186ea4fc5'"
5226  " WHERE data = 'f5c2a364-47d2-4700-b21d-0a7693daddab';");
5227 
5228  sql ("UPDATE alert_method_data"
5229  " SET data = 'a0b5bfb2-1f62-11e1-85db-406186ea4fc5'"
5230  " WHERE data = '1a60a67e-97d0-4cbf-bc77-f71b08e7043d';");
5231 
5232  sql ("UPDATE alert_method_data"
5233  " SET data = 'a3810a62-1f62-11e1-9219-406186ea4fc5'"
5234  " WHERE data = '19f6f1b3-7128-4433-888c-ccc764fe6ed5';");
5235 
5236  sql ("UPDATE alert_method_data"
5237  " SET data = 'a994b278-1f62-11e1-96ac-406186ea4fc5'"
5238  " WHERE data = 'd5da9f67-8551-4e51-807b-b6a873d70e34';");
5239 
5240  /* Set the database version to 59. */
5241 
5242  set_db_version (59);
5243 
5244  sql_commit ();
5245 
5246  return 0;
5247 }
5248 
5254 int
5256 {
5258 
5259  /* Ensure that the database is currently version 59. */
5260 
5261  if (manage_db_version () != 59)
5262  {
5263  sql_rollback ();
5264  return -1;
5265  }
5266 
5267  /* Update the database. */
5268 
5271  /* Every task must now have an in_assets task preference. */
5272 
5273  sql ("INSERT INTO task_preferences (task, name, value)"
5274  " SELECT id, 'in_assets', 'yes' FROM tasks;");
5275 
5276  /* Set the database version to 60. */
5277 
5278  set_db_version (60);
5279 
5280  sql_commit ();
5281 
5282  return 0;
5283 }
5284 
5290 int
5292 {
5294 
5295  /* Ensure that the database is currently version 60. */
5296 
5297  if (manage_db_version () != 60)
5298  {
5299  sql_rollback ();
5300  return -1;
5301  }
5302 
5303  /* Update the database. */
5304 
5307  /* The alerts and alerts_trash tables got filter columns. */
5308 
5309  sql ("ALTER TABLE alerts ADD COLUMN filter INTEGER;");
5310  sql ("UPDATE alerts SET filter = 0;");
5311 
5312  sql ("ALTER TABLE alerts_trash ADD COLUMN filter INTEGER;");
5313  sql ("UPDATE alerts_trash SET filter = 0;");
5314 
5315  sql ("ALTER TABLE alerts_trash ADD COLUMN filter_location INTEGER;");
5316  sql ("UPDATE alerts_trash SET filter_location = 0;");
5317 
5318  /* Set the database version to 61. */
5319 
5320  set_db_version (61);
5321 
5322  sql_commit ();
5323 
5324  return 0;
5325 }
5326 
5332 int
5334 {
5336 
5337  /* Ensure that the database is currently version 61. */
5338 
5339  if (manage_db_version () != 61)
5340  {
5341  sql_rollback ();
5342  return -1;
5343  }
5344 
5345  /* Update the database. */
5346 
5349  /* The reports table got count cache columns. */
5350 
5351  sql ("ALTER TABLE reports ADD COLUMN highs;");
5352  sql ("ALTER TABLE reports ADD COLUMN mediums;");
5353  sql ("ALTER TABLE reports ADD COLUMN lows;");
5354  sql ("ALTER TABLE reports ADD COLUMN logs;");
5355  sql ("ALTER TABLE reports ADD COLUMN fps;");
5356  sql ("ALTER TABLE reports ADD COLUMN override_highs;");
5357  sql ("ALTER TABLE reports ADD COLUMN override_mediums;");
5358  sql ("ALTER TABLE reports ADD COLUMN override_lows;");
5359  sql ("ALTER TABLE reports ADD COLUMN override_logs;");
5360  sql ("ALTER TABLE reports ADD COLUMN override_fps;");
5361 
5362  sql ("UPDATE reports SET highs = -1, override_highs = -1;");
5363 
5364  /* Set the database version to 62. */
5365 
5366  set_db_version (62);
5367 
5368  sql_commit ();
5369 
5370  return 0;
5371 }
5372 
5378 int
5380 {
5382 
5383  /* Ensure that the database is currently version 62. */
5384 
5385  if (manage_db_version () != 62)
5386  {
5387  sql_rollback ();
5388  return -1;
5389  }
5390 
5391  /* Update the database. */
5392 
5395  /* Ensure the new tables exist for the migrator. */
5396 
5397  sql ("CREATE TABLE IF NOT EXISTS schedules_trash"
5398  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment,"
5399  " first_time, period, period_months, duration);");
5400 
5401  /* The reports table got count cache columns. */
5402 
5403  sql ("ALTER TABLE schedules ADD COLUMN timezone;");
5404  sql ("ALTER TABLE schedules ADD COLUMN initial_offset;");
5405 
5406  sql ("UPDATE schedules"
5407  " SET timezone = (SELECT users.timezone FROM users"
5408  " WHERE id = schedules.owner);");
5409 
5410  sql ("UPDATE schedules SET initial_offset = current_offset (timezone);");
5411 
5412  sql ("ALTER TABLE schedules_trash ADD COLUMN timezone;");
5413  sql ("ALTER TABLE schedules_trash ADD COLUMN initial_offset;");
5414 
5415  sql ("UPDATE schedules_trash"
5416  " SET timezone = (SELECT users.timezone FROM users"
5417  " WHERE id = schedules_trash.owner);");
5418 
5419  sql ("UPDATE schedules_trash"
5420  " SET initial_offset = current_offset (timezone);");
5421 
5422  /* Set the database version to 63. */
5423 
5424  set_db_version (63);
5425 
5426  sql_commit ();
5427 
5428  return 0;
5429 }
5430 
5436 int
5438 {
5440 
5441  /* Ensure that the database is currently version 63. */
5442 
5443  if (manage_db_version () != 63)
5444  {
5445  sql_rollback ();
5446  return -1;
5447  }
5448 
5449  /* Update the database. */
5450 
5453  /* The results table got a report column. */
5454 
5455  sql ("ALTER TABLE results ADD COLUMN report;");
5456 
5457  sql ("UPDATE results SET report = (SELECT report FROM report_results"
5458  " WHERE result = results.id);");
5459 
5460  sql ("CREATE INDEX IF NOT EXISTS results_by_report_host"
5461  " ON results (report, host);");
5462 
5463  /* Set the database version to 64. */
5464 
5465  set_db_version (64);
5466 
5467  sql_commit ();
5468 
5469  return 0;
5470 }
5471 
5477 int
5479 {
5481 
5482  /* Ensure that the database is currently version 64. */
5483 
5484  if (manage_db_version () != 64)
5485  {
5486  sql_rollback ();
5487  return -1;
5488  }
5489 
5490  /* Update the database. */
5491 
5494  /* The report column on new results was left blank. */
5495 
5496  sql ("UPDATE results SET report = (SELECT report FROM report_results"
5497  " WHERE result = results.id);");
5498 
5499  sql ("REINDEX results_by_report_host;");
5500 
5501  /* Set the database version to 65. */
5502 
5503  set_db_version (65);
5504 
5505  sql_commit ();
5506 
5507  return 0;
5508 }
5509 
5515 int
5517 {
5519 
5520  /* Ensure that the database is currently version 65. */
5521 
5522  if (manage_db_version () != 65)
5523  {
5524  sql_rollback ();
5525  return -1;
5526  }
5527 
5528  /* Update the database. */
5529 
5532  /* Schedules got creation and modification times. */
5533 
5534  sql ("ALTER TABLE schedules ADD COLUMN creation_time;");
5535  sql ("ALTER TABLE schedules ADD COLUMN modification_time;");
5536  sql ("UPDATE schedules SET creation_time = 0, modification_time = 0;");
5537 
5538  sql ("ALTER TABLE schedules_trash ADD COLUMN creation_time;");
5539  sql ("ALTER TABLE schedules_trash ADD COLUMN modification_time;");
5540  sql ("UPDATE schedules_trash SET creation_time = 0, modification_time = 0;");
5541 
5542  /* Set the database version to 66. */
5543 
5544  set_db_version (66);
5545 
5546  sql_commit ();
5547 
5548  return 0;
5549 }
5550 
5556 int
5558 {
5560 
5561  /* Ensure that the database is currently version 66. */
5562 
5563  if (manage_db_version () != 66)
5564  {
5565  sql_rollback ();
5566  return -1;
5567  }
5568 
5569  /* Update the database. */
5570 
5573  /* Tasks got creation and modification times. */
5574 
5575  sql ("ALTER TABLE tasks ADD COLUMN creation_time;");
5576  sql ("ALTER TABLE tasks ADD COLUMN modification_time;");
5577  sql ("UPDATE tasks SET creation_time = 0, modification_time = 0;");
5578 
5579  /* Set the database version to 67. */
5580 
5581  set_db_version (67);
5582 
5583  sql_commit ();
5584 
5585  return 0;
5586 }
5587 
5593 int
5595 {
5597 
5598  /* Ensure that the database is currently version 67. */
5599 
5600  if (manage_db_version () != 67)
5601  {
5602  sql_rollback ();
5603  return -1;
5604  }
5605 
5606  /* Update the database. */
5607 
5610  /* Ensure the new tables exist for the migrator. */
5611 
5612  sql ("CREATE TABLE IF NOT EXISTS slaves_trash"
5613  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment, host,"
5614  " port, login, password);");
5615 
5616  /* Slaves got creation and modification times. */
5617 
5618  sql ("ALTER TABLE slaves ADD COLUMN creation_time;");
5619  sql ("ALTER TABLE slaves ADD COLUMN modification_time;");
5620  sql ("UPDATE slaves SET creation_time = 0, modification_time = 0;");
5621 
5622  sql ("ALTER TABLE slaves_trash ADD COLUMN creation_time;");
5623  sql ("ALTER TABLE slaves_trash ADD COLUMN modification_time;");
5624  sql ("UPDATE slaves_trash SET creation_time = 0, modification_time = 0;");
5625 
5626  /* Set the database version to 68. */
5627 
5628  set_db_version (68);
5629 
5630  sql_commit ();
5631 
5632  return 0;
5633 }
5634 
5640 int
5642 {
5644 
5645  /* Ensure that the database is currently version 68. */
5646 
5647  if (manage_db_version () != 68)
5648  {
5649  sql_rollback ();
5650  return -1;
5651  }
5652 
5653  /* Update the database. */
5654 
5657  /* Ensure the new tables exist for the migrator. */
5658 
5659  sql ("CREATE TABLE IF NOT EXISTS report_formats_trash"
5660  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, extension,"
5661  " content_type, summary, description, signature, trust INTEGER,"
5662  " trust_time, flags INTEGER, original_uuid);");
5663 
5664  /* Schedules got creation and modification times. */
5665 
5666  sql ("ALTER TABLE report_formats ADD COLUMN creation_time;");
5667  sql ("ALTER TABLE report_formats ADD COLUMN modification_time;");
5668  sql ("UPDATE report_formats SET creation_time = 0, modification_time = 0;");
5669 
5670  sql ("ALTER TABLE report_formats_trash ADD COLUMN creation_time;");
5671  sql ("ALTER TABLE report_formats_trash ADD COLUMN modification_time;");
5672  sql ("UPDATE report_formats_trash SET"
5673  " creation_time = 0, modification_time = 0;");
5674 
5675  /* Set the database version to 69. */
5676 
5677  set_db_version (69);
5678 
5679  sql_commit ();
5680 
5681  return 0;
5682 }
5683 
5689 int
5691 {
5693 
5694  /* Ensure that the database is currently version 69. */
5695 
5696  if (manage_db_version () != 69)
5697  {
5698  sql_rollback ();
5699  return -1;
5700  }
5701 
5702  /* Update the database. */
5703 
5706  /* Add creation and modification times to Port Lists. */
5707 
5708  sql ("ALTER TABLE port_lists ADD COLUMN creation_time;");
5709  sql ("ALTER TABLE port_lists ADD COLUMN modification_time;");
5710  sql ("UPDATE port_lists SET creation_time = 0, modification_time = 0;");
5711 
5712  sql ("ALTER TABLE port_lists_trash ADD COLUMN creation_time;");
5713  sql ("ALTER TABLE port_lists_trash ADD COLUMN modification_time;");
5714  sql ("UPDATE port_lists_trash SET"
5715  " creation_time = 0, modification_time = 0;");
5716 
5717  /* Set the database version to 70. */
5718 
5719  set_db_version (70);
5720 
5721  sql_commit ();
5722 
5723  return 0;
5724 }
5725 
5731 int
5733 {
5735 
5736  /* Ensure that the database is currently version 70. */
5737 
5738  if (manage_db_version () != 70)
5739  {
5740  sql_rollback ();
5741  return -1;
5742  }
5743 
5744  /* Update the database. */
5745 
5748  /* Add creation and modification times to alerts. */
5749 
5750  sql ("ALTER TABLE alerts ADD COLUMN creation_time;");
5751  sql ("ALTER TABLE alerts ADD COLUMN modification_time;");
5752  sql ("UPDATE alerts SET creation_time = 0, modification_time = 0;");
5753 
5754  sql ("ALTER TABLE alerts_trash ADD COLUMN creation_time;");
5755  sql ("ALTER TABLE alerts_trash ADD COLUMN modification_time;");
5756  sql ("UPDATE alerts_trash SET"
5757  " creation_time = 0, modification_time = 0;");
5758 
5759  /* Set the database version to 71. */
5760 
5761  set_db_version (71);
5762 
5763  sql_commit ();
5764 
5765  return 0;
5766 }
5767 
5773 int
5775 {
5777 
5778  /* Ensure that the database is currently version 71. */
5779 
5780  if (manage_db_version () != 71)
5781  {
5782  sql_rollback ();
5783  return -1;
5784  }
5785 
5786  /* Update the database. */
5787 
5790  /* Ensure the new tables exist for the migrator. */
5791 
5792  sql ("CREATE TABLE IF NOT EXISTS lsc_credentials_trash"
5793  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, login,"
5794  " password, comment, public_key TEXT, private_key TEXT, rpm TEXT,"
5795  " deb TEXT, exe TEXT);");
5796 
5797  /* Add creation and modification times to LSC Credentials. */
5798 
5799  sql ("ALTER TABLE lsc_credentials ADD COLUMN creation_time;");
5800  sql ("ALTER TABLE lsc_credentials ADD COLUMN modification_time;");
5801  sql ("UPDATE lsc_credentials SET creation_time = 0, modification_time = 0;");
5802 
5803  sql ("ALTER TABLE lsc_credentials_trash ADD COLUMN creation_time;");
5804  sql ("ALTER TABLE lsc_credentials_trash ADD COLUMN modification_time;");
5805  sql ("UPDATE lsc_credentials_trash SET"
5806  " creation_time = 0, modification_time = 0;");
5807 
5808  /* Set the database version to 72. */
5809 
5810  set_db_version (72);
5811 
5812  sql_commit ();
5813 
5814  return 0;
5815 }
5816 
5822 int
5824 {
5826 
5827  /* Ensure that the database is currently version 72. */
5828 
5829  if (manage_db_version () != 72)
5830  {
5831  sql_rollback ();
5832  return -1;
5833  }
5834 
5835  /* Update the database. */
5836 
5839  /* Ensure the new tables exist for the migrator. */
5840 
5841  sql ("CREATE TABLE IF NOT EXISTS configs_trash"
5842  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name,"
5843  " nvt_selector, comment, family_count INTEGER, nvt_count INTEGER,"
5844  " families_growing INTEGER, nvts_growing INTEGER);");
5845 
5846  /* Add creation and modification times to Scan Configs. */
5847 
5848  sql ("ALTER TABLE configs ADD COLUMN creation_time;");
5849  sql ("ALTER TABLE configs ADD COLUMN modification_time;");
5850  sql ("UPDATE configs SET creation_time = 0, modification_time = 0;");
5851 
5852  sql ("ALTER TABLE configs_trash ADD COLUMN creation_time;");
5853  sql ("ALTER TABLE configs_trash ADD COLUMN modification_time;");
5854  sql ("UPDATE configs_trash SET creation_time = 0, modification_time = 0;");
5855 
5856  /* Set the database version to 73. */
5857 
5858  set_db_version (73);
5859 
5860  sql_commit ();
5861 
5862  return 0;
5863 }
5864 
5870 int
5872 {
5874 
5875  /* Ensure that the database is currently version 73. */
5876 
5877  if (manage_db_version () != 73)
5878  {
5879  sql_rollback ();
5880  return -1;
5881  }
5882 
5883  /* Update the database. */
5884 
5887  /* Add creation and modification times to Scan Configs. */
5888 
5889  sql ("ALTER TABLE nvts ADD COLUMN uuid;");
5890  sql ("UPDATE nvts SET uuid = oid;");
5891 
5892  sql ("ALTER TABLE nvts ADD COLUMN comment;");
5893  sql ("UPDATE nvts SET comment = '';");
5894 
5895  sql ("ALTER TABLE nvts ADD COLUMN creation_time;");
5896  sql ("ALTER TABLE nvts ADD COLUMN modification_time;");
5897  sql ("UPDATE nvts SET"
5898  " creation_time = parse_time (tag (tag, 'creation_date')),"
5899  " modification_time = parse_time (tag (tag, 'last_modification'));");
5900 
5901  /* Set the database version to 74. */
5902 
5903  set_db_version (74);
5904 
5905  sql_commit ();
5906 
5907  return 0;
5908 }
5909 
5915 int
5917 {
5919 
5920  /* Ensure that the database is currently version 74. */
5921 
5922  if (manage_db_version () != 74)
5923  {
5924  sql_rollback ();
5925  return -1;
5926  }
5927 
5928  /* Update the database. */
5929 
5930  /* Ensure the tables exist for the migrator. */
5931 
5932  sql ("CREATE TABLE IF NOT EXISTS permissions"
5933  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner, name, comment,"
5934  " resource_type, resource, resource_uuid, resource_location,"
5935  " subject_type, subject, creation_time, modification_time);");
5936 
5937  sql ("CREATE TABLE IF NOT EXISTS task_users"
5938  " (id INTEGER PRIMARY KEY, task INTEGER, user INTEGER,"
5939  " actions INTEGER);");
5940 
5943  /* Task observers are now handled by permissions. */
5944 
5945  sql ("INSERT INTO permissions"
5946  " (uuid, owner, name, comment, resource_type, resource, resource_uuid,"
5947  " resource_location, subject_type, subject, creation_time,"
5948  " modification_time)"
5949  " SELECT make_uuid (),"
5950  " (SELECT owner FROM tasks WHERE id = task),"
5951  " 'get', '', 'task', task,"
5952  " (SELECT uuid FROM tasks WHERE id = task),"
5953  " " G_STRINGIFY (LOCATION_TABLE) ", 'user', user, m_now (), m_now ()"
5954  " FROM task_users;");
5955 
5956  sql ("DROP TABLE task_users;");
5957 
5958  /* Set the database version to 75. */
5959 
5960  set_db_version (75);
5961 
5962  sql_commit ();
5963 
5964  return 0;
5965 }
5966 
5972 int
5974 {
5976 
5977  /* Ensure that the database is currently version 75. */
5978 
5979  if (manage_db_version () != 75)
5980  {
5981  sql_rollback ();
5982  return -1;
5983  }
5984 
5985  /* Update the database. */
5986 
5987  /* Delete any nvts_checksum leftovers. */
5988  sql ("DELETE FROM %s.meta WHERE name = \"nvts_checksum\";",
5989  sql_schema ());
5990 
5991  /* Rename nvts_md5sum into nvts_feed_version */
5992  sql ("UPDATE %s.meta SET name = \"nvts_feed_version\""
5993  " WHERE name = \"nvts_md5sum\";",
5994  sql_schema ());
5995 
5996  /* Set the database version to 76. */
5997 
5998  set_db_version (76);
5999 
6000  sql_commit ();
6001 
6002  return 0;
6003 }
6004 
6010 int
6012 {
6014 
6015  /* Ensure that the database is currently version 76. */
6016 
6017  if (manage_db_version () != 76)
6018  {
6019  sql_rollback ();
6020  return -1;
6021  }
6022 
6023  /* Update the database. */
6024 
6025  /* Users got standard columns, and columns to mirror info stored on disk. */
6026 
6027  sql ("ALTER TABLE users ADD COLUMN owner;");
6028  sql ("ALTER TABLE users ADD COLUMN comment;");
6029  sql ("ALTER TABLE users ADD COLUMN creation_time;");
6030  sql ("ALTER TABLE users ADD COLUMN modification_time;");
6031  sql ("ALTER TABLE users ADD COLUMN role;");
6032  sql ("ALTER TABLE users ADD COLUMN hosts;");
6033  sql ("ALTER TABLE users ADD COLUMN hosts_allow;");
6034  sql ("UPDATE users SET"
6035  " owner = NULL,"
6036  " comment = '',"
6037  " creation_time = 0,"
6038  " modification_time = 0,"
6039  /* These are temporary, and only used for clone. */
6040  " role = 'User',"
6041  " hosts = '',"
6042  " hosts_allow = 2;");
6043 
6044  /* Set the database version to 77. */
6045 
6046  set_db_version (77);
6047 
6048  sql_commit ();
6049 
6050  return 0;
6051 }
6052 
6058 int
6060 {
6062 
6063  /* Ensure that the database is currently version 77. */
6064  if (manage_db_version () != 77)
6065  {
6066  sql_rollback ();
6067  return -1;
6068  }
6069 
6070  /* Update the database. */
6071 
6072  /* Set schedule durations and periods to 0 if they were -1,
6073  which was the old default value of the create_schedule command. */
6074  sql ("UPDATE schedules"
6075  " SET duration = 0"
6076  " WHERE duration = -1;");
6077 
6078  sql ("UPDATE schedules"
6079  " SET period = 0"
6080  " WHERE period = -1;");
6081 
6082  sql ("UPDATE schedules_trash"
6083  " SET duration = 0"
6084  " WHERE duration = -1;");
6085 
6086  sql ("UPDATE schedules_trash"
6087  " SET period = 0"
6088  " WHERE period = -1;");
6089 
6090  /* Set the database version to 78. */
6091 
6092  set_db_version (78);
6093 
6094  sql_commit ();
6095 
6096  return 0;
6097 }
6098 
6104 int
6106 {
6108 
6109  /* Ensure that the database is currently version 78. */
6110 
6111  if (manage_db_version () != 78)
6112  {
6113  sql_rollback ();
6114  return -1;
6115  }
6116 
6117  /* Update the database. */
6118 
6119  /* Remove tcp timestamps nvt from Discovery Scan Config. */
6120 
6121  sql ("DELETE FROM nvt_selectors WHERE "
6123  " AND family_or_nvt='1.3.6.1.4.1.25623.1.0.80091';");
6124 
6125  /* Add preferences for "Ping Host" nvt in Discovery Scan Config. */
6126  sql ("INSERT INTO config_preferences (config, type, name, value)"
6127  " VALUES ((SELECT id FROM configs WHERE uuid = '"
6128  CONFIG_UUID_DISCOVERY "'),"
6129  " 'PLUGINS_PREFS',"
6130  " 'Ping Host[checkbox]:Mark unrechable Hosts as dead (not scanning)',"
6131  " 'yes');");
6132  sql ("INSERT INTO config_preferences (config, type, name, value)"
6133  " VALUES ((SELECT id FROM configs WHERE uuid = '"
6134  CONFIG_UUID_DISCOVERY "'),"
6135  " 'PLUGINS_PREFS',"
6136  " 'Ping Host[checkbox]:Report about unrechable Hosts',"
6137  " 'yes');");
6138 
6139  /* Add preferences for "Services" nvt in Discovery Scan Config. */
6140  sql ("INSERT INTO config_preferences (config, type, name, value)"
6141  " VALUES ((SELECT id FROM configs WHERE uuid = '"
6142  CONFIG_UUID_DISCOVERY "'),"
6143  " 'PLUGINS_PREFS',"
6144  " 'Services[radio]:Test SSL based services',"
6145  " 'All;Known SSL ports;None');");
6146 
6147  /* Set the database version to 79. */
6148 
6149  set_db_version (79);
6150 
6151  sql_commit ();
6152 
6153  return 0;
6154 }
6155 
6156 #define MIGRATE_79_to_80_DELETE(table) \
6157  sql ("DELETE FROM " table \
6158  " WHERE owner IN (SELECT id FROM users WHERE %s);", \
6159  where)
6160 
6166 void
6168 {
6169  /* Remove everything that is owned by the user. */
6170  MIGRATE_79_to_80_DELETE ("agents");
6171  MIGRATE_79_to_80_DELETE ("agents_trash");
6172  sql ("DELETE FROM config_preferences"
6173  " WHERE config IN (SELECT id FROM configs"
6174  " WHERE owner IN (SELECT id FROM users"
6175  " WHERE %s));",
6176  where);
6177  sql ("DELETE FROM config_preferences_trash"
6178  " WHERE config IN (SELECT id FROM configs"
6179  " WHERE owner IN (SELECT id FROM users"
6180  " WHERE %s));",
6181  where);
6182  sql ("DELETE FROM nvt_selectors"
6183  " WHERE name IN (SELECT nvt_selector FROM configs"
6184  " WHERE owner IN (SELECT id FROM users"
6185  " WHERE %s));",
6186  where);
6187  MIGRATE_79_to_80_DELETE ("configs");
6188  MIGRATE_79_to_80_DELETE ("configs_trash");
6189  sql ("DELETE FROM alert_condition_data"
6190  " WHERE alert IN (SELECT id FROM alerts"
6191  " WHERE owner IN (SELECT id FROM users"
6192  " WHERE %s));",
6193  where);
6194  sql ("DELETE FROM alert_condition_data_trash"
6195  " WHERE alert IN (SELECT id FROM alerts_trash"
6196  " WHERE owner IN (SELECT id FROM users"
6197  " WHERE %s));",
6198  where);
6199  sql ("DELETE FROM alert_event_data"
6200  " WHERE alert IN (SELECT id FROM alerts"
6201  " WHERE owner IN (SELECT id FROM users"
6202  " WHERE %s));",
6203  where);
6204  sql ("DELETE FROM alert_event_data_trash"
6205  " WHERE alert IN (SELECT id FROM alerts_trash"
6206  " WHERE owner IN (SELECT id FROM users"
6207  " WHERE %s));",
6208  where);
6209  sql ("DELETE FROM alert_method_data"
6210  " WHERE alert IN (SELECT id FROM alerts"
6211  " WHERE owner IN (SELECT id FROM users"
6212  " WHERE %s));",
6213  where);
6214  sql ("DELETE FROM alert_method_data_trash"
6215  " WHERE alert IN (SELECT id FROM alerts_trash"
6216  " WHERE owner IN (SELECT id FROM users"
6217  " WHERE %s));",
6218  where);
6219  MIGRATE_79_to_80_DELETE ("alerts");
6220  MIGRATE_79_to_80_DELETE ("alerts_trash");
6221  MIGRATE_79_to_80_DELETE ("filters");
6222  MIGRATE_79_to_80_DELETE ("filters_trash");
6223  sql ("DELETE FROM group_users"
6224  " WHERE `group` IN (SELECT id FROM groups"
6225  " WHERE owner IN (SELECT id FROM users"
6226  " WHERE %s));",
6227  where);
6228  MIGRATE_79_to_80_DELETE ("groups");
6229  MIGRATE_79_to_80_DELETE ("lsc_credentials");
6230  MIGRATE_79_to_80_DELETE ("lsc_credentials_trash");
6231  MIGRATE_79_to_80_DELETE ("notes");
6232  MIGRATE_79_to_80_DELETE ("notes_trash");
6233  MIGRATE_79_to_80_DELETE ("overrides");
6234  MIGRATE_79_to_80_DELETE ("overrides_trash");
6235  MIGRATE_79_to_80_DELETE ("permissions");
6236  MIGRATE_79_to_80_DELETE ("permissions_trash");
6237  MIGRATE_79_to_80_DELETE ("port_lists");
6238  MIGRATE_79_to_80_DELETE ("port_lists_trash");
6239  sql ("DELETE FROM port_ranges"
6240  " WHERE port_list IN (SELECT id FROM port_lists"
6241  " WHERE owner IN (SELECT id FROM users"
6242  " WHERE %s));",
6243  where);
6244  sql ("DELETE FROM port_ranges_trash"
6245  " WHERE port_list IN (SELECT id FROM port_lists_trash"
6246  " WHERE owner IN (SELECT id FROM users"
6247  " WHERE %s));",
6248  where);
6249  sql ("DELETE FROM report_format_param_options"
6250  " WHERE report_format_param"
6251  " IN (SELECT id FROM report_format_params"
6252  " WHERE report_format"
6253  " IN (SELECT id FROM report_formats"
6254  " WHERE owner IN (SELECT id FROM users"
6255  " WHERE %s)));",
6256  where);
6257  sql ("DELETE FROM report_format_param_options_trash"
6258  " WHERE report_format_param"
6259  " IN (SELECT id FROM report_format_params_trash"
6260  " WHERE report_format"
6261  " IN (SELECT id FROM report_formats"
6262  " WHERE owner IN (SELECT id FROM users"
6263  " WHERE %s)));",
6264  where);
6265  sql ("DELETE FROM report_format_params"
6266  " WHERE report_format IN (SELECT id FROM report_formats"
6267  " WHERE owner IN (SELECT id FROM users"
6268  " WHERE %s));",
6269  where);
6270  sql ("DELETE FROM report_format_params_trash"
6271  " WHERE report_format IN (SELECT id FROM report_formats"
6272  " WHERE owner IN (SELECT id FROM users"
6273  " WHERE %s));",
6274  where);
6275  MIGRATE_79_to_80_DELETE ("report_formats");
6276  MIGRATE_79_to_80_DELETE ("report_formats_trash");
6277  sql ("DELETE FROM report_host_details"
6278  " WHERE report_host"
6279  " IN (SELECT id FROM report_hosts"
6280  " WHERE report IN (SELECT id FROM reports"
6281  " WHERE owner IN (SELECT id FROM users"
6282  " WHERE %s)));",
6283  where);
6284  sql ("DELETE FROM report_results"
6285  " WHERE report IN (SELECT id FROM reports"
6286  " WHERE owner IN (SELECT id FROM users"
6287  " WHERE %s));",
6288  where);
6289  sql ("DELETE FROM results"
6290  " WHERE report IN (SELECT id FROM reports"
6291  " WHERE owner IN (SELECT id FROM users"
6292  " WHERE %s));",
6293  where);
6294  MIGRATE_79_to_80_DELETE ("reports");
6295  MIGRATE_79_to_80_DELETE ("schedules");
6296  MIGRATE_79_to_80_DELETE ("schedules_trash");
6297  MIGRATE_79_to_80_DELETE ("slaves");
6298  MIGRATE_79_to_80_DELETE ("slaves_trash");
6299  MIGRATE_79_to_80_DELETE ("settings");
6300  MIGRATE_79_to_80_DELETE ("tags");
6301  MIGRATE_79_to_80_DELETE ("tags_trash");
6302  MIGRATE_79_to_80_DELETE ("targets");
6303  MIGRATE_79_to_80_DELETE ("targets_trash");
6304  sql ("DELETE FROM task_files"
6305  " WHERE task IN (SELECT id FROM tasks"
6306  " WHERE owner IN (SELECT id FROM users"
6307  " WHERE %s));",
6308  where);
6309  sql ("DELETE FROM task_alerts"
6310  " WHERE task IN (SELECT id FROM tasks"
6311  " WHERE owner IN (SELECT id FROM users"
6312  " WHERE %s));",
6313  where);
6314  sql ("DELETE FROM task_preferences"
6315  " WHERE task IN (SELECT id FROM tasks"
6316  " WHERE owner IN (SELECT id FROM users"
6317  " WHERE %s));",
6318  where);
6319  MIGRATE_79_to_80_DELETE ("tasks");
6320  sql ("DELETE FROM users WHERE %s;",
6321  where);
6322 }
6323 
6324 #define RULES_HEADER "# This file is managed by the OpenVAS Administrator.\n# Any modifications must keep to the format that the Administrator expects.\n"
6325 
6335 int
6336 migrate_79_to_80_user_access (const gchar *user_dir, gchar ** hosts,
6337  int *hosts_allow)
6338 {
6339  gchar *rules_file, *rules;
6340  GError *error = NULL;
6341 
6342  assert (hosts != NULL);
6343  assert (hosts_allow != NULL);
6344 
6345  rules_file = g_build_filename (user_dir, "auth", "rules", NULL);
6346  if (g_file_test (rules_file, G_FILE_TEST_EXISTS) == FALSE)
6347  {
6348  *hosts = NULL;
6349  *hosts_allow = 2;
6350  return 0;
6351  }
6352 
6353  g_file_get_contents (rules_file, &rules, NULL, &error);
6354  if (error)
6355  {
6356  g_warning ("%s", error->message);
6357  g_error_free (error);
6358  g_free (rules_file);
6359  return -1;
6360  }
6361  g_free (rules_file);
6362 
6363  if (strlen (rules))
6364  {
6365  int count, end = 0;
6366 
6367  /* "# " ("allow " | "deny ") hosts */
6368 
6369  count = sscanf (rules, RULES_HEADER "# allow %*[^\n]%n\n", &end);
6370  if (count == 0 && end > 0)
6371  {
6372  *hosts =
6373  g_strndup (rules + strlen (RULES_HEADER "# allow "),
6374  end - strlen (RULES_HEADER "# allow "));
6375  *hosts_allow = 1;
6376  g_free (rules);
6377  return 0;
6378  }
6379 
6380  count = sscanf (rules, RULES_HEADER "# deny %*[^\n]%n\n", &end);
6381  if (count == 0 && end > 0)
6382  {
6383  *hosts =
6384  g_strndup (rules + strlen (RULES_HEADER "# deny "),
6385  end - strlen (RULES_HEADER "# deny "));
6386  *hosts_allow = 0;
6387  g_free (rules);
6388  return 0;
6389  }
6390 
6391  if (strcmp (RULES_HEADER, rules) == 0)
6392  {
6393  *hosts = NULL;
6394  *hosts_allow = 2;
6395  g_free (rules);
6396  return 0;
6397  }
6398 
6399  /* Failed to parse content. */
6400  *hosts = NULL;
6401  *hosts_allow = 3;
6402  g_free (rules);
6403  return 0;
6404  }
6405 
6406  *hosts = NULL;
6407  *hosts_allow = 2;
6408  g_free (rules);
6409  return 0;
6410 }
6411 
6417 int
6419 {
6420  struct dirent **names;
6421  int count, index;
6422  array_t *dirs;
6423  gchar *dir;
6424  struct stat state;
6425 
6427 
6428  /* Ensure that the database is currently version 79. */
6429 
6430  if (manage_db_version () != 79)
6431  {
6432  sql_rollback ();
6433  return -1;
6434  }
6435 
6436  /* Update the database. */
6437 
6438  /* Ensure that all tables exists. */
6439  sql ("CREATE TABLE IF NOT EXISTS alert_condition_data_trash"
6440  " (id INTEGER PRIMARY KEY, alert INTEGER, name, data);");
6441  sql ("CREATE TABLE IF NOT EXISTS config_preferences_trash"
6442  " (id INTEGER PRIMARY KEY, config INTEGER, type, name, value);");
6443  sql ("CREATE TABLE IF NOT EXISTS groups"
6444  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
6445  " creation_time, modification_time);");
6446  sql ("CREATE TABLE IF NOT EXISTS group_users"
6447  " (id INTEGER PRIMARY KEY, `group` INTEGER, user INTEGER);");
6448  sql ("CREATE TABLE IF NOT EXISTS filters"
6449  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
6450  " type, term, creation_time, modification_time);");
6451  sql ("CREATE TABLE IF NOT EXISTS filters_trash"
6452  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
6453  " type, term, creation_time, modification_time);");
6454  sql ("CREATE TABLE IF NOT EXISTS notes_trash"
6455  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt,"
6456  " creation_time, modification_time, text, hosts, port, threat,"
6457  " task INTEGER, result INTEGER, end_time);");
6458  sql ("CREATE TABLE IF NOT EXISTS overrides_trash"
6459  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt,"
6460  " creation_time, modification_time, text, hosts, port, threat,"
6461  " new_threat, task INTEGER, result INTEGER, end_time);");
6462  sql ("CREATE TABLE IF NOT EXISTS permissions"
6463  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner, name, comment,"
6464  " resource_type, resource, resource_uuid, resource_location,"
6465  " subject_type, subject, creation_time, modification_time);");
6466  sql ("CREATE TABLE IF NOT EXISTS permissions_trash"
6467  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner, name, comment,"
6468  " resource_type, resource, resource_uuid, resource_location,"
6469  " subject_type, subject, creation_time, modification_time);");
6470  sql ("CREATE TABLE IF NOT EXISTS port_names"
6471  " (id INTEGER PRIMARY KEY, number INTEGER, protocol, name,"
6472  " UNIQUE (number, protocol) ON CONFLICT REPLACE);");
6473  sql ("CREATE TABLE IF NOT EXISTS report_format_params_trash"
6474  " (id INTEGER PRIMARY KEY, report_format, name, type INTEGER, value,"
6475  " type_min, type_max, type_regex, fallback);");
6476  sql ("CREATE TABLE IF NOT EXISTS report_format_param_options_trash"
6477  " (id INTEGER PRIMARY KEY, report_format_param, value);");
6478  sql ("CREATE TABLE IF NOT EXISTS settings"
6479  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment, value);");
6480  sql ("CREATE TABLE IF NOT EXISTS tags"
6481  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner, name, comment,"
6482  " creation_time, modification_time, attach_type, attach_id,"
6483  " active, value);");
6484  sql ("CREATE TABLE IF NOT EXISTS tags_trash"
6485  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner, name, comment,"
6486  " creation_time, modification_time, attach_type, attach_id,"
6487  " active, value);");
6488 
6489  /* Users got new column "method". User data moved from disk to database. */
6490 
6491  sql ("ALTER TABLE users ADD COLUMN method;");
6492  sql ("UPDATE users SET method = 'file';");
6493 
6494  count = scandir (OPENVAS_STATE_DIR "/users", &names, NULL, alphasort);
6495  if (count < 0)
6496  {
6497  g_warning ("%s: failed to open dir %s/users: %s\n",
6498  __FUNCTION__,
6499  OPENVAS_STATE_DIR,
6500  strerror (errno));
6501  sql_rollback ();
6502  return -1;
6503  }
6504 
6505  dirs = make_array ();
6506 
6507  /* Set a flag on every user, to see which are left over. */
6508 
6509  sql ("UPDATE users SET password = -1;");
6510 
6511  /* Update db users from classic classic users, checking for ldap_connect at
6512  * the same time. Assume that all ldap_connect users have at least one
6513  * classic user of the same name. Remove all other users, both from disk
6514  * and from the db. Remove special user "om" from the database. */
6515 
6516  for (index = 0; index < count; index++)
6517  {
6518  gchar *role, *role_file, *uuid, *uuid_file, *classic_dir, *remote_dir;
6519  gchar *hash, *hosts, *quoted_hash, *quoted_hosts, *quoted_method;
6520  gchar *quoted_name, *quoted_uuid, *where, *file, *remote_flag_file;
6521  auth_method_t method;
6522  GError *error;
6523  user_t user;
6524  int hosts_allow;
6525 
6526  if ((strcmp (names[index]->d_name, ".") == 0)
6527  || (strcmp (names[index]->d_name, "..") == 0)
6528  || (strcmp (names[index]->d_name, "om") == 0))
6529  {
6530  free (names[index]);
6531  continue;
6532  }
6533 
6534  /* Figure out the user dir. */
6535 
6536  remote_dir = g_build_filename (OPENVAS_STATE_DIR,
6537  "users-remote",
6538  "ldap_connect",
6539  names[index]->d_name,
6540  NULL);
6541  classic_dir = g_build_filename (OPENVAS_STATE_DIR, "users",
6542  names[index]->d_name,
6543  NULL);
6544  remote_flag_file = g_build_filename (classic_dir,
6545  "auth",
6546  "methods",
6547  "ldap_connect",
6548  NULL);
6549  g_debug (" user: %s\n", names[index]->d_name);
6550  g_debug (" remote dir: %s\n", remote_dir);
6551  g_debug (" classic dir: %s\n", classic_dir);
6552  g_debug (" flag file: %s\n", remote_flag_file);
6553  if (g_file_test (remote_dir, G_FILE_TEST_IS_DIR)
6554  && g_file_test (remote_flag_file, G_FILE_TEST_EXISTS))
6555  method = AUTHENTICATION_METHOD_LDAP_CONNECT;
6556  else
6557  {
6558  g_free (remote_dir);
6559  method = AUTHENTICATION_METHOD_FILE;
6560  if (g_file_test (classic_dir, G_FILE_TEST_IS_DIR) == FALSE)
6561  {
6562  free (names[index]);
6563  g_free (classic_dir);
6564  g_free (remote_flag_file);
6565  continue;
6566  }
6567  remote_dir = g_strdup (classic_dir);
6568  }
6569  g_free (remote_flag_file);
6570 
6571  /* Get UUID from file. */
6572 
6573  uuid_file = g_build_filename (remote_dir, "uuid", NULL);
6574  error = NULL;
6575  g_file_get_contents (uuid_file,
6576  &uuid,
6577  NULL,
6578  &error);
6579  if (error)
6580  {
6581  g_warning ("%s: Failed to read %s: %s\n",
6582  __FUNCTION__,
6583  uuid_file,
6584  error->message);
6585  g_free (classic_dir);
6586  g_free (remote_dir);
6587  g_free (uuid_file);
6588  g_error_free (error);
6589  sql_rollback ();
6590  return -1;
6591  }
6592  g_free (uuid_file);
6593 
6594  /* Check UUID. */
6595 
6596  if (uuid == NULL || strlen (g_strchomp (uuid)) != 36)
6597  {
6598  g_warning ("%s: Error in UUID: %s\n",
6599  __FUNCTION__,
6600  uuid);
6601  g_free (classic_dir);
6602  g_free (remote_dir);
6603  g_free (uuid);
6604  sql_rollback ();
6605  return -1;
6606  }
6607  g_debug (" uuid: %s\n", uuid);
6608 
6609  /* Get role. */
6610 
6611  role = "User";
6612 
6613  role_file = g_build_filename (remote_dir, "isobserver", NULL);
6614  if (g_file_test (role_file, G_FILE_TEST_EXISTS))
6615  role = "Observer";
6616  g_free (role_file);
6617 
6618  role_file = g_build_filename (remote_dir, "isadmin", NULL);
6619  if (g_file_test (role_file, G_FILE_TEST_EXISTS))
6620  role = "Admin";
6621  g_free (role_file);
6622 
6623  /* Find user in db. */
6624 
6625  quoted_uuid = sql_quote (uuid);
6626  switch (sql_int64 (&user,
6627  "SELECT id FROM users WHERE uuid = '%s';",
6628  quoted_uuid))
6629  {
6630  case 0:
6631  break;
6632  case 1: /* Too few rows in result of query. */
6633  quoted_name = sql_quote (names[index]->d_name);
6634  sql ("INSERT INTO users"
6635  " (uuid, owner, name, comment, password, timezone, method,"
6636  " hosts, hosts_allow)"
6637  " VALUES"
6638  " ('%s', NULL, '%s', '', NULL, NULL, 'file', '', 2);",
6639  quoted_uuid,
6640  quoted_name);
6641  g_free (quoted_name);
6642  user = sql_last_insert_id ();
6643  break;
6644  default: /* Programming error. */
6645  assert (0);
6646  case -1:
6647  g_warning ("%s: Error finding user %s\n",
6648  __FUNCTION__,
6649  uuid);
6650  g_free (uuid);
6651  g_free (quoted_uuid);
6652  g_free (classic_dir);
6653  g_free (remote_dir);
6654  sql_rollback ();
6655  return -1;
6656  break;
6657  }
6658 
6659  /* Get hash. */
6660 
6661  file = g_build_filename (classic_dir, "auth", "hash", NULL);
6662  error = NULL;
6663  if (file && g_file_test (file, G_FILE_TEST_EXISTS))
6664  {
6665  g_file_get_contents (file,
6666  &hash,
6667  NULL,
6668  &error);
6669  if (error)
6670  {
6671  g_warning ("%s: Failed to read %s: %s\n",
6672  __FUNCTION__,
6673  file,
6674  error->message);
6675  g_free (classic_dir);
6676  g_free (remote_dir);
6677  g_free (quoted_uuid);
6678  g_free (file);
6679  g_error_free (error);
6680  sql_rollback ();
6681  return -1;
6682  }
6683  assert (hash);
6684  g_strchomp (hash);
6685  }
6686  else
6687  hash = NULL;
6688  g_free (file);
6689 
6690  /* Get host access rules. */
6691 
6692  hosts = NULL;
6693  hosts_allow = 2;
6694  if (migrate_79_to_80_user_access (classic_dir, &hosts, &hosts_allow))
6695  {
6696  g_warning ("%s: Failed to get user rules from %s\n",
6697  __FUNCTION__,
6698  classic_dir);
6699  g_free (classic_dir);
6700  g_free (remote_dir);
6701  g_free (quoted_uuid);
6702  g_error_free (error);
6703  sql_rollback ();
6704  return -1;
6705  }
6706 
6707  if (hosts_allow == 3)
6708  /* If they were custom rules, just make is allow all. */
6709  hosts_allow = 2;
6710 
6711  /* Update db from disk. */
6712 
6713  quoted_method = sql_quote (auth_method_name (method));
6714  quoted_hash = sql_quote (hash ? hash : "");
6715  quoted_hosts = sql_quote (hosts ? hosts : "");
6716  sql ("UPDATE users"
6717  " SET role = '%s',"
6718  " uuid = '%s',"
6719  " method = '%s',"
6720  " password = %s%s%s,"
6721  " hosts = '%s',"
6722  " hosts_allow = %i"
6723  " WHERE id = %llu;",
6724  role,
6725  quoted_uuid,
6726  quoted_method,
6727  hash ? "'" : "",
6728  hash ? quoted_hash : "NULL",
6729  hash ? "'" : "",
6730  quoted_hosts,
6731  hosts_allow,
6732  user);
6733  g_free (quoted_uuid);
6734  g_free (quoted_method);
6735  g_free (quoted_hash);
6736  g_free (quoted_hosts);
6737 
6738  /* Remove all other users with this name from the db. */
6739 
6740  quoted_name = sql_quote (names[index]->d_name);
6741  where = g_strdup_printf ("name = '%s' AND id != %llu",
6742  quoted_name,
6743  user);
6744  g_free (quoted_name);
6746  g_free (where);
6747 
6748  /* Store user directory for removal after last possible ROLLBACK. */
6749 
6750  array_add (dirs, classic_dir);
6751 
6752  free (names[index]);
6753  }
6754  free (names);
6755 
6756  /* TODO To preserve ldap and ads, create db entries here. */
6757 
6758  /* Remove remaining users. */
6759 
6760  migrate_79_to_80_remove_users ("password = -1");
6761 
6762  /* Remove entire user-remote dir. */
6763 
6764  dir = g_build_filename (OPENVAS_STATE_DIR, "users-remote", NULL);
6765  if (g_lstat (dir, &state))
6766  {
6767  if (errno != ENOENT)
6768  g_warning ("%s: g_lstat (%s) failed: %s\n",
6769  __FUNCTION__, dir, g_strerror (errno));
6770  }
6771  else
6772  openvas_file_remove_recurse (dir);
6773 
6774  g_free (dir);
6775 
6776  /* Remove user dirs. */
6777 
6778  for (index = 0; index < dirs->len; index++)
6779  openvas_file_remove_recurse (g_ptr_array_index (dirs, index));
6780  array_free (dirs);
6781 
6782  /* Set the database version to 80. */
6783 
6784  set_db_version (80);
6785 
6786  sql_commit ();
6787 
6788  return 0;
6789 }
6790 
6796 int
6798 {
6800 
6801  /* Ensure that the database is currently version 80. */
6802 
6803  if (manage_db_version () != 80)
6804  {
6805  sql_rollback ();
6806  return -1;
6807  }
6808 
6809  /* Update the database. */
6810 
6811  /* Ensure new tables exist. */
6812  sql ("CREATE TABLE IF NOT EXISTS roles"
6813  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
6814  " creation_time, modification_time);");
6815  sql ("CREATE TABLE IF NOT EXISTS role_users"
6816  " (id INTEGER PRIMARY KEY, role INTEGER, user INTEGER);");
6817 
6818  /* User roles moved to their own table. */
6819 
6820  sql ("INSERT INTO roles"
6821  " (uuid, owner, name, comment, creation_time, modification_time)"
6822  " VALUES"
6823  " ('" ROLE_UUID_ADMIN "', NULL, 'Admin', 'Administrator', m_now (),"
6824  " m_now ());");
6825 
6826  sql ("INSERT INTO roles"
6827  " (uuid, owner, name, comment, creation_time, modification_time)"
6828  " VALUES"
6829  " ('" ROLE_UUID_USER "', NULL, 'User', 'User', m_now (), m_now ());");
6830 
6831  sql ("INSERT INTO roles"
6832  " (uuid, owner, name, comment, creation_time, modification_time)"
6833  " VALUES"
6834  " ('" ROLE_UUID_OBSERVER "', NULL, 'Observer', 'Observer', m_now (),"
6835  " m_now ());");
6836 
6837  sql ("INSERT INTO role_users (role, user)"
6838  " SELECT (SELECT id FROM roles WHERE roles.name = users.role),"
6839  " users.id"
6840  " FROM users;");
6841 
6842  /* This leaves an extra column in older databases, which is not ideal, for
6843  * example when iterating over all columns. Migrating to Postgres, however,
6844  * removes the column. */
6845  sql ("UPDATE users SET role = NULL;");
6846 
6847  /* Set the database version to 81. */
6848 
6849  set_db_version (81);
6850 
6851  sql_commit ();
6852 
6853  return 0;
6854 }
6855 
6861 int
6863 {
6865 
6866  /* Ensure that the database is currently version 80. */
6867 
6868  if (manage_db_version () != 81)
6869  {
6870  sql_rollback ();
6871  return -1;
6872  }
6873 
6874  /* Changes are already done by init_manage */
6875 
6876  /* Set the database version to 82. */
6877 
6878  set_db_version (82);
6879 
6880  sql_commit ();
6881 
6882  return 0;
6883 }
6884 
6890 int
6892 {
6894 
6895  /* Ensure that the database is currently version 82. */
6896 
6897  if (manage_db_version () != 82)
6898  {
6899  sql_rollback ();
6900  return -1;
6901  }
6902 
6903  /* Update the database. */
6904 
6905  /* Remove risk_factor from NVTs table. */
6906 
6907  /* Move the table away. */
6908 
6909  sql ("ALTER TABLE nvts RENAME TO nvts_82;");
6910 
6911  /* Create the table in the new format. */
6912 
6913  sql ("CREATE TABLE IF NOT EXISTS nvts"
6914  " (id INTEGER PRIMARY KEY, uuid, oid, version, name, comment, summary,"
6915  " description, copyright, cve, bid, xref, tag, sign_key_ids,"
6916  " category INTEGER, family, cvss_base, creation_time,"
6917  " modification_time);");
6918 
6919  /* Copy the data into the new table. */
6920 
6921  sql ("INSERT into nvts"
6922  " (id, uuid, oid, version, name, comment, summary, description,"
6923  " copyright, cve, bid, xref, tag, sign_key_ids, category, family,"
6924  " cvss_base, creation_time, modification_time)"
6925  " SELECT"
6926  " id, uuid, oid, version, name, comment, summary, description,"
6927  " copyright, cve, bid, xref, tag, sign_key_ids, category, family,"
6928  " cvss_base, creation_time, modification_time"
6929  " FROM nvts_82;");
6930 
6931  /* Drop the old table. */
6932 
6933  sql ("DROP TABLE nvts_82;");
6934 
6935  /* Set the database version to 83. */
6936 
6937  set_db_version (83);
6938 
6939  sql_commit ();
6940 
6941  return 0;
6942 }
6943 
6949 int
6951 {
6953 
6954  /* Ensure that the database is currently version 83. */
6955 
6956  if (manage_db_version () != 83)
6957  {
6958  sql_rollback ();
6959  return -1;
6960  }
6961 
6962  /* Update the database. */
6963  /* Add columns "nvt_revision" and "severity" to results table */
6964  sql ("ALTER TABLE results ADD COLUMN nvt_version;");
6965  sql ("ALTER TABLE results ADD COLUMN severity REAL;");
6966 
6967  /* Set the database version to 84. */
6968 
6969  set_db_version (84);
6970 
6971  sql_commit ();
6972 
6973  return 0;
6974 }
6975 
6981 int
6983 {
6985 
6986  /* Ensure that the database is currently version 84. */
6987 
6988  if (manage_db_version () != 84)
6989  {
6990  sql_rollback ();
6991  return -1;
6992  }
6993 
6994  /* Update the database. */
6995  /* Add columns "severity" and "override_severity" to reports table */
6996  sql ("ALTER TABLE reports ADD COLUMN severity REAL;");
6997  sql ("ALTER TABLE reports ADD COLUMN override_severity REAL;");
6998 
6999  /* Clear counts cache so the severity columns are updated */
7000  sql ("UPDATE reports SET highs = -1;");
7001  sql ("UPDATE reports SET override_highs = -1;");
7002 
7003  /* Set the database version to 85. */
7004 
7005  set_db_version (85);
7006 
7007  sql_commit ();
7008 
7009  return 0;
7010 }
7011 
7017 int
7019 {
7021 
7022  /* Ensure that the database is currently version 85. */
7023 
7024  if (manage_db_version () != 85)
7025  {
7026  sql_rollback ();
7027  return -1;
7028  }
7029 
7030  /* Update the database. */
7031  /* Add column "new_severity" to overrides and overrides_trash */
7032  sql ("ALTER TABLE overrides ADD COLUMN new_severity REAL;");
7033  sql ("ALTER TABLE overrides_trash ADD COLUMN new_severity REAL;");
7034 
7035  /* Clear counts cache so the severity columns are updated */
7036  sql ("UPDATE reports SET override_highs = -1;");
7037 
7038  /* Set the database version to 86. */
7039 
7040  set_db_version (86);
7041 
7042  sql_commit ();
7043 
7044  return 0;
7045 }
7046 
7052 int
7054 {
7056 
7057  /* Ensure that the database is currently version 86. */
7058 
7059  if (manage_db_version () != 86)
7060  {
7061  sql_rollback ();
7062  return -1;
7063  }
7064 
7065  /* Update the database. */
7066 
7067  /* The scanner message types "Security Hole", "Security Warning" and
7068  * "Security Note" were merged into a single type, "Alarm".
7069  *
7070  * Update the severity of old high, medium and low results at the same
7071  * time, because the severity of these results can only be determined by
7072  * their message type. */
7073 
7074  sql ("UPDATE results"
7075  " SET severity = (CASE type"
7076  " WHEN 'Security Hole' THEN 10.0"
7077  " WHEN 'Security Warning' THEN 5.0"
7078  " WHEN 'Security Note' THEN 2.0"
7079  " WHEN 'Log Message' THEN 0.0"
7080  " ELSE NULL END)"
7081  " WHERE severity IS NULL;");
7082 
7083  sql ("UPDATE results SET type = 'Alarm'"
7084  " WHERE type = 'Security Hole'"
7085  " OR type = 'Security Warning'"
7086  " OR type = 'Security Note';");
7087 
7088  /* Set the database version to 87. */
7089 
7090  set_db_version (87);
7091 
7092  sql_commit ();
7093 
7094  return 0;
7095 }
7096 
7102 int
7104 {
7106 
7107  /* Ensure that the database is currently version 87. */
7108 
7109  if (manage_db_version () != 87)
7110  {
7111  sql_rollback ();
7112  return -1;
7113  }
7114 
7115  /* Update the database. */
7116 
7117  /* Rename reports table */
7118  sql ("ALTER TABLE reports RENAME TO reports_87;");
7119  /* Create a new one without severity and counts. */
7120  sql ("CREATE TABLE IF NOT EXISTS reports"
7121  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, hidden INTEGER,"
7122  " task INTEGER, date INTEGER, start_time, end_time, nbefile, comment,"
7123  " scan_run_status INTEGER, slave_progress, slave_task_uuid);");
7124  /* Create a new dedicated report_counts table. */
7125  sql ("CREATE TABLE IF NOT EXISTS report_counts"
7126  " (id INTEGER PRIMARY KEY, report INTEGER, user INTEGER,"
7127  " severity, override_severity, highs, mediums, lows, logs, fps,"
7128  " override_highs, override_mediums, override_lows, override_logs,"
7129  " override_fps);");
7130  /* Copy old report data to new reports table. */
7131  sql ("INSERT INTO reports"
7132  " (id, uuid, owner, hidden, task, date, start_time, end_time,"
7133  " nbefile, comment, scan_run_status, slave_progress, slave_task_uuid)"
7134  " SELECT id, uuid, owner, hidden, task, date, start_time, end_time,"
7135  " nbefile, comment, scan_run_status, slave_progress, slave_task_uuid"
7136  " FROM reports_87;");
7137  /* Delete old results table. */
7138  sql ("DROP TABLE reports_87;");
7139 
7140  /* Set the database version to 88. */
7141 
7142  set_db_version (88);
7143 
7144  sql_commit ();
7145 
7146  return 0;
7147 }
7148 
7154 int
7156 {
7158 
7159  /* Ensure that the database is currently version 87. */
7160 
7161  if (manage_db_version () != 88)
7162  {
7163  sql_rollback ();
7164  return -1;
7165  }
7166 
7167  /* Update the database. */
7168 
7169  /* Rename overrides tables */
7170  sql ("ALTER TABLE overrides RENAME TO overrides_88;");
7171  sql ("ALTER TABLE overrides_trash RENAME TO overrides_trash_88;");
7172 
7173  /* Create a new one without threat and new_threat. */
7174  sql ("CREATE TABLE IF NOT EXISTS overrides"
7175  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt,"
7176  " creation_time, modification_time, text, hosts, port, severity,"
7177  " new_severity, task INTEGER, result INTEGER, end_time);");
7178 
7179  sql ("CREATE TABLE IF NOT EXISTS overrides_trash"
7180  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt,"
7181  " creation_time, modification_time, text, hosts, port, severity,"
7182  " new_severity, task INTEGER, result INTEGER, end_time);");
7183 
7184  /* Migrate old override data to new tables. */
7185  sql ("INSERT INTO overrides"
7186  " (id, uuid, owner, nvt, creation_time, modification_time, text,"
7187  " hosts, port, severity, new_severity, task, result, end_time)"
7188  " SELECT id, uuid, owner, nvt, creation_time, modification_time, text,"
7189  " hosts, port,"
7190  " (CASE threat"
7191  " WHEN 'Security Hole' THEN 0.1"
7192  " WHEN 'Security Warning' THEN 0.1"
7193  " WHEN 'Security Note' THEN 0.1"
7194  " WHEN 'Alarm' THEN 0.1"
7195  " WHEN 'Log Message' THEN 0.0"
7196  " WHEN 'False Positive' THEN -1.0"
7197  " WHEN 'Debug Message' THEN -2.0"
7198  " WHEN 'Error Message' THEN -3.0"
7199  " ELSE NULL"
7200  " END),"
7201  " coalesce (new_severity,"
7202  " CASE new_threat"
7203  " WHEN 'Security Hole' THEN 10.0"
7204  " WHEN 'Security Warning' THEN 5.0"
7205  " WHEN 'Security Note' THEN 2.0"
7206  " WHEN 'Log Message' THEN 0.0"
7207  " WHEN 'False Positive' THEN -1.0"
7208  " WHEN 'Debug Message' THEN -2.0"
7209  " WHEN 'Error Message' THEN -3.0"
7210  " END),"
7211  " task, result, end_time"
7212  " FROM overrides_88;");
7213 
7214  sql ("INSERT INTO overrides_trash"
7215  " (id, uuid, owner, nvt, creation_time, modification_time, text,"
7216  " hosts, port, severity, new_severity, task, result, end_time)"
7217  " SELECT id, uuid, owner, nvt, creation_time, modification_time, text,"
7218  " hosts, port,"
7219  " (CASE threat"
7220  " WHEN 'Security Hole' THEN 0.1"
7221  " WHEN 'Security Warning' THEN 0.1"
7222  " WHEN 'Security Note' THEN 0.1"
7223  " WHEN 'Alarm' THEN 0.1"
7224  " WHEN 'Log Message' THEN 0.0"
7225  " WHEN 'False Positive' THEN -1.0"
7226  " WHEN 'Debug Message' THEN -2.0"
7227  " WHEN 'Error Message' THEN -3.0"
7228  " ELSE NULL"
7229  " END),"
7230  " coalesce (new_severity,"
7231  " CASE new_threat"
7232  " WHEN 'Security Hole' THEN 10.0"
7233  " WHEN 'Security Warning' THEN 5.0"
7234  " WHEN 'Security Note' THEN 2.0"
7235  " WHEN 'Log Message' THEN 0.0"
7236  " WHEN 'False Positive' THEN -1.0"
7237  " WHEN 'Debug Message' THEN -2.0"
7238  " WHEN 'Error Message' THEN -3.0"
7239  " END),"
7240  " task, result, end_time"
7241  " FROM overrides_trash_88;");
7242 
7243  /* Delete old overrides tables. */
7244  sql ("DROP TABLE overrides_88;");
7245  sql ("DROP TABLE overrides_trash_88;");
7246 
7247  /* Clear overridden result counts cache */
7248  sql ("UPDATE report_counts set override_highs = -1;");
7249 
7250  /* Set the database version to 89. */
7251 
7252  set_db_version (89);
7253 
7254  sql_commit ();
7255 
7256  return 0;
7257 }
7258 
7264 int
7266 {
7268 
7269  /* Ensure that the database is currently version 89. */
7270 
7271  if (manage_db_version () != 89)
7272  {
7273  sql_rollback ();
7274  return -1;
7275  }
7276 
7277  /* Update the database. */
7278 
7279  /* Groups, roles and users became owned by all admins. */
7280 
7281  sql ("UPDATE groups SET owner = NULL;");
7282  sql ("UPDATE roles SET owner = NULL;");
7283  sql ("UPDATE users SET owner = NULL;");
7284 
7285  /* Set the database version to 90. */
7286 
7287  set_db_version (90);
7288 
7289  sql_commit ();
7290 
7291  return 0;
7292 }
7293 
7299 int
7301 {
7303 
7304  /* Ensure that the database is currently version 90. */
7305 
7306  if (manage_db_version () != 90)
7307  {
7308  sql_rollback ();
7309  return -1;
7310  }
7311 
7312  /* Update the database. */
7313 
7314  /* Rename notes tables */
7315  sql ("ALTER TABLE notes RENAME TO notes_90;");
7316  sql ("ALTER TABLE notes_trash RENAME TO notes_trash_90;");
7317 
7318  /* Create new ones without threat and new_threat. */
7319  sql ("CREATE TABLE IF NOT EXISTS notes"
7320  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt,"
7321  " creation_time, modification_time, text, hosts, port, severity,"
7322  " task INTEGER, result INTEGER, end_time);");
7323  sql ("CREATE TABLE IF NOT EXISTS notes_trash"
7324  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt,"
7325  " creation_time, modification_time, text, hosts, port, severity,"
7326  " task INTEGER, result INTEGER, end_time);");
7327 
7328  /* Migrate old notes data to new tables. */
7329  sql ("INSERT INTO notes"
7330  " (id, uuid, owner , nvt, creation_time, modification_time, text,"
7331  " hosts, port, severity, task, result, end_time)"
7332  " SELECT id, uuid, owner, nvt, creation_time, modification_time, text,"
7333  " hosts, port,"
7334  " (CASE threat"
7335  " WHEN 'Security Hole' THEN 0.1"
7336  " WHEN 'Security Warning' THEN 0.1"
7337  " WHEN 'Security Note' THEN 0.1"
7338  " WHEN 'Alarm' THEN 0.1"
7339  " WHEN 'Log Message' THEN 0.0"
7340  " WHEN 'False Positive' THEN -1.0"
7341  " WHEN 'Debug Message' THEN -2.0"
7342  " WHEN 'Error Message' THEN -3.0"
7343  " ELSE NULL"
7344  " END),"
7345  " task, result, end_time"
7346  " FROM notes_90;");
7347 
7348  sql ("INSERT INTO notes_trash"
7349  " (id, uuid, owner , nvt, creation_time, modification_time, text,"
7350  " hosts, port, severity, task, result, end_time)"
7351  " SELECT id, uuid, owner, nvt, creation_time, modification_time, text,"
7352  " hosts, port,"
7353  " (CASE threat"
7354  " WHEN 'Security Hole' THEN 0.1"
7355  " WHEN 'Security Warning' THEN 0.1"
7356  " WHEN 'Security Note' THEN 0.1"
7357  " WHEN 'Alarm' THEN 0.1"
7358  " WHEN 'Log Message' THEN 0.0"
7359  " WHEN 'False Positive' THEN -1.0"
7360  " WHEN 'Debug Message' THEN -2.0"
7361  " WHEN 'Error Message' THEN -3.0"
7362  " ELSE NULL"
7363  " END),"
7364  " task, result, end_time"
7365  " FROM notes_trash_90;");
7366 
7367  /* Delete old overrides tables. */
7368  sql ("DROP TABLE notes_90;");
7369  sql ("DROP TABLE notes_trash_90;");
7370 
7371  /* Set the database version 91. */
7372 
7373  set_db_version (91);
7374 
7375  sql_commit ();
7376 
7377  return 0;
7378 
7379 }
7380 
7386 int
7388 {
7390 
7391  /* Ensure that the database is currently version 91. */
7392 
7393  if (manage_db_version () != 91)
7394  {
7395  sql_rollback ();
7396  return -1;
7397  }
7398 
7399  /* Update the database. */
7400 
7401  /* The default setting Reports Filter was renamed to Results Filter.
7402  * Report result filters are now of type "result". Type "report" filters
7403  * are for report filters. */
7404 
7405  sql ("INSERT INTO settings (uuid, owner, name, comment, value)"
7406  " SELECT '739ab810-163d-11e3-9af6-406186ea4fc5', owner,"
7407  " 'Results Filter', comment, value"
7408  " FROM settings"
7409  " WHERE name = 'Reports Filter';");
7410 
7411  sql ("DELETE FROM settings WHERE name = 'Reports Filter';");
7412 
7413  sql ("UPDATE filters SET type = 'result' WHERE type = 'report';");
7414 
7415  /* Set the database version 92. */
7416 
7417  set_db_version (92);
7418 
7419  sql_commit ();
7420 
7421  return 0;
7422 }
7423 
7429 int
7431 {
7433 
7434  /* Ensure that the database is currently version 92. */
7435 
7436  if (manage_db_version () != 92)
7437  {
7438  sql_rollback ();
7439  return -1;
7440  }
7441 
7442  /* Update the database. */
7443 
7444  /* The scanner preference host_expansion was removed. */
7445 
7446  sql ("DELETE FROM config_preferences WHERE name = 'host_expansion';");
7447  sql ("DELETE FROM config_preferences_trash WHERE name = 'host_expansion';");
7448 
7449  /* Set the database version 93. */
7450 
7451  set_db_version (93);
7452 
7453  sql_commit ();
7454 
7455  return 0;
7456 }
7457 
7463 int
7465 {
7467 
7468  /* Ensure that the database is currently version 93. */
7469 
7470  if (manage_db_version () != 93)
7471  {
7472  sql_rollback ();
7473  return -1;
7474  }
7475 
7476  /* Update the database. */
7477  /* Add column "exclude_hosts" to targets and targets_trash */
7478  sql ("ALTER TABLE targets ADD COLUMN exclude_hosts;");
7479  sql ("ALTER TABLE targets_trash ADD COLUMN exclude_hosts;");
7480 
7481  /* Set the database version to 94. */
7482 
7483  set_db_version (94);
7484 
7485  sql_commit ();
7486 
7487  return 0;
7488 }
7489 
7495 int
7497 {
7499 
7500  /* Ensure that the database is currently version 94. */
7501 
7502  if (manage_db_version () != 94)
7503  {
7504  sql_rollback ();
7505  return -1;
7506  }
7507 
7508  /* Update the database. */
7509  /* Drop and replace the report_counts table */
7510  sql ("DROP TABLE report_counts;");
7511  sql ("CREATE TABLE IF NOT EXISTS report_counts"
7512  " (id INTEGER PRIMARY KEY, report INTEGER, user INTEGER,"
7513  " severity, count, override);");
7514 
7515  /* Set the database version to 95. */
7516 
7517  set_db_version (95);
7518 
7519  sql_commit ();
7520 
7521  return 0;
7522 }
7523 
7529 int
7531 {
7533 
7534  /* Ensure that the database is currently version 95. */
7535 
7536  if (manage_db_version () != 95)
7537  {
7538  sql_rollback ();
7539  return -1;
7540  }
7541 
7542  /* Update the database. */
7543  /* Add column reverse lookup columns to targets and targets_trash */
7544  sql ("ALTER TABLE targets ADD COLUMN reverse_lookup_only;");
7545  sql ("ALTER TABLE targets ADD COLUMN reverse_lookup_unify;");
7546  sql ("UPDATE targets SET reverse_lookup_only = 0, reverse_lookup_unify = 0;");
7547 
7548  sql ("ALTER TABLE targets_trash ADD COLUMN reverse_lookup_only;");
7549  sql ("ALTER TABLE targets_trash ADD COLUMN reverse_lookup_unify;");
7550  sql ("UPDATE targets_trash SET reverse_lookup_only = 0, "
7551  " reverse_lookup_unify = 0;");
7552 
7553  /* Set the database version to 96. */
7554 
7555  set_db_version (96);
7556 
7557  sql_commit ();
7558 
7559  return 0;
7560 }
7561 
7567 int
7569 {
7571 
7572  /* Ensure that the database is currently version 96. */
7573 
7574  if (manage_db_version () != 96)
7575  {
7576  sql_rollback ();
7577  return -1;
7578  }
7579 
7580  /* Update the database. */
7581  /* Add column hosts_ordering to tasks */
7582  sql ("ALTER TABLE tasks ADD COLUMN hosts_ordering;");
7583  sql ("UPDATE tasks SET hosts_ordering = 'sequential';");
7584 
7585  /* Set the database version to 97. */
7586 
7587  set_db_version (97);
7588 
7589  sql_commit ();
7590 
7591  return 0;
7592 }
7593 
7599 int
7601 {
7603 
7604  /* Ensure that the database is currently version 97. */
7605 
7606  if (manage_db_version () != 97)
7607  {
7608  sql_rollback ();
7609  return -1;
7610  }
7611 
7612  /* Update the database. */
7613  /* Set default value for Dynamic Severity to 0 (disabled) */
7614  sql ("UPDATE settings SET value = 0"
7615  " WHERE name = 'Dynamic Severity'"
7616  " AND owner IS NULL;");
7617 
7618  /* Set the database version to 98. */
7619 
7620  set_db_version (98);
7621 
7622  sql_commit ();
7623 
7624  return 0;
7625 }
7626 
7632 int
7634 {
7636 
7637  /* Ensure that the database is currently version 98. */
7638 
7639  if (manage_db_version () != 98)
7640  {
7641  sql_rollback ();
7642  return -1;
7643  }
7644 
7645  /* Update the database. */
7646 
7647  /* Remove reverse_lookup and slice_network_addresses scanner preferences. */
7648 
7649  sql ("DELETE FROM config_preferences WHERE name = 'reverse_lookup';");
7650  sql ("DELETE FROM config_preferences"
7651  " WHERE name = 'slice_network_addresses';");
7652  sql ("DELETE FROM config_preferences_trash WHERE name = 'reverse_lookup';");
7653  sql ("DELETE FROM config_preferences_trash"
7654  " WHERE name = 'slice_network_addresses';");
7655 
7656  /* Set the database version to 99. */
7657 
7658  set_db_version (99);
7659 
7660  sql_commit ();
7661 
7662  return 0;
7663 }
7664 
7670 int
7672 {
7674 
7675  /* Ensure that the database is currently version 99. */
7676 
7677  if (manage_db_version () != 99)
7678  {
7679  sql_rollback ();
7680  return -1;
7681  }
7682 
7683  /* Update the database. */
7684 
7685  /* Rename results tables */
7686  sql ("ALTER TABLE results RENAME TO results_99;");
7687 
7688  /* Create new one without subnet. */
7689  sql ("CREATE TABLE IF NOT EXISTS results"
7690  " (id INTEGER PRIMARY KEY, uuid, task INTEGER, host, port, nvt,"
7691  " type, description, report, nvt_version, severity REAL)");
7692 
7693  /* Migrate old results data to new table. */
7694  sql ("INSERT INTO results"
7695  " (id, uuid, task, host, port, nvt, type,"
7696  " description, report, nvt_version, severity)"
7697  " SELECT id, uuid, task, host, port, nvt, type, description, report,"
7698  " nvt_version, severity FROM results_99");
7699 
7700  /* Delete old results table. */
7701  sql ("DROP TABLE results_99;");
7702 
7703  /* Set the database version to 100. */
7704 
7705  set_db_version (100);
7706 
7707  sql_commit ();
7708 
7709  return 0;
7710 }
7711 
7717 int
7719 {
7721 
7722  /* Ensure that the database is currently version 100. */
7723 
7724  if (manage_db_version () != 100)
7725  {
7726  sql_rollback ();
7727  return -1;
7728  }
7729 
7730  /* Update the database. */
7731 
7732  /* Migrate level alert condition data to severity */
7733  sql ("UPDATE alert_condition_data SET"
7734  " name = 'severity',"
7735  " data = CASE data"
7736  " WHEN 'High' THEN 5.1"
7737  " WHEN 'Medium' THEN 2.1"
7738  " WHEN 'Meduim' THEN 2.1" // Fix for typo in GSA
7739  " WHEN 'Low' THEN 0.1"
7740  " WHEN 'Log' THEN 0.0"
7741  " WHEN 'False Positive' THEN -1.0"
7742  " ELSE data END"
7743  " WHERE name = 'level';");
7744 
7745  sql ("UPDATE alert_condition_data_trash SET"
7746  " name = 'severity',"
7747  " data = CASE data"
7748  " WHEN 'High' THEN 5.1"
7749  " WHEN 'Medium' THEN 2.1"
7750  " WHEN 'Meduim' THEN 2.1" // Fix for typo in GSA
7751  " WHEN 'Low' THEN 0.1"
7752  " WHEN 'Log' THEN 0.0"
7753  " WHEN 'False Positive' THEN -1.0"
7754  " ELSE data END"
7755  " WHERE name = 'level';");
7756 
7757  /* Set the database version to 101. */
7758 
7759  set_db_version (101);
7760 
7761  sql_commit ();
7762 
7763  return 0;
7764 }
7765 
7771 int
7773 {
7775 
7776  /* Ensure that the database is currently version 101. */
7777 
7778  if (manage_db_version () != 101)
7779  {
7780  sql_rollback ();
7781  return -1;
7782  }
7783 
7784  /* Update the database. */
7785 
7786  /* Rename nvts tables. */
7787  sql ("ALTER TABLE nvts RENAME TO nvts_101;");
7788 
7789  /* Create new one without description column. */
7790  sql ("CREATE TABLE IF NOT EXISTS nvts"
7791  " (id INTEGER PRIMARY KEY, uuid, oid, version, name, comment, summary,"
7792  " copyright, cve, bid, xref, tag, sign_key_ids,"
7793  " category INTEGER, family, cvss_base, creation_time,"
7794  " modification_time);");
7795 
7796  /* Migrate old nvts data to new table. */
7797  sql ("INSERT INTO nvts"
7798  " (id, uuid, oid, version, name, comment, summary,"
7799  " copyright, cve, bid, xref, tag, sign_key_ids,"
7800  " category, family, cvss_base, creation_time, modification_time)"
7801  " SELECT id, uuid, oid, version, name, comment, summary,"
7802  " copyright, cve, bid, xref, tag, sign_key_ids,"
7803  " category, family, cvss_base, creation_time, modification_time"
7804  " FROM nvts_101;");
7805 
7806  /* Delete old nvts table. */
7807  sql ("DROP TABLE nvts_101;");
7808 
7809  /* Set the database version to 102. */
7810 
7811  set_db_version (102);
7812 
7813  sql_commit ();
7814 
7815  return 0;
7816 }
7817 
7823 int
7825 {
7827 
7828  /* Ensure that the database is currently version 102. */
7829 
7830  if (manage_db_version () != 102)
7831  {
7832  sql_rollback ();
7833  return -1;
7834  }
7835 
7836  /* Update the database. */
7837 
7838  /* Clear cache for affected reports */
7839  sql ("DELETE FROM report_counts WHERE report IN"
7840  " (SELECT report FROM results"
7841  " WHERE severity = 'NULL' OR severity = '' OR severity IS NULL);");
7842 
7843  /* Add missing severity values */
7844  sql ("UPDATE results SET"
7845  " severity = CASE type"
7846  " WHEN 'Error Message' THEN -3.0"
7847  " WHEN 'Debug Message' THEN -2.0"
7848  " WHEN 'False Positive' THEN -1.0"
7849  " WHEN 'Log Message' THEN 0.0"
7850  " ELSE NULL END"
7851  " WHERE severity = 'NULL' OR severity = '' OR severity IS NULL;");
7852 
7853  /* Set the database version to 103. */
7854 
7855  set_db_version (103);
7856 
7857  sql_commit ();
7858 
7859  return 0;
7860 }
7861 
7867 int
7869 {
7871 
7872  /* Ensure that the database is currently version 103. */
7873 
7874  if (manage_db_version () != 103)
7875  {
7876  sql_rollback ();
7877  return -1;
7878  }
7879 
7880  /* Update the database. */
7881 
7882  /* Tasks got an alterable flag. */
7883  sql ("ALTER TABLE tasks ADD column alterable;");
7884  sql ("UPDATE tasks SET alterable = 0;");
7885 
7886  /* Set the database version to 104. */
7887 
7888  set_db_version (104);
7889 
7890  sql_commit ();
7891 
7892  return 0;
7893 }
7894 
7900 int
7902 {
7904 
7905  /* Ensure that the database is currently version 104. */
7906 
7907  if (manage_db_version () != 104)
7908  {
7909  sql_rollback ();
7910  return -1;
7911  }
7912 
7913  /* Update the database. */
7914 
7915  /* Add expiration date column to reports cache. */
7916  sql ("ALTER TABLE report_counts ADD COLUMN end_time INTEGER;");
7917 
7918  /* Update cache to set expiration dates. */
7919  sql ("UPDATE report_counts"
7920  " SET end_time = (SELECT coalesce(min(end_time), 0)"
7921  " FROM overrides, results"
7922  " WHERE overrides.nvt = results.nvt"
7923  " AND results.report = report_counts.report"
7924  " AND overrides.end_time > 1)"
7925  " WHERE report_counts.override = 1;");
7926 
7927  sql ("UPDATE report_counts"
7928  " SET end_time = 0"
7929  " WHERE report_counts.override = 0;");
7930 
7931  /* Clear cache for reports with already expired overrides */
7932  sql ("DELETE FROM report_counts"
7933  " WHERE end_time != 0 AND end_time <= m_now ()");
7934 
7935  /* Set the database version to 105. */
7936 
7937  set_db_version (105);
7938 
7939  sql_commit ();
7940 
7941  return 0;
7942 }
7943 
7949 int
7951 {
7953 
7954  /* Ensure that the database is currently version 105. */
7955 
7956  if (manage_db_version () != 105)
7957  {
7958  sql_rollback ();
7959  return -1;
7960  }
7961 
7962  /* Update the database. */
7963 
7964  sql ("ALTER TABLE users ADD COLUMN ifaces;");
7965  sql ("ALTER TABLE users ADD COLUMN ifaces_allow;");
7966  sql ("UPDATE users SET ifaces = '', ifaces_allow = 2");
7967 
7968  /* Set the database version to 106. */
7969 
7970  set_db_version (106);
7971 
7972  sql_commit ();
7973 
7974  return 0;
7975 }
7976 
7982 int
7984 {
7986 
7987  /* Ensure that the database is currently version 106. */
7988 
7989  if (manage_db_version () != 106)
7990  {
7991  sql_rollback ();
7992  return -1;
7993  }
7994 
7995  /* Update the database. */
7996 
7997  /* Results in container tasks were being given a task of 0. */
7998  sql ("UPDATE results"
7999  " SET task = (SELECT task FROM reports WHERE reports.id = report);");
8000 
8001  /* Set the database version to 107. */
8002 
8003  set_db_version (107);
8004 
8005  sql_commit ();
8006 
8007  return 0;
8008 }
8009 
8015 int
8017 {
8019 
8020  /* Ensure that the database is currently version 107. */
8021 
8022  if (manage_db_version () != 107)
8023  {
8024  sql_rollback ();
8025  return -1;
8026  }
8027 
8028  /* Update the database. */
8029 
8030  /* Change hosts and interfaces Access "Allow All" to "Deny none". */
8031  sql ("UPDATE users"
8032  " SET hosts = '', hosts_allow = 0 WHERE hosts_allow = 2;");
8033  sql ("UPDATE users"
8034  " SET ifaces = '', ifaces_allow = 0 WHERE ifaces_allow = 2;");
8035 
8036  /* Set the database version to 108. */
8037 
8038  set_db_version (108);
8039 
8040  sql_commit ();
8041 
8042  return 0;
8043 }
8044 
8050 int
8052 {
8054 
8055  /* Ensure that the database is currently version 108. */
8056 
8057  if (manage_db_version () != 108)
8058  {
8059  sql_rollback ();
8060  return -1;
8061  }
8062 
8063  /* Update the database. */
8064 
8065  /* Permission names changed to full command names. */
8066 
8067  sql ("UPDATE permissions SET name = 'create_' || resource_type"
8068  " WHERE name = 'create';");
8069  sql ("DELETE FROM permissions WHERE name = 'create_';");
8070 
8071  sql ("UPDATE permissions SET name = 'delete_' || resource_type"
8072  " WHERE name = 'delete';");
8073  sql ("DELETE FROM permissions WHERE name = 'delete_';");
8074 
8075  sql ("UPDATE permissions SET name = 'get_' || resource_type || 's'"
8076  " WHERE name = 'get';");
8077  sql ("DELETE FROM permissions WHERE name = 'get_';");
8078 
8079  sql ("UPDATE permissions SET name = 'modify_' || resource_type"
8080  " WHERE name = 'modify';");
8081  sql ("DELETE FROM permissions WHERE name = 'modify_';");
8082 
8083  /* Set the database version to 109. */
8084 
8085  set_db_version (109);
8086 
8087  sql_commit ();
8088 
8089  return 0;
8090 }
8091 
8097 int
8099 {
8101 
8102  /* Ensure that the database is currently version 109. */
8103 
8104  if (manage_db_version () != 109)
8105  {
8106  sql_rollback ();
8107  return -1;
8108  }
8109 
8110  /* Update the database. */
8111 
8112  /* The permissions tables got subject_location fields. */
8113 
8114  sql ("ALTER TABLE permissions ADD COLUMN subject_location;");
8115  sql ("UPDATE permissions SET subject_location = 0;");
8116 
8117  sql ("ALTER TABLE permissions_trash ADD COLUMN subject_location;");
8118  sql ("UPDATE permissions_trash SET subject_location = 0;");
8119 
8120  /* Set the database version to 110. */
8121 
8122  set_db_version (110);
8123 
8124  sql_commit ();
8125 
8126  return 0;
8127 }
8128 
8134 int
8136 {
8138 
8139  /* Ensure that the database is currently version 110. */
8140 
8141  if (manage_db_version () != 110)
8142  {
8143  sql_rollback ();
8144  return -1;
8145  }
8146 
8147  /* Update the database. */
8148 
8149  /* The targets tables got an alive_test field. */
8150 
8151  sql ("ALTER TABLE targets ADD COLUMN alive_test;");
8152  sql ("UPDATE targets SET alive_test = 0;");
8153 
8154  sql ("ALTER TABLE targets_trash ADD COLUMN alive_test;");
8155  sql ("UPDATE targets_trash SET alive_test = 0;");
8156 
8157  /* Set the database version to 111. */
8158 
8159  set_db_version (111);
8160 
8161  sql_commit ();
8162 
8163  return 0;
8164 }
8165 
8171 int
8173 {
8175 
8176  /* Ensure that the database is currently version 111. */
8177 
8178  if (manage_db_version () != 111)
8179  {
8180  sql_rollback ();
8181  return -1;
8182  }
8183 
8184  /* Update the database. */
8185 
8186  /* Some prefs were removed from config Host Discovery so that the NVT
8187  * defaults will be used instead. */
8188 
8189  sql ("DELETE FROM config_preferences"
8190  " WHERE config = (SELECT id FROM configs"
8191  " WHERE uuid = '" CONFIG_UUID_HOST_DISCOVERY "')"
8192  " AND (name = 'Ping Host[checkbox]:Do a TCP ping'"
8193  " OR name = 'Ping Host[checkbox]:Do an ICMP ping'"
8194  " OR name = 'Ping Host[checkbox]:Use ARP'"
8195  " OR name = 'Ping Host[checkbox]:Use nmap'"
8196  " OR name = 'Ping Host[checkbox]:nmap: try also with only -sP'"
8197  " OR name = 'Ping Host[entry]:nmap additional ports for -PA');");
8198 
8199  /* Set the database version to 112. */
8200 
8201  set_db_version (112);
8202 
8203  sql_commit ();
8204 
8205  return 0;
8206 }
8207 
8213 int
8215 {
8217 
8218  /* Ensure that the database is currently version 112. */
8219 
8220  if (manage_db_version () != 112)
8221  {
8222  sql_rollback ();
8223  return -1;
8224  }
8225 
8226  /* Update the database. */
8227 
8228  /* Certain levels may have been missing from the result counts cache due
8229  * to floating point approximation. */
8230 
8231  sql ("DELETE FROM report_counts;");
8232 
8233  /* Set the database version to 113. */
8234 
8235  set_db_version (113);
8236 
8237  sql_commit ();
8238 
8239  return 0;
8240 }
8241 
8247 int
8249 {
8251 
8252  /* Ensure that the database is currently version 113. */
8253 
8254  if (manage_db_version () != 113)
8255  {
8256  sql_rollback ();
8257  return -1;
8258  }
8259 
8260  /* Update the database. */
8261 
8262  /* Reports got information from scan time. */
8263 
8264  sql ("ALTER TABLE reports ADD COLUMN slave_uuid;");
8265  sql ("ALTER TABLE reports ADD COLUMN slave_name;");
8266  sql ("ALTER TABLE reports ADD COLUMN slave_host;");
8267  sql ("ALTER TABLE reports ADD COLUMN slave_port;");
8268  sql ("ALTER TABLE reports ADD COLUMN source_iface;");
8269 
8270  /* Set the database version to 114. */
8271 
8272  set_db_version (114);
8273 
8274  sql_commit ();
8275 
8276  return 0;
8277 }
8278 
8284 int
8286 {
8288 
8289  /* Ensure that the database is currently version 114. */
8290 
8291  if (manage_db_version () != 114)
8292  {
8293  sql_rollback ();
8294  return -1;
8295  }
8296 
8297  /* Rename nvts tables. */
8298  sql ("ALTER TABLE nvts RENAME TO nvts_114;");
8299 
8300  /* Create new one without sign_key_ids column. */
8301  sql ("CREATE TABLE IF NOT EXISTS nvts"
8302  " (id INTEGER PRIMARY KEY, uuid, oid, version, name, comment, summary,"
8303  " copyright, cve, bid, xref, tag, category INTEGER, family, cvss_base,"
8304  " creation_time, modification_time);");
8305 
8306  /* Migrate old nvts data to new table. */
8307  sql ("INSERT INTO nvts"
8308  " (id, uuid, oid, version, name, comment, summary,"
8309  " copyright, cve, bid, xref, tag,"
8310  " category, family, cvss_base, creation_time, modification_time)"
8311  " SELECT id, uuid, oid, version, name, comment, summary,"
8312  " copyright, cve, bid, xref, tag,"
8313  " category, family, cvss_base, creation_time, modification_time"
8314  " FROM nvts_114;");
8315 
8316  /* Delete old nvts table. */
8317  sql ("DROP TABLE nvts_114;");
8318 
8319  /* Set the database version to 115. */
8320 
8321  set_db_version (115);
8322 
8323  sql_commit ();
8324 
8325  return 0;
8326 }
8327 
8333 int
8335 {
8337 
8338  /* Ensure that the database is currently version 115. */
8339 
8340  if (manage_db_version () != 115)
8341  {
8342  sql_rollback ();
8343  return -1;
8344  }
8345 
8346  /* Update the database. */
8347 
8348  /* NVT "CPE Inventory" was removed from config "Discovery". */
8349 
8350  sql ("DELETE FROM nvt_selectors"
8351  " WHERE name = '" MANAGE_NVT_SELECTOR_UUID_DISCOVERY "'"
8352  " AND type = " G_STRINGIFY (NVT_SELECTOR_TYPE_NVT)
8353  " AND family_or_nvt = '1.3.6.1.4.1.25623.1.0.810002'");
8354 
8355  /* Set the database version to 116. */
8356 
8357  set_db_version (116);
8358 
8359  sql_commit ();
8360 
8361  return 0;
8362 }
8363 
8364 #define ID_WHEN_WITH_TRASH(type) \
8365  " WHEN '" G_STRINGIFY (type) "' THEN" \
8366  " COALESCE ((SELECT id FROM " G_STRINGIFY (type) "s" \
8367  " WHERE uuid = attach_id)," \
8368  " (SELECT id FROM " G_STRINGIFY (type) "s_trash" \
8369  " WHERE uuid = attach_id)," \
8370  " 0)"
8371 
8372 #define ID_WHEN_WITHOUT_TRASH(type) \
8373  " WHEN '" G_STRINGIFY (type) "' THEN" \
8374  " COALESCE ((SELECT id FROM " G_STRINGIFY (type) "s" \
8375  " WHERE uuid = attach_id)," \
8376  " 0)"
8377 
8378 #define RESOURCE_TRASH(type) \
8379  " WHEN '" G_STRINGIFY (type) "' THEN" \
8380  " (SELECT CASE WHEN " \
8381  " (EXISTS (SELECT * FROM " G_STRINGIFY (type) "s_trash" \
8382  " WHERE uuid = attach_id))" \
8383  " THEN " G_STRINGIFY (LOCATION_TRASH) \
8384  " ELSE " G_STRINGIFY (LOCATION_TABLE) " END)"
8385 
8391 int
8393 {
8394  int scap_loaded = manage_scap_loaded ();
8395  int cert_loaded = manage_cert_loaded ();
8397 
8398  /* Ensure that the database is currently version 116. */
8399 
8400  if (manage_db_version () != 116)
8401  {
8402  sql_rollback ();
8403  return -1;
8404  }
8405 
8406  /* Update the database. */
8407 
8408  /* Rename attach_[...] columns in tags to resource_[...], reference
8409  * resources by id and add new column for resource UUID. */
8410 
8411  sql ("ALTER TABLE tags RENAME TO tags_117;");
8412  sql ("ALTER TABLE tags_trash RENAME TO tags_trash_117;");
8413 
8414  sql ("CREATE TABLE IF NOT EXISTS tags"
8415  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner, name, comment,"
8416  " creation_time, modification_time, resource_type, resource,"
8417  " resource_uuid, resource_location, active, value);");
8418 
8419  sql ("CREATE TABLE IF NOT EXISTS groups_trash"
8420  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
8421  " type, term, creation_time, modification_time);");
8422 
8423  sql ("CREATE TABLE IF NOT EXISTS roles_trash"
8424  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, comment,"
8425  " creation_time, modification_time);");
8426 
8427  sql ("INSERT INTO tags"
8428  " (id, uuid, owner, name, comment,"
8429  " creation_time, modification_time, resource_type, resource,"
8430  " resource_uuid, resource_location, active, value)"
8431  " SELECT"
8432  " id, uuid, owner, name, comment, creation_time, modification_time,"
8433  " attach_type,"
8434  " (SELECT CASE attach_type"
8435  ID_WHEN_WITH_TRASH (agent)
8436  ID_WHEN_WITH_TRASH (alert)
8437  "%s" // CPE and CVE
8438  ID_WHEN_WITH_TRASH (config)
8439  "%s" // DFN_CERT_ADV
8440  ID_WHEN_WITH_TRASH (filter)
8441  ID_WHEN_WITH_TRASH (group)
8442  ID_WHEN_WITH_TRASH (lsc_credential)
8443  ID_WHEN_WITH_TRASH (note)
8444  ID_WHEN_WITHOUT_TRASH (nvt)
8445  ID_WHEN_WITH_TRASH (override)
8446  "%s" // OVALDEF
8447  ID_WHEN_WITH_TRASH (permission)
8448  ID_WHEN_WITH_TRASH (port_list)
8449  ID_WHEN_WITH_TRASH (report_format)
8450  ID_WHEN_WITHOUT_TRASH (report)
8451  ID_WHEN_WITHOUT_TRASH (result)
8452  ID_WHEN_WITHOUT_TRASH (role)
8453  ID_WHEN_WITH_TRASH (schedule)
8454  ID_WHEN_WITH_TRASH (slave)
8455  ID_WHEN_WITH_TRASH (target)
8456  ID_WHEN_WITHOUT_TRASH (task) // uses attribute "hidden" for trash
8457  ID_WHEN_WITHOUT_TRASH (user)
8458  " ELSE 0 END),"
8459  " attach_id,"
8460  " (SELECT CASE attach_type"
8461  RESOURCE_TRASH (alert)
8462  RESOURCE_TRASH (config)
8463  RESOURCE_TRASH (filter)
8464  RESOURCE_TRASH (group)
8465  RESOURCE_TRASH (lsc_credential)
8466  RESOURCE_TRASH (note)
8467  RESOURCE_TRASH (override)
8468  RESOURCE_TRASH (permission)
8469  RESOURCE_TRASH (port_list)
8470  RESOURCE_TRASH (report_format)
8471  RESOURCE_TRASH (schedule)
8472  RESOURCE_TRASH (slave)
8473  RESOURCE_TRASH (target)
8474  " WHEN 'task' THEN"
8475  " COALESCE ((SELECT CASE WHEN hidden = 2 THEN "
8476  G_STRINGIFY (LOCATION_TRASH)
8477  " ELSE "
8478  G_STRINGIFY (LOCATION_TABLE)
8479  " END"
8480  " FROM tasks WHERE uuid = attach_id),"
8481  G_STRINGIFY (LOCATION_TABLE) ")"
8482  " WHEN 'report' THEN"
8483  " COALESCE ((SELECT CASE WHEN tasks.hidden = 2 THEN "
8484  G_STRINGIFY (LOCATION_TRASH)
8485  " ELSE "
8486  G_STRINGIFY (LOCATION_TABLE)
8487  " END"
8488  " FROM (SELECT task FROM reports"
8489  " WHERE reports.uuid = attach_id) AS report_task"
8490  " JOIN tasks ON tasks.id = report_task.task),"
8491  G_STRINGIFY (LOCATION_TABLE) ")"
8492  " WHEN 'result' THEN"
8493  " COALESCE ((SELECT CASE WHEN tasks.hidden = 2 THEN "
8494  G_STRINGIFY (LOCATION_TRASH)
8495  " ELSE "
8496  G_STRINGIFY (LOCATION_TABLE)
8497  " END"
8498  " FROM (SELECT task FROM results"
8499  " WHERE results.uuid = attach_id) AS result_task"
8500  " JOIN tasks ON tasks.id = result_task.task),"
8501  G_STRINGIFY (LOCATION_TABLE) ")"
8502  " ELSE " G_STRINGIFY (LOCATION_TABLE) " END),"
8503  " active, value"
8504  " FROM tags_117;",
8505  scap_loaded ? ID_WHEN_WITHOUT_TRASH (cpe)
8506  ID_WHEN_WITHOUT_TRASH (cve)
8507  : "",
8508  cert_loaded ? ID_WHEN_WITHOUT_TRASH (dfn_cert_adv)
8509  : "",
8510  scap_loaded ? ID_WHEN_WITHOUT_TRASH (ovaldef)
8511  : "");
8512 
8513  sql ("DROP TABLE tags_117;");
8514 
8515  /* Rename attach_[...] columns in tags_trash to resource_[...], reference
8516  * resources by id and add new column for resource UUID. */
8517  sql ("CREATE TABLE IF NOT EXISTS tags_trash"
8518  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner, name, comment,"
8519  " creation_time, modification_time, resource_type, resource,"
8520  " resource_uuid, resource_location, active, value);");
8521 
8522  sql ("INSERT INTO tags_trash"
8523  " (id, uuid, owner, name, comment,"
8524  " creation_time, modification_time, resource_type, resource,"
8525  " resource_uuid, resource_location, active, value)"
8526  " SELECT"
8527  " id, uuid, owner, name, comment, creation_time, modification_time,"
8528  " attach_type,"
8529  " (SELECT CASE attach_type"
8530  ID_WHEN_WITH_TRASH (agent)
8531  ID_WHEN_WITH_TRASH (alert)
8532  "%s" // CPE and CVE
8533  ID_WHEN_WITH_TRASH (config)
8534  "%s" // DFN_CERT_ADV
8535  ID_WHEN_WITH_TRASH (filter)
8536  ID_WHEN_WITH_TRASH (group)
8537  ID_WHEN_WITH_TRASH (lsc_credential)
8538  ID_WHEN_WITH_TRASH (note)
8539  ID_WHEN_WITHOUT_TRASH (nvt)
8540  ID_WHEN_WITH_TRASH (override)
8541  "%s" // OVALDEF
8542  ID_WHEN_WITH_TRASH (permission)
8543  ID_WHEN_WITH_TRASH (port_list)
8544  ID_WHEN_WITH_TRASH (report_format)
8545  ID_WHEN_WITHOUT_TRASH (report)
8546  ID_WHEN_WITHOUT_TRASH (result)
8547  ID_WHEN_WITHOUT_TRASH (role)
8548  ID_WHEN_WITH_TRASH (schedule)
8549  ID_WHEN_WITH_TRASH (slave)
8550  ID_WHEN_WITH_TRASH (target)
8551  ID_WHEN_WITHOUT_TRASH (task) // uses attribute "hidden" for trash
8552  ID_WHEN_WITHOUT_TRASH (user)
8553  " ELSE 0 END),"
8554  " attach_id,"
8555  " (SELECT CASE attach_type"
8556  RESOURCE_TRASH (alert)
8557  RESOURCE_TRASH (config)
8558  RESOURCE_TRASH (filter)
8559  RESOURCE_TRASH (group)
8560  RESOURCE_TRASH (lsc_credential)
8561  RESOURCE_TRASH (note)
8562  RESOURCE_TRASH (override)
8563  RESOURCE_TRASH (permission)
8564  RESOURCE_TRASH (port_list)
8565  RESOURCE_TRASH (report_format)
8566  RESOURCE_TRASH (schedule)
8567  RESOURCE_TRASH (slave)
8568  RESOURCE_TRASH (target)
8569  " WHEN 'task' THEN"
8570  " COALESCE ((SELECT CASE WHEN hidden = 2 THEN "
8571  G_STRINGIFY (LOCATION_TRASH)
8572  " ELSE "
8573  G_STRINGIFY (LOCATION_TABLE)
8574  " END"
8575  " FROM tasks WHERE uuid = attach_id),"
8576  G_STRINGIFY (LOCATION_TABLE) ")"
8577  " WHEN 'report' THEN"
8578  " COALESCE ((SELECT CASE WHEN tasks.hidden = 2 THEN "
8579  G_STRINGIFY (LOCATION_TRASH)
8580  " ELSE "
8581  G_STRINGIFY (LOCATION_TABLE)
8582  " END"
8583  " FROM (SELECT task FROM reports"
8584  " WHERE reports.uuid = attach_id) AS report_task"
8585  " JOIN tasks ON tasks.id = report_task.task),"
8586  G_STRINGIFY (LOCATION_TABLE) ")"
8587  " WHEN 'result' THEN"
8588  " COALESCE ((SELECT CASE WHEN tasks.hidden = 2 THEN "
8589  G_STRINGIFY (LOCATION_TRASH)
8590  " ELSE "
8591  G_STRINGIFY (LOCATION_TABLE)
8592  " END"
8593  " FROM (SELECT task FROM results"
8594  " WHERE results.uuid = attach_id) AS result_task"
8595  " JOIN tasks ON tasks.id = result_task.task),"
8596  G_STRINGIFY (LOCATION_TABLE) ")"
8597  " ELSE " G_STRINGIFY (LOCATION_TABLE) " END),"
8598  " active, value"
8599  " FROM tags_trash_117;",
8600  scap_loaded ? ID_WHEN_WITHOUT_TRASH (cpe)
8601  ID_WHEN_WITHOUT_TRASH (cve)
8602  : "",
8603  cert_loaded ? ID_WHEN_WITHOUT_TRASH (dfn_cert_adv)
8604  : "",
8605  scap_loaded ? ID_WHEN_WITHOUT_TRASH (ovaldef)
8606  : "");
8607 
8608  sql ("DROP TABLE tags_trash_117;");
8609 
8610  /* Set the database version to 117. */
8611 
8612  set_db_version (117);
8613 
8614  sql_commit ();
8615 
8616  return 0;
8617 }
8618 #undef ID_WHEN_WITH_TRASH
8619 #undef ID_WHEN_WITHOUT_TRASH
8620 #undef RESOURCE_TRASH
8621 
8622 
8623 #define RESOURCE_TRASH(type) \
8624  " WHEN '" G_STRINGIFY (type) "' THEN" \
8625  " (SELECT CASE WHEN " \
8626  " (EXISTS (SELECT * FROM " G_STRINGIFY (type) "s_trash" \
8627  " WHERE uuid = resource_uuid))" \
8628  " THEN " G_STRINGIFY (LOCATION_TRASH) \
8629  " ELSE " G_STRINGIFY (LOCATION_TABLE) " END)"
8630 
8636 int
8638 {
8640 
8641  /* Ensure that the database is currently version 117. */
8642 
8643  if (manage_db_version () != 117)
8644  {
8645  sql_rollback ();
8646  return -1;
8647  }
8648 
8649  /* Update the database. */
8650 
8651  /* Rebuild the resource_location column in tags and tags_trash */
8652  sql ("UPDATE tags SET resource_location = "
8653  " (SELECT CASE resource_type"
8654  RESOURCE_TRASH (alert)
8655  RESOURCE_TRASH (config)
8656  RESOURCE_TRASH (filter)
8657  RESOURCE_TRASH (group)
8658  RESOURCE_TRASH (lsc_credential)
8659  RESOURCE_TRASH (note)
8660  RESOURCE_TRASH (override)
8661  RESOURCE_TRASH (permission)
8662  RESOURCE_TRASH (port_list)
8663  RESOURCE_TRASH (report_format)
8664  RESOURCE_TRASH (schedule)
8665  RESOURCE_TRASH (slave)
8666  RESOURCE_TRASH (target)
8667  " WHEN 'task' THEN"
8668  " COALESCE ((SELECT CASE WHEN hidden = 2 THEN "
8669  G_STRINGIFY (LOCATION_TRASH)
8670  " ELSE "
8671  G_STRINGIFY (LOCATION_TABLE)
8672  " END"
8673  " FROM tasks WHERE uuid = resource_uuid),"
8674  G_STRINGIFY (LOCATION_TABLE) ")"
8675  " WHEN 'report' THEN"
8676  " COALESCE ((SELECT CASE WHEN tasks.hidden = 2 THEN "
8677  G_STRINGIFY (LOCATION_TRASH)
8678  " ELSE "
8679  G_STRINGIFY (LOCATION_TABLE)
8680  " END"
8681  " FROM (SELECT task FROM reports"
8682  " WHERE reports.uuid = resource_uuid) AS report_task"
8683  " JOIN tasks ON tasks.id = report_task.task),"
8684  G_STRINGIFY (LOCATION_TABLE) ")"
8685  " WHEN 'result' THEN"
8686  " COALESCE ((SELECT CASE WHEN tasks.hidden = 2 THEN "
8687  G_STRINGIFY (LOCATION_TRASH)
8688  " ELSE "
8689  G_STRINGIFY (LOCATION_TABLE)
8690  " END"
8691  " FROM (SELECT task FROM results"
8692  " WHERE results.uuid = resource_uuid) AS result_task"
8693  " JOIN tasks ON tasks.id = result_task.task),"
8694  G_STRINGIFY (LOCATION_TABLE) ")"
8695  " ELSE " G_STRINGIFY (LOCATION_TABLE) " END);");
8696 
8697  sql ("UPDATE tags_trash SET resource_location = "
8698  " (SELECT CASE resource_type"
8699  RESOURCE_TRASH (alert)
8700  RESOURCE_TRASH (config)
8701  RESOURCE_TRASH (filter)
8702  RESOURCE_TRASH (group)
8703  RESOURCE_TRASH (lsc_credential)
8704  RESOURCE_TRASH (note)
8705  RESOURCE_TRASH (override)
8706  RESOURCE_TRASH (permission)
8707  RESOURCE_TRASH (port_list)
8708  RESOURCE_TRASH (report_format)
8709  RESOURCE_TRASH (schedule)
8710  RESOURCE_TRASH (slave)
8711  RESOURCE_TRASH (target)
8712  " WHEN 'task' THEN"
8713  " COALESCE ((SELECT CASE WHEN hidden = 2 THEN "
8714  G_STRINGIFY (LOCATION_TRASH)
8715  " ELSE "
8716  G_STRINGIFY (LOCATION_TABLE)
8717  " END"
8718  " FROM tasks WHERE uuid = resource_uuid),"
8719  G_STRINGIFY (LOCATION_TABLE) ")"
8720  " WHEN 'report' THEN"
8721  " COALESCE ((SELECT CASE WHEN tasks.hidden = 2 THEN "
8722  G_STRINGIFY (LOCATION_TRASH)
8723  " ELSE "
8724  G_STRINGIFY (LOCATION_TABLE)
8725  " END"
8726  " FROM (SELECT task FROM reports"
8727  " WHERE reports.uuid = resource_uuid) AS report_task"
8728  " JOIN tasks ON tasks.id = report_task.task),"
8729  G_STRINGIFY (LOCATION_TABLE) ")"
8730  " WHEN 'result' THEN"
8731  " COALESCE ((SELECT CASE WHEN tasks.hidden = 2 THEN "
8732  G_STRINGIFY (LOCATION_TRASH)
8733  " ELSE "
8734  G_STRINGIFY (LOCATION_TABLE)
8735  " END"
8736  " FROM (SELECT task FROM results"
8737  " WHERE results.uuid = resource_uuid) AS result_task"
8738  " JOIN tasks ON tasks.id = result_task.task),"
8739  G_STRINGIFY (LOCATION_TABLE) ")"
8740  " ELSE " G_STRINGIFY (LOCATION_TABLE) " END);");
8741 
8742  /* Set the database version to 118. */
8743 
8744  set_db_version (118);
8745 
8746  sql_commit ();
8747 
8748  return 0;
8749 }
8750 #undef RESOURCE_TRASH
8751 
8757 int
8759 {
8761 
8762  /* Ensure that the database is currently version 118. */
8763 
8764  if (manage_db_version () != 118)
8765  {
8766  sql_rollback ();
8767  return -1;
8768  }
8769 
8770  /* Update the database. */
8771 
8772  /* Cleaning up of orphaned results was removed from startup. */
8773 
8774  sql ("DELETE FROM results"
8775  " WHERE NOT EXISTS (SELECT * FROM report_results"
8776  " WHERE report_results.result = results.id);");
8777  if (sql_changes () > 0)
8778  {
8779  g_debug ("%s: Removed %d orphaned result(s).",
8780  __FUNCTION__, sql_changes ());
8781  sql ("DELETE FROM report_counts WHERE override = 0;");
8782  sql ("DELETE FROM report_counts WHERE override = 1;");
8783  }
8784 
8785  /* Set the database version to 119. */
8786 
8787  set_db_version (119);
8788 
8789  sql_commit ();
8790 
8791  return 0;
8792 }
8793 
8799 int
8801 {
8803 
8804  /* Ensure that the database is currently version 119. */
8805 
8806  if (manage_db_version () != 119)
8807  {
8808  sql_rollback ();
8809  return -1;
8810  }
8811 
8812  /* Update the database. */
8813 
8814  /* An omission in manage_empty_trashcan was leaving permissions referring to
8815  * removed resources. */
8816 
8817  sql ("DELETE FROM permissions"
8818  " WHERE resource_location = " G_STRINGIFY (LOCATION_TRASH)
8819  " AND resource > 0"
8820  " AND resource_exists (resource_type, resource, resource_location) == 0;");
8821 
8822  sql ("DELETE FROM permissions"
8823  " WHERE subject_location = " G_STRINGIFY (LOCATION_TRASH)
8824  " AND subject > 0"
8825  " AND resource_exists (subject_type, subject, subject_location) == 0;");
8826 
8827  /* Set the database version to 120. */
8828 
8829  set_db_version (120);
8830 
8831  sql_commit ();
8832 
8833  return 0;
8834 }
8835 
8841 int
8843 {
8845 
8846  /* Ensure that the database is currently version 120. */
8847 
8848  if (manage_db_version () != 120)
8849  {
8850  sql_rollback ();
8851  return -1;
8852  }
8853 
8854  /* Update the database. */
8855 
8856  /* Observer role was missing the AUTHENTICATE permission. Simply delete all
8857  * its permissions and they will be recreated (along with AUTHENTICATE
8858  * permission) on start-up. */
8859  sql ("DELETE FROM permissions WHERE subject_type = 'role'"
8860  " AND subject = (SELECT id FROM roles"
8861  " WHERE uuid = '" ROLE_UUID_OBSERVER "');");
8862 
8863  /* Set the database version to 121. */
8864 
8865  set_db_version (121);
8866 
8867  sql_commit ();
8868 
8869  return 0;
8870 }
8871 
8877 int
8879 {
8881 
8882  /* Ensure that the database is currently version 121. */
8883 
8884  if (manage_db_version () != 121)
8885  {
8886  sql_rollback ();
8887  return -1;
8888  }
8889 
8890  /* Update the database. */
8891 
8892  /* HELP now has a permission check, so delete User and Info roles' permissions
8893  * and they will be recreated (along with HELP permission) on start-up. */
8894  sql ("DELETE FROM permissions"
8895  " WHERE subject_type = 'role' AND subject IN"
8896  " (SELECT id FROM roles WHERE uuid = '" ROLE_UUID_USER "'"
8897  " OR uuid = '" ROLE_UUID_INFO"');");
8898 
8899  /* Set the database version to 122. */
8900 
8901  set_db_version (122);
8902 
8903  sql_commit ();
8904 
8905  return 0;
8906 }
8907 
8913 int
8915 {
8916  int column_found = 0;
8917  iterator_t column_data;
8918 
8920 
8921  /* Ensure that the database is currently version 122. */
8922 
8923  if (manage_db_version () != 122)
8924  {
8925  sql_rollback ();
8926  return -1;
8927  }
8928 
8929  /* Update the database. */
8930 
8931  /* Check if targets_trash has alive_test column, which was added in the
8932  * migration to version 111 but previously missing in create_tables. */
8933  init_iterator (&column_data, "PRAGMA table_info (targets_trash);");
8934  while (next (&column_data) && column_found == 0)
8935  {
8936  const char* column_name;
8937 
8938  column_name = iterator_string (&column_data, 1);
8939  column_found = (strcmp (column_name, "alive_test") == 0);
8940  }
8941  cleanup_iterator (&column_data);
8942 
8943  if (column_found == 0)
8944  {
8945  sql ("ALTER TABLE targets_trash ADD COLUMN alive_test;");
8946  sql ("UPDATE targets_trash SET alive_test = 0;");
8947  }
8948 
8949  /* Set the database version to 123. */
8950 
8951  set_db_version (123);
8952 
8953  sql_commit ();
8954 
8955  return 0;
8956 }
8957 
8963 int
8965 {
8967 
8968  /* Ensure that the database is currently version 123. */
8969 
8970  if (manage_db_version () != 123)
8971  {
8972  sql_rollback ();
8973  return -1;
8974  }
8975 
8976  /* Update the database. */
8977 
8978  /* Rename lsc_credentials tables. */
8979  sql ("ALTER TABLE lsc_credentials RENAME TO lsc_credentials_123;");
8980  sql ("ALTER TABLE lsc_credentials_trash RENAME TO lsc_credentials_trash_123;");
8981 
8982  /* Create new ones without public_key. */
8983  sql ("CREATE TABLE IF NOT EXISTS lsc_credentials"
8984  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name,"
8985  " login, password, comment, private_key, rpm, deb, exe,"
8986  " creation_time, modification_time);");
8987  sql ("CREATE TABLE IF NOT EXISTS lsc_credentials_trash"
8988  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name,"
8989  " login, password, comment, private_key, rpm, deb, exe,"
8990  " creation_time, modification_time);");
8991 
8992  /* Migrate old data to new tables. */
8993  sql ("INSERT INTO lsc_credentials"
8994  " (id, uuid, owner , name, login, password, comment, private_key, rpm,"
8995  " deb, exe, creation_time, modification_time)"
8996  " SELECT id, uuid, owner, name, login, password, comment, private_key,"
8997  " rpm, deb, exe, creation_time, modification_time"
8998  " FROM lsc_credentials_123;");
8999 
9000  sql ("INSERT INTO lsc_credentials_trash"
9001  " (id, uuid, owner , name, login, password, comment, private_key, rpm,"
9002  " deb, exe, creation_time, modification_time)"
9003  " SELECT id, uuid, owner, name, login, password, comment, private_key,"
9004  " rpm, deb, exe, creation_time, modification_time"
9005  " FROM lsc_credentials_123;");
9006 
9007  /* Delete old tables. */
9008  sql ("DROP TABLE lsc_credentials_123;");
9009  sql ("DROP TABLE lsc_credentials_trash_123;");
9010 
9011  /* Set the database version 124. */
9012 
9013  set_db_version (124);
9014 
9015  sql_commit ();
9016 
9017  return 0;
9018 }
9019 
9025 int
9027 {
9029 
9030  /* Ensure that the database is currently version 124. */
9031 
9032  if (manage_db_version () != 124)
9033  {
9034  sql_rollback ();
9035  return -1;
9036  }
9037 
9038  /* Update the database. */
9039 
9040  /* Add tasks scanner and configs type. */
9041  sql ("ALTER TABLE tasks ADD COLUMN scanner;");
9042  sql ("ALTER TABLE configs ADD COLUMN type;");
9043  sql ("ALTER TABLE configs_trash ADD COLUMN type;");
9044  sql ("UPDATE tasks SET scanner = 0;");
9045  sql ("UPDATE configs SET type = 0;");
9046  sql ("UPDATE configs_trash SET type = 0;");
9047 
9048  /* Set the database version 125. */
9049 
9050  set_db_version (125);
9051 
9052  sql_commit ();
9053 
9054  return 0;
9055 }
9056 
9062 int
9064 {
9066 
9067  /* Ensure that the database is currently version 125. */
9068 
9069  if (manage_db_version () != 125)
9070  {
9071  sql_rollback ();
9072  return -1;
9073  }
9074 
9075  /* Update the database. */
9076 
9077  /* The description column was removed from table tasks. */
9078 
9079  /* Move the table away. */
9080 
9081  sql ("ALTER TABLE tasks RENAME TO tasks_125;");
9082 
9083  /* Create the table in the new format. */
9084 
9085  sql ("CREATE TABLE IF NOT EXISTS tasks"
9086  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, hidden INTEGER,"
9087  " time, comment, run_status INTEGER, start_time, end_time,"
9088  " config INTEGER, target INTEGER, schedule INTEGER, schedule_next_time,"
9089  " slave INTEGER, config_location INTEGER, target_location INTEGER,"
9090  " schedule_location INTEGER, slave_location INTEGER,"
9091  " upload_result_count INTEGER, hosts_ordering, scanner, alterable,"
9092  " creation_time, modification_time);");
9093 
9094  /* Copy the data into the new table. */
9095 
9096  sql ("INSERT into tasks"
9097  " (id, uuid, owner, name, hidden,"
9098  " time, comment, run_status, start_time, end_time,"
9099  " config, target, schedule, schedule_next_time,"
9100  " slave, config_location, target_location,"
9101  " schedule_location, slave_location,"
9102  " upload_result_count, hosts_ordering, scanner, alterable,"
9103  " creation_time, modification_time)"
9104  " SELECT"
9105  " id, uuid, owner, name, hidden,"
9106  " time, comment, run_status, start_time, end_time,"
9107  " config, target, schedule, schedule_next_time,"
9108  " slave, config_location, target_location,"
9109  " schedule_location, slave_location,"
9110  " upload_result_count, hosts_ordering, scanner, alterable,"
9111  " creation_time, modification_time"
9112  " FROM tasks_125;");
9113 
9114  /* Drop the old table. */
9115 
9116  sql ("DROP TABLE tasks_125;");
9117 
9118  /* Set the database version to 126. */
9119 
9120  set_db_version (126);
9121 
9122  sql_commit ();
9123 
9124  return 0;
9125 }
9126 
9132 int
9134 {
9136 
9137  /* Ensure that the database is currently version 126. */
9138 
9139  if (manage_db_version () != 126)
9140  {
9141  sql_rollback ();
9142  return -1;
9143  }
9144 
9145  /* Update the database. */
9146 
9147  /* An error in copy_task gave some permissions wrong resource_uuid values. */
9148 
9149  /* Copy the data into the new table. */
9150 
9151  sql ("UPDATE permissions"
9152  " SET resource_uuid = (SELECT uuid FROM tasks WHERE tasks.id = resource)"
9153  " WHERE resource_type = 'task'"
9154  " AND resource != 0;");
9155 
9156  /* Set the database version to 127. */
9157 
9158  set_db_version (127);
9159 
9160  sql_commit ();
9161 
9162  return 0;
9163 }
9164 
9170 int
9172 {
9174 
9175  /* Ensure that the database is currently version 127. */
9176 
9177  if (manage_db_version () != 127)
9178  {
9179  sql_rollback ();
9180  return -1;
9181  }
9182 
9183  /* Update the database. */
9184 
9185  /* Results got a Quality of Detection column. */
9186 
9187  sql ("ALTER TABLE results ADD COLUMN qod INTEGER;");
9188  sql ("UPDATE results SET qod = -1;");
9189 
9190  /* Set the database version to 128. */
9191 
9192  set_db_version (128);
9193 
9194  sql_commit ();
9195 
9196  return 0;
9197 }
9198 
9204 int
9206 {
9208 
9209  /* Ensure that the database is currently version 128. */
9210 
9211  if (manage_db_version () != 128)
9212  {
9213  sql_rollback ();
9214  return -1;
9215  }
9216 
9217  /* Update the database. */
9218 
9219  /* Ensure new tables exists. */
9220  sql ("CREATE TABLE IF NOT EXISTS scanners"
9221  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment,"
9222  " host, port, type, creation_time, modification_time);");
9223  sql ("CREATE TABLE IF NOT EXISTS scanners_trash"
9224  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment,"
9225  " host, port, type, creation_time, modification_time);");
9226 
9227 
9228  /* Insert the default OpenVAS Scanner, if not present. */
9229  if (sql_int ("SELECT count(*) FROM scanners WHERE uuid = '%s';",
9230  SCANNER_UUID_DEFAULT) == 0)
9231  sql ("INSERT INTO scanners"
9232  " (uuid, owner, name, host, port, type,"
9233  " creation_time, modification_time)"
9234  " VALUES ('" SCANNER_UUID_DEFAULT "', NULL, 'OpenVAS Default',"
9235  " 'localhost', 9391, %d, m_now (), m_now ());", SCANNER_TYPE_OPENVAS);
9236 
9237  /* Tasks with no Scanner should use the default one. */
9238  sql ("UPDATE tasks SET scanner ="
9239  " (SELECT id FROM scanners WHERE uuid = '" SCANNER_UUID_DEFAULT "')"
9240  " WHERE scanner = 0 OR scanner IS NULL;");
9241 
9242  /* Set the database version to 129. */
9243 
9244  set_db_version (129);
9245 
9246  sql_commit ();
9247 
9248  return 0;
9249 }
9250 
9256 int
9258 {
9259  char *ca_pub, *key_pub, *key_priv;
9260  char *quoted_ca_pub, *quoted_key_pub, *quoted_key_priv;
9261  GError *error = NULL;
9262 
9264 
9265  /* Ensure that the database is currently version 129. */
9266 
9267  if (manage_db_version () != 129)
9268  {
9269  sql_rollback ();
9270  return -1;
9271  }
9272 
9273  /* Update the database. */
9274 
9275  /* Add columns for per-scanner certificates. */
9276 
9277  sql ("ALTER TABLE scanners ADD COLUMN ca_pub;");
9278  sql ("ALTER TABLE scanners ADD COLUMN key_pub;");
9279  sql ("ALTER TABLE scanners ADD COLUMN key_priv;");
9280  sql ("ALTER TABLE scanners_trash ADD COLUMN ca_pub;");
9281  sql ("ALTER TABLE scanners_trash ADD COLUMN key_pub;");
9282  sql ("ALTER TABLE scanners_trash ADD COLUMN key_priv;");
9283 
9284  /* Fetch default certificates content. */
9285  if (!g_file_get_contents (CACERT, &ca_pub, NULL, &error))
9286  {
9287  g_warning ("%s: %s\n", __FUNCTION__, error->message);
9288  g_error_free (error);
9289  return -1;
9290  }
9291  if (!g_file_get_contents (CLIENTCERT, &key_pub, NULL, &error))
9292  {
9293  g_warning ("%s: %s\n", __FUNCTION__, error->message);
9294  g_error_free (error);
9295  g_free (ca_pub);
9296  return -1;
9297  }
9298  if (!g_file_get_contents (CLIENTKEY, &key_priv, NULL, &error))
9299  {
9300  g_warning ("%s: %s\n", __FUNCTION__, error->message);
9301  g_error_free (error);
9302  g_free (ca_pub);
9303  g_free (key_pub);
9304  return -1;
9305  }
9306 
9307  /* Update current scanners to store default certificates in DB. */
9308  quoted_ca_pub = sql_quote (ca_pub);
9309  quoted_key_pub = sql_quote (key_pub);
9310  quoted_key_priv = sql_quote (key_priv);
9311  g_free (ca_pub);
9312  g_free (key_pub);
9313  g_free (key_priv);
9314  sql ("UPDATE scanners SET ca_pub = '%s', key_pub = '%s', key_priv = '%s';",
9315  quoted_ca_pub, quoted_key_pub, quoted_key_priv);
9316  sql ("UPDATE scanners_trash SET ca_pub = '%s', key_pub = '%s',"
9317  " key_priv = '%s';",
9318  quoted_ca_pub, quoted_key_pub, quoted_key_priv);
9319  g_free (quoted_ca_pub);
9320  g_free (quoted_key_pub);
9321  g_free (quoted_key_priv);
9322 
9323  /* Set the database version to 130. */
9324  set_db_version (130);
9325 
9326  sql_commit ();
9327 
9328  return 0;
9329 }
9330 
9336 int
9338 {
9340 
9341  /* Ensure that the database is currently version 130. */
9342 
9343  if (manage_db_version () != 130)
9344  {
9345  sql_rollback ();
9346  return -1;
9347  }
9348 
9349  /* Update the database. */
9350 
9351  /* Three commands were removed. */
9352 
9353  sql ("DELETE FROM permissions"
9354  " WHERE name = 'get_target_locators'"
9355  " OR name = 'pause_task'"
9356  " OR name = 'resume_paused_task';");
9357 
9358  sql ("DELETE FROM permissions_trash"
9359  " WHERE name = 'get_target_locators'"
9360  " OR name = 'pause_task'"
9361  " OR name = 'resume_paused_task';");
9362 
9363  /* Set the database version to 131. */
9364 
9365  set_db_version (131);
9366 
9367  sql_commit ();
9368 
9369  return 0;
9370 }
9371 
9377 int
9379 {
9381 
9382  /* Ensure that the database is currently version 131. */
9383 
9384  if (manage_db_version () != 131)
9385  {
9386  sql_rollback ();
9387  return -1;
9388  }
9389 
9390  /* Update the database. */
9391 
9392  /* rpm, deb and exe columns were removed from lsc_credentials and
9393  * lsc_credentials_trash table. */
9394 
9395  /* Move the tables away. */
9396 
9397  sql ("ALTER TABLE lsc_credentials RENAME TO lsc_credentials_131;");
9398  sql ("ALTER TABLE lsc_credentials_trash RENAME TO lsc_credentials_trash_131;");
9399 
9400  /* Create the tables in the new format. */
9401 
9402  if (sql_is_sqlite3 ())
9403  {
9404  sql ("CREATE TABLE IF NOT EXISTS lsc_credentials"
9405  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, login,"
9406  " password, comment, private_key TEXT,"
9407  " creation_time, modification_time);");
9408  sql ("CREATE TABLE IF NOT EXISTS lsc_credentials_trash"
9409  " (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, login,"
9410  " password, comment, private_key TEXT,"
9411  " creation_time, modification_time);");
9412  }
9413  else
9414  {
9415  sql ("CREATE TABLE IF NOT EXISTS lsc_credentials"
9416  " (id SERIAL PRIMARY KEY,"
9417  " uuid text UNIQUE NOT NULL,"
9418  " owner integer REFERENCES users (id) ON DELETE RESTRICT,"
9419  " name text NOT NULL,"
9420  " login text,"
9421  " password text,"
9422  " comment text,"
9423  " private_key text,"
9424  " creation_time integer,"
9425  " modification_time integer);");
9426 
9427  sql ("CREATE TABLE IF NOT EXISTS lsc_credentials_trash"
9428  " (id SERIAL PRIMARY KEY,"
9429  " uuid text UNIQUE NOT NULL,"
9430  " owner integer REFERENCES users (id) ON DELETE RESTRICT,"
9431  " name text NOT NULL,"
9432  " login text,"
9433  " password text,"
9434  " comment text,"
9435  " private_key text,"
9436  " creation_time integer,"
9437  " modification_time integer);");
9438  }
9439 
9440  /* Copy the data into the new table. */
9441 
9442  sql ("INSERT into lsc_credentials"
9443  " (id, uuid, owner, name, comment, login, password, private_key,"
9444  " creation_time, modification_time)"
9445  " SELECT"
9446  " id, uuid, owner, name, comment, login, password, private_key,"
9447  " creation_time, modification_time"
9448  " FROM lsc_credentials_131;");
9449  sql ("INSERT into lsc_credentials_trash"
9450  " (id, uuid, owner, name, comment, login, password, private_key,"
9451  " creation_time, modification_time)"
9452  " SELECT"
9453  " id, uuid, owner, name, comment, login, password, private_key,"
9454  " creation_time, modification_time"
9455  " FROM lsc_credentials_trash_131;");
9456 
9457  /* Drop the old tables. */
9458 
9459  sql ("DROP TABLE lsc_credentials_131;");
9460  sql ("DROP TABLE lsc_credentials_trash_131;");
9461 
9462  /* Set the database version to 132. */
9463 
9464  set_db_version (132);
9465 
9466  sql_commit ();
9467 
9468  return 0;
9469 }
9470 
9476 int
9478 {
9480 
9481  /* Ensure that the database is currently version 132. */
9482 
9483  if (manage_db_version () != 132)
9484  {
9485  sql_rollback ();
9486  return -1;
9487  }
9488 
9489  /* Update the database. */
9490 
9491  /* Add two new columns to results table. */
9492  if (sql_is_sqlite3 ())
9493  sql ("ALTER TABLE results ADD COLUMN owner INTEGER;");
9494  else
9495  sql ("ALTER TABLE results ADD COLUMN owner integer"
9496  " REFERENCES users (id) ON DELETE RESTRICT;");
9497 
9498  sql ("ALTER TABLE results ADD COLUMN date integer;");
9499 
9500  /* Set values for added columns */
9501  sql ("UPDATE results"
9502  " SET owner = (SELECT owner FROM reports"
9503  " WHERE reports.id = results.report),"
9504  " date = (SELECT date FROM reports"
9505  " WHERE reports.id = results.report);");
9506 
9507  /* Set the database version to 133. */
9508 
9509  set_db_version (133);
9510 
9511  sql_commit ();
9512 
9513  return 0;
9514 }
9515 
9521 int
9523 {
9525 
9526  /* Ensure that the database is currently version 133. */
9527 
9528  if (manage_db_version () != 133)
9529  {
9530  sql_rollback ();
9531  return -1;
9532  }
9533 
9534  /* Update the database. */
9535 
9536  /* Add a new columns for ESXi credentials to targets & targets_trash table. */
9537  if (sql_is_sqlite3 ())
9538  {
9539  sql ("ALTER TABLE targets ADD COLUMN esxi_lsc_credential;");
9540  sql ("ALTER TABLE targets_trash ADD COLUMN esxi_lsc_credential;");
9541  sql ("ALTER TABLE targets_trash ADD COLUMN esxi_location INTEGER;");
9542  }
9543  else
9544  {
9545  sql ("ALTER TABLE targets ADD COLUMN esxi_lsc_credential integer;");
9546  // REFERENCES lsc_credentials (id) ON DELETE RESTRICT
9547  sql ("ALTER TABLE targets_trash ADD COLUMN esxi_lsc_credential integer;");
9548  // REFERENCES lsc_credentials (id) ON DELETE RESTRICT
9549  sql ("ALTER TABLE targets_trash ADD COLUMN esxi_location integer;");
9550  }
9551 
9552  /* Set the database version to 134. */
9553 
9554  set_db_version (134);
9555 
9556  sql_commit ();
9557 
9558  return 0;
9559 }
9560 
9566 int
9568 {
9570 
9571  /* Ensure that the database is currently version 134. */
9572 
9573  if (manage_db_version () != 134)
9574  {
9575  sql_rollback ();
9576  return -1;
9577  }
9578 
9579  /* Update the database. */
9580 
9581  /* The table report_results was removed. */
9582 
9583  sql ("DROP TABLE report_results;");
9584 
9585  /* Set the database version to 135. */
9586 
9587  set_db_version (135);
9588 
9589  sql_commit ();
9590 
9591  return 0;
9592 }
9593 
9599 int
9601 {
9603 
9604  /* Ensure that the database is currently version 135. */
9605 
9606  if (manage_db_version () != 135)
9607  {
9608  sql_rollback ();
9609  return -1;
9610  }
9611 
9612  /* Update the database. */
9613 
9614  /* In the past the task end time was sometimes stored as a string instead
9615  * of as an integer. Update the end times from the reports, clearing the
9616  * end time when the task has no reports. */
9617 
9618  sql ("UPDATE tasks"
9619  " SET end_time = (SELECT reports.end_time FROM reports"
9620  " WHERE task = tasks.id ORDER BY id DESC LIMIT 1)"
9621  " WHERE EXISTS (SELECT id FROM reports WHERE task = tasks.id);");
9622 
9623  sql ("UPDATE tasks"
9624  " SET end_time = NULL"
9625  " WHERE NOT EXISTS (SELECT id FROM reports WHERE task = tasks.id);");
9626 
9627  /* Set the database version to 136. */
9628 
9629  set_db_version (136);
9630 
9631  sql_commit ();
9632 
9633  return 0;
9634 }
9635 
9641 int
9643 {
9645 
9646  /* Ensure that the database is currently version 136. */
9647 
9648  if (manage_db_version () != 136)
9649  {
9650  sql_rollback ();
9651  return -1;
9652  }
9653 
9654  /* Update the database. */
9655 
9656  /* Commands like get_scanners have a permission check, delete the roles'
9657  * permissions and they will be recreated (along with the new permissions) on
9658  * start-up. */
9659  sql ("DELETE FROM permissions"
9660  " WHERE subject_type = 'role' AND subject IN"
9661  " (SELECT id FROM roles WHERE uuid = '" ROLE_UUID_USER "'"
9662  " OR uuid = '" ROLE_UUID_OBSERVER "');");
9663 
9664  /* Set the database version to 137. */
9665 
9666  set_db_version (137);
9667 
9668  sql_commit ();
9669 
9670  return 0;
9671 }
9672 
9678 int
9680 {
9682 
9683  /* Ensure that the database is currently version 137. */
9684 
9685  if (manage_db_version () != 137)
9686  {
9687  sql_rollback ();
9688  return -1;
9689  }
9690 
9691  /* Update the database. */
9692 
9693  /* Add get_aggregates permissions to all predefined roles except Monitor. */
9694 
9695  sql ("INSERT INTO permissions"
9696  " (uuid, owner, name, comment, resource_type, resource, resource_uuid,"
9697  " resource_location, subject_type, subject, subject_location,"
9698  " creation_time, modification_time)"
9699  " VALUES"
9700  " (make_uuid (), NULL, 'get_aggregates', '', '',"
9701  " 0, '', " G_STRINGIFY (LOCATION_TABLE) ", 'role',"
9702  " (SELECT id FROM roles WHERE uuid = '%s'),"
9703  " " G_STRINGIFY (LOCATION_TABLE) ", m_now (), m_now ());",
9704  ROLE_UUID_GUEST);
9705 
9706  sql ("INSERT INTO permissions"
9707  " (uuid, owner, name, comment, resource_type, resource, resource_uuid,"
9708  " resource_location, subject_type, subject, subject_location,"
9709  " creation_time, modification_time)"
9710  " VALUES"
9711  " (make_uuid (), NULL, 'get_aggregates', '', '',"
9712  " 0, '', " G_STRINGIFY (LOCATION_TABLE) ", 'role',"
9713  " (SELECT id FROM roles WHERE uuid = '%s'),"
9714  " " G_STRINGIFY (LOCATION_TABLE) ", m_now (), m_now ());",
9715  ROLE_UUID_INFO);
9716 
9717  sql ("INSERT INTO permissions"
9718  " (uuid, owner, name, comment, resource_type, resource, resource_uuid,"
9719  " resource_location, subject_type, subject, subject_location,"
9720  " creation_time, modification_time)"
9721  " VALUES"
9722  " (make_uuid (), NULL, 'get_aggregates', '', '',"
9723  " 0, '', " G_STRINGIFY (LOCATION_TABLE) ", 'role',"
9724  " (SELECT id FROM roles WHERE uuid = '%s'),"
9725  " " G_STRINGIFY (LOCATION_TABLE) ", m_now (), m_now ());",
9727 
9728  sql ("INSERT INTO permissions"
9729  " (uuid, owner, name, comment, resource_type, resource, resource_uuid,"
9730  " resource_location, subject_type, subject, subject_location,"
9731  " creation_time, modification_time)"
9732  " VALUES"
9733  " (make_uuid (), NULL, 'get_aggregates', '', '',"
9734  " 0, '', " G_STRINGIFY (LOCATION_TABLE) ", 'role',"
9735  " (SELECT id FROM roles WHERE uuid = '%s'),"
9736  " " G_STRINGIFY (LOCATION_TABLE) ", m_now (), m_now ());",
9737  ROLE_UUID_USER);
9738 
9739  /* Set the database version to 138. */
9740 
9741  set_db_version (138);
9742 
9743  sql_commit ();
9744 
9745  return 0;
9746 }
9747 
9753 int
9755 {
9756  iterator_t nvts;
9757 
9759 
9760  /* Ensure that the database is currently version 138. */
9761 
9762  if (manage_db_version () != 138)
9763  {
9764  sql_rollback ();
9765  return -1;
9766  }
9767 
9768  /* Update the database. */
9769  /* Add new solution_type column */
9770  sql ("ALTER TABLE nvts ADD COLUMN solution_type text;");
9771 
9772  init_iterator (&nvts, "SELECT id, tag FROM nvts;");
9773 
9774  while (next (&nvts))
9775  {
9776  nvt_t nvt = iterator_int64 (&nvts, 0);
9777  const gchar* tags = iterator_string (&nvts, 1);
9778  gchar *solution_type = tag_value (tags, "solution_type");
9779  gchar *quoted_solution_type = sql_quote (solution_type);
9780 
9781  sql ("UPDATE nvts SET"
9782  " solution_type = '%s'"
9783  " WHERE id = %llu;",
9784  quoted_solution_type,
9785  nvt);
9786 
9787  g_free (solution_type);
9788  g_free (quoted_solution_type);
9789  }
9790 
9791  /* Set the database version to 139. */
9792 
9793  set_db_version (139);
9794 
9795  sql_commit ();
9796 
9797  return 0;
9798 }
9799 
9805 int
9807 {
9809 
9810  /* Ensure that the database is currently version 139. */
9811 
9812  if (manage_db_version () != 139)
9813  {
9814  sql_rollback ();
9815  return -1;
9816  }
9817 
9818  /* Update the database. */
9819 
9820  sql ("UPDATE permissions SET"
9821  " name = 'resume_task'"
9822  " WHERE name = 'resume_stopped_task';");
9823 
9824  sql ("UPDATE permissions_trash SET"
9825  " name = 'resume_task'"
9826  " WHERE name = 'resume_stopped_task';");
9827 
9828  /* Set the database version to 140. */
9829 
9830  set_db_version (140);
9831 
9832  sql_commit ();
9833 
9834  return 0;
9835 }
9836 
9842 int
9844 {
9846 
9847  /* Ensure that the database is currently version 140. */
9848 
9849  if (manage_db_version () != 140)
9850  {
9851  sql_rollback ();
9852  return -1;
9853  }
9854 
9855  /* Update the database. */
9856  sql ("ALTER TABLE config_preferences ADD COLUMN default_value text;");
9857  sql ("ALTER TABLE config_preferences_trash ADD COLUMN default_value text;");
9858 
9859  /* Set the database version to 141. */
9860 
9861  set_db_version (141);
9862 
9863  sql_commit ();
9864 
9865  return 0;
9866 }
9867 
9873 int
9875 {
9877 
9878  /* Ensure that the database is currently version 141. */
9879 
9880  if (manage_db_version () != 141)
9881  {
9882  sql_rollback ();
9883  return -1;
9884  }
9885 
9886  /* Update the database. */
9887 
9888  /* OMP command RESUME_OR_START_TASK was removed. */
9889 
9890  sql ("DELETE FROM permissions WHERE name = 'resume_or_start_task';");
9891 
9892  sql ("DELETE FROM permissions_trash WHERE name = 'resume_or_start_task';");
9893 
9894  /* Set the database version to 142. */
9895 
9896  set_db_version (142);
9897 
9898  sql_commit ();
9899 
9900  return 0;
9901 }
9902 
9908 int
9910 {
9912 
9913  /* Ensure that the database is currently version 142. */
9914 
9915  if (manage_db_version () != 142)
9916  {
9917  sql_rollback ();
9918  return -1;
9919  }
9920 
9921  /* Update the database. */
9922 
9923  /* Set QoD of results to default value for various cases where
9924  * no QoD was specified during the creation of the result. */
9925 
9926  sql ("UPDATE results SET qod = " G_STRINGIFY (QOD_DEFAULT)
9927  " WHERE (qod IS NULL) OR (qod <= 0);");
9928 
9929  /* Set the database version to 143. */
9930 
9931  set_db_version (143);
9932 
9933  sql_commit ();
9934 
9935  return 0;
9936 }
9937 
9943 int
9945 {
9946  iterator_t nvts;
9948 
9949  /* Ensure that the database is currently version 143. */
9950 
9951  if (manage_db_version () != 143)
9952  {
9953  sql_rollback ();
9954  return -1;
9955  }
9956 
9957  /* Update the database. */
9958 
9959  /* Add new QoD columns */
9960  sql ("ALTER TABLE nvts ADD COLUMN qod INTEGER;");
9961  sql ("ALTER TABLE nvts ADD COLUMN qod_type TEXT;");
9962  sql ("ALTER TABLE results ADD COLUMN qod_type TEXT;");
9963 
9964  /* Set default values */
9965  sql ("UPDATE nvts SET qod=%d, qod_type='';", QOD_DEFAULT);
9966  sql ("UPDATE results SET qod_type='';");
9967  /* Assign QoD values from NVT tags */
9968  init_iterator (&nvts, "SELECT id, tag FROM nvts WHERE tag LIKE '%%|qod%%';");
9969  while (next (&nvts))
9970  {
9971  gchar *qod_str, *qod_type, *quoted_qod_type;
9972  int qod;
9973 
9974  qod_str = tag_value (iterator_string (&nvts, 1), "qod");
9975  qod_type = tag_value (iterator_string (&nvts, 1), "qod_type");
9976  quoted_qod_type = sql_quote (qod_type);
9977 
9978  if (qod_str == NULL || sscanf (qod_str, "%d", &qod) != 1)
9979  qod = qod_from_type (qod_type);
9980 
9981  sql ("UPDATE nvts SET qod=%d, qod_type='%s' WHERE id=%llu;",
9982  qod, quoted_qod_type, iterator_int64 (&nvts, 0));
9983 
9984  g_free (qod_str);
9985  g_free (qod_type);
9986  g_free (quoted_qod_type);
9987  }
9988  cleanup_iterator (&nvts);
9989 
9990  /* Set the database version to 144. */
9991 
9992  set_db_version (144);
9993 
9994  sql_commit ();
9995 
9996  return 0;
9997 }
9998 
10004 int
10006 {
10008 
10009  /* Ensure that the database is currently version 144. */
10010 
10011  if (manage_db_version () != 144)
10012  {
10013  sql_rollback ();
10014  return -1;
10015  }
10016 
10017  /* Update the database. */
10018 
10019  /* Tasks got a new column schedule_periods. */
10020  sql ("ALTER TABLE tasks ADD COLUMN schedule_periods INTEGER;");
10021  sql ("UPDATE tasks SET schedule_periods=0;");
10022 
10023  /* Set the database version to 145. */
10024 
10025  set_db_version (145);
10026 
10027  sql_commit ();
10028 
10029  return 0;
10030 }
10031 
10037 int
10039 {
10041 
10042  /* Ensure that the database is currently version 145. */
10043 
10044  if (manage_db_version () != 145)
10045  {
10046  sql_rollback ();
10047  return -1;
10048  }
10049 
10050  /* Update the database. */
10051 
10052  /* The view result_overrides changed. */
10053  sql ("DROP VIEW IF EXISTS result_new_severities;");
10054  sql ("DROP VIEW IF EXISTS result_overrides;");
10055  sql ("DELETE FROM report_counts;");
10056 
10057  /* Set the database version to 146. */
10058 
10059  set_db_version (146);
10060 
10061  sql_commit ();
10062 
10063  return 0;
10064 }
10065 
10071 int
10073 {
10075 
10076  /* Ensure that the database is currently version 146. */
10077 
10078  if (manage_db_version () != 146)
10079  {
10080  sql_rollback ();
10081  return -1;
10082  }
10083 
10084  /* Update the database. */
10085 
10086  /* The report_counts table got a min_qod column. */
10087  sql ("ALTER TABLE report_counts ADD COLUMN min_qod INTEGER;");
10088  sql ("UPDATE report_counts SET min_qod = %d;", MIN_QOD_DEFAULT);
10089 
10090  /* Set the database version to 147. */
10091 
10092  set_db_version (147);
10093 
10094  sql_commit ();
10095 
10096  return 0;
10097 }
10098 
10104 int
10106 {
10108 
10109  /* Ensure that the database is currently version 147. */
10110 
10111  if (manage_db_version () != 147)
10112  {
10113  sql_rollback ();
10114  return -1;
10115  }
10116 
10117  /* Update the database. */
10118 
10119  /* The "generate" scripts of all report formats must now be executable. */
10120 
10122 
10123  /* Set the database version to 148. */
10124 
10125  set_db_version (148);
10126 
10127  sql_commit ();
10128 
10129  return 0;
10130 }
10131 
10137 int
10139 {
10141 
10142  /* Ensure that the database is currently version 148. */
10143 
10144  if (manage_db_version () != 148)
10145  {
10146  sql_rollback ();
10147  return -1;
10148  }
10149 
10150  /* Update the database. */
10151 
10152  /* The tasks table got a scanner_location column. */
10153  sql ("ALTER TABLE tasks ADD COLUMN scanner_location INTEGER;");
10154  sql ("UPDATE tasks SET scanner_location = " G_STRINGIFY (LOCATION_TABLE));
10155 
10156  /* Set the database version to 149. */
10157 
10158  set_db_version (149);
10159 
10160  sql_commit ();
10161 
10162  return 0;
10163 }
10164 
10170 int
10172 {
10174 
10175  /* Ensure that the database is currently version 149. */
10176 
10177  if (manage_db_version () != 149)
10178  {
10179  sql_rollback ();
10180  return -1;
10181  }
10182 
10183  /* Update the database. */
10184 
10185  /* The view result_new_severities changed. */
10186  sql ("DROP VIEW IF EXISTS result_new_severities;");
10187 
10188  /* Set the database version to 150. */
10189 
10190  set_db_version (150);
10191 
10192  sql_commit ();
10193 
10194  return 0;
10195 }
10196 
10197 #define INSERT_PERMISSION(name, role) \
10198  sql ("INSERT INTO permissions" \
10199  " (uuid, owner, name, comment, resource_type, resource, resource_uuid," \
10200  " resource_location, subject_type, subject, subject_location," \
10201  " creation_time, modification_time)" \
10202  " VALUES" \
10203  " (make_uuid (), NULL, '" G_STRINGIFY (name) "', '', ''," \
10204  " 0, '', " G_STRINGIFY (LOCATION_TABLE) ", 'role'," \
10205  " (SELECT id FROM roles WHERE uuid = '%s')," \
10206  " " G_STRINGIFY (LOCATION_TABLE) ", m_now (), m_now ());", \
10207  role)
10208 
10214 int
10216 {
10218 
10219  /* Ensure that the database is currently version 150. */
10220 
10221  if (manage_db_version () != 150)
10222  {
10223  sql_rollback ();
10224  return -1;
10225  }
10226 
10227  /* Update the database. */
10228 
10229  /* Commands GET_ASSETS and DELETE_ASSET were added. */
10230 
10231  INSERT_PERMISSION (get_assets, ROLE_UUID_ADMIN);
10232  INSERT_PERMISSION (get_assets, ROLE_UUID_OBSERVER);
10234  INSERT_PERMISSION (get_assets, ROLE_UUID_USER);
10235 
10239 
10240  /* Set the database version to 151. */
10241 
10242  set_db_version (151);
10243 
10244  sql_commit ();
10245 
10246  return 0;
10247 }
10248 
10254 int
10256 {
10258 
10259  /* Ensure that the database is currently version 151. */
10260 
10261  if (manage_db_version () != 151)
10262  {
10263  sql_rollback ();
10264  return -1;
10265  }
10266 
10267  /* Update the database. */
10268 
10269  /* Command CREATE_ASSET was added. */
10270 
10271  INSERT_PERMISSION (create_asset, ROLE_UUID_ADMIN);
10272  INSERT_PERMISSION (create_asset, ROLE_UUID_SUPER_ADMIN);
10273  INSERT_PERMISSION (create_asset, ROLE_UUID_USER);
10274 
10275  /* Set the database version to 152. */
10276 
10277  set_db_version (152);
10278 
10279  sql_commit ();
10280 
10281  return 0;
10282 }
10283 
10284 #define DELETE_PERMISSION(name, role) \
10285  sql ("DELETE FROM permissions" \
10286  " WHERE subject_type = 'role'" \
10287  " AND subject_location = " G_STRINGIFY (LOCATION_TABLE) \
10288  " AND subject = (SELECT id FROM roles WHERE uuid = '%s')" \
10289  " AND name = '" G_STRINGIFY (name) "';", \
10290  role)
10291 
10297 int
10299 {
10301 
10302  /* Ensure that the database is currently version 152. */
10303 
10304  if (manage_db_version () != 152)
10305  {
10306  sql_rollback ();
10307  return -1;
10308  }
10309 
10310  /* Update the database. */
10311 
10312  /* Command MODIFY_ASSET was added. Also remove permissions added in previous
10313  * two migrators on roles that have "Everything". */
10314 
10316 
10317  DELETE_PERMISSION (create_asset, ROLE_UUID_ADMIN);
10318  DELETE_PERMISSION (create_asset, ROLE_UUID_SUPER_ADMIN);
10319  DELETE_PERMISSION (get_assets, ROLE_UUID_ADMIN);
10323 
10324  /* Set the database version to 153. */
10325 
10326  set_db_version (153);
10327 
10328  sql_commit ();
10329 
10330  return 0;
10331 }
10332 
10338 int
10340 {
10341  const char *primary_key_type = sql_is_sqlite3 () ? "INTEGER" : "SERIAL";
10342  iterator_t credentials;
10343 
10345 
10346  /* Ensure that the database is currently version 153. */
10347 
10348  if (manage_db_version () != 153)
10349  {
10350  sql_rollback ();
10351  return -1;
10352  }
10353 
10354  /* Update the database. */
10355 
10356  /* Create new credentials tables */
10357  sql ("CREATE TABLE credentials"
10358  " (id %s PRIMARY KEY,"
10359  " uuid text UNIQUE NOT NULL,"
10360  " owner integer%s,"
10361  " name text NOT NULL,"
10362  " comment text,"
10363  " creation_time integer,"
10364  " modification_time integer,"
10365  " type text);",
10366  primary_key_type,
10367  sql_is_sqlite3() ? "" : " REFERENCES users (id) ON DELETE RESTRICT");
10368 
10369  sql ("CREATE TABLE credentials_trash"
10370  " (id %s PRIMARY KEY,"
10371  " uuid text UNIQUE NOT NULL,"
10372  " owner integer%s,"
10373  " name text NOT NULL,"
10374  " comment text,"
10375  " creation_time integer,"
10376  " modification_time integer,"
10377  " type text);",
10378  primary_key_type,
10379  sql_is_sqlite3() ? "" : " REFERENCES users (id) ON DELETE RESTRICT");
10380 
10381  sql ("CREATE TABLE credentials_data"
10382  " (id %s PRIMARY KEY,"
10383  " credential INTEGER%s,"
10384  " type TEXT,"
10385  " value TEXT);",
10386  primary_key_type,
10387  sql_is_sqlite3()
10388  ? ""
10389  : " REFERENCES credentials (id) ON DELETE RESTRICT");
10390 
10391  sql ("CREATE TABLE credentials_trash_data"
10392  " (id %s PRIMARY KEY,"
10393  " credential INTEGER%s,"
10394  " type TEXT,"
10395  " value TEXT);",
10396  primary_key_type,
10397  sql_is_sqlite3()
10398  ? ""
10399  : " REFERENCES credentials_trash (id) ON DELETE RESTRICT");
10400 
10401  /* Copy basic data from old tables */
10402  sql ("INSERT INTO credentials"
10403  " (id, uuid, owner, name, comment, creation_time, modification_time)"
10404  " SELECT"
10405  " id, uuid, owner, name, comment, creation_time, modification_time"
10406  " FROM lsc_credentials;");
10407 
10408  sql ("INSERT INTO credentials_trash"
10409  " (id, uuid, owner, name, comment, creation_time, modification_time)"
10410  " SELECT"
10411  " id, uuid, owner, name, comment, creation_time, modification_time"
10412  " FROM lsc_credentials_trash;");
10413 
10414  /* Copy credentials data */
10415  sql ("INSERT INTO credentials_data (credential, type, value)"
10416  " SELECT id, 'username', login FROM lsc_credentials"
10417  " WHERE login IS NOT NULL;");
10418 
10419  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
10420  " SELECT id, 'username', login FROM lsc_credentials_trash"
10421  " WHERE login IS NOT NULL;");
10422 
10423  sql ("INSERT INTO credentials_data (credential, type, value)"
10424  " SELECT id, 'password', password FROM lsc_credentials"
10425  " WHERE password IS NOT NULL AND private_key != ';;encrypted;;';");
10426 
10427  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
10428  " SELECT id, 'password', password FROM lsc_credentials_trash"
10429  " WHERE password IS NOT NULL AND private_key != ';;encrypted;;';");
10430 
10431  sql ("INSERT INTO credentials_data (credential, type, value)"
10432  " SELECT id, 'private_key', private_key FROM lsc_credentials"
10433  " WHERE password IS NOT NULL AND private_key != ';;encrypted;;';");
10434 
10435  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
10436  " SELECT id, 'private_key', private_key FROM lsc_credentials_trash"
10437  " WHERE password IS NOT NULL AND private_key != ';;encrypted;;';");
10438 
10439  sql ("INSERT INTO credentials_data (credential, type, value)"
10440  " SELECT id, 'secret', password FROM lsc_credentials"
10441  " WHERE password IS NOT NULL AND private_key = ';;encrypted;;';");
10442 
10443  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
10444  " SELECT id, 'secret', password FROM lsc_credentials_trash"
10445  " WHERE password IS NOT NULL AND private_key = ';;encrypted;;';");
10446 
10447  /* For Postgres, reset sequences because we messed with SERIAL column "id". */
10448 
10449  if (sql_is_sqlite3 () == 0)
10450  {
10451  sql ("SELECT setval ('credentials_id_seq',"
10452  " (SELECT max (id) + 1 FROM credentials));");
10453 
10454  sql ("SELECT setval ('credentials_trash_id_seq',"
10455  " (SELECT max (id) + 1 FROM credentials_trash));");
10456 
10457  sql ("SELECT setval ('credentials_data_id_seq',"
10458  " (SELECT max (id) + 1 FROM credentials_data));");
10459 
10460  sql ("SELECT setval ('credentials_trash_data_id_seq',"
10461  " (SELECT max (id) + 1"
10462  " FROM credentials_trash_data));");
10463  }
10464 
10465  /* Set type for existing credentials */
10466  init_iterator (&credentials,
10467  "SELECT id, password, private_key, 0"
10468  " FROM lsc_credentials"
10469  " UNION ALL"
10470  " SELECT id, password, private_key, 1"
10471  " FROM lsc_credentials_trash;");
10472 
10473  while (next (&credentials))
10474  {
10475  credential_t credential;
10476  int is_trash;
10477  const char *password, *privkey;
10478  const char *type;
10479 
10480  credential = iterator_int64 (&credentials, 0);
10481  password = iterator_string (&credentials, 1);
10482  privkey = iterator_string (&credentials, 2);
10483  is_trash = iterator_int (&credentials, 3);
10484 
10485  if (privkey == NULL)
10486  type = "up";
10487  else if (strcmp (privkey, ";;encrypted;;"))
10488  type = "usk";
10489  else
10490  {
10491  if (!credentials.crypt_ctx)
10492  credentials.crypt_ctx = lsc_crypt_new ();
10493 
10494  if (lsc_crypt_get_private_key (credentials.crypt_ctx, password))
10495  type = "usk";
10496  else
10497  type = "up";
10498  }
10499 
10500  sql ("UPDATE %s SET type = '%s' WHERE id = %llu;",
10501  is_trash ? "credentials_trash" : "credentials",
10502  type, credential);
10503  }
10504  cleanup_iterator (&credentials);
10505 
10506  /* Remove the old tables */
10507  sql ("DROP TABLE lsc_credentials;");
10508  sql ("DROP TABLE lsc_credentials_trash;");
10509 
10510  /* Update Tags */
10511  sql ("UPDATE tags SET resource_type = 'credential'"
10512  " WHERE resource_type = 'lsc_credential';");
10513  sql ("UPDATE tags_trash SET resource_type = 'credential'"
10514  " WHERE resource_type = 'lsc_credential';");
10515 
10516  /* Update permissions */
10517  sql ("UPDATE permissions SET name = 'create_credential'"
10518  " WHERE name = 'create_lsc_credential';");
10519  sql ("UPDATE permissions SET name = 'delete_credential'"
10520  " WHERE name = 'delete_lsc_credential';");
10521  sql ("UPDATE permissions SET name = 'get_credentials'"
10522  " WHERE name = 'get_lsc_credentials';");
10523  sql ("UPDATE permissions SET name = 'modify_credential'"
10524  " WHERE name = 'modify_lsc_credential';");
10525 
10526  sql ("UPDATE permissions_trash SET name = 'create_credential'"
10527  " WHERE name = 'create_lsc_credential';");
10528  sql ("UPDATE permissions_trash SET name = 'delete_credential'"
10529  " WHERE name = 'delete_lsc_credential';");
10530  sql ("UPDATE permissions_trash SET name = 'get_credentials'"
10531  " WHERE name = 'get_lsc_credentials';");
10532  sql ("UPDATE permissions_trash SET name = 'modify_credential'"
10533  " WHERE name = 'modify_lsc_credential';");
10534 
10535  /* Set the database version to 154. */
10536 
10537  set_db_version (154);
10538 
10539  sql_commit ();
10540 
10541  return 0;
10542 }
10543 
10549 int
10551 {
10553 
10554  /* Ensure that the database is currently version 154. */
10555 
10556  if (manage_db_version () != 154)
10557  {
10558  sql_rollback ();
10559  return -1;
10560  }
10561 
10562  /* Update the database. */
10563 
10564  /* r23581 added ALERT_METHOD_START_TASK in the middle of alert_method_t,
10565  * instead of at the end. Adjust alerts accordingly. r23581 was released
10566  * first with 6.1+beta2 which had db version 155, so it's safe to do this
10567  * adjustment to any database that is older than 155. */
10568  sql ("UPDATE alerts SET method = method + 1 WHERE method >= 4;");
10569 
10570  /* Reports got a new column "flags". */
10571  sql ("ALTER TABLE reports ADD COLUMN flags INTEGER;");
10572  sql ("UPDATE reports SET flags = 0;");
10573 
10574  /* Set the database version to 155. */
10575 
10576  set_db_version (155);
10577 
10578  sql_commit ();
10579 
10580  return 0;
10581 }
10582 
10588 int
10590 {
10592 
10593  /* Ensure that the database is currently version 155. */
10594 
10595  if (manage_db_version () != 155)
10596  {
10597  sql_rollback ();
10598  return -1;
10599  }
10600 
10601  /* Update the database. */
10602 
10603  if (sql_is_sqlite3 ())
10604  {
10605  /* Remove and rename columns by copying tables in SQLite */
10606  /* Rename old targets tables. */
10607  sql ("ALTER TABLE targets RENAME TO targets_155;");
10608  sql ("ALTER TABLE targets_trash RENAME TO targets_trash_155;");
10609 
10610  /* Create new targets tables */
10611  sql ("CREATE TABLE IF NOT EXISTS targets"
10612  " (id INTEGER PRIMARY KEY,"
10613  " uuid text UNIQUE NOT NULL,"
10614  " owner integer,"
10615  " name text NOT NULL,"
10616  " hosts text,"
10617  " exclude_hosts text,"
10618  " reverse_lookup_only integer,"
10619  " reverse_lookup_unify integer,"
10620  " comment text,"
10621  " port_list integer,"
10622  " alive_test integer,"
10623  " creation_time integer,"
10624  " modification_time integer);");
10625 
10626  sql ("CREATE TABLE IF NOT EXISTS targets_trash"
10627  " (id INTEGER PRIMARY KEY,"
10628  " uuid text UNIQUE NOT NULL,"
10629  " owner integer,"
10630  " name text NOT NULL,"
10631  " hosts text,"
10632  " exclude_hosts text,"
10633  " reverse_lookup_only integer,"
10634  " reverse_lookup_unify integer,"
10635  " comment text,"
10636  " port_list integer,"
10637  " port_list_location integer,"
10638  " alive_test integer,"
10639  " creation_time integer,"
10640  " modification_time integer);");
10641 
10642  sql ("CREATE TABLE IF NOT EXISTS targets_login_data"
10643  " (id INTEGER PRIMARY KEY,"
10644  " target INTEGER,"
10645  " type TEXT,"
10646  " credential INTEGER,"
10647  " port INTEGER);");
10648 
10649  sql ("CREATE TABLE IF NOT EXISTS targets_trash_login_data"
10650  " (id INTEGER PRIMARY KEY,"
10651  " target INTEGER,"
10652  " type TEXT,"
10653  " credential INTEGER,"
10654  " port INTEGER,"
10655  " credential_location INTEGER);");
10656 
10657  /* Copy existing basic data */
10658  sql ("INSERT INTO targets"
10659  " (id, uuid, owner, name, hosts, exclude_hosts,"
10660  " reverse_lookup_only, reverse_lookup_unify, comment,"
10661  " port_list, alive_test, creation_time, modification_time)"
10662  " SELECT id, uuid, owner, name, hosts, exclude_hosts,"
10663  " reverse_lookup_only, reverse_lookup_unify, comment,"
10664  " port_range, alive_test, creation_time, modification_time"
10665  " FROM targets_155;");
10666 
10667  sql ("INSERT INTO targets_trash"
10668  " (id, uuid, owner, name, hosts, exclude_hosts,"
10669  " reverse_lookup_only, reverse_lookup_unify, comment,"
10670  " port_list, alive_test, creation_time, modification_time)"
10671  " SELECT id, uuid, owner, name, hosts, exclude_hosts,"
10672  " reverse_lookup_only, reverse_lookup_unify, comment,"
10673  " port_range, alive_test, creation_time, modification_time"
10674  " FROM targets_trash_155;");
10675 
10676  /* Copy existing credentials data */
10677  sql ("INSERT INTO targets_login_data"
10678  " (target, type, credential, port)"
10679  " SELECT id, 'ssh', lsc_credential, CAST (ssh_port AS integer)"
10680  " FROM targets_155 WHERE lsc_credential != 0;");
10681 
10682  sql ("INSERT INTO targets_login_data"
10683  " (target, type, credential, port)"
10684  " SELECT id, 'smb', smb_lsc_credential, 0"
10685  " FROM targets_155 WHERE smb_lsc_credential != 0;");
10686 
10687  sql ("INSERT INTO targets_login_data"
10688  " (target, type, credential, port)"
10689  " SELECT id, 'esxi', esxi_lsc_credential, 0"
10690  " FROM targets_155 WHERE esxi_lsc_credential != 0;");
10691 
10692  /* Copy existing trash credentials data */
10693  sql ("INSERT INTO targets_trash_login_data"
10694  " (target, type, credential, port, credential_location)"
10695  " SELECT id, 'ssh', lsc_credential, CAST (ssh_port AS integer),"
10696  " ssh_location"
10697  " FROM targets_trash_155 WHERE lsc_credential != 0;");
10698 
10699  sql ("INSERT INTO targets_trash_login_data"
10700  " (target, type, credential, port, credential_location)"
10701  " SELECT id, 'smb', smb_lsc_credential, 0, smb_location"
10702  " FROM targets_trash_155 WHERE smb_lsc_credential != 0;");
10703 
10704  sql ("INSERT INTO targets_trash_login_data"
10705  " (target, type, credential, port, credential_location)"
10706  " SELECT id, 'esxi', esxi_lsc_credential, 0, esxi_location"
10707  " FROM targets_trash_155 WHERE esxi_lsc_credential != 0;");
10708 
10709  /* Remove old tables */
10710  sql ("DROP TABLE targets_155;");
10711  sql ("DROP TABLE targets_trash_155;");
10712  }
10713  else
10714  {
10715  /* Use ALTER TABLE to remove and rename columns in Postgres */
10716  /* Create login data tables */
10717  sql ("CREATE TABLE IF NOT EXISTS targets_login_data"
10718  " (id SERIAL PRIMARY KEY,"
10719  " target INTEGER REFERENCES targets (id),"
10720  " type TEXT,"
10721  " credential INTEGER REFERENCES credentials (id),"
10722  " port INTEGER);");
10723 
10724  sql ("CREATE TABLE IF NOT EXISTS targets_trash_login_data"
10725  " (id SERIAL PRIMARY KEY,"
10726  " target INTEGER REFERENCES targets_trash (id),"
10727  " type TEXT,"
10728  " credential INTEGER,"
10729  " port INTEGER,"
10730  " credential_location INTEGER);");
10731 
10732  /* Copy existing credentials data */
10733  sql ("INSERT INTO targets_login_data"
10734  " (target, type, credential, port)"
10735  " SELECT id, 'ssh', lsc_credential, CAST (ssh_port AS integer)"
10736  " FROM targets WHERE lsc_credential != 0;");
10737 
10738  sql ("INSERT INTO targets_login_data"
10739  " (target, type, credential, port)"
10740  " SELECT id, 'smb', smb_lsc_credential, 0"
10741  " FROM targets WHERE smb_lsc_credential != 0;");
10742 
10743  sql ("INSERT INTO targets_login_data"
10744  " (target, type, credential, port)"
10745  " SELECT id, 'esxi', esxi_lsc_credential, 0"
10746  " FROM targets WHERE esxi_lsc_credential != 0;");
10747 
10748  /* Copy existing trash credentials data */
10749  sql ("INSERT INTO targets_trash_login_data"
10750  " (target, type, credential, port, credential_location)"
10751  " SELECT id, 'ssh', lsc_credential, CAST (ssh_port AS integer),"
10752  " ssh_location"
10753  " FROM targets_trash WHERE lsc_credential != 0;");
10754 
10755  sql ("INSERT INTO targets_trash_login_data"
10756  " (target, type, credential, port, credential_location)"
10757  " SELECT id, 'smb', smb_lsc_credential, 0, smb_location"
10758  " FROM targets_trash WHERE smb_lsc_credential != 0;");
10759 
10760  sql ("INSERT INTO targets_trash_login_data"
10761  " (target, type, credential, port, credential_location)"
10762  " SELECT id, 'esxi', esxi_lsc_credential, 0, esxi_location"
10763  " FROM targets_trash WHERE esxi_lsc_credential != 0;");
10764 
10765  /* Drop and remove now unused columns */
10766  sql ("ALTER TABLE targets DROP COLUMN lsc_credential;");
10767  sql ("ALTER TABLE targets DROP COLUMN ssh_port;");
10768  sql ("ALTER TABLE targets DROP COLUMN smb_lsc_credential;");
10769  sql ("ALTER TABLE targets DROP COLUMN esxi_lsc_credential;");
10770  sql ("ALTER TABLE targets RENAME COLUMN port_range TO port_list;");
10771 
10772  sql ("ALTER TABLE targets_trash DROP COLUMN lsc_credential;");
10773  sql ("ALTER TABLE targets_trash DROP COLUMN ssh_location;");
10774  sql ("ALTER TABLE targets_trash DROP COLUMN ssh_port;");
10775  sql ("ALTER TABLE targets_trash DROP COLUMN smb_lsc_credential;");
10776  sql ("ALTER TABLE targets_trash DROP COLUMN smb_location;");
10777  sql ("ALTER TABLE targets_trash DROP COLUMN esxi_lsc_credential;");
10778  sql ("ALTER TABLE targets_trash DROP COLUMN esxi_location;");
10779  sql ("ALTER TABLE targets_trash RENAME COLUMN port_range TO port_list;");
10780  }
10781 
10782  /* Set the database version to 156. */
10783 
10784  set_db_version (156);
10785 
10786  sql_commit ();
10787 
10788  return 0;
10789 }
10790 
10796 int
10798 {
10799  iterator_t slaves;
10801 
10802  /* Ensure that the database is currently version 156. */
10803 
10804  if (manage_db_version () != 156)
10805  {
10806  sql_rollback ();
10807  return -1;
10808  }
10809 
10810  /* Update the database. */
10811 
10812  /* Add new columns to slaves tables. */
10813  if (sql_is_sqlite3 ())
10814  {
10815  sql ("ALTER TABLE slaves ADD COLUMN credential INTEGER;");
10816  }
10817  else
10818  {
10819  sql ("ALTER TABLE slaves ADD COLUMN credential INTEGER"
10820  " REFERENCES credentials (id) ON DELETE RESTRICT;");
10821  }
10822  sql ("ALTER TABLE slaves_trash ADD COLUMN credential INTEGER;");
10823  sql ("ALTER TABLE slaves_trash ADD COLUMN credential_location INTEGER;");
10824 
10825  /* Create new credentials. */
10826  init_iterator (&slaves,
10827  "SELECT id, name, login, password, owner FROM slaves;");
10828 
10829  while (next (&slaves))
10830  {
10831  resource_t slave;
10832  const char *name, *login, *password;
10833  user_t owner;
10834  credential_t new_credential;
10835  gchar *quoted_name, *quoted_login;
10836 
10837  slave = iterator_int64 (&slaves, 0);
10838  name = iterator_string (&slaves, 1);
10839  login = iterator_string (&slaves, 2);
10840  password = iterator_string (&slaves, 3);
10841  owner = iterator_int64 (&slaves, 4);
10842 
10843  quoted_name = sql_quote (name);
10844  quoted_login = sql_quote (login);
10845 
10846  if (sql_int ("SELECT count(*) FROM credentials"
10847  " WHERE name = 'Credential for Slave %s'"
10848  " AND owner = %llu;",
10849  quoted_name, owner))
10850  sql ("INSERT INTO credentials"
10851  " (uuid, name, owner, comment, type,"
10852  " creation_time, modification_time)"
10853  " VALUES"
10854  " (make_uuid (),"
10855  " uniquify ('credential', 'Credential for Slave %s', %llu, ''),"
10856  " %llu, 'Autogenerated by migration', 'up',"
10857  " m_now (), m_now ());",
10858  quoted_name, owner, owner);
10859  else
10860  sql ("INSERT INTO credentials"
10861  " (uuid, name, owner, comment, type,"
10862  " creation_time, modification_time)"
10863  " VALUES"
10864  " (make_uuid (), 'Credential for Slave %s',"
10865  " %llu, 'Autogenerated by migration', 'up',"
10866  " m_now (), m_now ());",
10867  quoted_name, owner);
10868 
10869  new_credential = sql_last_insert_id ();
10870 
10871  sql ("UPDATE slaves SET credential = %llu WHERE id = %llu;",
10872  new_credential, slave);
10873 
10874  sql ("INSERT INTO credentials_data (credential, type, value)"
10875  " VALUES (%llu, 'username', '%s');",
10876  new_credential, quoted_login);
10877 
10879  {
10880  gchar *quoted_password;
10881  quoted_password = sql_quote (password);
10882  sql ("INSERT INTO credentials_data (credential, type, value)"
10883  " VALUES (%llu, 'password', '%s');",
10884  new_credential, quoted_password);
10885  g_free (quoted_password);
10886  }
10887  else
10888  {
10889  char *secret;
10890  gchar *quoted_secret;
10891 
10892  if (!slaves.crypt_ctx)
10893  slaves.crypt_ctx = lsc_crypt_new ();
10894 
10895  secret = lsc_crypt_encrypt (slaves.crypt_ctx,
10896  "password", password, NULL);
10897  if (!secret)
10898  {
10899  g_free (quoted_name);
10900  g_free (quoted_login);
10901  cleanup_iterator (&slaves);
10902  sql_rollback ();
10903  return -1;
10904  }
10905  quoted_secret = sql_quote (secret);
10906  sql ("INSERT INTO credentials_data (credential, type, value)"
10907  " VALUES (%llu, 'secret', '%s');",
10908  new_credential, quoted_secret);
10909  g_free (quoted_secret);
10910  }
10911 
10912  sql ("INSERT INTO"
10913  " permissions (uuid, owner, name,"
10914  " comment, resource_type, resource,"
10915  " resource_uuid,"
10916  " resource_location, subject_type, subject,"
10917  " subject_location, creation_time, modification_time)"
10918  " SELECT make_uuid(), owner, 'get_credentials',"
10919  " 'Autogenerated by Slave migration', 'credential', %llu,"
10920  " (SELECT uuid FROM credentials WHERE id=%llu),"
10921  " " G_STRINGIFY (LOCATION_TABLE) ", subject_type, subject,"
10922  " subject_location, m_now (), m_now ()"
10923  " FROM permissions"
10924  " WHERE resource = %llu"
10925  " AND resource_type = 'slave'"
10926  " AND resource_location = " G_STRINGIFY (LOCATION_TABLE)
10927  " GROUP BY owner, subject_type, subject, subject_location;",
10928  new_credential, new_credential, slave);
10929 
10930  sql ("INSERT INTO"
10931  " permissions_trash (uuid, owner, name,"
10932  " comment, resource_type, resource,"
10933  " resource_uuid,"
10934  " resource_location, subject_type, subject,"
10935  " subject_location,"
10936  " creation_time, modification_time)"
10937  " SELECT make_uuid(), owner, 'get_credentials',"
10938  " 'Autogenerated by Slave migration', 'credential', %llu,"
10939  " (SELECT uuid FROM credentials WHERE id=%llu),"
10940  " " G_STRINGIFY (LOCATION_TABLE) ", subject_type, subject,"
10941  " subject_location, m_now (), m_now ()"
10942  " FROM permissions_trash"
10943  " WHERE resource = %llu"
10944  " AND resource_type = 'slave'"
10945  " AND resource_location = " G_STRINGIFY (LOCATION_TABLE)
10946  " GROUP BY owner, subject_type, subject, subject_location;",
10947  new_credential, new_credential, slave);
10948 
10949  g_free (quoted_name);
10950  g_free (quoted_login);
10951  }
10952  cleanup_iterator (&slaves);
10953 
10954  /* Create new credentials for trashcan. */
10955  init_iterator (&slaves,
10956  "SELECT id, name, login, password, owner"
10957  " FROM slaves_trash;");
10958 
10959  while (next (&slaves))
10960  {
10961  resource_t slave;
10962  const char *name, *login, *password;
10963  user_t owner;
10964  credential_t new_credential;
10965  gchar *quoted_name, *quoted_login;
10966 
10967  slave = iterator_int64 (&slaves, 0);
10968  name = iterator_string (&slaves, 1);
10969  login = iterator_string (&slaves, 2);
10970  password = iterator_string (&slaves, 3);
10971  owner = iterator_int64 (&slaves, 4);
10972 
10973  quoted_name = sql_quote (name);
10974  quoted_login = sql_quote (login);
10975 
10976  sql ("INSERT INTO credentials_trash"
10977  " (uuid, name, owner, comment, type,"
10978  " creation_time, modification_time)"
10979  " VALUES"
10980  " (make_uuid (), 'Credential for Slave %s',"
10981  " %llu, 'Autogenerated by migration', 'up',"
10982  " m_now (), m_now ());",
10983  quoted_name, owner);
10984 
10985  new_credential = sql_last_insert_id ();
10986 
10987  sql ("UPDATE slaves_trash SET credential = %llu,"
10988  " credential_location = " G_STRINGIFY (LOCATION_TRASH)
10989  " WHERE id = %llu;",
10990  new_credential, slave);
10991 
10992  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
10993  " VALUES (%llu, 'username', '%s');",
10994  new_credential, quoted_login);
10995 
10997  {
10998  gchar *quoted_password;
10999  quoted_password = sql_quote (password);
11000  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
11001  " VALUES (%llu, 'password', '%s');",
11002  new_credential, quoted_password);
11003  g_free (quoted_password);
11004  }
11005  else
11006  {
11007  char *secret;
11008  gchar *quoted_secret;
11009 
11010  if (!slaves.crypt_ctx)
11011  slaves.crypt_ctx = lsc_crypt_new ();
11012 
11013  secret = lsc_crypt_encrypt (slaves.crypt_ctx,
11014  "password", password, NULL);
11015  if (!secret)
11016  {
11017  g_free (quoted_name);
11018  g_free (quoted_login);
11019  cleanup_iterator (&slaves);
11020  sql_rollback ();
11021  return -1;
11022  }
11023  quoted_secret = sql_quote (secret);
11024  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
11025  " VALUES (%llu, 'secret', '%s');",
11026  new_credential, quoted_secret);
11027  g_free (quoted_secret);
11028  }
11029 
11030  sql ("INSERT INTO"
11031  " permissions (uuid, owner, name,"
11032  " comment, resource_type, resource,"
11033  " resource_uuid,"
11034  " resource_location, subject_type, subject,"
11035  " subject_location,"
11036  " creation_time, modification_time)"
11037  " SELECT make_uuid(), owner, 'get_credentials',"
11038  " 'Autogenerated by Slave migration', 'credential', %llu,"
11039  " (SELECT uuid FROM credentials_trash WHERE id=%llu),"
11040  " " G_STRINGIFY (LOCATION_TRASH) ", subject_type, subject,"
11041  " subject_location,"
11042  " m_now (), m_now ()"
11043  " FROM permissions"
11044  " WHERE resource = %llu"
11045  " AND resource_type = 'slave'"
11046  " AND resource_location = " G_STRINGIFY (LOCATION_TRASH)
11047  " GROUP BY owner, subject_type, subject, subject_location;",
11048  new_credential, new_credential, slave);
11049 
11050  sql ("INSERT INTO"
11051  " permissions_trash (uuid, owner, name,"
11052  " comment, resource_type, resource,"
11053  " resource_uuid,"
11054  " resource_location, subject_type, subject,"
11055  " subject_location,"
11056  " creation_time, modification_time)"
11057  " SELECT make_uuid(), owner, 'get_credentials',"
11058  " 'Autogenerated by Slave migration', 'credential', %llu,"
11059  " (SELECT uuid FROM credentials_trash WHERE id=%llu),"
11060  " " G_STRINGIFY (LOCATION_TRASH) ", subject_type, subject,"
11061  " subject_location,"
11062  " m_now (), m_now ()"
11063  " FROM permissions_trash"
11064  " WHERE resource = %llu"
11065  " AND resource_type = 'slave'"
11066  " AND resource_location = " G_STRINGIFY (LOCATION_TRASH)
11067  " GROUP BY owner, subject_type, subject, subject_location;",
11068  new_credential, new_credential, slave);
11069 
11070  g_free (quoted_name);
11071  g_free (quoted_login);
11072  }
11073  cleanup_iterator (&slaves);
11074 
11075  /* Remove unused columns */
11076  if (sql_is_sqlite3 ())
11077  {
11078  sql ("ALTER TABLE slaves RENAME TO slaves_156;");
11079  sql ("ALTER TABLE slaves_trash RENAME TO slaves_trash_156;");
11080 
11081  sql ("CREATE TABLE IF NOT EXISTS slaves"
11082  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment, host,"
11083  " port, creation_time, modification_time, credential INTEGER);");
11084  sql ("CREATE TABLE IF NOT EXISTS slaves_trash"
11085  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment, host,"
11086  " port, creation_time, modification_time, credential INTEGER,"
11087  " credential_location INTEGER);");
11088 
11089  sql ("INSERT INTO slaves"
11090  " (id, uuid, owner, name, comment, host, port,"
11091  " creation_time, modification_time, credential)"
11092  " SELECT id, uuid, owner, name, comment, host, port,"
11093  " creation_time, modification_time, credential"
11094  " FROM slaves_156;");
11095  sql ("INSERT INTO slaves_trash"
11096  " (id, uuid, owner, name, comment, host, port,"
11097  " creation_time, modification_time, credential,"
11098  " credential_location)"
11099  " SELECT id, uuid, owner, name, comment, host, port,"
11100  " creation_time, modification_time, credential,"
11101  " credential_location"
11102  " FROM slaves_trash_156;");
11103 
11104  sql ("DROP TABLE slaves_156;");
11105  sql ("DROP TABLE slaves_trash_156;");
11106  }
11107  else
11108  {
11109  sql ("ALTER TABLE slaves DROP COLUMN login;");
11110  sql ("ALTER TABLE slaves DROP COLUMN password;");
11111  sql ("ALTER TABLE slaves_trash DROP COLUMN login;");
11112  sql ("ALTER TABLE slaves_trash DROP COLUMN password;");
11113  }
11114 
11115  /* Set the database version to 157. */
11116 
11117  set_db_version (157);
11118 
11119  sql_commit ();
11120 
11121  return 0;
11122 }
11123 
11129 int
11131 {
11133 
11134  /* Ensure that the database is currently version 157. */
11135 
11136  if (manage_db_version () != 157)
11137  {
11138  sql_rollback ();
11139  return -1;
11140  }
11141 
11142  /* Update the database. */
11143 
11144  /* Add new column to configs tables. */
11145  if (sql_is_sqlite3 ())
11146  {
11147  sql ("ALTER TABLE configs ADD COLUMN scanner INTEGER;");
11148  sql ("ALTER TABLE configs_trash ADD COLUMN scanner INTEGER;");
11149  }
11150  else
11151  {
11152  sql ("ALTER TABLE configs ADD COLUMN scanner INTEGER"
11153  " REFERENCES scanners (id) ON DELETE RESTRICT;");
11154  sql ("ALTER TABLE configs_trash ADD COLUMN scanner INTEGER"
11155  " REFERENCES scanners (id) ON DELETE RESTRICT;");
11156  }
11157 
11158  /* Add first OSP scanner in scanners table, as scanner of OSP configs. */
11159  sql ("UPDATE configs"
11160  " SET scanner = (SELECT id FROM scanners WHERE type = %d LIMIT 1)"
11161  " WHERE type = 1;", SCANNER_TYPE_OSP);
11162 
11163  /* Set the database version to 158. */
11164 
11165  set_db_version (158);
11166 
11167  sql_commit ();
11168 
11169  return 0;
11170 }
11171 
11177 int
11179 {
11180  iterator_t scanners;
11182 
11183  /* Ensure that the database is currently version 158. */
11184 
11185  if (manage_db_version () != 158)
11186  {
11187  sql_rollback ();
11188  return -1;
11189  }
11190 
11191  /* Update the database. */
11192 
11193  /* Add new columns to scanners tables. */
11194  if (sql_is_sqlite3 ())
11195  {
11196  sql ("ALTER TABLE scanners ADD COLUMN credential INTEGER;");
11197  }
11198  else
11199  {
11200  sql ("ALTER TABLE scanners ADD COLUMN credential INTEGER"
11201  " REFERENCES credentials (id) ON DELETE RESTRICT;");
11202  }
11203  sql ("ALTER TABLE scanners_trash ADD COLUMN credential INTEGER;");
11204  sql ("ALTER TABLE scanners_trash ADD COLUMN credential_location INTEGER;");
11205 
11206  /* Create new credentials */
11207  init_iterator (&scanners,
11208  "SELECT id, name, key_pub, key_priv, owner FROM scanners;");
11209 
11210  while (next (&scanners))
11211  {
11212  scanner_t scanner;
11213  const char *name, *key_pub, *key_priv;
11214  user_t owner;
11215  credential_t new_credential;
11216  gchar *quoted_name, *quoted_key_pub;
11217 
11218  scanner = iterator_int64 (&scanners, 0);
11219  name = iterator_string (&scanners, 1);
11220  key_pub = iterator_string (&scanners, 2);
11221  key_priv = iterator_string (&scanners, 3);
11222  owner = iterator_int64 (&scanners, 4);
11223 
11224  // Skip if scanner has no key (internal CVE scanner)
11225  if (key_pub == NULL || key_priv == NULL)
11226  continue;
11227 
11228  quoted_name = sql_quote (name);
11229  quoted_key_pub = sql_quote (key_pub);
11230 
11231  if (owner)
11232  {
11233  if (sql_int ("SELECT count(*) FROM credentials"
11234  " WHERE name = 'Credential for Scanner %s'"
11235  " AND owner = %llu;",
11236  quoted_name, owner))
11237  sql ("INSERT INTO credentials"
11238  " (uuid, name, owner, comment, type,"
11239  " creation_time, modification_time)"
11240  " VALUES"
11241  " (make_uuid (),"
11242  " uniquify ('credential',"
11243  " 'Credential for Scanner %s', %llu, ''),"
11244  " %llu, 'Autogenerated by migration', 'cc',"
11245  " m_now (), m_now ());",
11246  quoted_name, owner, owner);
11247  else
11248  sql ("INSERT INTO credentials"
11249  " (uuid, name, owner, comment, type,"
11250  " creation_time, modification_time)"
11251  " VALUES"
11252  " (make_uuid (), 'Credential for Scanner %s',"
11253  " %llu, 'Autogenerated by migration', 'cc',"
11254  " m_now (), m_now ());",
11255  quoted_name, owner);
11256  }
11257  else
11258  {
11259  if (sql_int ("SELECT count(*) FROM credentials"
11260  " WHERE name = 'Credential for Scanner %s'"
11261  " AND owner = NULL;",
11262  quoted_name, owner))
11263  sql ("INSERT INTO credentials"
11264  " (uuid, name, owner, comment, type,"
11265  " creation_time, modification_time)"
11266  " VALUES"
11267  " (make_uuid (),"
11268  " uniquify ('credential',"
11269  " 'Credential for Scanner %s', NULL, ''),"
11270  " NULL, 'Autogenerated by migration', 'cc',"
11271  " m_now (), m_now ());",
11272  quoted_name);
11273  else
11274  sql ("INSERT INTO credentials"
11275  " (uuid, name, owner, comment, type,"
11276  " creation_time, modification_time)"
11277  " VALUES"
11278  " (make_uuid (), 'Credential for Scanner %s',"
11279  " NULL, 'Autogenerated by migration', 'cc',"
11280  " m_now (), m_now ());",
11281  quoted_name);
11282  }
11283 
11284  new_credential = sql_last_insert_id ();
11285 
11286  sql ("UPDATE scanners SET credential = %llu WHERE id = %llu;",
11287  new_credential, scanner);
11288 
11289  sql ("INSERT INTO credentials_data (credential, type, value)"
11290  " VALUES (%llu, 'certificate', '%s');",
11291  new_credential, quoted_key_pub);
11292 
11294  {
11295  gchar *quoted_key_priv;
11296  quoted_key_priv = sql_quote (key_priv);
11297  sql ("INSERT INTO credentials_data (credential, type, value)"
11298  " VALUES (%llu, 'private_key', '%s');",
11299  new_credential, quoted_key_priv);
11300  g_free (quoted_key_priv);
11301  }
11302  else
11303  {
11304  char *secret;
11305  gchar *quoted_secret;
11306 
11307  if (!scanners.crypt_ctx)
11308  scanners.crypt_ctx = lsc_crypt_new ();
11309 
11310  secret = lsc_crypt_encrypt (scanners.crypt_ctx,
11311  "private_key", key_priv, NULL);
11312  if (!secret)
11313  {
11314  g_free (quoted_name);
11315  g_free (quoted_key_pub);
11316  cleanup_iterator (&scanners);
11317  sql_rollback ();
11318  return -1;
11319  }
11320  quoted_secret = sql_quote (secret);
11321  sql ("INSERT INTO credentials_data (credential, type, value)"
11322  " VALUES (%llu, 'secret', '%s');",
11323  new_credential, quoted_secret);
11324  g_free (quoted_secret);
11325  }
11326 
11327  sql ("INSERT INTO"
11328  " permissions (uuid, owner, name,"
11329  " comment, resource_type, resource,"
11330  " resource_uuid,"
11331  " resource_location, subject_type, subject,"
11332  " subject_location, creation_time, modification_time)"
11333  " SELECT make_uuid(), owner, 'get_credentials',"
11334  " 'Autogenerated by Scanner migration', 'credential', %llu,"
11335  " (SELECT uuid FROM credentials WHERE id=%llu),"
11336  " " G_STRINGIFY (LOCATION_TABLE) ", subject_type, subject,"
11337  " subject_location, m_now (), m_now ()"
11338  " FROM permissions"
11339  " WHERE resource = %llu"
11340  " AND resource_type = 'scanner'"
11341  " AND resource_location = " G_STRINGIFY (LOCATION_TABLE)
11342  " GROUP BY owner, subject_type, subject, subject_location;",
11343  new_credential, new_credential, scanner);
11344 
11345  sql ("INSERT INTO"
11346  " permissions_trash (uuid, owner, name,"
11347  " comment, resource_type, resource,"
11348  " resource_uuid,"
11349  " resource_location, subject_type, subject,"
11350  " subject_location,"
11351  " creation_time, modification_time)"
11352  " SELECT make_uuid(), owner, 'get_credentials',"
11353  " 'Autogenerated by Scanner migration', 'credential', %llu,"
11354  " (SELECT uuid FROM credentials WHERE id=%llu),"
11355  " " G_STRINGIFY (LOCATION_TABLE) ", subject_type, subject,"
11356  " subject_location, m_now (), m_now ()"
11357  " FROM permissions_trash"
11358  " WHERE resource = %llu"
11359  " AND resource_type = 'scanner'"
11360  " AND resource_location = " G_STRINGIFY (LOCATION_TABLE)
11361  " GROUP BY owner, subject_type, subject, subject_location;",
11362  new_credential, new_credential, scanner);
11363 
11364  g_free (quoted_name);
11365  g_free (quoted_key_pub);
11366  }
11367  cleanup_iterator (&scanners);
11368 
11369  /* Create new credentials for trashcan. */
11370  init_iterator (&scanners,
11371  "SELECT id, name, key_pub, key_priv, owner"
11372  " FROM scanners_trash;");
11373 
11374  while (next (&scanners))
11375  {
11376  scanner_t scanner;
11377  const char *name, *key_pub, *key_priv;
11378  user_t owner;
11379  credential_t new_credential;
11380  gchar *quoted_name, *quoted_key_pub;
11381 
11382  scanner = iterator_int64 (&scanners, 0);
11383  name = iterator_string (&scanners, 1);
11384  key_pub = iterator_string (&scanners, 2);
11385  key_priv = iterator_string (&scanners, 3);
11386  owner = iterator_int64 (&scanners, 4);
11387 
11388  /* Skip if scanner has no key (internal CVE scanner). */
11389  if (key_pub == NULL || key_priv == NULL)
11390  continue;
11391 
11392  quoted_name = sql_quote (name);
11393  quoted_key_pub = sql_quote (key_pub);
11394 
11395  if (owner)
11396  sql ("INSERT INTO credentials_trash"
11397  " (uuid, name, owner, comment, type,"
11398  " creation_time, modification_time)"
11399  " VALUES"
11400  " (make_uuid (), 'Credential for Scanner %s',"
11401  " %llu, 'Autogenerated by migration', 'cc',"
11402  " m_now (), m_now ());",
11403  quoted_name, owner);
11404  else
11405  sql ("INSERT INTO credentials_trash"
11406  " (uuid, name, owner, comment, type,"
11407  " creation_time, modification_time)"
11408  " VALUES"
11409  " (make_uuid (), 'Credential for Scanner %s',"
11410  " NULL, 'Autogenerated by migration', 'cc',"
11411  " m_now (), m_now ());",
11412  quoted_name);
11413 
11414  new_credential = sql_last_insert_id ();
11415 
11416  sql ("UPDATE scanners_trash SET credential = %llu,"
11417  " credential_location = " G_STRINGIFY (LOCATION_TRASH)
11418  " WHERE id = %llu;",
11419  new_credential, scanner);
11420 
11421  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
11422  " VALUES (%llu, 'certificate', '%s');",
11423  new_credential, quoted_key_pub);
11424 
11426  {
11427  gchar *quoted_key_priv;
11428  quoted_key_priv = sql_quote (key_priv);
11429  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
11430  " VALUES (%llu, 'private_key', '%s');",
11431  new_credential, quoted_key_priv);
11432  g_free (quoted_key_priv);
11433  }
11434  else
11435  {
11436  char *secret;
11437  gchar *quoted_secret;
11438 
11439  if (!scanners.crypt_ctx)
11440  scanners.crypt_ctx = lsc_crypt_new ();
11441 
11442  secret = lsc_crypt_encrypt (scanners.crypt_ctx,
11443  "private_key", key_priv, NULL);
11444  if (!secret)
11445  {
11446  g_free (quoted_name);
11447  g_free (quoted_key_pub);
11448  cleanup_iterator (&scanners);
11449  sql_rollback ();
11450  return -1;
11451  }
11452  quoted_secret = sql_quote (secret);
11453  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
11454  " VALUES (%llu, 'secret', '%s');",
11455  new_credential, quoted_secret);
11456  g_free (quoted_secret);
11457  }
11458 
11459  sql ("INSERT INTO"
11460  " permissions (uuid, owner, name,"
11461  " comment, resource_type, resource,"
11462  " resource_uuid,"
11463  " resource_location, subject_type, subject,"
11464  " subject_location,"
11465  " creation_time, modification_time)"
11466  " SELECT make_uuid(), owner, 'get_credentials',"
11467  " 'Autogenerated by Scanner migration', 'credential', %llu,"
11468  " (SELECT uuid FROM credentials_trash WHERE id=%llu),"
11469  " " G_STRINGIFY (LOCATION_TRASH) ", subject_type, subject,"
11470  " subject_location,"
11471  " m_now (), m_now ()"
11472  " FROM permissions"
11473  " WHERE resource = %llu"
11474  " AND resource_type = 'scanner'"
11475  " AND resource_location = " G_STRINGIFY (LOCATION_TRASH)
11476  " GROUP BY owner, subject_type, subject, subject_location;",
11477  new_credential, new_credential, scanner);
11478 
11479  sql ("INSERT INTO"
11480  " permissions_trash (uuid, owner, name,"
11481  " comment, resource_type, resource,"
11482  " resource_uuid,"
11483  " resource_location, subject_type, subject,"
11484  " subject_location,"
11485  " creation_time, modification_time)"
11486  " SELECT make_uuid(), owner, 'get_credentials',"
11487  " 'Autogenerated by Scanner migration', 'credential', %llu,"
11488  " (SELECT uuid FROM credentials_trash WHERE id=%llu),"
11489  " " G_STRINGIFY (LOCATION_TRASH) ", subject_type, subject,"
11490  " subject_location,"
11491  " m_now (), m_now ()"
11492  " FROM permissions_trash"
11493  " WHERE resource = %llu"
11494  " AND resource_type = 'scanner'"
11495  " AND resource_location = " G_STRINGIFY (LOCATION_TRASH)
11496  " GROUP BY owner, subject_type, subject, subject_location;",
11497  new_credential, new_credential, scanner);
11498 
11499  g_free (quoted_name);
11500  g_free (quoted_key_pub);
11501  }
11502  cleanup_iterator (&scanners);
11503 
11504  /* Remove unused columns. */
11505  if (sql_is_sqlite3 ())
11506  {
11507  sql ("ALTER TABLE scanners RENAME TO scanners_158;");
11508  sql ("ALTER TABLE scanners_trash RENAME TO scanners_trash_158;");
11509 
11510  sql ("CREATE TABLE IF NOT EXISTS scanners"
11511  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment,"
11512  " host, port, type, ca_pub, credential INTEGER,"
11513  " creation_time, modification_time);");
11514  sql ("CREATE TABLE IF NOT EXISTS scanners_trash"
11515  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment,"
11516  " host, port, type, ca_pub, credential INTEGER,"
11517  " credential_location INTEGER, creation_time, modification_time);");
11518 
11519  sql ("INSERT INTO scanners"
11520  " (id, uuid, owner, name, comment, host, port, type,"
11521  " ca_pub, credential, creation_time, modification_time)"
11522  " SELECT id, uuid, owner, name, comment, host, port, type,"
11523  " ca_pub, credential, creation_time, modification_time"
11524  " FROM scanners_158;");
11525  sql ("INSERT INTO scanners_trash"
11526  " (id, uuid, owner, name, comment, host, port, type,"
11527  " ca_pub, credential, credential_location, creation_time,"
11528  " modification_time)"
11529  " SELECT id, uuid, owner, name, comment, host, port, type,"
11530  " ca_pub, credential, credential_location, creation_time,"
11531  " modification_time"
11532  " FROM scanners_trash_158;");
11533 
11534  sql ("DROP TABLE scanners_158;");
11535  sql ("DROP TABLE scanners_trash_158;");
11536  }
11537  else
11538  {
11539  sql ("ALTER TABLE scanners DROP COLUMN key_pub;");
11540  sql ("ALTER TABLE scanners DROP COLUMN key_priv;");
11541  sql ("ALTER TABLE scanners_trash DROP COLUMN key_pub;");
11542  sql ("ALTER TABLE scanners_trash DROP COLUMN key_priv;");
11543  }
11544 
11545  /* Set the database version to 159. */
11546 
11547  set_db_version (159);
11548 
11549  sql_commit ();
11550 
11551  return 0;
11552 }
11553 
11559 int
11561 {
11563 
11564  /* Ensure that the database is currently version 159. */
11565 
11566  if (manage_db_version () != 159)
11567  {
11568  sql_rollback ();
11569  return -1;
11570  }
11571 
11572  /* Update the database. */
11573 
11574  /* Report format "Verinice ISM" was missing a param. */
11575 
11576  sql ("INSERT INTO report_format_params (report_format, name, type, value,"
11577  " type_min, type_max, type_regex, fallback)"
11578  " VALUES ((SELECT id FROM report_formats"
11579  " WHERE uuid = 'c15ad349-bd8d-457a-880a-c7056532ee15'),"
11580  " 'Attach HTML report', %i, 1, 0, 1, '', 1);",
11582 
11583  /* Set the database version to 160. */
11584 
11585  set_db_version (160);
11586 
11587  sql_commit ();
11588 
11589  return 0;
11590 }
11591 
11597 int
11599 {
11600  iterator_t iter;
11601  iter.crypt_ctx = NULL;
11602 
11604 
11605  /* Ensure that the database is currently version 160. */
11606 
11607  if (manage_db_version () != 160)
11608  {
11609  sql_rollback ();
11610  return -1;
11611  }
11612 
11613  /* Update the database. */
11614 
11615  /* Create copies of SSH key credentials that are used in place of
11616  * username + password ones. */
11617  init_iterator (&iter,
11618  "SELECT 0, id, name, owner,"
11619  " (SELECT value FROM credentials_data"
11620  " WHERE credential = credentials.id"
11621  " AND type = 'secret'),"
11622  " (SELECT value FROM credentials_data"
11623  " WHERE credential = credentials.id"
11624  " AND type = 'password')"
11625  " FROM credentials"
11626  " WHERE type = 'usk'"
11627  " AND (id IN (SELECT credential"
11628  " FROM targets_login_data"
11629  " WHERE type='smb' OR type='esxi')"
11630  " OR id IN (SELECT credential"
11631  " FROM targets_trash_login_data"
11632  " WHERE (type='smb' OR type='esxi')"
11633  " AND credential_location"
11634  " = " G_STRINGIFY (LOCATION_TABLE) "))"
11635  " UNION ALL"
11636  " SELECT 1, id, name, owner,"
11637  " (SELECT value FROM credentials_trash_data"
11638  " WHERE credential = credentials_trash.id"
11639  " AND type = 'secret'),"
11640  " (SELECT value FROM credentials_trash_data"
11641  " WHERE credential = credentials_trash.id"
11642  " AND type = 'password')"
11643  " FROM credentials_trash"
11644  " WHERE type = 'usk'"
11645  " AND id IN (SELECT credential"
11646  " FROM targets_trash_login_data"
11647  " WHERE (type='smb' OR type='esxi')"
11648  " AND credential_location"
11649  " = " G_STRINGIFY (LOCATION_TRASH) ");");
11650 
11651  while (next (&iter))
11652  {
11653  int trash;
11654  credential_t credential, new_credential;
11655  const char *name, *old_secret, *old_password;
11656  gchar* quoted_name;
11657  user_t owner;
11658 
11659  trash = iterator_int (&iter, 0);
11660  credential = iterator_int64 (&iter, 1);
11661  name = iterator_string (&iter, 2);
11662  quoted_name = sql_quote (name);
11663  owner = iterator_int64 (&iter, 3);
11664  old_secret = iterator_string (&iter, 4);
11665 
11666  // Copy credential base
11667  if (trash)
11668  {
11669  sql ("INSERT INTO credentials_trash"
11670  " (uuid, name, owner, comment, type,"
11671  " creation_time, modification_time)"
11672  " VALUES"
11673  " (make_uuid (), '%s - user and password',"
11674  " %llu, 'Autogenerated by migration', 'up',"
11675  " m_now (), m_now ());",
11676  quoted_name, owner);
11677  }
11678  else
11679  {
11680  if (sql_int ("SELECT count(*) FROM credentials"
11681  " WHERE name = '%s - user and password'"
11682  " AND owner = %llu;",
11683  quoted_name, owner))
11684  sql ("INSERT INTO credentials"
11685  " (uuid, name, owner, comment, type,"
11686  " creation_time, modification_time)"
11687  " VALUES"
11688  " (make_uuid (),"
11689  " uniquify ('credential', '%s - user and password', %llu, ''),"
11690  " %llu, 'Autogenerated by migration', 'up',"
11691  " m_now (), m_now ());",
11692  quoted_name, owner, owner);
11693  else
11694  sql ("INSERT INTO credentials"
11695  " (uuid, name, owner, comment, type,"
11696  " creation_time, modification_time)"
11697  " VALUES"
11698  " (make_uuid (), '%s - user and password',"
11699  " %llu, 'Autogenerated by migration', 'up',"
11700  " m_now (), m_now ());",
11701  quoted_name, owner);
11702  }
11703 
11704  new_credential = sql_last_insert_id ();
11705 
11706  // Copy username
11707  sql ("INSERT INTO %s (credential, type, value)"
11708  " SELECT %llu, 'username', value FROM %s"
11709  " WHERE credential = %llu AND type = 'username'",
11710  trash ? "credentials_trash_data" : "credentials_data",
11711  new_credential,
11712  trash ? "credentials_trash_data" : "credentials_data",
11713  credential);
11714 
11715  // Copy password
11716  if (iter.crypt_ctx == NULL)
11717  iter.crypt_ctx = lsc_crypt_new ();
11718 
11719  if (old_secret)
11720  old_password = lsc_crypt_get_password (iter.crypt_ctx, old_secret);
11721  else
11722  old_password = iterator_string (&iter, 5);
11723 
11725  {
11726  gchar *quoted_password = sql_quote (old_password ? old_password : "");
11727  sql ("INSERT INTO %s (credential, type, value)"
11728  " VALUES (%llu, 'password', '%s');",
11729  trash ? "credentials_trash_data" : "credentials_data",
11730  new_credential,
11731  quoted_password);
11732  g_free (quoted_password);
11733  }
11734  else
11735  {
11736  lsc_crypt_ctx_t encrypt_ctx = lsc_crypt_new ();
11737  gchar *new_secret = lsc_crypt_encrypt (encrypt_ctx,
11738  "password", old_password,
11739  NULL);
11740  sql ("INSERT INTO %s (credential, type, value)"
11741  " VALUES (%llu, 'password', '%s');",
11742  trash ? "credentials_trash_data" : "credentials_data",
11743  new_credential,
11744  new_secret);
11745  lsc_crypt_release (encrypt_ctx);
11746  g_free (new_secret);
11747  }
11748 
11749  // Update targets
11750  if (trash)
11751  {
11752  sql ("UPDATE targets_trash_login_data SET credential = %llu"
11753  " WHERE credential = %llu"
11754  " AND (type = 'smb' OR type = 'esxi')"
11755  " AND credential_location = " G_STRINGIFY (LOCATION_TRASH) ";",
11756  new_credential, credential);
11757  }
11758  else
11759  {
11760  sql ("UPDATE targets_login_data SET credential = %llu"
11761  " WHERE credential = %llu"
11762  " AND (type = 'smb' OR type = 'esxi');",
11763  new_credential, credential);
11764  sql ("UPDATE targets_trash_login_data SET credential = %llu"
11765  " WHERE credential = %llu"
11766  " AND (type = 'smb' OR type = 'esxi')"
11767  " AND credential_location = " G_STRINGIFY (LOCATION_TABLE) ";",
11768  new_credential, credential);
11769  }
11770 
11771  }
11772  cleanup_iterator(&iter);
11773 
11774  /* Set the database version to 161. */
11775 
11776  set_db_version (161);
11777 
11778  sql_commit ();
11779 
11780  return 0;
11781 }
11782 
11788 int
11790 {
11792 
11793  /* Ensure that the database is currently version 161. */
11794 
11795  if (manage_db_version () != 161)
11796  {
11797  sql_rollback ();
11798  return -1;
11799  }
11800 
11801  /* Update the database. */
11802 
11803  /* Add allow_insecure column to credentials and credentials_trash */
11804  sql ("ALTER TABLE credentials ADD COLUMN allow_insecure INTEGER;");
11805  sql ("ALTER TABLE credentials_trash ADD COLUMN allow_insecure INTEGER;");
11806 
11807  /* Set the value of the new column */
11808  sql ("UPDATE credentials SET allow_insecure = 0;");
11809  sql ("UPDATE credentials_trash SET allow_insecure = 0;");
11810 
11811  /* Set the database version to 162. */
11812 
11813  set_db_version (162);
11814 
11815  sql_commit ();
11816 
11817  return 0;
11818 }
11819 
11823 #define MIGRATE_162_TO_163_CONTROL_DESCRIPTION \
11824  "Dear IS Coordinator,\n" \
11825  "\n" \
11826  "A new scan has been carried out and the results are now available in Verinice.\n" \
11827  "If responsible persons are linked to the asset groups, the tasks are already created.\n" \
11828  "\n" \
11829  "Please check the results in a timely manner.\n" \
11830  "\n" \
11831  "Best regards\n" \
11832  "CIS"
11833 
11839 int
11841 {
11843 
11844  /* Ensure that the database is currently version 162. */
11845 
11846  if (manage_db_version () != 162)
11847  {
11848  sql_rollback ();
11849  return -1;
11850  }
11851 
11852  /* Update the database. */
11853 
11854  /* Report format "Verinice ISM" got a new param. */
11855 
11856  sql ("INSERT INTO report_format_params (report_format, name, type, value,"
11857  " type_min, type_max, type_regex, fallback)"
11858  " VALUES ((SELECT id FROM report_formats"
11859  " WHERE uuid = 'c15ad349-bd8d-457a-880a-c7056532ee15'),"
11860  " 'ISM Control Description', %i, '%s', 0, 100000, '', '%s');",
11864 
11865  /* Set the database version to 163. */
11866 
11867  set_db_version (163);
11868 
11869  sql_commit ();
11870 
11871  return 0;
11872 }
11873 
11874 
11875 #define UPDATE_CHART_SETTINGS(type, default, left_uuid, right_uuid) \
11876  sql ("INSERT INTO settings (owner, uuid, name, value)" \
11877  " SELECT owner, '%s', 'Dummy', 'left-' || '%s' FROM settings" \
11878  " WHERE uuid = '%s'" \
11879  " AND NOT EXISTS (SELECT * FROM settings AS old_settings" \
11880  " WHERE old_settings.uuid = '%s'" \
11881  " AND old_settings.owner = settings.owner);", \
11882  left_uuid, default, right_uuid, left_uuid); \
11883  sql ("UPDATE settings" \
11884  " SET name = '%s Top Dashboard Components'," \
11885  " value = coalesce ((SELECT substr (old_settings.value, 6)" \
11886  " FROM settings AS old_settings" \
11887  " WHERE old_settings.uuid = '%s'" \
11888  " AND old_settings.owner = settings.owner)," \
11889  " '" default "')" \
11890  " || '|'" \
11891  " || coalesce ((SELECT substr (old_settings.value, 7)" \
11892  " FROM settings AS old_settings" \
11893  " WHERE old_settings.uuid = '%s'" \
11894  " AND old_settings.owner = settings.owner),"\
11895  " '" default "')" \
11896  " WHERE uuid = '%s';", \
11897  type, left_uuid, right_uuid, left_uuid); \
11898  sql ("DELETE FROM settings" \
11899  " WHERE uuid = '%s';", \
11900  right_uuid);
11901 
11902 #define UPDATE_DASHBOARD_SETTINGS(type, default, \
11903  uuid_1, uuid_2, uuid_3, uuid_4, \
11904  filter_1, filter_2, filter_3, filter_4) \
11905  sql ("INSERT INTO settings (owner, uuid, name, value)" \
11906  " SELECT DISTINCT owner, '%s', 'dummy', '%s' FROM settings" \
11907  " WHERE uuid IN ('%s', '%s', '%s')" \
11908  " AND NOT EXISTS (SELECT * FROM settings AS old_settings" \
11909  " WHERE uuid = '%s'" \
11910  " AND old_settings.owner = settings.owner);", \
11911  uuid_1, default, uuid_2, uuid_3, uuid_4, uuid_1); \
11912  sql ("UPDATE settings" \
11913  " SET name = '%s Dashboard Components'," \
11914  " value = coalesce ((SELECT substr (old_settings.value," \
11915  " length ('" type "') + 4)" \
11916  " FROM settings AS old_settings" \
11917  " WHERE old_settings.uuid = '%s'" \
11918  " AND old_settings.owner = settings.owner)," \
11919  " '" default "')" \
11920  " || '|'" \
11921  " || coalesce ((SELECT substr (old_settings.value," \
11922  " length ('" type "') + 4)" \
11923  " FROM settings AS old_settings" \
11924  " WHERE old_settings.uuid = '%s'" \
11925  " AND old_settings.owner = settings.owner),"\
11926  " '" default "')" \
11927  " || '#'" \
11928  " || coalesce ((SELECT substr (old_settings.value," \
11929  " length ('" type "') + 4)" \
11930  " FROM settings AS old_settings" \
11931  " WHERE old_settings.uuid = '%s'" \
11932  " AND old_settings.owner = settings.owner),"\
11933  " '" default "')" \
11934  " || '|'" \
11935  " || coalesce ((SELECT substr (old_settings.value," \
11936  " length ('" type "') + 4)" \
11937  " FROM settings AS old_settings" \
11938  " WHERE old_settings.uuid = '%s'" \
11939  " AND old_settings.owner = settings.owner),"\
11940  " '" default "')" \
11941  " WHERE uuid = '%s';", \
11942  type, uuid_1, uuid_2, uuid_3, uuid_4, uuid_1); \
11943  sql ("INSERT INTO settings (owner, uuid, name, value)" \
11944  " SELECT DISTINCT owner, '%s', 'dummy', '' FROM settings" \
11945  " WHERE uuid IN ('%s', '%s', '%s')" \
11946  " AND NOT EXISTS (SELECT * FROM settings AS old_settings" \
11947  " WHERE uuid = '%s'" \
11948  " AND old_settings.owner = settings.owner);", \
11949  filter_1, filter_2, filter_3, filter_4, filter_1); \
11950  sql ("UPDATE settings" \
11951  " SET name = '%s Dashboard Filters'," \
11952  " value = coalesce ((SELECT old_settings.value" \
11953  " FROM settings AS old_settings" \
11954  " WHERE old_settings.uuid = '%s'" \
11955  " AND old_settings.owner = settings.owner)," \
11956  " '')" \
11957  " || '|'" \
11958  " || coalesce ((SELECT old_settings.value" \
11959  " FROM settings AS old_settings" \
11960  " WHERE old_settings.uuid = '%s'" \
11961  " AND old_settings.owner = settings.owner),"\
11962  " '')" \
11963  " || '#'" \
11964  " || coalesce ((SELECT old_settings.value" \
11965  " FROM settings AS old_settings" \
11966  " WHERE old_settings.uuid = '%s'" \
11967  " AND old_settings.owner = settings.owner),"\
11968  " '')" \
11969  " || '|'" \
11970  " || coalesce ((SELECT old_settings.value" \
11971  " FROM settings AS old_settings" \
11972  " WHERE old_settings.uuid = '%s'" \
11973  " AND old_settings.owner = settings.owner),"\
11974  " '')" \
11975  " WHERE uuid = '%s';", \
11976  type, filter_1, filter_2, filter_3, filter_4, filter_1); \
11977  sql ("DELETE FROM settings" \
11978  " WHERE uuid IN ('%s', '%s', '%s', '%s', '%s', '%s');", \
11979  uuid_2, uuid_3, uuid_4, filter_2, filter_3, filter_4);
11980 
11986 int
11988 {
11990 
11991  /* Ensure that the database is currently version 163. */
11992 
11993  if (manage_db_version () != 163)
11994  {
11995  sql_rollback ();
11996  return -1;
11997  }
11998 
11999  /* Update the database. */
12000 
12001  /* Remove duplicate settings */
12002  sql ("DELETE FROM settings"
12003  " WHERE id NOT IN (SELECT min(id) FROM settings"
12004  " GROUP BY uuid, owner);");
12005 
12006  /* Change top chart settings to new format */
12007  UPDATE_CHART_SETTINGS ("Tasks", "by-cvss",
12008  "3d5db3c7-5208-4b47-8c28-48efc621b1e0",
12009  "ce8608af-7e66-45a8-aa8a-76def4f9f838")
12010  UPDATE_CHART_SETTINGS ("Reports", "by-cvss",
12011  "e599bb6b-b95a-4bb2-a6bb-fe8ac69bc071",
12012  "fc875cd4-16bf-42d1-98ed-c0c9bd6015cd")
12013  UPDATE_CHART_SETTINGS ("Results", "by-cvss",
12014  "0b8ae70d-d8fc-4418-8a72-e65ac8d2828e",
12015  "cb7db2fe-3fe4-4704-9fa1-efd4b9e522a8")
12016 
12017  UPDATE_CHART_SETTINGS ("NVTs", "by-cvss",
12018  "f68d9369-1945-477b-968f-121c6029971b",
12019  "af89a84a-d3ec-43a8-97a8-aa688bf093bc")
12020  UPDATE_CHART_SETTINGS ("CVEs", "by-cvss",
12021  "815ddd2e-8654-46c7-a05b-d73224102240",
12022  "418a5746-d68a-4a2d-864a-0da993b32220")
12023  UPDATE_CHART_SETTINGS ("CPEs", "by-cvss",
12024  "9cff9b4d-b164-43ce-8687-f2360afc7500",
12025  "629fdb73-35fa-4247-9018-338c202f7c03")
12026  UPDATE_CHART_SETTINGS ("OVAL Definitions", "by-cvss",
12027  "9563efc0-9f4e-4d1f-8f8d-0205e32b90a4",
12028  "fe1610a3-4e87-4b0d-9b7a-f0f66fef586b")
12029  UPDATE_CHART_SETTINGS ("CERT Bund Advisories", "by-cvss",
12030  "a6946f44-480f-4f37-8a73-28a4cd5310c4",
12031  "469d50da-880a-4bfc-88ed-22e53764c683")
12032  UPDATE_CHART_SETTINGS ("DFN CERT Advisories", "by-cvss",
12033  "9812ea49-682d-4f99-b3cc-eca051d1ce59",
12034  "72014b52-4389-435d-9438-8c13601ecbd2")
12035  UPDATE_CHART_SETTINGS ("All SecInfo", "by-cvss",
12036  "4c7b1ea7-b7e6-4d12-9791-eb9f72b6f864",
12037  "985f38eb-1a30-4a35-abb6-3eec05b5d54a")
12038 
12039  /* Update standalone dashboard */
12040  UPDATE_DASHBOARD_SETTINGS ("SecInfo", "nvts-by-cvss",
12041  "84ab32da-fe69-44d8-8a8f-70034cf28d4e",
12042  "42d48049-3153-43bf-b30d-72ca5ab1eb49",
12043  "76f34fe0-254a-4481-97aa-c6f1da2f842b",
12044  "71106ed7-b677-414e-bf67-2e7716441db3",
12045  "517d0efe-426e-49a9-baa7-eda2832c93e8",
12046  "3c693fb2-4f87-4b1f-a09e-cb9aa66440f4",
12047  "bffa72a5-8110-49f9-aa5e-f431ce834826",
12048  "268079c6-f353-414f-9b7c-43f5419edf2d")
12049 
12050  /* Set the database version to 164. */
12051 
12052  set_db_version (164);
12053 
12054  sql_commit ();
12055 
12056  return 0;
12057 }
12058 
12064 int
12066 {
12068 
12069  /* Ensure that the database is currently version 164. */
12070 
12071  if (manage_db_version () != 164)
12072  {
12073  sql_rollback ();
12074  return -1;
12075  }
12076 
12077  /* Update database */
12078 
12079  /* Add hr_name column to config_preferences table
12080  * and initialize it with name for OSP results. */
12081  sql ("ALTER TABLE config_preferences ADD COLUMN hr_name TEXT;");
12082  sql ("UPDATE config_preferences"
12083  " SET hr_name = name"
12084  " WHERE type != 'SERVER_PREFS' AND type != 'PLUGINS_PREFS';");
12085 
12086  /* Add hr_name column to config_preferences_trash table
12087  * and initialize it with name for OSP results. */
12088  sql ("ALTER TABLE config_preferences_trash ADD COLUMN hr_name TEXT;");
12089  sql ("UPDATE config_preferences_trash"
12090  " SET hr_name = name"
12091  " WHERE type != 'SERVER_PREFS' AND type != 'PLUGINS_PREFS';");
12092 
12093  /* Set the database version to 165. */
12094 
12095  set_db_version (165);
12096 
12097  sql_commit ();
12098 
12099  return 0;
12100 }
12101 
12107 int
12109 {
12112 
12113  /* Ensure that the database is currently version 165. */
12114 
12115  if (manage_db_version () != 165)
12116  {
12117  sql_rollback ();
12118  return -1;
12119  }
12120 
12121  /* Update the database. */
12122 
12123  /* Create new credentials. */
12125  "SELECT id, name,"
12126  " (SELECT data FROM alert_method_data"
12127  " WHERE alert = alerts.id"
12128  " AND (name='scp_username'"
12129  " OR name='verinice_server_username')),"
12130  " (SELECT data FROM alert_method_data"
12131  " WHERE alert = alerts.id"
12132  " AND (name='scp_password'"
12133  " OR name='verinice_server_password')),"
12134  " owner, method"
12135  " FROM alerts WHERE method = 8 OR method = 6;");
12136 
12137  while (next (&alert_data))
12138  {
12139  alert_t alert;
12140  const char *name, *login, *password;
12141  user_t owner;
12142  credential_t new_credential;
12143  gchar *new_credential_id, *quoted_name, *quoted_login;
12144  int method;
12145 
12146  alert = iterator_int64 (&alert_data, 0);
12147  name = iterator_string (&alert_data, 1);
12148  login = iterator_string (&alert_data, 2);
12149  password = iterator_string (&alert_data, 3);
12150  owner = iterator_int64 (&alert_data, 4);
12151  method = iterator_int (&alert_data, 5);
12152 
12153  /* Skip the alert if it is missing login info. */
12154  if (name == NULL || password == NULL)
12155  continue;
12156 
12157  quoted_name = sql_quote (name);
12158  quoted_login = sql_quote (login);
12159 
12160  /* Create basic credential. */
12161  if (sql_int ("SELECT count(*) FROM credentials"
12162  " WHERE name = 'Credential for Alert %s'"
12163  " AND owner = %llu;",
12164  quoted_name, owner))
12165  sql ("INSERT INTO credentials"
12166  " (uuid, name, owner, comment, type,"
12167  " creation_time, modification_time)"
12168  " VALUES"
12169  " (make_uuid (),"
12170  " uniquify ('credential', 'Credential for Alert %s', %llu, ''),"
12171  " %llu, 'Autogenerated by migration', 'up',"
12172  " m_now (), m_now ());",
12173  quoted_name, owner, owner);
12174  else
12175  sql ("INSERT INTO credentials"
12176  " (uuid, name, owner, comment, type,"
12177  " creation_time, modification_time)"
12178  " VALUES"
12179  " (make_uuid (), 'Credential for Alert %s',"
12180  " %llu, 'Autogenerated by migration', 'up',"
12181  " m_now (), m_now ());",
12182  quoted_name, owner);
12183 
12184  /* Add credential data. */
12185  new_credential = sql_last_insert_id ();
12186  new_credential_id = sql_string ("SELECT uuid FROM credentials"
12187  " WHERE id = %llu;",
12188  new_credential);
12189 
12190  sql ("INSERT INTO credentials_data (credential, type, value)"
12191  " VALUES (%llu, 'username', '%s');",
12192  new_credential,
12193  quoted_login);
12194 
12196  {
12197  gchar *quoted_password;
12198  quoted_password = sql_quote (password);
12199  sql ("INSERT INTO credentials_data (credential, type, value)"
12200  " VALUES (%llu, 'password', '%s');",
12201  new_credential, quoted_password);
12202  g_free (quoted_password);
12203  }
12204  else
12205  {
12206  char *secret;
12207  gchar *quoted_secret;
12208 
12209  if (!alert_data.crypt_ctx)
12210  alert_data.crypt_ctx = lsc_crypt_new ();
12211 
12212  secret = lsc_crypt_encrypt (alert_data.crypt_ctx,
12213  "password", password, NULL);
12214  if (!secret)
12215  {
12216  g_free (quoted_name);
12217  g_free (quoted_login);
12219  sql_rollback ();
12220  return -1;
12221  }
12222  quoted_secret = sql_quote (secret);
12223  sql ("INSERT INTO credentials_data (credential, type, value)"
12224  " VALUES (%llu, 'secret', '%s');",
12225  new_credential, quoted_secret);
12226  g_free (quoted_secret);
12227  }
12228 
12229  /* Update alert_method_data. */
12230  sql ("INSERT INTO alert_method_data (alert, name, data)"
12231  " VALUES (%llu, '%s_credential', '%s');",
12232  alert,
12233  method == 8 ? "scp" : "verinice_server",
12234  new_credential_id);
12235 
12236  /* Create permissions. */
12237  sql ("INSERT INTO"
12238  " permissions (uuid, owner, name,"
12239  " comment, resource_type, resource,"
12240  " resource_uuid,"
12241  " resource_location, subject_type, subject,"
12242  " subject_location, creation_time, modification_time)"
12243  " SELECT make_uuid(), owner, 'get_credentials',"
12244  " 'Autogenerated by Alert migration', 'credential', %llu,"
12245  " (SELECT uuid FROM credentials WHERE id=%llu),"
12246  " " G_STRINGIFY (LOCATION_TABLE) ", subject_type, subject,"
12247  " subject_location, m_now (), m_now ()"
12248  " FROM permissions"
12249  " WHERE resource = %llu"
12250  " AND resource_type = 'alert'"
12251  " AND resource_location = " G_STRINGIFY (LOCATION_TABLE)
12252  " GROUP BY owner, subject_type, subject, subject_location;",
12253  new_credential, new_credential, alert);
12254 
12255  sql ("INSERT INTO"
12256  " permissions_trash (uuid, owner, name,"
12257  " comment, resource_type, resource,"
12258  " resource_uuid,"
12259  " resource_location, subject_type, subject,"
12260  " subject_location,"
12261  " creation_time, modification_time)"
12262  " SELECT make_uuid(), owner, 'get_credentials',"
12263  " 'Autogenerated by Alert migration', 'credential', %llu,"
12264  " (SELECT uuid FROM credentials WHERE id=%llu),"
12265  " " G_STRINGIFY (LOCATION_TABLE) ", subject_type, subject,"
12266  " subject_location, m_now (), m_now ()"
12267  " FROM permissions_trash"
12268  " WHERE resource = %llu"
12269  " AND resource_type = 'alert'"
12270  " AND resource_location = " G_STRINGIFY (LOCATION_TABLE)
12271  " GROUP BY owner, subject_type, subject, subject_location;",
12272  new_credential, new_credential, alert);
12273 
12274  g_free (new_credential_id);
12275  g_free (quoted_name);
12276  g_free (quoted_login);
12277  }
12279 
12280  /* Create new trash credentials. */
12282  "SELECT id, name,"
12283  " (SELECT data FROM alert_method_data_trash"
12284  " WHERE alert = alerts_trash.id"
12285  " AND (name='scp_username'"
12286  " OR name='verinice_server_username')),"
12287  " (SELECT data FROM alert_method_data_trash"
12288  " WHERE alert = alerts_trash.id"
12289  " AND (name='scp_password'"
12290  " OR name='verinice_server_password')),"
12291  " owner, method"
12292  " FROM alerts_trash WHERE method = 8 OR method = 6;");
12293 
12294  while (next (&alert_data))
12295  {
12296  alert_t alert;
12297  const char *name, *login, *password;
12298  user_t owner;
12299  credential_t new_credential;
12300  gchar *new_credential_id, *quoted_name, *quoted_login;
12301  int method;
12302 
12303  alert = iterator_int64 (&alert_data, 0);
12304  name = iterator_string (&alert_data, 1);
12305  login = iterator_string (&alert_data, 2);
12306  password = iterator_string (&alert_data, 3);
12307  owner = iterator_int64 (&alert_data, 4);
12308  method = iterator_int (&alert_data, 5);
12309 
12310  /* Skip the alert if it is missing login info. */
12311  if (name == NULL || password == NULL)
12312  continue;
12313 
12314  quoted_name = sql_quote (name);
12315  quoted_login = sql_quote (login);
12316 
12317  /* Create basic credential. */
12318 
12319  sql ("INSERT INTO credentials_trash"
12320  " (uuid, name, owner, comment, type,"
12321  " creation_time, modification_time)"
12322  " VALUES"
12323  " (make_uuid (), 'Credential for Alert %s',"
12324  " %llu, 'Autogenerated by migration', 'up',"
12325  " m_now (), m_now ());",
12326  quoted_name, owner);
12327 
12328  new_credential = sql_last_insert_id ();
12329  new_credential_id = sql_string ("SELECT uuid FROM credentials_trash"
12330  " WHERE id = %llu;",
12331  new_credential);
12332 
12333  /* Add credential data. */
12334  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
12335  " VALUES (%llu, 'username', '%s');",
12336  new_credential, quoted_login);
12337 
12339  {
12340  gchar *quoted_password;
12341  quoted_password = sql_quote (password);
12342  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
12343  " VALUES (%llu, 'password', '%s');",
12344  new_credential, quoted_password);
12345  g_free (quoted_password);
12346  }
12347  else
12348  {
12349  char *secret;
12350  gchar *quoted_secret;
12351 
12352  if (!alert_data.crypt_ctx)
12353  alert_data.crypt_ctx = lsc_crypt_new ();
12354 
12355  secret = lsc_crypt_encrypt (alert_data.crypt_ctx,
12356  "password", password, NULL);
12357  if (!secret)
12358  {
12359  g_free (quoted_name);
12360  g_free (quoted_login);
12362  sql_rollback ();
12363  return -1;
12364  }
12365  quoted_secret = sql_quote (secret);
12366  sql ("INSERT INTO credentials_trash_data (credential, type, value)"
12367  " VALUES (%llu, 'secret', '%s');",
12368  new_credential, quoted_secret);
12369  g_free (quoted_secret);
12370  }
12371 
12372  /* Update alert_method_data. */
12373  sql ("INSERT INTO alert_method_data_trash (alert, name, data)"
12374  " VALUES (%llu, '%s_credential', '%s');",
12375  alert,
12376  method == 8 ? "scp" : "verinice_server",
12377  new_credential_id);
12378 
12379  sql ("INSERT INTO alert_method_data_trash (alert, name, data)"
12380  " VALUES (%llu, '%s_credential_location', %d);",
12381  alert,
12382  method == 8 ? "scp" : "verinice_server",
12383  LOCATION_TRASH);
12384 
12385  /* Create permissions. */
12386  sql ("INSERT INTO"
12387  " permissions (uuid, owner, name,"
12388  " comment, resource_type, resource,"
12389  " resource_uuid,"
12390  " resource_location, subject_type, subject,"
12391  " subject_location,"
12392  " creation_time, modification_time)"
12393  " SELECT make_uuid(), owner, 'get_credentials',"
12394  " 'Autogenerated by Alert migration', 'credential', %llu,"
12395  " (SELECT uuid FROM credentials_trash WHERE id=%llu),"
12396  " " G_STRINGIFY (LOCATION_TRASH) ", subject_type, subject,"
12397  " subject_location,"
12398  " m_now (), m_now ()"
12399  " FROM permissions"
12400  " WHERE resource = %llu"
12401  " AND resource_type = 'alert'"
12402  " AND resource_location = " G_STRINGIFY (LOCATION_TRASH)
12403  " GROUP BY owner, subject_type, subject, subject_location;",
12404  new_credential, new_credential, alert);
12405 
12406  sql ("INSERT INTO"
12407  " permissions_trash (uuid, owner, name,"
12408  " comment, resource_type, resource,"
12409  " resource_uuid,"
12410  " resource_location, subject_type, subject,"
12411  " subject_location,"
12412  " creation_time, modification_time)"
12413  " SELECT make_uuid(), owner, 'get_credentials',"
12414  " 'Autogenerated by Alert migration', 'credential', %llu,"
12415  " (SELECT uuid FROM credentials_trash WHERE id=%llu),"
12416  " " G_STRINGIFY (LOCATION_TRASH) ", subject_type, subject,"
12417  " subject_location,"
12418  " m_now (), m_now ()"
12419  " FROM permissions_trash"
12420  " WHERE resource = %llu"
12421  " AND resource_type = 'alert'"
12422  " AND resource_location = " G_STRINGIFY (LOCATION_TRASH)
12423  " GROUP BY owner, subject_type, subject, subject_location;",
12424  new_credential, new_credential, alert);
12425 
12426  g_free (new_credential_id);
12427  g_free (quoted_name);
12428  g_free (quoted_login);
12429  }
12431 
12432  /* Remove now obsolete rows from alert_method_data and ..._trash. */
12433  sql ("DELETE FROM alert_method_data"
12434  " WHERE name='scp_username'"
12435  " OR name='verinice_server_username'"
12436  " OR name='scp_password'"
12437  " OR name='verinice_server_password';");
12438 
12439  sql ("DELETE FROM alert_method_data_trash"
12440  " WHERE name='scp_username'"
12441  " OR name='verinice_server_username'"
12442  " OR name='scp_password'"
12443  " OR name='verinice_server_password';");
12444 
12445  /* Set the database version to 166. */
12446 
12447  set_db_version (166);
12448 
12449  sql_commit ();
12450 
12451  return 0;
12452 }
12453 
12457 static void
12458 insert_predefined (const gchar *uuid)
12459 {
12460  if (sql_int ("SELECT EXISTS (SELECT * FROM report_formats"
12461  " WHERE uuid = '%s');",
12462  uuid))
12463  sql ("INSERT INTO resources_predefined (resource_type, resource)"
12464  " VALUES ('report_format',"
12465  " (SELECT id FROM report_formats WHERE uuid = '%s'));",
12466  uuid);
12467 }
12468 
12474 int
12476 {
12478 
12479  /* Ensure that the database is currently version 166. */
12480 
12481  if (manage_db_version () != 166)
12482  {
12483  sql_rollback ();
12484  return -1;
12485  }
12486 
12487  /* Update the database. */
12488 
12489  /* Ensure the tables exist for the migrator. */
12490 
12491  if (sql_is_sqlite3 ())
12492  sql ("CREATE TABLE IF NOT EXISTS resources_predefined"
12493  " (id INTEGER PRIMARY KEY, resource_type, resource INTEGER)");
12494  else
12495  sql ("CREATE TABLE IF NOT EXISTS resources_predefined"
12496  " (id SERIAL PRIMARY KEY, resource_type text, resource INTEGER)");
12497 
12498  /* Mark predefined report formats. */
12499 
12500  insert_predefined ("5057e5cc-b825-11e4-9d0e-28d24461215b");
12501  insert_predefined ("910200ca-dc05-11e1-954f-406186ea4fc5");
12502  insert_predefined ("5ceff8ba-1f62-11e1-ab9f-406186ea4fc5");
12503  insert_predefined ("c1645568-627a-11e3-a660-406186ea4fc5");
12504  insert_predefined ("9087b18c-626c-11e3-8892-406186ea4fc5");
12505  insert_predefined ("6c248850-1f62-11e1-b082-406186ea4fc5");
12506  insert_predefined ("77bd6c4a-1f62-11e1-abf0-406186ea4fc5");
12507  insert_predefined ("a684c02c-b531-11e1-bdc2-406186ea4fc5");
12508  insert_predefined ("9ca6fe72-1f62-11e1-9e7c-406186ea4fc5");
12509  insert_predefined ("c402cc3e-b531-11e1-9163-406186ea4fc5");
12510  insert_predefined ("a3810a62-1f62-11e1-9219-406186ea4fc5");
12511  insert_predefined ("a994b278-1f62-11e1-96ac-406186ea4fc5");
12512  insert_predefined ("9e5e5deb-879e-4ecc-8be6-a71cd0875cdd");
12513  insert_predefined ("c15ad349-bd8d-457a-880a-c7056532ee15");
12514  insert_predefined ("50c9950a-f326-11e4-800c-28d24461215b");
12515 
12516  /* Set the database version to 167. */
12517 
12518  set_db_version (167);
12519 
12520  sql_commit ();
12521 
12522  return 0;
12523 }
12524 
12530 int
12532 {
12533  const char *uuid;
12534 
12536 
12537  /* Ensure that the database is currently version 167. */
12538 
12539  if (manage_db_version () != 167)
12540  {
12541  sql_rollback ();
12542  return -1;
12543  }
12544 
12545  /* Update the database. */
12546 
12547  /* The example task was removed. */
12548 
12549  uuid = "343435d6-91b0-11de-9478-ffd71f4c6f29";
12550 
12551  sql ("DELETE FROM report_counts"
12552  " WHERE report IN (SELECT id FROM reports"
12553  " WHERE task = (SELECT id FROM tasks"
12554  " WHERE uuid = '%s'));",
12555  uuid);
12556 
12557  sql ("DELETE FROM report_hosts"
12558  " WHERE report IN (SELECT id FROM reports"
12559  " WHERE task = (SELECT id FROM tasks"
12560  " WHERE uuid = '%s'));",
12561  uuid);
12562 
12563  sql ("DELETE FROM results"
12564  " WHERE task = (SELECT id FROM tasks"
12565  " WHERE uuid = '%s');",
12566  uuid);
12567 
12568  sql ("DELETE FROM reports"
12569  " WHERE task = (SELECT id FROM tasks"
12570  " WHERE uuid = '%s');",
12571  uuid);
12572 
12573  sql ("DELETE FROM task_preferences"
12574  " WHERE task = (SELECT id FROM tasks"
12575  " WHERE uuid = '%s');",
12576  uuid);
12577 
12578  sql ("DELETE FROM tasks WHERE uuid = '%s';",
12579  uuid);
12580 
12581  /* Set the database version to 168. */
12582 
12583  set_db_version (168);
12584 
12585  sql_commit ();
12586 
12587  return 0;
12588 }
12589 
12599 static void
12600 migrate_168_to_169_copy_target (user_t owner, const gchar *uuid, target_t *new)
12601 {
12602  assert (new);
12603 
12604  sql ("INSERT INTO targets"
12605  " (uuid, owner, name, comment, creation_time, modification_time,"
12606  " hosts, exclude_hosts, port_list, reverse_lookup_only,"
12607  " reverse_lookup_unify)"
12608  " SELECT make_uuid (), %llu, name, comment, m_now (), m_now (),"
12609  " hosts, exclude_hosts, port_list, reverse_lookup_only,"
12610  " reverse_lookup_unify"
12611  " FROM targets"
12612  " WHERE uuid = '%s';",
12613  owner,
12614  uuid);
12615 
12616  *new = sql_last_insert_id ();
12617 
12618  sql ("INSERT INTO tags"
12619  " (uuid, owner, name, comment, creation_time, modification_time,"
12620  " resource_type, resource, resource_uuid, resource_location,"
12621  " active, value)"
12622  " SELECT make_uuid (), %llu, name, comment, m_now (), m_now (),"
12623  " resource_type, %llu,"
12624  " (SELECT uuid FROM targets WHERE id = %llu),"
12625  " resource_location, active, value"
12626  " FROM tags WHERE resource_type = 'target'"
12627  " AND resource = (SELECT id FROM targets WHERE uuid = '%s')"
12628  " AND resource_location = " G_STRINGIFY (LOCATION_TABLE) ";",
12629  owner,
12630  *new,
12631  *new,
12632  uuid);
12633 }
12634 
12640 int
12642 {
12643  const char *uuid;
12644  iterator_t users;
12645 
12647 
12648  /* Ensure that the database is currently version 168. */
12649 
12650  if (manage_db_version () != 168)
12651  {
12652  sql_rollback ();
12653  return -1;
12654  }
12655 
12656  /* Update the database. */
12657 
12658  /* The predefined target Localhost was removed. */
12659 
12660  uuid = "b493b7a8-7489-11df-a3ec-002264764cea";
12661 
12662  init_iterator (&users, "SELECT id FROM users;");
12663  while (next (&users))
12664  {
12665  user_t owner;
12666 
12667  owner = iterator_int64 (&users, 0);
12668 
12669  if (sql_int ("SELECT count (*) FROM tasks"
12670  " WHERE owner = %llu"
12671  " AND target = (SELECT id FROM targets"
12672  " WHERE uuid = '%s');",
12673  owner,
12674  uuid))
12675  {
12676  target_t new;
12677 
12678  /* This user is using Localhost. Create a copy owned by the user. */
12679 
12680  current_credentials.username = sql_string ("SELECT name FROM users"
12681  " WHERE owner = %llu;",
12682  owner);
12683  current_credentials.uuid = sql_string ("SELECT uuid FROM users"
12684  " WHERE owner = %llu;",
12685  owner);
12686 
12687  migrate_168_to_169_copy_target (owner, uuid, &new);
12688 
12689  free (current_credentials.username);
12690  free (current_credentials.uuid);
12691 
12692  /* Assign the copy to the user's tasks. */
12693 
12694  sql ("UPDATE tasks SET target = %llu"
12695  " WHERE owner = %llu"
12696  " AND target = (SELECT id FROM targets WHERE uuid = '%s');",
12697  new,
12698  owner,
12699  uuid);
12700  }
12701  }
12702  cleanup_iterator (&users);
12703 
12704  /* Delete the old Localhost. */
12705 
12706  sql ("DELETE FROM targets WHERE uuid = '%s';",
12707  uuid);
12708 
12709  /* Set the database version to 169. */
12710 
12711  set_db_version (169);
12712 
12713  sql_commit ();
12714 
12715  return 0;
12716 }
12717 
12726 static void
12727 migrate_169_to_170_add_permission (const gchar *role, const gchar *permission)
12728 {
12729  sql ("INSERT INTO permissions"
12730  " (uuid, owner, name, comment, resource_type, resource, resource_uuid,"
12731  " resource_location, subject_type, subject, subject_location,"
12732  " creation_time, modification_time)"
12733  " VALUES"
12734  " (make_uuid (), NULL, lower ('%s'), '', '',"
12735  " 0, '', " G_STRINGIFY (LOCATION_TABLE) ", 'role',"
12736  " (SELECT id FROM roles WHERE uuid = '%s'),"
12737  " " G_STRINGIFY (LOCATION_TABLE) ", m_now (), m_now ());",
12738  permission,
12739  role);
12740 }
12741 
12747 int
12749 {
12751 
12752  /* Ensure that the database is currently version 169. */
12753 
12754  if (manage_db_version () != 169)
12755  {
12756  sql_rollback ();
12757  return -1;
12758  }
12759 
12760  /* Update the database. */
12761 
12762  /* Role "User" got more DESCRIBE permissions. */
12763 
12764  migrate_169_to_170_add_permission ("8d453140-b74d-11e2-b0be-406186ea4fc5",
12765  "DESCRIBE_CERT");
12766  migrate_169_to_170_add_permission ("8d453140-b74d-11e2-b0be-406186ea4fc5",
12767  "DESCRIBE_FEED");
12768  migrate_169_to_170_add_permission ("8d453140-b74d-11e2-b0be-406186ea4fc5",
12769  "DESCRIBE_SCAP");
12770 
12771  /* Set the database version to 170. */
12772 
12773  set_db_version (170);
12774 
12775  sql_commit ();
12776 
12777  return 0;
12778 }
12779 
12785 int
12787 {
12788  gchar *old_dir, *new_dir;
12789  struct stat state;
12790 
12792 
12793  /* Ensure that the database is currently version 170. */
12794 
12795  if (manage_db_version () != 170)
12796  {
12797  sql_rollback ();
12798  return -1;
12799  }
12800 
12801  /* Update the database. */
12802 
12803  /* The report formats trash moved to an FHS compliant location. */
12804 
12805  new_dir = g_build_filename (OPENVAS_STATE_DIR,
12806  "openvasmd",
12807  NULL);
12808 
12809  if (g_mkdir_with_parents (new_dir, 0755 /* "rwxr-xr-x" */))
12810  {
12811  g_warning ("%s: failed to create dir %s", __FUNCTION__, new_dir);
12812  g_free (new_dir);
12813  sql_rollback ();
12814  return -1;
12815  }
12816 
12817  old_dir = g_build_filename (OPENVAS_DATA_DIR,
12818  "openvasmd",
12819  "report_formats_trash",
12820  NULL);
12821 
12822  if (g_lstat (old_dir, &state))
12823  {
12824  /* The old dir is missing. Assume there are no trash report formats.
12825  * This helps when the database has been restored without the trash
12826  * directory. */
12827 
12828  if (errno != ENOENT)
12829  g_warning ("%s: g_lstat (%s) failed: %s\n",
12830  __FUNCTION__, old_dir, g_strerror (errno));
12831  else
12832  g_warning ("%s: trash report formats directory missing (%s)\n",
12833  __FUNCTION__, old_dir);
12834  g_warning ("%s: any trash report formats will be removed on startup\n",
12835  __FUNCTION__);
12836  }
12837  else
12838  {
12839  gchar **cmd;
12840  gchar *standard_out = NULL;
12841  gchar *standard_err = NULL;
12842  gint exit_status;
12843 
12844  /* Move the directory. */
12845 
12846  g_mkdir_with_parents (old_dir, 0755 /* "rwxr-xr-x" */);
12847 
12848  cmd = (gchar **) g_malloc (4 * sizeof (gchar *));
12849  cmd[0] = g_strdup ("mv");
12850  cmd[1] = old_dir;
12851  cmd[2] = new_dir;
12852  cmd[3] = NULL;
12853  g_debug ("%s: Spawning in .: %s %s %s\n",
12854  __FUNCTION__, cmd[0], cmd[1], cmd[2]);
12855  if ((g_spawn_sync (".",
12856  cmd,
12857  NULL, /* Environment. */
12858  G_SPAWN_SEARCH_PATH,
12859  NULL, /* Setup function. */
12860  NULL,
12861  &standard_out,
12862  &standard_err,
12863  &exit_status,
12864  NULL)
12865  == FALSE)
12866  || (WIFEXITED (exit_status) == 0)
12867  || WEXITSTATUS (exit_status))
12868  {
12869  g_warning ("%s: failed rename: %d (WIF %i, WEX %i)",
12870  __FUNCTION__,
12871  exit_status,
12872  WIFEXITED (exit_status),
12873  WEXITSTATUS (exit_status));
12874  g_debug ("%s: stdout: %s\n", __FUNCTION__, standard_out);
12875  g_debug ("%s: stderr: %s\n", __FUNCTION__, standard_err);
12876  g_free (old_dir);
12877  g_free (new_dir);
12878  g_free (cmd[0]);
12879  g_free (cmd);
12880  sql_rollback ();
12881  return -1;
12882  }
12883 
12884  g_free (cmd[0]);
12885  g_free (cmd);
12886  }
12887 
12888  g_free (old_dir);
12889  g_free (new_dir);
12890 
12891  /* Set the database version to 171. */
12892 
12893  set_db_version (171);
12894 
12895  sql_commit ();
12896 
12897  return 0;
12898 }
12899 
12905 int
12907 {
12908  GError *error;
12909  gchar *old_dir_path, *new_dir_path;
12910  const gchar *subdir_name;
12911  struct stat state;
12912 
12914 
12915  /* Ensure that the database is currently version 171. */
12916 
12917  if (manage_db_version () != 171)
12918  {
12919  sql_rollback ();
12920  return -1;
12921  }
12922 
12923  /* Update the database. */
12924 
12925  /* The global report formats moved to an FHS compliant location. */
12926 
12927  new_dir_path = g_build_filename (OPENVAS_STATE_DIR,
12928  "openvasmd",
12929  NULL);
12930 
12931  if (g_mkdir_with_parents (new_dir_path, 0755 /* "rwxr-xr-x" */))
12932  {
12933  g_warning ("%s: failed to create dir %s", __FUNCTION__, new_dir_path);
12934  g_free (new_dir_path);
12935  sql_rollback ();
12936  return -1;
12937  }
12938 
12939  old_dir_path = g_build_filename (OPENVAS_DATA_DIR,
12940  "openvasmd",
12941  "global_report_formats",
12942  NULL);
12943 
12944  if (g_lstat (old_dir_path, &state))
12945  {
12946  /* The old dir is missing. Probably we are on a fresh install with an
12947  * old db, so skip the moves. There are no report formats files around
12948  * to move anyway, and the Manager install should have put the actual
12949  * files in the right place. */
12950  if (errno != ENOENT)
12951  g_warning ("%s: g_lstat (%s) failed: %s\n",
12952  __FUNCTION__, old_dir_path, g_strerror (errno));
12953  else
12954  g_info ("%s: old global report formats directory missing (%s)\n",
12955  __FUNCTION__, old_dir_path);
12956  }
12957  else
12958  {
12959  GDir *old_dir;
12960  int move_failed;
12961 
12962  /* Iterate over subdirectories of old dir */
12963 
12964  error = NULL;
12965  old_dir = g_dir_open (old_dir_path, 0, &error);
12966  if (old_dir == NULL)
12967  {
12968  g_warning ("%s: Failed to open directory '%s': %s",
12969  __FUNCTION__, old_dir_path, error->message);
12970  g_error_free (error);
12971  g_free (old_dir_path);
12972  g_free (new_dir_path);
12973  sql_rollback ();
12974  return -1;
12975  }
12976 
12977  subdir_name = g_dir_read_name (old_dir);
12978  move_failed = 0;
12979  while (subdir_name && move_failed == 0)
12980  {
12981  gchar *old_subdir_path, *new_subdir_path;
12982  GDir *new_subdir;
12983 
12984  error = NULL;
12985  old_subdir_path = g_build_filename (old_dir_path, subdir_name, NULL);
12986  new_subdir_path = g_build_filename (new_dir_path, subdir_name, NULL);
12987  new_subdir = g_dir_open (new_subdir_path, 0, &error);
12988  if (new_subdir)
12989  {
12990  g_debug ("%s: Skipping '%s', directory already exists",
12991  __FUNCTION__, new_subdir_path);
12992  openvas_file_remove_recurse (old_subdir_path);
12993  g_dir_close (new_subdir);
12994  }
12995  else if (error->code == G_FILE_ERROR_NOENT)
12996  {
12997  gchar **cmd;
12998  gchar *standard_out = NULL;
12999  gchar *standard_err = NULL;
13000  gint exit_status;
13001 
13002  cmd = (gchar **) g_malloc (4 * sizeof (gchar *));
13003  cmd[0] = g_strdup ("mv");
13004  cmd[1] = old_subdir_path;
13005  cmd[2] = new_subdir_path;
13006  cmd[3] = NULL;
13007  g_debug ("%s: Spawning in .: %s %s %s\n",
13008  __FUNCTION__, cmd[0], cmd[1], cmd[2]);
13009  if ((g_spawn_sync (".",
13010  cmd,
13011  NULL, /* Environment. */
13012  G_SPAWN_SEARCH_PATH,
13013  NULL, /* Setup function. */
13014  NULL,
13015  &standard_out,
13016  &standard_err,
13017  &exit_status,
13018  NULL)
13019  == FALSE)
13020  || (WIFEXITED (exit_status) == 0)
13021  || WEXITSTATUS (exit_status))
13022  {
13023  g_warning ("%s: failed rename: %d (WIF %i, WEX %i)",
13024  __FUNCTION__,
13025  exit_status,
13026  WIFEXITED (exit_status),
13027  WEXITSTATUS (exit_status));
13028  g_debug ("%s: stdout: %s\n", __FUNCTION__, standard_out);
13029  g_debug ("%s: stderr: %s\n", __FUNCTION__, standard_err);
13030  move_failed = 1;
13031  }
13032  g_free (cmd[0]);
13033  g_free (cmd);
13034  }
13035  else
13036  {
13037  g_warning ("%s: failed to check directory '%s' : %s",
13038  __FUNCTION__, new_subdir_path, error->message);
13039  move_failed = 1;
13040  }
13041  g_free (old_subdir_path);
13042  g_free (new_subdir_path);
13043  if (error)
13044  g_error_free (error);
13045  subdir_name = g_dir_read_name (old_dir);
13046  }
13047 
13048  g_dir_close (old_dir);
13049 
13050  if (move_failed)
13051  {
13052  sql_rollback ();
13053  return -1;
13054  }
13055  }
13056  g_free (old_dir_path);
13057  g_free (new_dir_path);
13058 
13059  /* Set the database version to 172. */
13060 
13061  set_db_version (172);
13062 
13063  sql_commit ();
13064 
13065  return 0;
13066 }
13067 
13073 int
13075 {
13077 
13078  /* Ensure that the database is currently version 172. */
13079 
13080  if (manage_db_version () != 172)
13081  {
13082  sql_rollback ();
13083  return -1;
13084  }
13085 
13086  /* Remove unused columns. */
13087  if (sql_is_sqlite3 ())
13088  {
13089  sql ("ALTER TABLE nvts RENAME TO nvts_172;");
13090 
13091  sql ("CREATE TABLE IF NOT EXISTS nvts"
13092  " (id INTEGER PRIMARY KEY, uuid, oid, version, name, comment,"
13093  " copyright, cve, bid, xref, tag, category INTEGER, family, cvss_base,"
13094  " creation_time, modification_time, solution_type TEXT, qod INTEGER,"
13095  " qod_type TEXT);");
13096 
13097  sql ("INSERT INTO nvts"
13098  " (id, uuid, oid, version, name, comment, copyright, cve,"
13099  " bid, xref, tag, category, family, cvss_base, creation_time,"
13100  " modification_time, solution_type, qod, qod_type)"
13101  " SELECT id, uuid, oid, version, name, comment, copyright, cve,"
13102  " bid, xref, tag, category, family, cvss_base, creation_time,"
13103  " modification_time, solution_type, qod, qod_type"
13104  " FROM nvts_172;");
13105 
13106  sql ("DROP TABLE nvts_172;");
13107  }
13108  else
13109  sql ("ALTER TABLE nvts DROP COLUMN summary;");
13110 
13111  /* Set the database version to 173. */
13112 
13113  set_db_version (173);
13114 
13115  sql_commit ();
13116 
13117  return 0;
13118 }
13119 
13125 int
13127 {
13129  report_format_t report_format;
13130 
13131  /* Ensure that the database is currently version 173. */
13132 
13133  if (manage_db_version () != 173)
13134  {
13135  sql_rollback ();
13136  return -1;
13137  }
13138 
13139  /* Update the database. */
13140 
13141  /* Get row id of Verinice ISM report format */
13142  sql_int64 (&report_format,
13143  "SELECT id FROM report_formats"
13144  " WHERE uuid='c15ad349-bd8d-457a-880a-c7056532ee15';");
13145 
13146  // Update version number in summary and description
13147  sql ("UPDATE report_formats"
13148  " SET summary='Greenbone Verinice ISM Report, v3.0.0.',"
13149  " description='Information Security Management Report for Verinice import, version 3.0.0.\n'"
13150  " WHERE id = %llu",
13151  report_format);
13152 
13153  // Remove old attach params
13154  sql ("DELETE FROM report_format_params"
13155  " WHERE report_format = %llu"
13156  " AND name LIKE 'Attach %%%% report'",
13157  report_format);
13158 
13159  // Add new attach param
13160  sql ("INSERT INTO report_format_params (report_format, name, type, value,"
13161  " type_min, type_max, type_regex, fallback)"
13162  " VALUES (%lli, 'Attached report formats', %i, '%s', 0, 0, '', 1);",
13163  report_format,
13165  "6c248850-1f62-11e1-b082-406186ea4fc5");
13166 
13167  /* Set the database version to 174. */
13168 
13169  set_db_version (174);
13170 
13171  sql_commit ();
13172 
13173  return 0;
13174 }
13175 
13181 int
13183 {
13184  GError *error;
13185  int move_failed;
13186  gchar *old_dir_path, *new_dir_path;
13187  const gchar *subdir_name;
13188  GDir *old_dir;
13189 
13191 
13192  /* Ensure that the database is currently version 174. */
13193 
13194  if (manage_db_version () != 174)
13195  {
13196  sql_rollback ();
13197  return -1;
13198  }
13199 
13200  /* Update the database. */
13201 
13202  /* The global report formats moved back to the DATA directory, because
13203  * they are being merged into the predefined report formats. */
13204 
13205  new_dir_path = g_build_filename (OPENVAS_DATA_DIR,
13206  "openvasmd",
13207  "report_formats",
13208  NULL);
13209 
13210  /* The new dir should exist already, so this will work even if we don't
13211  * have write permission in OPENVAS_DATA_DIR. */
13212  if (g_mkdir_with_parents (new_dir_path, 0755 /* "rwxr-xr-x" */))
13213  {
13214  g_warning ("%s: failed to create dir %s", __FUNCTION__, new_dir_path);
13215  g_free (new_dir_path);
13216  sql_rollback ();
13217  return -1;
13218  }
13219 
13220  old_dir_path = g_build_filename (OPENVAS_STATE_DIR,
13221  "openvasmd",
13222  "global_report_formats",
13223  NULL);
13224 
13225  /* Ensure the old dir exists. */
13226 
13227  g_mkdir_with_parents (old_dir_path, 0755 /* "rwxr-xr-x" */);
13228 
13229  /* Iterate over subdirectories of old dir. */
13230 
13231  error = NULL;
13232  old_dir = g_dir_open (old_dir_path, 0, &error);
13233  if (old_dir == NULL)
13234  {
13235  g_warning ("%s: Failed to open directory '%s': %s",
13236  __FUNCTION__, old_dir_path, error->message);
13237  g_error_free (error);
13238  g_free (old_dir_path);
13239  g_free (new_dir_path);
13240  sql_rollback ();
13241  return -1;
13242  }
13243 
13244  subdir_name = g_dir_read_name (old_dir);
13245  move_failed = 0;
13246  while (subdir_name && move_failed == 0)
13247  {
13248  gchar *old_subdir_path, *new_subdir_path;
13249  GDir *new_subdir;
13250 
13251  error = NULL;
13252  old_subdir_path = g_build_filename (old_dir_path, subdir_name, NULL);
13253  new_subdir_path = g_build_filename (new_dir_path, subdir_name, NULL);
13254  new_subdir = g_dir_open (new_subdir_path, 0, &error);
13255  if (new_subdir)
13256  {
13257  g_debug ("%s: Skipping '%s', directory already exists",
13258  __FUNCTION__, new_subdir_path);
13259  openvas_file_remove_recurse (old_subdir_path);
13260  g_dir_close (new_subdir);
13261  }
13262  else if (error->code == G_FILE_ERROR_NOENT)
13263  {
13264  gchar **cmd;
13265  gchar *standard_out = NULL;
13266  gchar *standard_err = NULL;
13267  gint exit_status;
13268 
13269  cmd = (gchar **) g_malloc (4 * sizeof (gchar *));
13270  cmd[0] = g_strdup ("mv");
13271  cmd[1] = old_subdir_path;
13272  cmd[2] = new_subdir_path;
13273  cmd[3] = NULL;
13274  g_debug ("%s: Spawning in .: %s %s %s\n",
13275  __FUNCTION__, cmd[0], cmd[1], cmd[2]);
13276  if ((g_spawn_sync (".",
13277  cmd,
13278  NULL, /* Environment. */
13279  G_SPAWN_SEARCH_PATH,
13280  NULL, /* Setup function. */
13281  NULL,
13282  &standard_out,
13283  &standard_err,
13284  &exit_status,
13285  NULL)
13286  == FALSE)
13287  || (WIFEXITED (exit_status) == 0)
13288  || WEXITSTATUS (exit_status))
13289  {
13290  g_warning ("%s: failed rename: %d (WIF %i, WEX %i)",
13291  __FUNCTION__,
13292  exit_status,
13293  WIFEXITED (exit_status),
13294  WEXITSTATUS (exit_status));
13295  g_debug ("%s: stdout: %s\n", __FUNCTION__, standard_out);
13296  g_debug ("%s: stderr: %s\n", __FUNCTION__, standard_err);
13297  move_failed = 1;
13298  }
13299  g_free (cmd[0]);
13300  g_free (cmd);
13301  }
13302  else
13303  {
13304  g_warning ("%s: failed to check directory '%s' : %s",
13305  __FUNCTION__, new_subdir_path, error->message);
13306  move_failed = 1;
13307  }
13308  g_free (old_subdir_path);
13309  g_free (new_subdir_path);
13310  if (error)
13311  g_error_free (error);
13312  subdir_name = g_dir_read_name (old_dir);
13313  }
13314  g_free (new_dir_path);
13315  g_dir_close (old_dir);
13316 
13317  if (move_failed)
13318  {
13319  g_free (old_dir_path);
13320  sql_rollback ();
13321  return -1;
13322  }
13323 
13324  openvas_file_remove_recurse (old_dir_path);
13325  g_free (old_dir_path);
13326 
13327  /* Set the database version to 175. */
13328 
13329  set_db_version (175);
13330 
13331  sql_commit ();
13332 
13333  return 0;
13334 }
13335 
13341 int
13343 {
13344 
13346 
13347  /* Ensure that the database is currently version 175. */
13348 
13349  if (manage_db_version () != 175)
13350  {
13351  sql_rollback ();
13352  return -1;
13353  }
13354 
13355  /* Update the database. */
13356 
13357  /* Change the default scanner to use unix file sockets. */
13358  sql ("UPDATE scanners SET host = '" OPENVAS_RUN_DIR "/openvassd.sock'"
13359  " WHERE uuid = '" SCANNER_UUID_DEFAULT "';");
13360 
13361  /* Set the database version to 176. */
13362 
13363  set_db_version (176);
13364 
13365  sql_commit ();
13366 
13367  return 0;
13368 }
13369 
13375 int
13377 {
13378  int now;
13379 
13381 
13382  /* Ensure that the database is currently version 176. */
13383 
13384  if (manage_db_version () != 176)
13385  {
13386  sql_rollback ();
13387  return -1;
13388  }
13389 
13390  /* Update the database. */
13391 
13392  /* The feed DESCRIBE commands were merged to new command GET_FEEDS. */
13393 
13394  now = time (NULL);
13395 
13396  sql ("INSERT INTO permissions"
13397  " (uuid, owner, name, comment, resource_type, resource, resource_uuid,"
13398  " resource_location, subject_type, subject, subject_location,"
13399  " creation_time, modification_time)"
13400  " SELECT make_uuid (), *, %i, %i"
13401  " FROM (SELECT DISTINCT owner, 'get_feeds', comment, resource_type,"
13402  " resource, resource_uuid, resource_location,"
13403  " subject_type, subject, subject_location"
13404  " FROM permissions"
13405  " WHERE (name = 'describe_feed'"
13406  " OR name = 'describe_scap'"
13407  " OR name = 'describe_cert'))"
13408  " AS subquery;",
13409  now,
13410  now);
13411 
13412  sql ("DELETE FROM permissions"
13413  " WHERE (name = 'describe_feed'"
13414  " OR name = 'describe_scap'"
13415  " OR name = 'describe_cert');");
13416 
13417  /* Set the database version to 177. */
13418 
13419  set_db_version (177);
13420 
13421  sql_commit ();
13422 
13423  return 0;
13424 }
13425 
13431 int
13433 {
13434  credential_t credential;
13436 
13437  /* Ensure that the database is currently version 177. */
13438 
13439  if (manage_db_version () != 177)
13440  {
13441  sql_rollback ();
13442  return -1;
13443  }
13444 
13445  /* Update the database. */
13446 
13447  /* Remove CA certificate from default scanner. */
13448  sql ("UPDATE scanners SET ca_pub = NULL"
13449  " WHERE uuid = '" SCANNER_UUID_DEFAULT "';");
13450 
13451  /* Get credential to delete it if possible */
13452  sql_int64 (&credential,
13453  "SELECT credential FROM scanners"
13454  " WHERE uuid = '" SCANNER_UUID_DEFAULT "'");
13455 
13456  /* Remove reference to credential from default scanner. */
13457  sql ("UPDATE scanners SET credential = NULL"
13458  " WHERE uuid = '" SCANNER_UUID_DEFAULT "';");
13459 
13460  /* Delete credential of default scanner if it is not used elsewhere. */
13461  if ((sql_int ("SELECT count(*) FROM scanners"
13462  " WHERE credential = %llu"
13463  " AND uuid != '" SCANNER_UUID_DEFAULT "';",
13464  credential) == 0)
13465  && (sql_int ("SELECT count(*) FROM scanners_trash"
13466  " WHERE credential = %llu"
13467  " AND credential_location = %d;",
13468  credential, LOCATION_TABLE) == 0)
13469  && (sql_int ("SELECT count(*) FROM targets_login_data"
13470  " WHERE credential = %llu;",
13471  credential) == 0)
13472  && (sql_int ("SELECT count(*) FROM targets_trash_login_data"
13473  " WHERE credential = %llu"
13474  " AND credential_location = %d;",
13475  credential, LOCATION_TABLE) == 0)
13476  && (sql_int ("SELECT count(*) FROM slaves"
13477  " WHERE credential = %llu;",
13478  credential) == 0)
13479  && (sql_int ("SELECT count(*) FROM slaves_trash"
13480  " WHERE credential = %llu"
13481  " AND credential_location = %d;",
13482  credential, LOCATION_TABLE) == 0))
13483  {
13484  sql ("DELETE FROM credentials_data WHERE credential = %llu",
13485  credential);
13486  sql ("DELETE FROM credentials WHERE id = %llu",
13487  credential);
13488  }
13489 
13490  /* Set the database version to 178. */
13491 
13492  set_db_version (178);
13493 
13494  sql_commit ();
13495 
13496  return 0;
13497 }
13498 
13504 int
13506 {
13508 
13509  /* Ensure that the database is currently version 178. */
13510 
13511  if (manage_db_version () != 178)
13512  {
13513  sql_rollback ();
13514  return -1;
13515  }
13516 
13517  /* Update the database. */
13518 
13519  /* Reports got new columns for slave username and password. */
13520 
13521  sql ("ALTER TABLE reports ADD COLUMN slave_username TEXT;");
13522  sql ("ALTER TABLE reports ADD COLUMN slave_password TEXT;");
13523 
13524  sql ("UPDATE reports"
13525  " SET slave_username = (SELECT credentials_data.value"
13526  " FROM slaves, credentials_data"
13527  " WHERE slaves.id = (SELECT id FROM slaves"
13528  " WHERE uuid = slave_uuid)"
13529  " AND credentials_data.credential"
13530  " = slaves.credential"
13531  " AND credentials_data.type = 'username');");
13532 
13533  sql ("UPDATE reports"
13534  " SET slave_password = (SELECT credentials_data.value"
13535  " FROM slaves, credentials_data"
13536  " WHERE slaves.id = (SELECT id FROM slaves"
13537  " WHERE uuid = slave_uuid)"
13538  " AND credentials_data.credential"
13539  " = slaves.credential"
13540  " AND credentials_data.type = 'username');");
13541 
13542  /* Set the database version to 179. */
13543 
13544  set_db_version (179);
13545 
13546  sql_commit ();
13547 
13548  return 0;
13549 }
13550 
13559 void
13560 migrate_179_to_180_update_ref (const gchar *table, int trash)
13561 {
13562  sql ("UPDATE %s"
13563  " SET resource_type = 'scanner',"
13564  " resource = (SELECT id FROM scanners%s"
13565  " WHERE uuid = resource_uuid)"
13566  " WHERE resource_type = 'slave'"
13567  " AND resource_location = %i;",
13568  table,
13569  trash ? "_trash" : "",
13570  trash ? LOCATION_TRASH : LOCATION_TABLE);
13571 }
13572 
13578 int
13580 {
13582 
13583  /* Ensure that the database is currently version 179. */
13584 
13585  if (manage_db_version () != 179)
13586  {
13587  sql_rollback ();
13588  return -1;
13589  }
13590 
13591  /* Update the database. */
13592 
13593  /* Slaves were replaced by OMP scanners. */
13594 
13595  sql ("DELETE FROM settings"
13596  " WHERE uuid = 'aec201fa-8a82-4b61-bebe-a44ea93b2909'"
13597  " OR uuid = '2681c32a-8dfd-40c9-a9c6-8d4e2c7799eb';");
13598 
13599  sql ("UPDATE filters"
13600  " SET type = replace (type, 'slave', 'scanner');");
13601 
13602  sql ("UPDATE filters_trash"
13603  " SET type = replace (type, 'slave', 'scanner');");
13604 
13605  sql ("INSERT INTO scanners (uuid, owner, name, comment, host, port,"
13606  " creation_time, modification_time, credential,"
13607  " type, ca_pub)"
13608  " SELECT uuid, owner, name, comment, host, CAST (port AS INTEGER),"
13609  " creation_time, modification_time, credential, %i, NULL"
13610  " FROM slaves;",
13612 
13613  migrate_179_to_180_update_ref ("tags", 0);
13614  migrate_179_to_180_update_ref ("tags_trash", 0);
13615  migrate_179_to_180_update_ref ("permissions", 0);
13616  migrate_179_to_180_update_ref ("permissions_trash", 0);
13617 
13618  sql ("UPDATE tasks"
13619  " SET scanner = (SELECT id FROM scanners"
13620  " WHERE uuid = (SELECT uuid FROM slaves"
13621  " WHERE id = tasks.slave)),"
13622  " slave = 0"
13623  " WHERE slave != 0"
13624  " AND slave_location = " G_STRINGIFY (LOCATION_TABLE) ";");
13625 
13626  sql ("INSERT INTO scanners_trash (uuid, owner, name, comment, host, port,"
13627  " creation_time, modification_time,"
13628  " credential, type, ca_pub)"
13629  " SELECT uuid, owner, name, comment, host, CAST (port AS INTEGER),"
13630  " creation_time, modification_time, credential, %i, NULL"
13631  " FROM slaves_trash;",
13633 
13634  migrate_179_to_180_update_ref ("tags", 1);
13635  migrate_179_to_180_update_ref ("tags_trash", 1);
13636  migrate_179_to_180_update_ref ("permissions", 1);
13637  migrate_179_to_180_update_ref ("permissions_trash", 1);
13638 
13639  sql ("UPDATE permissions"
13640  " SET name = replace (name, 'slave', 'scanner');");
13641 
13642  sql ("UPDATE permissions_trash"
13643  " SET name = replace (name, 'slave', 'scanner');");
13644 
13645  sql ("UPDATE tasks"
13646  " SET scanner = (SELECT id FROM scanners_trash"
13647  " WHERE uuid = (SELECT uuid FROM slaves_trash"
13648  " WHERE id = tasks.slave)),"
13649  " slave = 0"
13650  " WHERE slave != 0"
13651  " AND slave_location = " G_STRINGIFY (LOCATION_TRASH) ";");
13652 
13653  sql ("DROP TABLE slaves;");
13654  sql ("DROP TABLE slaves_trash;");
13655 
13656  /* Set the database version to 180. */
13657 
13658  set_db_version (180);
13659 
13660  sql_commit ();
13661 
13662  return 0;
13663 }
13664 
13670 int
13672 {
13674 
13675  /* Ensure that the database is currently version 180. */
13676 
13677  if (manage_db_version () != 180)
13678  {
13679  sql_rollback ();
13680  return -1;
13681  }
13682 
13683  /* Update the database. */
13684 
13685  /* Unused column "time" was removed from table tasks.
13686  *
13687  * Remove slave columns from task at the same time. */
13688 
13689  if (sql_is_sqlite3 ())
13690  {
13691  sql ("ALTER TABLE tasks RENAME TO tasks_180;");
13692 
13693  sql ("CREATE TABLE IF NOT EXISTS tasks"
13694  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, hidden INTEGER,"
13695  " comment, run_status INTEGER, start_time, end_time,"
13696  " config INTEGER, target INTEGER, schedule INTEGER, schedule_next_time,"
13697  " schedule_periods INTEGER, config_location INTEGER,"
13698  " target_location INTEGER, schedule_location INTEGER,"
13699  " scanner_location INTEGER, upload_result_count INTEGER,"
13700  " hosts_ordering, scanner, alterable, creation_time,"
13701  " modification_time);");
13702 
13703  sql ("INSERT INTO tasks"
13704  " (id, uuid, owner, name, hidden, comment, run_status, start_time,"
13705  " end_time, config, target, schedule, schedule_next_time,"
13706  " schedule_periods, config_location, target_location,"
13707  " schedule_location, scanner_location, upload_result_count,"
13708  " hosts_ordering, scanner, alterable, creation_time,"
13709  " modification_time)"
13710  " SELECT id, uuid, owner, name, hidden, comment, run_status,"
13711  " start_time, end_time, config, target, schedule,"
13712  " schedule_next_time, schedule_periods, config_location,"
13713  " target_location, schedule_location, scanner_location,"
13714  " upload_result_count, hosts_ordering, scanner, alterable,"
13715  " creation_time, modification_time"
13716  " FROM tasks_180;");
13717 
13718  sql ("DROP TABLE tasks_180;");
13719  }
13720  else
13721  {
13722  sql ("ALTER TABLE tasks DROP COLUMN slave;");
13723  sql ("ALTER TABLE tasks DROP COLUMN slave_location;");
13724  }
13725 
13726  /* Set the database version to 181. */
13727 
13728  set_db_version (181);
13729 
13730  sql_commit ();
13731 
13732  return 0;
13733 }
13734 
13742 int
13743 migrate_181_to_182_move (const char *dest)
13744 {
13745  gchar *new_dir_path, *old_dir_path;
13746  GError *error;
13747  GDir *old_dir;
13748  const gchar *asc_name;
13749  int move_failed;
13750 
13751  new_dir_path = g_build_filename (OPENVAS_STATE_DIR,
13752  "openvasmd",
13753  "signatures",
13754  dest,
13755  NULL);
13756 
13757  if (g_mkdir_with_parents (new_dir_path, 0755 /* "rwxr-xr-x" */))
13758  {
13759  g_warning ("%s: failed to create dir %s", __FUNCTION__, new_dir_path);
13760  g_free (new_dir_path);
13761  return -1;
13762  }
13763 
13764  old_dir_path = g_build_filename (OPENVAS_NVT_DIR,
13765  "private",
13766  dest,
13767  NULL);
13768 
13769  error = NULL;
13770  old_dir = g_dir_open (old_dir_path, 0, &error);
13771  if (old_dir == NULL)
13772  {
13773  if (error->code == G_FILE_ERROR_NOENT)
13774  /* No directory means no signatures to copy. */
13775  goto free_exit;
13776  g_warning ("%s: Failed to open directory '%s': %s",
13777  __FUNCTION__, old_dir_path, error->message);
13778  g_error_free (error);
13779  g_free (old_dir_path);
13780  g_free (new_dir_path);
13781  return -1;
13782  }
13783 
13784  asc_name = g_dir_read_name (old_dir);
13785  move_failed = 0;
13786  while (asc_name && move_failed == 0)
13787  {
13788  gchar *old_asc_path, *new_asc_path;
13789 
13790  gchar **cmd;
13791  gchar *standard_out = NULL;
13792  gchar *standard_err = NULL;
13793  gint exit_status;
13794 
13795  error = NULL;
13796  old_asc_path = g_build_filename (old_dir_path, asc_name, NULL);
13797  new_asc_path = g_build_filename (new_dir_path, asc_name, NULL);
13798 
13799  cmd = (gchar **) g_malloc (4 * sizeof (gchar *));
13800  cmd[0] = g_strdup ("mv");
13801  cmd[1] = old_asc_path;
13802  cmd[2] = new_asc_path;
13803  cmd[3] = NULL;
13804  g_debug ("%s: Spawning in .: %s %s %s\n",
13805  __FUNCTION__, cmd[0], cmd[1], cmd[2]);
13806  if ((g_spawn_sync (".",
13807  cmd,
13808  NULL, /* Environment. */
13809  G_SPAWN_SEARCH_PATH,
13810  NULL, /* Setup function. */
13811  NULL,
13812  &standard_out,
13813  &standard_err,
13814  &exit_status,
13815  NULL)
13816  == FALSE)
13817  || (WIFEXITED (exit_status) == 0)
13818  || WEXITSTATUS (exit_status))
13819  {
13820  g_warning ("%s: failed rename: %d (WIF %i, WEX %i)",
13821  __FUNCTION__,
13822  exit_status,
13823  WIFEXITED (exit_status),
13824  WEXITSTATUS (exit_status));
13825  g_debug ("%s: stdout: %s\n", __FUNCTION__, standard_out);
13826  g_debug ("%s: stderr: %s\n", __FUNCTION__, standard_err);
13827  move_failed = 1;
13828  }
13829  g_free (cmd[0]);
13830  g_free (cmd);
13831  g_free (old_asc_path);
13832  g_free (new_asc_path);
13833  if (error)
13834  g_error_free (error);
13835  asc_name = g_dir_read_name (old_dir);
13836  }
13837  g_free (new_dir_path);
13838  g_dir_close (old_dir);
13839 
13840  if (move_failed)
13841  {
13842  g_free (old_dir_path);
13843  return -1;
13844  }
13845 
13846  openvas_file_remove_recurse (old_dir_path);
13847  free_exit:
13848  g_free (old_dir_path);
13849 
13850  return 0;
13851 }
13852 
13858 int
13860 {
13862 
13863  /* Ensure that the database is currently version 181. */
13864 
13865  if (manage_db_version () != 181)
13866  {
13867  sql_rollback ();
13868  return -1;
13869  }
13870 
13871  /* Update the database. */
13872 
13873  /* The directories used by users to provide report format signatures for
13874  * their own report formats and agents moved from
13875  * FEED/plugins/private/report_formats to
13876  * STATE/var/lib/openvas/openvasmd/report_formats. */
13877 
13878  if (migrate_181_to_182_move ("report_formats"))
13879  {
13880  sql_rollback ();
13881  return -1;
13882  }
13883 
13884  /* Set the database version to 182. */
13885 
13886  set_db_version (182);
13887 
13888  sql_commit ();
13889 
13890  return 0;
13891 }
13892 
13898 int
13900 {
13902 
13903  /* Ensure that the database is currently version 182. */
13904 
13905  if (manage_db_version () != 182)
13906  {
13907  sql_rollback ();
13908  return -1;
13909  }
13910 
13911  /* Update the database. */
13912 
13913  /* Slave usernames and passwords were removed from table reports. */
13914 
13915  if (sql_is_sqlite3 ())
13916  {
13917  sql ("ALTER TABLE reports RENAME TO reports_182;");
13918 
13919  sql ("CREATE TABLE reports"
13920  " (id INTEGER PRIMARY KEY, uuid, owner INTEGER, hidden INTEGER,"
13921  " task INTEGER, date INTEGER, start_time, end_time, nbefile, comment,"
13922  " scan_run_status INTEGER, slave_progress, slave_task_uuid,"
13923  " slave_uuid, slave_name, slave_host, slave_port, source_iface,"
13924  " flags INTEGER);");
13925 
13926  sql ("INSERT INTO reports"
13927  " (id, uuid, owner, hidden, task, date, start_time, end_time,"
13928  " nbefile, comment, scan_run_status, slave_progress,"
13929  " slave_task_uuid, slave_uuid, slave_name, slave_host,"
13930  " slave_port, source_iface, flags)"
13931  " SELECT id, uuid, owner, hidden, task, date, start_time, end_time,"
13932  " nbefile, comment, scan_run_status, slave_progress,"
13933  " slave_task_uuid, slave_uuid, slave_name, slave_host,"
13934  " slave_port, source_iface, flags"
13935  " FROM reports_182;");
13936 
13937  sql ("DROP TABLE reports_182;");
13938  }
13939  else
13940  {
13941  sql ("ALTER TABLE reports DROP COLUMN slave_username;");
13942  sql ("ALTER TABLE reports DROP COLUMN slave_password;");
13943  }
13944 
13945  /* Set the database version to 183. */
13946 
13947  set_db_version (183);
13948 
13949  sql_commit ();
13950 
13951  return 0;
13952 }
13953 
13959 int
13961 {
13963 
13964  /* Ensure that the database is currently version 183. */
13965 
13966  if (manage_db_version () != 183)
13967  {
13968  sql_rollback ();
13969  return -1;
13970  }
13971 
13972  /* Update the database. */
13973 
13974  /* OMP command GET_NVT_FEED_VERSION was removed. */
13975 
13976  sql ("DELETE FROM permissions WHERE name = 'get_nvt_feed_version';");
13977 
13978  sql ("DELETE FROM permissions_trash WHERE name = 'get_nvt_feed_version';");
13979 
13980  /* Deactivate report formats that are not predefined,
13981  * as some older ones may cause problems.
13982  */
13983  sql ("UPDATE report_formats SET flags = (flags & ~1) WHERE id NOT IN"
13984  " (SELECT resource FROM resources_predefined"
13985  " WHERE resource_type='report_format');");
13986 
13987  /* Set the database version to 184. */
13988 
13989  set_db_version (184);
13990 
13991  sql_commit ();
13992 
13993  return 0;
13994 }
13995 
13996 #undef UPDATE_CHART_SETTINGS
13997 #undef UPDATE_DASHBOARD_SETTINGS
13998 
13999 #ifdef SQL_IS_SQLITE
14000 #define SQLITE_OR_NULL(function) function
14001 #else
14002 #define SQLITE_OR_NULL(function) NULL
14003 #endif
14004 
14008 static migrator_t database_migrators[]
14009  = {{0, NULL},
14073  {64, migrate_63_to_64},
14074  {65, migrate_64_to_65},
14075  {66, migrate_65_to_66},
14076  {67, migrate_66_to_67},
14077  {68, migrate_67_to_68},
14078  {69, migrate_68_to_69},
14079  {70, migrate_69_to_70},
14080  {71, migrate_70_to_71},
14081  {72, migrate_71_to_72},
14082  {73, migrate_72_to_73},
14083  {74, migrate_73_to_74},
14084  {75, migrate_74_to_75},
14085  {76, migrate_75_to_76},
14086  {77, migrate_76_to_77},
14087  {78, migrate_77_to_78},
14088  {79, migrate_78_to_79},
14089  {80, migrate_79_to_80},
14090  {81, migrate_80_to_81},
14091  {82, migrate_81_to_82},
14092  {83, migrate_82_to_83},
14093  {84, migrate_83_to_84},
14094  {85, migrate_84_to_85},
14095  {86, migrate_85_to_86},
14096  {87, migrate_86_to_87},
14097  {88, migrate_87_to_88},
14098  {89, migrate_88_to_89},
14099  {90, migrate_89_to_90},
14100  {91, migrate_90_to_91},
14101  {92, migrate_91_to_92},
14102  {93, migrate_92_to_93},
14103  {94, migrate_93_to_94},
14104  {95, migrate_94_to_95},
14105  {96, migrate_95_to_96},
14106  {97, migrate_96_to_97},
14107  {98, migrate_97_to_98},
14108  {99, migrate_98_to_99},
14109  {100, migrate_99_to_100},
14110  {101, migrate_100_to_101},
14111  {102, migrate_101_to_102},
14112  {103, migrate_102_to_103},
14113  {104, migrate_103_to_104},
14114  {105, migrate_104_to_105},
14115  {106, migrate_105_to_106},
14116  {107, migrate_106_to_107},
14117  {108, migrate_107_to_108},
14118  {109, migrate_108_to_109},
14119  {110, migrate_109_to_110},
14120  {111, migrate_110_to_111},
14121  {112, migrate_111_to_112},
14122  {113, migrate_112_to_113},
14123  {114, migrate_113_to_114},
14124  {115, migrate_114_to_115},
14125  {116, migrate_115_to_116},
14126  {117, migrate_116_to_117},
14127  {118, migrate_117_to_118},
14128  {119, migrate_118_to_119},
14129  {120, migrate_119_to_120},
14130  {121, migrate_120_to_121},
14131  {122, migrate_121_to_122},
14132  {123, migrate_122_to_123},
14133  {124, migrate_123_to_124},
14134  {125, migrate_124_to_125},
14135  {126, migrate_125_to_126},
14136  {127, migrate_126_to_127},
14137  {128, migrate_127_to_128},
14138  {129, migrate_128_to_129},
14139  {130, migrate_129_to_130},
14140  {131, migrate_130_to_131},
14141  {132, migrate_131_to_132},
14142  {133, migrate_132_to_133},
14143  {134, migrate_133_to_134},
14144  {135, migrate_134_to_135},
14145  {136, migrate_135_to_136},
14146  {137, migrate_136_to_137},
14147  {138, migrate_137_to_138},
14148  {139, migrate_138_to_139},
14149  {140, migrate_139_to_140},
14150  {141, migrate_140_to_141},
14151  {142, migrate_141_to_142},
14152  {143, migrate_142_to_143},
14153  {144, migrate_143_to_144},
14154  {145, migrate_144_to_145},
14155  {146, migrate_145_to_146},
14156  {147, migrate_146_to_147},
14157  {148, migrate_147_to_148},
14158  {149, migrate_148_to_149},
14159  {150, migrate_149_to_150},
14160  {151, migrate_150_to_151},
14161  {152, migrate_151_to_152},
14162  {153, migrate_152_to_153},
14163  {154, migrate_153_to_154},
14164  {155, migrate_154_to_155},
14165  {156, migrate_155_to_156},
14166  {157, migrate_156_to_157},
14167  {158, migrate_157_to_158},
14168  {159, migrate_158_to_159},
14169  {160, migrate_159_to_160},
14170  {161, migrate_160_to_161},
14171  {162, migrate_161_to_162},
14172  {163, migrate_162_to_163},
14173  {164, migrate_163_to_164},
14174  {165, migrate_164_to_165},
14175  {166, migrate_165_to_166},
14176  {167, migrate_166_to_167},
14177  {168, migrate_167_to_168},
14178  {169, migrate_168_to_169},
14179  {170, migrate_169_to_170},
14180  {171, migrate_170_to_171},
14181  {172, migrate_171_to_172},
14182  {173, migrate_172_to_173},
14183  {174, migrate_173_to_174},
14184  {175, migrate_174_to_175},
14185  {176, migrate_175_to_176},
14186  {177, migrate_176_to_177},
14187  {178, migrate_177_to_178},
14188  {179, migrate_178_to_179},
14189  {180, migrate_179_to_180},
14190  {181, migrate_180_to_181},
14191  {182, migrate_181_to_182},
14192  {183, migrate_182_to_183},
14193  {184, migrate_183_to_184},
14194  /* End marker. */
14195  {-1, NULL}};
14196 
14205 gboolean
14206 manage_migrate_needs_timezone (GSList *log_config, const gchar *database)
14207 {
14208  int db_version;
14209  g_log_set_handler (G_LOG_DOMAIN,
14211  (GLogFunc) openvas_log_func,
14212  log_config);
14213  init_manage_process (0, database);
14214  db_version = manage_db_version ();
14215  cleanup_manage_process (TRUE);
14216  return db_version > 0 && db_version < 52;
14217 }
14218 
14227 int
14228 migrate_is_available (int old_version, int new_version)
14229 {
14230  migrator_t *migrators;
14231 
14232  migrators = database_migrators + old_version + 1;
14233 
14234  while ((migrators->version >= 0) && (migrators->version <= new_version))
14235  {
14236  if (migrators->function == NULL) return 0;
14237  if (migrators->version == new_version) return 1;
14238  migrators++;
14239  }
14240 
14241  return -1;
14242 }
14243 
14254 int
14255 manage_migrate (GSList *log_config, const gchar *database)
14256 {
14257  migrator_t *migrators;
14258  /* The version on the disk. */
14259  int old_version, old_scap_version, old_cert_version;
14260  /* The version that this program requires. */
14261  int new_version, new_scap_version, new_cert_version;
14262  int version_current = 0, scap_version_current = 0, cert_version_current = 0;
14263 
14264  g_log_set_handler (G_LOG_DOMAIN,
14266  (GLogFunc) openvas_log_func,
14267  log_config);
14268 
14269  init_manage_process (0, database);
14270 
14271  old_version = manage_db_version ();
14272  new_version = manage_db_supported_version ();
14273 
14274  if (old_version == -1)
14275  {
14276  cleanup_manage_process (TRUE);
14277  return -1;
14278  }
14279 
14280  if (old_version == -2)
14281  {
14282  g_warning ("%s: no task tables yet, run a --rebuild to create them.\n",
14283  __FUNCTION__);
14284  version_current = 1;
14285  }
14286  else if (old_version == new_version)
14287  {
14288  version_current = 1;
14289  }
14290  else
14291  {
14292  switch (migrate_is_available (old_version, new_version))
14293  {
14294  case -1:
14295  cleanup_manage_process (TRUE);
14296  return -1;
14297  case 0:
14298  cleanup_manage_process (TRUE);
14299  return 2;
14300  }
14301 
14302  /* Call the migrators to take the DB from the old version to the new. */
14303 
14304  migrators = database_migrators + old_version + 1;
14305 
14306  while ((migrators->version >= 0) && (migrators->version <= new_version))
14307  {
14308  if (migrators->function == NULL)
14309  {
14310  cleanup_manage_process (TRUE);
14311  return -1;
14312  }
14313 
14314  g_info (" Migrating to %i", migrators->version);
14315 
14316  if (migrators->function ())
14317  {
14318  cleanup_manage_process (TRUE);
14319  return -1;
14320  }
14321  migrators++;
14322  }
14323  }
14324 
14325  /* Migrate SCAP and CERT databases */
14326  old_scap_version = manage_scap_db_version ();
14327  new_scap_version = manage_scap_db_supported_version ();
14328  old_cert_version = manage_cert_db_version ();
14329  new_cert_version = manage_cert_db_supported_version ();
14330 
14331  if (old_scap_version == new_scap_version)
14332  {
14333  g_debug ("SCAP database already at current version");
14334  scap_version_current = 1;
14335  }
14336  else if (old_scap_version == -1)
14337  {
14338  g_message ("No SCAP database found for migration");
14339  scap_version_current = 1;
14340  }
14341  else if (old_scap_version > new_scap_version)
14342  {
14343  g_warning ("SCAP database version too new: %d", old_scap_version);
14344  return 11;
14345  }
14346  else
14347  {
14348  g_message ("Migrating SCAP database");
14349  switch (openvas_migrate_secinfo (SBINDIR "/openvas-scapdata-sync",
14350  SCAP_FEED))
14351  {
14352  case 0:
14353  g_message ("SCAP database migrated successfully");
14354  break;
14355  case 1:
14356  g_warning ("SCAP sync already running");
14357  cleanup_manage_process (TRUE);
14358  return 11;
14359  break;
14360  default:
14361  assert (0);
14362  case -1:
14363  cleanup_manage_process (TRUE);
14364  return -11;
14365  break;
14366  }
14367  }
14368 
14369  if (old_cert_version == new_cert_version)
14370  {
14371  g_debug ("CERT database already at current version");
14372  cert_version_current = 1;
14373  }
14374  else if (old_cert_version == -1)
14375  {
14376  g_message ("No CERT database found for migration");
14377  cert_version_current = 1;
14378  }
14379  else if (old_cert_version > new_cert_version)
14380  {
14381  g_warning ("CERT database version too new: %d", old_cert_version);
14382  return 12;
14383  }
14384  else
14385  {
14386  g_message ("Migrating CERT database");
14387  switch (openvas_migrate_secinfo (SBINDIR "/openvas-certdata-sync",
14388  CERT_FEED))
14389  {
14390  case 0:
14391  g_message ("CERT database migrated successfully");
14392  break;
14393  case 1:
14394  g_warning ("CERT sync already running");
14395  cleanup_manage_process (TRUE);
14396  return 12;
14397  break;
14398  default:
14399  assert (0);
14400  case -1:
14401  cleanup_manage_process (TRUE);
14402  return -12;
14403  break;
14404  }
14405  }
14406 
14407  if (version_current && scap_version_current && cert_version_current)
14408  {
14409  cleanup_manage_process (TRUE);
14410  return 1;
14411  }
14412 
14413  /* We now run ANALYZE after migrating, instead of on every startup. ANALYZE
14414  * made startup too slow, especially for large databases. Running it here
14415  * is preferred over removing it entirely, because users may have very
14416  * different use patterns of the database.
14417  *
14418  * Reopen the database before the ANALYZE, in case the schema has changed. */
14419  cleanup_manage_process (TRUE);
14420  init_manage_process (0, database);
14421  sql ("ANALYZE;");
14422 
14423  cleanup_manage_process (TRUE);
14424  return 0;
14425 }
int sql_is_sqlite3()
Get whether backend is SQLite3.
Definition: sql_pg.c:92
int migrate_58_to_59()
Migrate the database from version 58 to version 59.
void init_manage_process(int, const gchar *)
Initialize the manage library for a process.
Definition: manage_sql.c:14303
char * alert_data(alert_t alert, const char *type, const char *name)
Return data associated with an alert.
Definition: manage_sql.c:8810
GSList * log_config
Logging parameters, as passed to setup_log_handlers.
Definition: openvasmd.c:310
void migrate_179_to_180_update_ref(const gchar *table, int trash)
Update a reference for migrate_179_to_180.
void migrate_17_to_18_set_pref(config_t config)
Set the pref for migrate_17_to_18.
int migrate_174_to_175()
Migrate the database from version 174 to version 175.
int migrate_150_to_151()
Migrate the database from version 150 to version 151.
int migrate_148_to_149()
Migrate the database from version 148 to version 149.
long long int credential_t
Definition: manage.h:279
int migrate_27_to_28()
Migrate the database from version 27 to version 28.
#define INSERT_PERMISSION(name, role)
int migrate_32_to_33()
Migrate the database from version 32 to version 33.
#define MIN_QOD_DEFAULT
Default min quality of detection percentage for filters.
Definition: manage.h:1110
int migrate_88_to_89()
Migrate the database from version 88 to version 89.
int migrate_54_to_55_format(const char *old_uuid, const char *new_uuid)
Migrate a report format from version 54 to version 55.
int migrate_160_to_161()
Migrate the database from version 160 to version 161.
int migrate_113_to_114()
Migrate the database from version 113 to version 114.
lsc_crypt_ctx_t lsc_crypt_new()
Return a new context for LSC encryption.
Definition: lsc_crypt.c:507
void migrate_33_to_34_set_pref(config_t config)
Set the pref for migrate_33_to_34.
void parse_tags(const char *scanner_tags, gchar **tags, gchar **cvss_base)
Split up the tags received from the scanner.
Definition: manage.c:7241
int migrate_138_to_139()
Migrate the database from version 138 to version 139.
int migrate_60_to_61()
Migrate the database from version 60 to version 61.
int migrate_36_to_37()
Migrate the database from version 36 to version 37.
const char * sql_schema()
Get main schema name.
Definition: sql_pg.c:103
int migrate_97_to_98()
Migrate the database from version 97 to version 98.
void migrate_55_to_56_ensure_predefined_port_lists_exist()
Ensure that the predefined port lists exist.
int migrate_139_to_140()
Migrate the database from version 139 to version 140.
long long int alert_t
Definition: manage.h:280
int manage_scap_db_supported_version()
Return the database version supported by this manager.
Definition: manage_sql.c:6288
#define LOCATION_TRASH
Location of a constituent of a trashcan resource.
Definition: manage_sql.h:86
int migrate_17_to_18()
Migrate the database from version 17 to version 18.
long long int target_t
Definition: manage.h:285
int migrate_66_to_67()
Migrate the database from version 66 to version 67.
int manage_scap_loaded()
Check whether SCAP is available.
Definition: manage_pg.c:2659
int sql_int(char *sql,...)
Get a particular cell from a SQL query, as an int.
Definition: sql.c:438
int migrate_19_to_20()
Migrate the database from version 19 to version 20.
int migrate_159_to_160()
Migrate the database from version 159 to version 160.
long long int config_t
Definition: manage.h:278
#define CONFIG_UUID_FULL_AND_FAST
UUID of 'Full and fast' config.
Definition: manage_sql.h:39
int migrate_35_to_36()
Migrate the database from version 35 to version 36.
int migrate_38_to_39()
Migrate the database from version 38 to version 39.
int migrate_176_to_177()
Migrate the database from version 176 to version 177.
int version
Version that the migrator produces.
int migrate_20_to_21()
Migrate the database from version 20 to version 21.
#define CONFIG_UUID_FULL_AND_FAST_ULTIMATE
UUID of 'Full and fast ultimate' config.
Definition: manage_sql.h:44
int iterator_int(iterator_t *iterator, int col)
Get a int column from an iterator.
Definition: sql.c:622
int migrate_98_to_99()
Migrate the database from version 98 to version 99.
int migrate_100_to_101()
Migrate the database from version 100 to version 101.
int migrate_22_to_23()
Migrate the report formats from version 22 to version 23.
int migrate_2_to_3()
Migrate the database from version 2 to version 3.
int migrate_12_to_13()
Migrate the database from version 12 to version 13.
int migrate_80_to_81()
Migrate the database from version 80 to version 81.
void sql_begin_exclusive()
Begin an exclusive transaction.
Definition: sql_pg.c:598
resource_t sql_last_insert_id()
Get the ID of the last inserted row.
Definition: sql_pg.c:395
int migrate_50_to_51()
Migrate the database from version 50 to version 51.
int migrate_155_to_156()
Migrate the database from version 155 to version 156.
A generic SQL iterator.
Definition: iterator.h:52
int migrate_128_to_129()
Migrate the database from version 128 to version 129.
int migrate_43_to_44()
Migrate the database from version 43 to version 44.
void cleanup_manage_process(gboolean)
Cleanup the manage library.
Definition: manage_sql.c:17128
int migrate_86_to_87()
Migrate the database from version 86 to version 87.
int migrate_182_to_183()
Migrate the database from version 182 to version 183.
int migrate_178_to_179()
Migrate the database from version 178 to version 179.
int migrate_129_to_130()
Migrate the database from version 129 to version 130.
int migrate_127_to_128()
Migrate the database from version 127 to version 128.
int migrate_156_to_157()
Migrate the database from version 156 to version 157.
int migrate_122_to_123()
Migrate the database from version 122 to version 123.
target_t migrate_35_to_36_duplicate_target(target_t target, const char *name)
Make a copy of a target.
void sql_rollback()
Roll a transaction back.
Definition: sql_pg.c:658
int migrate_5_to_6()
Migrate the database from version 5 to version 6.
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:199
#define QOD_DEFAULT
Default quality of detection percentage.
Definition: manage.h:1105
int manage_cert_loaded()
Check whether CERT is available.
Definition: manage_pg.c:2643
int migrate_137_to_138()
Migrate the database from version 137 to version 138.
lsc_crypt_ctx_t crypt_ctx
Encryption context.
Definition: iterator.h:57
int migrate_163_to_164()
Migrate the database from version 163 to version 164.
int migrate_57_to_58()
Migrate the database from version 57 to version 58.
int migrate_134_to_135()
Migrate the database from version 134 to version 135.
int migrate_145_to_146()
Migrate the database from version 145 to version 146.
int migrate_71_to_72()
Migrate the database from version 71 to version 72.
int migrate_85_to_86()
Migrate the database from version 85 to version 86.
int migrate_104_to_105()
Migrate the database from version 104 to version 105.
#define CONFIG_ID_FULL_AND_VERY_DEEP
Database ROWID of 'Full and very deep' config.
#define MIGRATE_162_TO_163_CONTROL_DESCRIPTION
Description for Verinice ISM report format.
long long int nvt_t
Definition: manage.h:294
int sql_int64(long long int *ret, char *sql,...)
Get a particular cell from a SQL query, as an int64.
Definition: sql.c:501
int migrate_10_to_11()
Migrate the database from version 10 to version 11.
int migrate_147_to_148()
Migrate the database from version 147 to version 148.
int migrate_154_to_155()
Migrate the database from version 154 to version 155.
int migrate_169_to_170()
Migrate the database from version 169 to version 170.
int migrate_52_to_53()
Migrate the database from version 52 to version 53.
int migrate_7_to_8()
Migrate the database from version 7 to version 8.
int migrate_55_to_56()
Migrate the database from version 55 to version 56.
int migrate_105_to_106()
Migrate the database from version 105 to version 106.
int migrate_119_to_120()
Migrate the database from version 119 to version 120.
int migrate_103_to_104()
Migrate the database from version 103 to version 104.
#define CONFIG_UUID_DISCOVERY
UUID of 'Discovery' config.
Definition: manage_sql.h:66
int migrate_79_to_80()
Migrate the database from version 79 to version 80.
int migrate_110_to_111()
Migrate the database from version 110 to version 111.
int sql_exec(sql_stmt_t *stmt)
Execute a prepared statement.
Definition: sql.c:776
#define ROLE_UUID_USER
Predefined role UUID.
Definition: manage_sql.h:188
int migrate_158_to_159()
Migrate the database from version 158 to version 159.
int migrate_4_to_5()
Migrate the database from version 4 to version 5.
#define ROLE_UUID_SUPER_ADMIN
Predefined role UUID.
Definition: manage_sql.h:193
int migrate_14_to_15()
Migrate the database from version 14 to version 15.
int migrate_132_to_133()
Migrate the database from version 132 to version 133.
int migrate_101_to_102()
Migrate the database from version 101 to version 102.
const char * lsc_crypt_get_private_key(lsc_crypt_ctx_t ctx, const char *ciphertext)
Return an encrypted private key in the clear.
Definition: lsc_crypt.c:864
int migrate_125_to_126()
Migrate the database from version 125 to version 126.
int migrate_45_to_46()
Migrate the database from version 45 to version 46.
int migrate_114_to_115()
Migrate the database from version 114 to version 115.
int migrate_181_to_182()
Migrate the database from version 181 to version 182.
int migrate_124_to_125()
Migrate the database from version 124 to version 125.
int migrate_89_to_90()
Migrate the database from version 89 to version 90.
int migrate_29_to_30()
Migrate the database from version 29 to version 30.
int migrate_133_to_134()
Migrate the database from version 133 to version 134.
int migrate_64_to_65()
Migrate the database from version 64 to version 65.
#define NVT_SELECTOR_TYPE_NVT
NVT selector type for "NVT" rule.
Definition: manage.h:2001
const char * nvt_selector_iterator_nvt(iterator_t *)
gchar * sql_quote(const char *string)
Quotes a string to be passed to sql statements.
Definition: sql.c:121
int modify_asset(const char *, const char *)
#define ID_WHEN_WITH_TRASH(type)
int sql_changes()
Return 0.
Definition: sql_pg.c:385
int disable_encrypted_credentials
Flag indicating that encrypted credentials are disabled.
Definition: openvasmd.c:275
#define ID_WHEN_WITHOUT_TRASH(type)
int migrate_90_to_91()
Migrate the database from version 90 to version 91.
int migrate_170_to_171()
Migrate the database from version 170 to version 171.
long long int user_t
Definition: manage.h:302
int migrate_99_to_100()
Migrate the database from version 99 to version 100.
#define ROLE_UUID_OBSERVER
Predefined role UUID.
Definition: manage_sql.h:198
#define MIGRATE_55_TO_56_RANGE(type, start, end)
Insert a port range.
int migrate_84_to_85()
Migrate the database from version 84 to version 85.
#define MIGRATE_79_to_80_DELETE(table)
int migrate_106_to_107()
Migrate the database from version 106 to version 107.
int migrate_177_to_178()
Migrate the database from version 177 to version 178.
int migrate_180_to_181()
Migrate the database from version 180 to version 181.
#define ROLE_UUID_GUEST
Predefined role UUID.
Definition: manage_sql.h:173
#define CONFIG_UUID_FULL_AND_VERY_DEEP
UUID of 'Full and very deep' config.
Definition: manage_sql.h:50
gchar * task_db_name
Name of the database file.
Definition: manage_sql.c:403
#define DELETE_PERMISSION(name, role)
int migrate_9_to_10()
Migrate the database from version 9 to version 10.
int migrate_123_to_124()
Migrate the database from version 123 to version 124.
int migrate_161_to_162()
Migrate the database from version 161 to version 162.
int migrate_126_to_127()
Migrate the database from version 126 to version 127.
int migrate_181_to_182_move(const char *dest)
Move signatures.
int migrate_54_to_55()
Migrate the database from version 54 to version 55.
int migrate_72_to_73()
Migrate the database from version 72 to version 73.
int migrate_79_to_80_user_access(const gchar *user_dir, gchar **hosts, int *hosts_allow)
Get access information for a user.
int migrate_48_to_49()
Migrate the database from version 48 to version 49.
#define CONFIG_UUID_EMPTY
UUID of 'Empty' config.
Definition: manage_sql.h:61
int migrate_59_to_60()
Migrate the database from version 59 to version 60.
const char * lsc_crypt_get_password(lsc_crypt_ctx_t ctx, const char *ciphertext)
Return an encrypted password in the clear.
Definition: lsc_crypt.c:840
int migrate_135_to_136()
Migrate the database from version 135 to version 136.
void migrate_5_to_6_move_other_config(const char *predefined_config_name, config_t predefined_config_id)
Move a config that is using a predefined ID.
The context object for encryption operations.
Definition: lsc_crypt.c:87
int migrate_116_to_117()
Migrate the database from version 116 to version 117.
#define CONFIG_ID_FULL_AND_FAST_ULTIMATE
Database ROWID of 'Full and fast ultimate' config.
#define SCANNER_UUID_DEFAULT
UUID of 'OpenVAS Default' scanner.
Definition: manage_sql.h:203
int migrate_69_to_70()
Migrate the database from version 69 to version 70.
int migrate_39_to_40()
Migrate the database from version 39 to version 40.
int migrate_83_to_84()
Migrate the database from version 83 to version 84.
#define RESOURCE_TRASH(type)
#define CONFIG_ID_FULL_AND_FAST
Database ROWID of 'Full and fast' config.
#define CLIENTCERT
Location of client certificate.
Definition: openvasmd.c:172
int migrate_15_to_16()
Migrate the database from version 15 to version 16.
int migrate_33_to_34()
Migrate the database from version 33 to version 34.
int migrate_95_to_96()
Migrate the database from version 95 to version 96.
int migrate_94_to_95()
Migrate the database from version 94 to version 95.
int migrate_146_to_147()
Migrate the database from version 146 to version 147.
#define RULES_HEADER
int migrate_167_to_168()
Migrate the database from version 167 to version 168.
A migrator.
int migrate_96_to_97()
Migrate the database from version 96 to version 97.
int migrate_75_to_76()
Migrate the database from version 75 to version 76.
#define UPDATE_CHART_SETTINGS(type, default, left_uuid, right_uuid)
#define MANAGE_NVT_SELECTOR_UUID_DISCOVERY
UUID of 'Discovery' NVT selector.
Definition: manage_sql.h:96
int migrate_111_to_112()
Migrate the database from version 111 to version 112.
int delete_asset(const char *, const char *, int)
int migrate_37_to_38()
Migrate the database from version 37 to version 38.
int migrate_74_to_75()
Migrate the database from version 74 to version 75.
int migrate_140_to_141()
Migrate the database from version 140 to version 141.
int migrate_6_to_7()
Migrate the database from version 6 to version 7.
int migrate_81_to_82()
Migrate the database from version 81 to version 82.
int migrate_91_to_92()
Migrate the database from version 91 to version 92.
int migrate_47_to_48()
Migrate the database from version 47 to version 48.
int migrate_152_to_153()
Migrate the database from version 152 to version 153.
int migrate_164_to_165()
Migrate the database from version 163 to version 164.
int migrate_76_to_77()
Migrate the database from version 76 to version 77.
int migrate_62_to_63()
Migrate the database from version 62 to version 63.
int migrate_56_to_57()
Migrate the database from version 56 to version 57.
int migrate_30_to_31()
Migrate the database from version 30 to version 31.
gchar * migrate_9_to_10_user_uuid(const char *name)
Return the UUID of a user from the OpenVAS user UUID file.
#define TRUST_UNKNOWN
Trust constant for unknown.
Definition: manage_sql.h:243
int openvas_migrate_secinfo(const gchar *, int)
int migrate_34_to_35()
Migrate the database from version 34 to version 35.
int migrate_67_to_68()
Migrate the database from version 67 to version 68.
int migrate_92_to_93()
Migrate the database from version 92 to version 93.
int qod_from_type(const char *)
int migrate_23_to_24()
Migrate the database from version 23 to version 24.
int migrate_118_to_119()
Migrate the database from version 118 to version 119.
int migrate_142_to_143()
Migrate the database from version 142 to version 143.
void init_nvt_selector_iterator(iterator_t *, const char *, config_t, int)
int migrate_16_to_17()
Migrate the database from version 16 to version 17.
int migrate_82_to_83()
Migrate the database from version 82 to version 83.
int migrate_31_to_32()
Migrate the database from version 31 to version 32.
int migrate_171_to_172()
Migrate the database from version 171 to version 172.
int migrate_21_to_22()
Migrate the report formats from version 21 to version 22.
#define SCAP_FEED
Definition: manage.h:4142
void migrate_39_to_40_set_pref(config_t config)
Set the pref for migrate_39_to_40.
#define ALL_LOG_LEVELS
Flag with all Glib log levels.
Definition: manage.h:47
#define UPDATE_DASHBOARD_SETTINGS(type, default, uuid_1, uuid_2, uuid_3, uuid_4, filter_1, filter_2, filter_3, filter_4)
void migrate_4_to_5_copy_data()
Move all the data to the new tables for the 4 to 5 migrator.
int migrate_13_to_14()
Migrate the database from version 13 to version 14.
void cleanup_iterator(iterator_t *)
Cleanup an iterator.
Definition: sql.c:664
#define CERT_FEED
Definition: manage.h:4143
int migrate_18_to_19()
Migrate the database from version 18 to version 19.
int migrate_144_to_145()
Migrate the database from version 144 to version 145.
gchar * tag_value(const gchar *tags, const gchar *tag)
Extract a tag from an OTP tag list.
Definition: manage_sql.c:1269
int migrate_168_to_169()
Migrate the database from version 168 to version 169.
int migrate_78_to_79()
Migrate the database from version 78 to version 79.
char * lsc_crypt_encrypt(lsc_crypt_ctx_t ctx, const char *first_name,...)
Encrypt a list of name/value pairs.
Definition: lsc_crypt.c:616
int migrate_153_to_154()
Migrate the database from version 152 to version 153.
int migrate_131_to_132()
Migrate the database from version 131 to version 132.
int migrate_166_to_167()
Migrate the database from version 166 to version 167.
int migrate_117_to_118()
Migrate the database from version 117 to version 118.
int manage_migrate(GSList *log_config, const gchar *database)
Migrate database to version supported by this manager.
int manage_db_supported_version()
Return the database version supported by this manager.
Definition: manage_sql.c:6250
int migrate_11_to_12()
Migrate the database from version 11 to version 12.
credentials_t current_credentials
Current credentials during any OMP command.
Definition: manage.c:717
#define g_info(...)
Defines g_info for glib versions older than 2.40.
Definition: manage.h:55
#define G_LOG_DOMAIN
GLib log domain.
int manage_cert_db_version()
Return the database version of the actual database.
Definition: manage_sql.c:6333
#define CONFIG_ID_FULL_AND_VERY_DEEP_ULTIMATE
Database ROWID of 'Full and very deep ultimate' config.
int migrate_136_to_137()
Migrate the database from version 136 to version 137.
int migrate_26_to_27()
Migrate the database from version 26 to version 27.
int migrate_8_to_9()
Migrate the database from version 8 to version 9.
const char * iterator_string(iterator_t *iterator, int col)
Get a string column from an iterator.
Definition: sql.c:652
#define CLIENTKEY
Location of client certificate private key.
Definition: openvasmd.c:179
int manage_create_migrate_51_to_52_convert()
Dummy for SQLite3 compatibility.
Definition: manage_pg.c:2695
int migrate_46_to_47()
Migrate the database from version 46 to version 47.
#define CACERT
Location of Certificate Authority certificate.
Definition: openvasmd.c:165
int migrate_63_to_64()
Migrate the database from version 63 to version 64.
int migrate_70_to_71()
Migrate the database from version 70 to version 71.
int migrate_109_to_110()
Migrate the database from version 109 to version 110.
#define CONFIG_UUID_HOST_DISCOVERY
UUID of 'Host Discovery' config.
Definition: manage_sql.h:71
int migrate_49_to_50()
Migrate the database from version 49 to version 50.
#define CONFIG_UUID_FULL_AND_VERY_DEEP_ULTIMATE
UUID of 'Full and very deep ultimate' config.
Definition: manage_sql.h:55
sql_stmt_t * sql_prepare(const char *sql,...)
Prepare a statement.
Definition: sql.c:753
#define ROLE_UUID_ADMIN
Predefined role UUID.
Definition: manage_sql.h:168
int migrate_141_to_142()
Migrate the database from version 141 to version 142.
int migrate_173_to_174()
Migrate the database from version 173 to version 174.
int migrate_44_to_45()
Migrate the database from version 44 to version 45.
int migrate_61_to_62()
Migrate the database from version 61 to version 62.
int migrate_162_to_163()
Migrate the database from version 158 to version 162.
int migrate_115_to_116()
Migrate the database from version 115 to version 116.
int manage_db_version()
Return the database version of the actual database.
Definition: manage_sql.c:6262
char * sql_string(char *sql,...)
Get a particular cell from a SQL query, as an string.
Definition: sql.c:469
int migrate_183_to_184()
Migrate the database from version 183 to version 184.
int migrate_179_to_180()
Migrate the database from version 179 to version 180.
void sql_commit()
Commit a transaction.
Definition: sql_pg.c:649
int migrate_24_to_25()
Migrate the database from version 24 to version 25.
int migrate_165_to_166()
Migrate the database from version 165 to version 166.
int migrate_53_to_54()
Migrate the database from version 53 to version 54.
int migrate_112_to_113()
Migrate the database from version 112 to version 113.
int migrate_151_to_152()
Migrate the database from version 151 to version 152.
int manage_cert_db_supported_version()
Return the database version supported by this manager.
Definition: manage_sql.c:6322
gchar * sql_insert(const char *string)
Get the SQL insert expression for a string.
Definition: sql.c:136
int migrate_121_to_122()
Migrate the database from version 121 to version 122.
int migrate_3_to_4()
Migrate the database from version 3 to version 4.
int migrate_172_to_173()
Migrate the database from version 172 to version 173.
int migrate_1_to_2()
Migrate the database from version 1 to version 2.
void migrate_79_to_80_remove_users(const char *where)
Delete users according to a condition.
void sql_rename_column(const char *old_table, const char *new_table, const char *old_name, const char *new_name)
Move data from a table to a new table, heeding column rename.
Definition: manage_pg.c:101
gboolean next(iterator_t *)
Increment an iterator.
Definition: sql.c:689
int migrate_25_to_26()
Migrate the database from version 25 to version 26.
int migrate_65_to_66()
Migrate the database from version 65 to version 66.
int migrate_77_to_78()
Migrate the database from version 77 to version 78.
int migrate_143_to_144()
Migrate the database from version 143 to version 144.
int migrate_149_to_150()
Migrate the database from version 149 to version 150.
int migrate_42_to_43()
Migrate the database from version 42 to version 43.
void lsc_crypt_release(lsc_crypt_ctx_t ctx)
Release an LSC encryption context.
Definition: lsc_crypt.c:528
#define LOCATION_TABLE
Location of a constituent of a trashcan resource.
Definition: manage_sql.h:81
void init_iterator(iterator_t *iterator, const char *sql,...)
Initialise an iterator.
Definition: sql.c:577
int migrate_40_to_41()
Migrate the database from version 40 to version 41.
void check_generate_scripts()
Ensure the generate scripts are all executable.
Definition: manage_sql.c:16716
#define PORT_LIST_UUID_DEFAULT
UUID of 'OpenVAS Default' port list.
Definition: manage_sql.h:123
gchar * sql_nquote(const char *string, size_t length)
Quotes a string of a known length to be passed to sql statements.
Definition: sql.c:76
int migrate_0_to_1()
Migrate the database from version 0 to version 1.
int migrate_51_to_52()
Migrate the database from version 51 to version 52.
int migrate_28_to_29()
Migrate the database from version 27 to version 28.
int migrate_93_to_94()
Migrate the database from version 93 to version 94.
int migrate_120_to_121()
Migrate the database from version 120 to version 121.
int(* function)()
Function that does the migration. NULL if too hard.
long long int iterator_int64(iterator_t *iterator, int col)
Get an integer column from an iterator.
Definition: sql.c:637
int migrate_is_available(int old_version, int new_version)
Check whether a migration is available.
int sql_bind_text(sql_stmt_t *, int, const gchar *, gsize)
Bind a text value to a statement.
Definition: sql_pg.c:807
#define SQLITE_OR_NULL(function)
long long int scanner_t
Definition: manage.h:300
int manage_scap_db_version()
Return the database version of the actual database.
Definition: manage_sql.c:6299
#define MANAGE_NVT_SELECTOR_UUID_ALL
UUID of 'All' NVT selector.
Definition: manage_sql.h:91
int migrate_107_to_108()
Migrate the database from version 107 to version 108.
#define ROLE_UUID_INFO
Predefined role UUID.
Definition: manage_sql.h:178
int migrate_73_to_74()
Migrate the database from version 73 to version 74.
int migrate_108_to_109()
Migrate the database from version 108 to version 109.
int migrate_157_to_158()
Migrate the database from version 157 to version 158.
int migrate_175_to_176()
Migrate the database from version 175 to version 176.
const char * nvt_selector_iterator_name(iterator_t *)
gboolean manage_migrate_needs_timezone(GSList *log_config, const gchar *database)
Check whether the migration needs the real timezone.
int migrate_87_to_88()
Migrate the database from version 87 to version 88.
void sql_finalize(sql_stmt_t *)
Free a prepared statement.
Definition: sql_pg.c:824
int migrate_102_to_103()
Migrate the database from version 102 to version 103.
int migrate_41_to_42()
Migrate the database from version 41 to version 42.
long long int resource_t
A resource, like a task or target.
Definition: iterator.h:42
int migrate_68_to_69()
Migrate the database from version 68 to version 69.
long long int report_format_t
Definition: manage.h:290
int migrate_130_to_131()
Migrate the database from version 130 to version 131.
void set_db_version(int version)
Set the database version of the actual database.
Definition: manage_sql.c:6416