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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Nov 23 18:55:06 CET 2011


Author: mattm
Date: 2011-11-23 18:55:02 +0100 (Wed, 23 Nov 2011)
New Revision: 12174

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 note end times.  Hence increase database version to 54.  Add
	database migration from version 53 to 54.

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

	* src/manage_sql.c (create_tables): Add end_time to notes.
	(migrate_53_to_54): New functions.
	(database_migrators): Add migrate_53_to_54.
	(modify_note): Add active param.
	(NOTE_COLUMNS): Add active fields.
	(note_iterator_end_time, note_iterator_active): New functions.

	* src/manage.h: Update headers accordingly.

	* src/omp.c (modify_note_data_t, modify_note_data_reset)
	(client_state_t): Add active.
	(omp_xml_handle_start_element, omp_xml_handle_end_element)
	(omp_xml_handle_text): Add ACTIVE to MODIFY_NOTE and CREATE_NOTE.
	(buffer_notes_xml): Add ACTIVE and END_TIME to NOTE.

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

	* doc/db.png: Update from source.

Modified: trunk/openvas-manager/CMakeLists.txt
===================================================================
--- trunk/openvas-manager/CMakeLists.txt	2011-11-23 14:53:02 UTC (rev 12173)
+++ trunk/openvas-manager/CMakeLists.txt	2011-11-23 17:55:02 UTC (rev 12174)
@@ -105,7 +105,7 @@
 
 ## Variables
 
-set (OPENVASMD_DATABASE_VERSION 53)
+set (OPENVASMD_DATABASE_VERSION 54)
 
 if (SYSCONF_INSTALL_DIR)
   set (SYSCONFDIR "${SYSCONF_INSTALL_DIR}")

Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog	2011-11-23 14:53:02 UTC (rev 12173)
+++ trunk/openvas-manager/ChangeLog	2011-11-23 17:55:02 UTC (rev 12174)
@@ -1,3 +1,29 @@
+2011-11-22  Matthew Mundell <matthew.mundell at greenbone.net>
+
+	Add note end times.  Hence increase database version to 54.  Add
+	database migration from version 53 to 54.
+
+	* CMakeLists.txt (OPENVASMD_DATABASE_VERSION): Increase to 54.
+
+	* src/manage_sql.c (create_tables): Add end_time to notes.
+	(migrate_53_to_54): New functions.
+	(database_migrators): Add migrate_53_to_54.
+	(modify_note): Add active param.
+	(NOTE_COLUMNS): Add active fields.
+	(note_iterator_end_time, note_iterator_active): New functions.
+
+	* src/manage.h: Update headers accordingly.
+
+	* src/omp.c (modify_note_data_t, modify_note_data_reset)
+	(client_state_t): Add active.
+	(omp_xml_handle_start_element, omp_xml_handle_end_element)
+	(omp_xml_handle_text): Add ACTIVE to MODIFY_NOTE and CREATE_NOTE.
+	(buffer_notes_xml): Add ACTIVE and END_TIME to NOTE.
+
+	* doc/db_postgres.sql (notes): Add column end_time.
+
+	* doc/db.png: Update from source.
+
 2011-11-23  Michael Wiegand <michael.wiegand at greenbone.net>
 
 	Post release version bump.

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-23 14:53:02 UTC (rev 12173)
+++ trunk/openvas-manager/doc/db_postgres.sql	2011-11-23 17:55:02 UTC (rev 12174)
@@ -382,7 +382,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 overrides (
 	id integer PRIMARY KEY,

Modified: trunk/openvas-manager/src/manage.h
===================================================================
--- trunk/openvas-manager/src/manage.h	2011-11-23 14:53:02 UTC (rev 12173)
+++ trunk/openvas-manager/src/manage.h	2011-11-23 17:55:02 UTC (rev 12174)
@@ -1453,7 +1453,7 @@
 
 int
 modify_note (note_t, const char*, const char*, const char*, const char*,
-             task_t, result_t);
+             const char*, task_t, result_t);
 
 void
 init_note_iterator (iterator_t*, note_t, nvt_t, result_t, task_t, int,
@@ -1489,6 +1489,12 @@
 result_t
 note_iterator_result (iterator_t*);
 
+time_t
+note_iterator_end_time (iterator_t*);
+
+int
+note_iterator_active (iterator_t*);
+
 const char*
 note_iterator_nvt_name (iterator_t *);
 

Modified: trunk/openvas-manager/src/manage_sql.c
===================================================================
--- trunk/openvas-manager/src/manage_sql.c	2011-11-23 14:53:02 UTC (rev 12173)
+++ trunk/openvas-manager/src/manage_sql.c	2011-11-23 17:55:02 UTC (rev 12174)
@@ -1430,7 +1430,7 @@
   sql ("CREATE TABLE IF NOT EXISTS lsc_credentials (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, login, password, comment, public_key TEXT, private_key TEXT, rpm TEXT, deb TEXT, exe TEXT);");
   sql ("CREATE TABLE IF NOT EXISTS lsc_credentials_trash (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, login, password, comment, public_key TEXT, private_key TEXT, rpm TEXT, deb TEXT, exe TEXT);");
   sql ("CREATE TABLE IF NOT EXISTS meta (id INTEGER PRIMARY KEY, name UNIQUE, value);");
-  sql ("CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt, creation_time, modification_time, text, hosts, port, threat, task INTEGER, result INTEGER);");
+  sql ("CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, nvt, creation_time, modification_time, text, hosts, port, threat, task INTEGER, result INTEGER, end_time);");
   sql ("CREATE TABLE IF NOT EXISTS nvt_preferences (id INTEGER PRIMARY KEY, name, value);");
   /* nvt_selectors types: 0 all, 1 family, 2 NVT (NVT_SELECTOR_TYPE_* in manage.h). */
   sql ("CREATE TABLE IF NOT EXISTS nvt_selectors (id INTEGER PRIMARY KEY, name, exclude INTEGER, type INTEGER, family_or_nvt, family);");
@@ -5541,6 +5541,40 @@
 }
 
 /**
+ * @brief Migrate the database from version 53 to version 54.
+ *
+ * @return 0 success, -1 error.
+ */
+static int
+migrate_53_to_54 ()
+{
+  sql ("BEGIN EXCLUSIVE;");
+
+  /* Ensure that the database is currently version 53. */
+
+  if (manage_db_version () != 53)
+    {
+      sql ("ROLLBACK;");
+      return -1;
+    }
+
+  /* Update the database. */
+
+  /* The notes table got a end_time column. */
+
+  sql ("ALTER TABLE notes ADD column end_time;");
+  sql ("UPDATE notes SET end_time = 0;");
+
+  /* Set the database version to 54. */
+
+  set_db_version (54);
+
+  sql ("COMMIT;");
+
+  return 0;
+}
+
+/**
  * @brief Array of database version migrators.
  */
 static migrator_t database_migrators[]
@@ -5598,6 +5632,7 @@
     {51, migrate_50_to_51},
     {52, migrate_51_to_52},
     {53, migrate_52_to_53},
+    {54, migrate_53_to_54},
     /* End marker. */
     {-1, NULL}};
 
@@ -28288,6 +28323,8 @@
  * @brief Modify a note.
  *
  * @param[in]  note        Note.
+ * @param[in]  active      NULL or -2 leave as is, -1 on, 0 off, n on for n
+ *                         days.
  * @param[in]  text        Note text.
  * @param[in]  hosts       Hosts to apply note to, NULL for any host.
  * @param[in]  port        Port to apply note to, NULL for any port.
@@ -28295,12 +28332,12 @@
  * @param[in]  task        Task to apply note to, 0 for any task.
  * @param[in]  result      Result to apply note to, 0 for any result.
  *
- * @return 0 success, -1 error.
+ * @return 0 success, -1 error, 1 syntax error in active.
  */
 int
-modify_note (note_t note, const char* text, const char* hosts,
-             const char* port, const char* threat, task_t task,
-             result_t result)
+modify_note (note_t note, const char *active, const char* text,
+             const char* hosts, const char* port, const char* threat,
+             task_t task, result_t result)
 {
   gchar *quoted_text, *quoted_hosts, *quoted_port, *quoted_threat;
 
@@ -28321,23 +28358,58 @@
   quoted_threat = sql_insert ((threat && strlen (threat))
                                 ? threat_message_type (threat) : NULL);
 
-  sql ("UPDATE notes SET"
-       " modification_time = %i,"
-       " text = %s,"
-       " hosts = %s,"
-       " port = %s,"
-       " threat = %s,"
-       " task = %llu,"
-       " result = %llu"
-       " WHERE ROWID = %llu;",
-       time (NULL),
-       quoted_text,
-       quoted_hosts,
-       quoted_port,
-       quoted_threat,
-       task,
-       result,
-       note);
+  if ((active == NULL) || (strcmp (active, "-2") == 0))
+    sql ("UPDATE notes SET"
+         " modification_time = %i,"
+         " text = %s,"
+         " hosts = %s,"
+         " port = %s,"
+         " threat = %s,"
+         " task = %llu,"
+         " result = %llu"
+         " WHERE ROWID = %llu;",
+         time (NULL),
+         quoted_text,
+         quoted_hosts,
+         quoted_port,
+         quoted_threat,
+         task,
+         result,
+         note);
+  else
+    {
+      const char *point;
+      point = active;
+      if (strcmp (point, "-1"))
+        {
+          while (*point && isdigit (*point)) point++;
+          if (*point)
+            return 1;
+        }
+      sql ("UPDATE notes SET"
+           " end_time = %i,"
+           " modification_time = %i,"
+           " text = %s,"
+           " hosts = %s,"
+           " port = %s,"
+           " threat = %s,"
+           " task = %llu,"
+           " result = %llu"
+           " WHERE ROWID = %llu;",
+           (strcmp (active, "-1")
+             ? (strcmp (active, "0")
+                 ? (time (NULL) + atoi (active) * 60 * 60 * 24)
+                 : 1)
+             : 0),
+           time (NULL),
+           quoted_text,
+           quoted_hosts,
+           quoted_port,
+           quoted_threat,
+           task,
+           result,
+           note);
+    }
 
   g_free (quoted_text);
   g_free (quoted_hosts);
@@ -28353,7 +28425,9 @@
 #define NOTE_COLUMNS "notes.ROWID, notes.uuid, notes.nvt,"                 \
                      " notes.creation_time, notes.modification_time,"      \
                      " notes.text, notes.hosts, notes.port, notes.threat," \
-                     " notes.task, notes.result"
+                     " notes.task, notes.result, notes.end_time,"          \
+                     " (notes.end_time = 0)"                               \
+                     "  OR (notes.end_time >= now ())"
 
 /**
  * @brief Initialise a note iterator.
@@ -28602,6 +28676,39 @@
 }
 
 /**
+ * @brief Get the end time from an note iterator.
+ *
+ * @param[in]  iterator  Iterator.
+ *
+ * @return Time until which note applies.  0 for always.  1 means the
+ *         note has been explicitly turned off.
+ */
+time_t
+note_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 note iterator.
+ *
+ * @param[in]  iterator  Iterator.
+ *
+ * @return 1 if active, else 0.
+ */
+int
+note_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 a note iterator.
  *
  * @param[in]  iterator  Iterator.

Modified: trunk/openvas-manager/src/omp.c
===================================================================
--- trunk/openvas-manager/src/omp.c	2011-11-23 14:53:02 UTC (rev 12173)
+++ trunk/openvas-manager/src/omp.c	2011-11-23 17:55:02 UTC (rev 12174)
@@ -2264,6 +2264,7 @@
  */
 typedef struct
 {
+  char *active;       ///< Whether the override is active.
   char *hosts;        ///< Hosts to which to limit override.
   char *note_id;      ///< ID of note to modify.
   char *nvt_oid;      ///< NVT to which to limit override.
@@ -2282,6 +2283,7 @@
 static void
 modify_note_data_reset (modify_note_data_t *data)
 {
+  free (data->active);
   free (data->hosts);
   free (data->note_id);
   free (data->nvt_oid);
@@ -3319,6 +3321,7 @@
   CLIENT_MODIFY_CONFIG_NVT_SELECTION_FAMILY,
   CLIENT_MODIFY_CONFIG_NVT_SELECTION_NVT,
   CLIENT_MODIFY_NOTE,
+  CLIENT_MODIFY_NOTE_ACTIVE,
   CLIENT_MODIFY_NOTE_HOSTS,
   CLIENT_MODIFY_NOTE_PORT,
   CLIENT_MODIFY_NOTE_RESULT,
@@ -7513,7 +7516,9 @@
         break;
 
       case CLIENT_MODIFY_NOTE:
-        if (strcasecmp ("HOSTS", element_name) == 0)
+        if (strcasecmp ("ACTIVE", element_name) == 0)
+          set_client_state (CLIENT_MODIFY_NOTE_ACTIVE);
+        else if (strcasecmp ("HOSTS", element_name) == 0)
           set_client_state (CLIENT_MODIFY_NOTE_HOSTS);
         else if (strcasecmp ("PORT", element_name) == 0)
           set_client_state (CLIENT_MODIFY_NOTE_PORT);
@@ -7547,7 +7552,7 @@
           set_client_state (CLIENT_MODIFY_NOTE_THREAT);
         else
           {
-            if (send_element_error_to_client ("MODIFY_note", element_name,
+            if (send_element_error_to_client ("MODIFY_NOTE", element_name,
                                               write_to_client,
                                               write_to_client_data))
               {
@@ -8134,12 +8139,14 @@
                                     "<nvt oid=\"%s\">"
                                     "<name>%s</name>"
                                     "</nvt>"
+                                    "<active>%i</active>"
                                     "<text excerpt=\"%i\">%s</text>"
                                     "<orphan>%i</orphan>"
                                     "</note>",
                                     note_iterator_uuid (notes),
                                     note_iterator_nvt_oid (notes),
                                     note_iterator_nvt_name (notes),
+                                    note_iterator_active (notes),
                                     strlen (excerpt) < strlen (text),
                                     excerpt,
                                     ((note_iterator_task (notes)
@@ -8154,6 +8161,7 @@
           time_t creation_time, mod_time;
           gchar *creation, *mod;
           int trash_task;
+          time_t end_time;
 
           if (uuid_task)
             {
@@ -8170,6 +8178,7 @@
           creation = g_strdup (iso_time (&creation_time));
           mod_time = note_iterator_modification_time (notes);
           mod = g_strdup (iso_time (&mod_time));
+          end_time = note_iterator_end_time (notes);
 
           buffer_xml_append_printf
            (buffer,
@@ -8177,6 +8186,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>"
@@ -8188,6 +8199,8 @@
             note_iterator_nvt_name (notes),
             creation,
             mod,
+            note_iterator_active (notes),
+            end_time > 1 ? iso_time (&end_time) : "",
             note_iterator_text (notes),
             note_iterator_hosts (notes)
              ? note_iterator_hosts (notes) : "",
@@ -14638,6 +14651,7 @@
                 }
             }
           else switch (modify_note (note,
+                                    modify_note_data->active,
                                     modify_note_data->text,
                                     modify_note_data->hosts,
                                     modify_note_data->port,
@@ -14662,6 +14676,10 @@
           set_client_state (CLIENT_AUTHENTIC);
           break;
         }
+      case CLIENT_MODIFY_NOTE_ACTIVE:
+        assert (strcasecmp ("ACTIVE", element_name) == 0);
+        set_client_state (CLIENT_MODIFY_NOTE);
+        break;
       case CLIENT_MODIFY_NOTE_HOSTS:
         assert (strcasecmp ("HOSTS", element_name) == 0);
         set_client_state (CLIENT_MODIFY_NOTE);
@@ -18277,6 +18295,9 @@
                              text_len);
         break;
 
+      case CLIENT_MODIFY_NOTE_ACTIVE:
+        openvas_append_text (&modify_note_data->active, text, text_len);
+        break;
       case CLIENT_MODIFY_NOTE_HOSTS:
         openvas_append_text (&modify_note_data->hosts, text, text_len);
         break;



More information about the Openvas-commits mailing list