[Openvas-commits] r12004 - in trunk/openvas-manager: . doc src

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Nov 4 19:52:49 CET 2011


Author: mattm
Date: 2011-11-04 19:52:45 +0100 (Fri, 04 Nov 2011)
New Revision: 12004

Modified:
   trunk/openvas-manager/CMakeLists.txt
   trunk/openvas-manager/ChangeLog
   trunk/openvas-manager/doc/db.png
   trunk/openvas-manager/doc/db_postgres.sql
   trunk/openvas-manager/src/manage.h
   trunk/openvas-manager/src/manage_sql.c
   trunk/openvas-manager/src/omp.c
Log:
	Add override end times.  Hence increase database version to 53.  Add
	database migration from version 52 to 53.

	* CMakeLists.txt (OPENVASMD_DATABASE_VERSION): Increase to 53.

	* src/manage_sql.c (sql_now, migrate_52_to_53): New functions.
	(create_tables): Add end_time to overrides.
	(database_migrators): Add migrate_52_to_53.
	(init_manage_process): Create SQL now function.
	(task_threat_level, task_previous_threat_level, init_result_iterator)
	(report_scan_result_count, report_count, report_count_filtered)
	(report_counts_id_filt, modify_override): Add end_time to override check.
	(override_iterator_end_time, override_iterator_active): New functions.

	* src/manage.h: Add headers accordingly.

	* src/omp.c (buffer_overrides_xml): Add ACTIVE and END_TIME.

	* doc/db_postgres.sql (overrides): Add column end_time.

	* doc/db.png: Update from source.

Modified: trunk/openvas-manager/CMakeLists.txt
===================================================================
--- trunk/openvas-manager/CMakeLists.txt	2011-11-04 16:43:50 UTC (rev 12003)
+++ trunk/openvas-manager/CMakeLists.txt	2011-11-04 18:52:45 UTC (rev 12004)
@@ -105,7 +105,7 @@
 
 ## Variables
 
-set (OPENVASMD_DATABASE_VERSION 52)
+set (OPENVASMD_DATABASE_VERSION 53)
 
 if (SYSCONF_INSTALL_DIR)
   set (SYSCONFDIR "${SYSCONF_INSTALL_DIR}")

Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog	2011-11-04 16:43:50 UTC (rev 12003)
+++ trunk/openvas-manager/ChangeLog	2011-11-04 18:52:45 UTC (rev 12004)
@@ -1,5 +1,29 @@
 2011-11-04  Matthew Mundell <matthew.mundell at greenbone.net>
 
+	Add override end times.  Hence increase database version to 53.  Add
+	database migration from version 52 to 53.
+
+	* CMakeLists.txt (OPENVASMD_DATABASE_VERSION): Increase to 53.
+
+	* src/manage_sql.c (sql_now, migrate_52_to_53): New functions.
+	(create_tables): Add end_time to overrides.
+	(database_migrators): Add migrate_52_to_53.
+	(init_manage_process): Create SQL now function.
+	(task_threat_level, task_previous_threat_level, init_result_iterator)
+	(report_scan_result_count, report_count, report_count_filtered)
+	(report_counts_id_filt, modify_override): Add end_time to override check.
+	(override_iterator_end_time, override_iterator_active): New functions.
+
+	* src/manage.h: Add headers accordingly.
+
+	* src/omp.c (buffer_overrides_xml): Add ACTIVE and END_TIME.
+
+	* doc/db_postgres.sql (overrides): Add column end_time.
+
+	* doc/db.png: Update from source.
+
+2011-11-04  Matthew Mundell <matthew.mundell at greenbone.net>
+
 	* src/omp.c (buffer_notes_xml, buffer_overrides_xml): Copy ctime return to
 	preserve it across multiple calls.
 

Modified: trunk/openvas-manager/doc/db.png
===================================================================
(Binary files differ)

Modified: trunk/openvas-manager/doc/db_postgres.sql
===================================================================
--- trunk/openvas-manager/doc/db_postgres.sql	2011-11-04 16:43:50 UTC (rev 12003)
+++ trunk/openvas-manager/doc/db_postgres.sql	2011-11-04 18:52:45 UTC (rev 12004)
@@ -397,7 +397,8 @@
 	port text,
 	threat text,
 	task integer REFERENCES tasks (id) ON DELETE RESTRICT,
-	result integer REFERENCES results (id) ON DELETE RESTRICT);
+	result integer REFERENCES results (id) ON DELETE RESTRICT,
+	end_time integer);
 
 CREATE TABLE schedules (
 	id integer PRIMARY KEY,

Modified: trunk/openvas-manager/src/manage.h
===================================================================
--- trunk/openvas-manager/src/manage.h	2011-11-04 16:43:50 UTC (rev 12003)
+++ trunk/openvas-manager/src/manage.h	2011-11-04 18:52:45 UTC (rev 12004)
@@ -1534,6 +1534,12 @@
 result_t
 override_iterator_result (iterator_t*);
 
+time_t
+override_iterator_end_time (iterator_t*);
+
+int
+override_iterator_active (iterator_t*);
+
 const char*
 override_iterator_nvt_name (iterator_t *);
 

Modified: trunk/openvas-manager/src/manage_sql.c
===================================================================
--- trunk/openvas-manager/src/manage_sql.c	2011-11-04 16:43:50 UTC (rev 12003)
+++ trunk/openvas-manager/src/manage_sql.c	2011-11-04 18:52:45 UTC (rev 12004)
@@ -891,6 +891,22 @@
     }
 }
 
+/**
+ * @brief Get the current time as an epoch integer.
+ *
+ * This is a callback for a scalar SQL function of zero arguments.
+ *
+ * @param[in]  context  SQL context.
+ * @param[in]  argc     Number of arguments.
+ * @param[in]  argv     Argument array.
+ */
+static void
+sql_now (sqlite3_context *context, int argc, sqlite3_value** argv)
+{
+  assert (argc == 0);
+  sqlite3_result_int (context, time (NULL));
+}
+
 
 /* General helpers. */
 
@@ -1333,7 +1349,7 @@
   sql ("CREATE INDEX IF NOT EXISTS nvts_by_oid ON nvts (oid);");
   sql ("CREATE INDEX IF NOT EXISTS nvts_by_name ON nvts (name);");
   sql ("CREATE INDEX IF NOT EXISTS nvts_by_family ON nvts (family);");
-  sql ("CREATE TABLE IF NOT EXISTS overrides (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt, creation_time, modification_time, text, hosts, port, threat, new_threat, task INTEGER, result INTEGER);");
+  sql ("CREATE TABLE IF NOT EXISTS overrides (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt, creation_time, modification_time, text, hosts, port, threat, new_threat, task INTEGER, result INTEGER, end_time);");
   sql ("CREATE TABLE IF NOT EXISTS report_host_details (id INTEGER PRIMARY KEY, report_host INTEGER, source_type, source_name, source_description, name, value);");
   sql ("CREATE TABLE IF NOT EXISTS report_hosts (id INTEGER PRIMARY KEY, report INTEGER, host, start_time, end_time, attack_state, current_port, max_port);");
   sql ("CREATE INDEX IF NOT EXISTS report_hosts_by_host ON report_hosts (host);");
@@ -5399,6 +5415,40 @@
 }
 
 /**
+ * @brief Migrate the database from version 52 to version 53.
+ *
+ * @return 0 success, -1 error.
+ */
+static int
+migrate_52_to_53 ()
+{
+  sql ("BEGIN EXCLUSIVE;");
+
+  /* Ensure that the database is currently version 52. */
+
+  if (manage_db_version () != 52)
+    {
+      sql ("ROLLBACK;");
+      return -1;
+    }
+
+  /* Update the database. */
+
+  /* The overrides table got a end_time column. */
+
+  sql ("ALTER TABLE overrides ADD column end_time;");
+  sql ("UPDATE overrides SET end_time = 0;");
+
+  /* Set the database version to 53. */
+
+  set_db_version (53);
+
+  sql ("COMMIT;");
+
+  return 0;
+}
+
+/**
  * @brief Array of database version migrators.
  */
 static migrator_t database_migrators[]
@@ -5455,6 +5505,7 @@
     {50, migrate_49_to_50},
     {51, migrate_50_to_51},
     {52, migrate_51_to_52},
+    {53, migrate_52_to_53},
     /* End marker. */
     {-1, NULL}};
 
@@ -8270,6 +8321,20 @@
           g_warning ("%s: failed to create uniquify", __FUNCTION__);
           abort ();
         }
+
+      if (sqlite3_create_function (task_db,
+                                   "now",
+                                   0,               /* Number of args. */
+                                   SQLITE_UTF8,
+                                   NULL,            /* Callback data. */
+                                   sql_now,
+                                   NULL,            /* xStep. */
+                                   NULL)            /* xFinal. */
+          != SQLITE_OK)
+        {
+          g_warning ("%s: failed to create now", __FUNCTION__);
+          abort ();
+        }
     }
 }
 
@@ -10125,6 +10190,8 @@
          " OR (overrides.owner ="
          " (SELECT ROWID FROM users"
          "  WHERE users.uuid = '%s')))"
+         " AND ((overrides.end_time = 0)"
+         "      OR (overrides.end_time >= now ()))"
          " AND (overrides.task ="
          "      (SELECT reports.task FROM reports"
          "       WHERE report_results.report = reports.ROWID)"
@@ -10233,6 +10300,8 @@
          " OR (overrides.owner ="
          " (SELECT ROWID FROM users"
          "  WHERE users.uuid = '%s')))"
+         " AND ((overrides.end_time = 0)"
+         "      OR (overrides.end_time >= now ()))"
          " AND (overrides.task ="
          "      (SELECT reports.task FROM reports"
          "       WHERE report_results.report = reports.ROWID)"
@@ -11841,6 +11910,8 @@
                  " OR (overrides.owner ="
                  " (SELECT ROWID FROM users"
                  "  WHERE users.uuid = '%s')))"
+                 " AND ((overrides.end_time = 0)"
+                 "      OR (overrides.end_time >= now ()))"
                  " AND (overrides.task ="
                  "      (SELECT reports.task FROM reports"
                  "       WHERE report_results.report = reports.ROWID)"
@@ -12004,6 +12075,8 @@
                  " OR (overrides.owner ="
                  " (SELECT ROWID FROM users"
                  "  WHERE users.uuid = '%s')))"
+                 " AND ((overrides.end_time = 0)"
+                 "      OR (overrides.end_time >= now ()))"
                  " AND (overrides.task ="
                  "      (SELECT reports.task FROM reports, report_results"
                  "       WHERE report_results.result = results.ROWID"
@@ -12974,6 +13047,8 @@
              " OR (overrides.owner ="
              " (SELECT ROWID FROM users"
              "  WHERE users.uuid = '%s')))"
+             " AND ((overrides.end_time = 0)"
+             "      OR (overrides.end_time >= now ()))"
              " AND (overrides.task ="
              "      (SELECT reports.task FROM reports"
              "       WHERE report_results.report = reports.ROWID)"
@@ -13041,10 +13116,12 @@
       && sql_int (0, 0,
                   "SELECT count(*)"
                   " FROM overrides"
-                  " WHERE (overrides.owner IS NULL)"
-                  " OR (overrides.owner ="
-                  " (SELECT ROWID FROM users"
-                  "  WHERE users.uuid = '%s'))",
+                  " WHERE ((overrides.owner IS NULL)"
+                  "        OR (overrides.owner ="
+                  "            (SELECT ROWID FROM users"
+                  "             WHERE users.uuid = '%s')))"
+                  " AND ((overrides.end_time = 0)"
+                  "      OR (overrides.end_time >= now ()))",
                   current_credentials.uuid))
     {
       int count;
@@ -13060,7 +13137,9 @@
       select = g_strdup_printf ("SELECT 1 FROM overrides"
                                 " WHERE (overrides.nvt = $nvt)"
                                 " AND ((overrides.owner IS NULL) OR (overrides.owner ="
-                                " (SELECT ROWID FROM users WHERE users.uuid = '%s')))",
+                                " (SELECT ROWID FROM users WHERE users.uuid = '%s')))"
+                                " AND ((overrides.end_time = 0)"
+                                "      OR (overrides.end_time >= now ()))",
                                 current_credentials.uuid);
       while (1)
         {
@@ -13098,6 +13177,8 @@
                  " OR (overrides.owner ="
                  " (SELECT users.ROWID FROM users"
                  "  WHERE users.uuid = '%s')))"
+                 " AND ((overrides.end_time = 0)"
+                 "      OR (overrides.end_time >= now ()))"
                  " AND (overrides.task = 0"
                  "      OR overrides.task = %llu)"
                  " AND (overrides.result = 0"
@@ -13443,10 +13524,12 @@
       && sql_int (0, 0,
                   "SELECT count(*)"
                   " FROM overrides"
-                  " WHERE (overrides.owner IS NULL)"
-                  " OR (overrides.owner ="
-                  " (SELECT ROWID FROM users"
-                  "  WHERE users.uuid = '%s'))",
+                  " WHERE ((overrides.owner IS NULL)"
+                  "        OR (overrides.owner ="
+                  "            (SELECT ROWID FROM users"
+                  "             WHERE users.uuid = '%s'))"
+                  " AND ((overrides.end_time = 0)"
+                  "      OR (overrides.end_time >= now ()))",
                   current_credentials.uuid))
     {
       int count;
@@ -13462,7 +13545,9 @@
       select = g_strdup_printf ("SELECT 1 FROM overrides"
                                 " WHERE (overrides.nvt = $nvt)"
                                 " AND ((overrides.owner IS NULL) OR (overrides.owner ="
-                                " (SELECT ROWID FROM users WHERE users.uuid = '%s')))",
+                                " (SELECT ROWID FROM users WHERE users.uuid = '%s')))"
+                                " AND ((overrides.end_time = 0)"
+                                "      OR (overrides.end_time >= now ()))",
                                 current_credentials.uuid);
       while (1)
         {
@@ -13500,6 +13585,8 @@
                  " OR (overrides.owner ="
                  " (SELECT users.ROWID FROM users"
                  "  WHERE users.uuid = '%s')))"
+                 " AND ((overrides.end_time = 0)"
+                 "      OR (overrides.end_time >= now ()))"
                  " AND (overrides.task = 0"
                  "      OR overrides.task = %llu)"
                  " AND (overrides.result = 0"
@@ -13865,10 +13952,12 @@
           && sql_int (0, 0,
                       "SELECT count(*)"
                       " FROM overrides"
-                      " WHERE (overrides.owner IS NULL)"
-                      " OR (overrides.owner ="
-                      " (SELECT ROWID FROM users"
-                      "  WHERE users.uuid = '%s'))",
+                      " WHERE ((overrides.owner IS NULL)"
+                      "        OR (overrides.owner ="
+                      "            (SELECT ROWID FROM users"
+                      "             WHERE users.uuid = '%s')))"
+                      " AND ((overrides.end_time = 0)"
+                      "      OR (overrides.end_time >= now ()))",
                       current_credentials.uuid))
         {
           iterator_t results;
@@ -13883,7 +13972,9 @@
           select = g_strdup_printf ("SELECT 1 FROM overrides"
                                     " WHERE (overrides.nvt = $nvt)"
                                     " AND ((overrides.owner IS NULL) OR (overrides.owner ="
-                                    " (SELECT ROWID FROM users WHERE users.uuid = '%s')))",
+                                    " (SELECT ROWID FROM users WHERE users.uuid = '%s')))"
+                                    " AND ((overrides.end_time = 0)"
+                                    "      OR (overrides.end_time >= now ()))",
                                     current_credentials.uuid);
           while (1)
             {
@@ -13921,6 +14012,8 @@
                      " OR (overrides.owner ="
                      " (SELECT users.ROWID FROM users"
                      "  WHERE users.uuid = '%s')))"
+                     " AND ((overrides.end_time = 0)"
+                     "      OR (overrides.end_time >= now ()))"
                      " AND (overrides.task = 0"
                      "      OR overrides.task = %llu)"
                      " AND (overrides.result = 0"
@@ -28460,7 +28553,9 @@
                          " overrides.modification_time, overrides.text,"       \
                          " overrides.hosts, overrides.port, overrides.threat," \
                          " overrides.new_threat, overrides.task,"              \
-                         " overrides.result"
+                         " overrides.result, overrides.end_time,"              \
+                         " (overrides.end_time = 0)"                           \
+                         "  OR (overrides.end_time >= now ())"
 
 /**
  * @brief Initialise an override iterator.
@@ -28726,6 +28821,39 @@
 }
 
 /**
+ * @brief Get the end time from an override iterator.
+ *
+ * @param[in]  iterator  Iterator.
+ *
+ * @return Time until which override applies.  0 for always.  1 means the
+ *         override has been explicitly turned off.
+ */
+time_t
+override_iterator_end_time (iterator_t* iterator)
+{
+  int ret;
+  if (iterator->done) return -1;
+  ret = (time_t) sqlite3_column_int (iterator->stmt, 12);
+  return ret;
+}
+
+/**
+ * @brief Get the active status from an override iterator.
+ *
+ * @param[in]  iterator  Iterator.
+ *
+ * @return 1 if active, else 0.
+ */
+int
+override_iterator_active (iterator_t* iterator)
+{
+  int ret;
+  if (iterator->done) return -1;
+  ret = sqlite3_column_int (iterator->stmt, 13);
+  return ret;
+}
+
+/**
  * @brief Get the NVT name from an override iterator.
  *
  * @param[in]  iterator  Iterator.

Modified: trunk/openvas-manager/src/omp.c
===================================================================
--- trunk/openvas-manager/src/omp.c	2011-11-04 16:43:50 UTC (rev 12003)
+++ trunk/openvas-manager/src/omp.c	2011-11-04 18:52:45 UTC (rev 12004)
@@ -8281,6 +8281,7 @@
                                     "<nvt oid=\"%s\">"
                                     "<name>%s</name>"
                                     "</nvt>"
+                                    "<active>%i</active>"
                                     "<text excerpt=\"%i\">%s</text>"
                                     "<threat>%s</threat>"
                                     "<new_threat>%s</new_threat>"
@@ -8289,6 +8290,7 @@
                                     override_iterator_uuid (overrides),
                                     override_iterator_nvt_oid (overrides),
                                     override_iterator_nvt_name (overrides),
+                                    override_iterator_active (overrides),
                                     strlen (excerpt) < strlen (text),
                                     excerpt,
                                     override_iterator_threat (overrides)
@@ -8307,6 +8309,7 @@
           time_t creation_time, mod_time;
           gchar *creation, *mod;
           int trash_task;
+          time_t end_time;
 
           if (uuid_task)
             {
@@ -8323,6 +8326,7 @@
           creation = g_strdup (ctime_strip_newline (&creation_time));
           mod_time = override_iterator_modification_time (overrides);
           mod = g_strdup (ctime_strip_newline (&mod_time));
+          end_time = override_iterator_end_time (overrides);
 
           buffer_xml_append_printf
            (buffer,
@@ -8330,6 +8334,8 @@
             "<nvt oid=\"%s\"><name>%s</name></nvt>"
             "<creation_time>%s</creation_time>"
             "<modification_time>%s</modification_time>"
+            "<active>%i</active>"
+            "<end_time>%s</end_time>"
             "<text>%s</text>"
             "<hosts>%s</hosts>"
             "<port>%s</port>"
@@ -8342,6 +8348,8 @@
             override_iterator_nvt_name (overrides),
             creation,
             mod,
+            override_iterator_active (overrides),
+            end_time ? ctime_strip_newline (&end_time) : "",
             override_iterator_text (overrides),
             override_iterator_hosts (overrides)
              ? override_iterator_hosts (overrides) : "",



More information about the Openvas-commits mailing list