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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon May 18 17:20:52 CEST 2009


Author: mattm
Date: 2009-05-18 17:20:51 +0200 (Mon, 18 May 2009)
New Revision: 3407

Modified:
   trunk/openvas-manager/ChangeLog
   trunk/openvas-manager/src/manage.h
   trunk/openvas-manager/src/omp.c
   trunk/openvas-manager/src/omp.h
   trunk/openvas-manager/src/ompd.c
   trunk/openvas-manager/src/ompd.h
   trunk/openvas-manager/src/openvasmd.c
   trunk/openvas-manager/src/tasks_sql.h
Log:
	Ensure that task statuses are consistent on startup.

	* src/openvasmd.c (main): Call init_ompd.

	* src/tasks_sql.h (init_manage_process): New function, was init_manage.
	(init_manage): Move body to init_manage_process.  Replace with function
	that makes database consistent.  Adjust callers.
	(cleanup_manage_process): New function, was cleanup_manage.
	(cleanup_manage): Rename cleanup_manage_process.  Adjust callers.
	* src/manage.h: Update headers.

	* src/omp.c (init_omp, init_omp_process): New functions.
	(init_omp_data): Rename init_omp_process.  Adjust callers.
	* src/omp.h: Update headers.

	* src/ompd.c (init_ompd): New function.
	* src/ompd.h: Update headers.


Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog	2009-05-18 13:51:14 UTC (rev 3406)
+++ trunk/openvas-manager/ChangeLog	2009-05-18 15:20:51 UTC (rev 3407)
@@ -1,5 +1,23 @@
 2009-05-18  Matthew Mundell <mmundell at intevation.de>
 
+	* src/openvasmd.c (main): Call init_ompd.
+
+	* src/tasks_sql.h (init_manage_process): New function, was init_manage.
+	(init_manage): Move body to init_manage_process.  Replace with function
+	that makes database consistent.  Adjust callers.
+	(cleanup_manage_process): New function, was cleanup_manage.
+	(cleanup_manage): Rename cleanup_manage_process.  Adjust callers.
+	* src/manage.h: Update headers.
+
+	* src/omp.c (init_omp, init_omp_process): New functions.
+	(init_omp_data): Rename init_omp_process.  Adjust callers.
+	* src/omp.h: Update headers.
+
+	* src/ompd.c (init_ompd): New function.
+	* src/ompd.h: Update headers.
+
+2009-05-18  Matthew Mundell <mmundell at intevation.de>
+
 	* src/tests/common.h (DO_CHILDREN, id_string): Move back to common.c.
 
 	* src/tests/common.c (DO_CHILDREN, id_string): Move back from common.h.

Modified: trunk/openvas-manager/src/manage.h
===================================================================
--- trunk/openvas-manager/src/manage.h	2009-05-18 13:51:14 UTC (rev 3406)
+++ trunk/openvas-manager/src/manage.h	2009-05-18 15:20:51 UTC (rev 3407)
@@ -30,12 +30,15 @@
 #include <glib.h>
 #include <ossp/uuid.h>
 
-void
+int
 init_manage ();
 
 void
-cleanup_manage ();
+init_manage_process ();
 
+void
+cleanup_manage_process ();
+
 
 /* Credentials. */
 

Modified: trunk/openvas-manager/src/omp.c
===================================================================
--- trunk/openvas-manager/src/omp.c	2009-05-18 13:51:14 UTC (rev 3406)
+++ trunk/openvas-manager/src/omp.c	2009-05-18 15:20:51 UTC (rev 3407)
@@ -1254,6 +1254,9 @@
                   {
                     if (content_error)
                       g_error_free (content_error);
+                    // FIX This can happen if task was removed (due to otp
+                    //     SCAN_END) after g_file_test above, so should be
+                    //     4xx.
                     SEND_TO_CLIENT_OR_FAIL ("<get_report_response>"
                                             "<status>50x</status>");
                   }
@@ -1763,15 +1766,24 @@
 extern buffer_size_t from_client_end;
 
 /**
- * @brief Initialise OMP library data.
+ * @brief Initialise OMP library.
+ */
+int
+init_omp ()
+{
+  return init_manage ();
+}
+
+/**
+ * @brief Initialise OMP library data for a process.
  *
  * This should run once per process, before the first call to \ref
  * process_omp_client_input.
  */
 void
-init_omp_data ()
+init_omp_process ()
 {
-  init_manage ();
+  init_manage_process ();
   /* Create the XML parser. */
   xml_parser.start_element = omp_xml_handle_start_element;
   xml_parser.end_element = omp_xml_handle_end_element;

Modified: trunk/openvas-manager/src/omp.h
===================================================================
--- trunk/openvas-manager/src/omp.h	2009-05-18 13:51:14 UTC (rev 3406)
+++ trunk/openvas-manager/src/omp.h	2009-05-18 15:20:51 UTC (rev 3407)
@@ -34,8 +34,11 @@
  */
 #define TO_CLIENT_BUFFER_SIZE 1048576
 
+int
+init_omp ();
+
 void
-init_omp_data ();
+init_omp_process ();
 
 int
 process_omp_client_input ();

Modified: trunk/openvas-manager/src/ompd.c
===================================================================
--- trunk/openvas-manager/src/ompd.c	2009-05-18 13:51:14 UTC (rev 3406)
+++ trunk/openvas-manager/src/ompd.c	2009-05-18 15:20:51 UTC (rev 3407)
@@ -31,8 +31,9 @@
  * Manager, a daemon that is layered between the real OpenVAS Server
  * (openvasd) and a client (such as OpenVAS-Client).
  *
- * The library provides a single function, \ref serve_omp.
- * This function serves OMP to a single client socket until end of file is
+ * The library provides two functions: \ref init_ompd and \ref serve_omp.
+ * \ref init_ompd initialises the daemon.
+ * \ref serve_omp serves OMP to a single client socket until end of file is
  * reached on the socket.
  */
 
@@ -94,6 +95,15 @@
 extern buffer_size_t from_server_end;
 
 /**
+ * @brief Initialise the OMP library for the OMP daemon.
+ */
+int
+init_ompd ()
+{
+  return init_omp ();
+}
+
+/**
  * @brief Read as much from the client as the \ref from_client buffer will hold.
  *
  * @param[in]  client_session  The TLS session with the client.
@@ -407,8 +417,8 @@
   /* Initialise server information. */
   init_otp_data ();
 
-  /* Initialise the XML parser. */
-  init_omp_data ();
+  /* Initialise the XML parser and the manage library. */
+  init_omp_process ();
 #if 0
   // FIX consider free_omp_data (); on return
   if (tasks) free_tasks ();

Modified: trunk/openvas-manager/src/ompd.h
===================================================================
--- trunk/openvas-manager/src/ompd.h	2009-05-18 13:51:14 UTC (rev 3406)
+++ trunk/openvas-manager/src/ompd.h	2009-05-18 15:20:51 UTC (rev 3407)
@@ -30,6 +30,9 @@
 #include <gnutls/gnutls.h>
 
 int
+init_ompd ();
+
+int
 serve_omp (gnutls_session_t*, gnutls_session_t*,
            gnutls_certificate_credentials_t*,
            int, int*);

Modified: trunk/openvas-manager/src/openvasmd.c
===================================================================
--- trunk/openvas-manager/src/openvasmd.c	2009-05-18 13:51:14 UTC (rev 3406)
+++ trunk/openvas-manager/src/openvasmd.c	2009-05-18 15:20:51 UTC (rev 3407)
@@ -540,7 +540,7 @@
 {
   tracef ("   Cleaning up.\n");
   // FIX should be via omp, maybe cleanup_omp ();
-  cleanup_manage ();
+  cleanup_manage_process ();
   if (manager_socket > -1) close (manager_socket);
 #if LOG
   if (log_stream != NULL)
@@ -691,6 +691,14 @@
   server.rules = NULL;
 #endif
 
+  /* Initialise OMP daemon. */
+
+  if (init_ompd ())
+    {
+      fprintf (stderr, "Failed to initialise OMP daemon.\n");
+      exit (EXIT_FAILURE);
+    }
+
   /* Register the `cleanup' function. */
 
   if (atexit (&cleanup))

Modified: trunk/openvas-manager/src/tasks_sql.h
===================================================================
--- trunk/openvas-manager/src/tasks_sql.h	2009-05-18 13:51:14 UTC (rev 3406)
+++ trunk/openvas-manager/src/tasks_sql.h	2009-05-18 15:20:51 UTC (rev 3407)
@@ -260,70 +260,7 @@
   g_free (quote);
 }
 
-
-
 /**
- * @brief Initialize the manage library.
- */
-void
-init_manage ()
-{
-  /* Open the database. */
-  int ret = sqlite3_open (PREFIX "/var/lib/openvas/mgr/tasks.db", &task_db);
-  if (ret)
-    {
-      fprintf (stderr, "sqlite3_open failed: %s\n",
-               sqlite3_errmsg (task_db));
-      abort ();
-    }
-}
-
-/**
- * @brief Cleanup the manage library.
- */
-void
-cleanup_manage ()
-{
-  if (task_db)
-    {
-      sqlite3_close (task_db);
-      task_db = NULL;
-    }
-}
-
-/**
- * @brief Authenticate credentials.
- *
- * @param[in]  credentials  Credentials.
- *
- * @return 1 if credentials are authentic, else 0.
- */
-int
-authenticate (credentials_t credentials)
-{
-  if (credentials.username)
-    {
-      sql ("CREATE TABLE IF NOT EXISTS tasks_%s (uuid, name, time, comment, description, run_status, start_time, end_time, report_count, attack_state, current_port, max_port, debugs_size, holes_size, infos_size, logs_size, notes_size)",
-           current_credentials.username);
-      return 1;
-    }
-  return 0;
-}
-
-/**
- * @brief Return the number of tasks associated with the current user.
- *
- * @return The number of tasks associated with the current user.
- */
-unsigned int
-task_count ()
-{
-  return (unsigned int) sql_int (0, 0,
-                                 "SELECT count(*) FROM tasks_%s;",
-                                 current_credentials.username);
-}
-
-/**
  * @brief Initialise a task iterator.
  *
  * @param[in]  iterator  Task iterator.
@@ -399,6 +336,154 @@
 }
 
 /**
+ * @brief Initialize the manage library for a process.
+ *
+ * Simply open the SQL database.
+ */
+void
+init_manage_process ()
+{
+  if (task_db) return;
+  /* Open the database. */
+  int ret = sqlite3_open (PREFIX "/var/lib/openvas/mgr/tasks.db", &task_db);
+  if (ret)
+    {
+      fprintf (stderr, "sqlite3_open failed: %s\n",
+               sqlite3_errmsg (task_db));
+      abort (); // FIX
+    }
+}
+
+/**
+ * @brief Initialize the manage library.
+ *
+ * Ensure all tasks are in a clean initial state.
+ *
+ * Beware that calling this function while tasks are running may lead to
+ * problems.
+ *
+ * @return 0.
+ */
+int
+init_manage ()
+{
+  const char* tail;
+  int ret;
+  sqlite3_stmt* stmt;
+
+  init_manage_process ();
+
+  /* Set requested and running tasks to stopped. */
+
+  ret = sqlite3_prepare (task_db,
+                         "SELECT name from sqlite_master WHERE type='table';",
+                         -1, &stmt, &tail);
+  if (ret != SQLITE_OK || stmt == NULL)
+    {
+      fprintf (stderr, "sqlite3_prepare 1 failed: %s\n",
+               sqlite3_errmsg (task_db));
+      abort ();
+    }
+  while (1)
+    {
+      const unsigned char* name;
+
+      ret = sqlite3_step (stmt);
+      if (ret == SQLITE_BUSY) continue;
+      if (ret == SQLITE_DONE) break;
+      if (ret == SQLITE_ERROR || ret == SQLITE_MISUSE)
+        {
+          if (ret == SQLITE_ERROR) ret = sqlite3_reset (stmt);
+          fprintf (stderr, "sqlite3_step 1 failed: %s\n",
+                   sqlite3_errmsg (task_db));
+          abort ();
+        }
+      name = sqlite3_column_text (stmt, 0);
+      tracef ("   table %s\n", name);
+
+      if (strlen ((const char*) name) > strlen ("tasks_"))
+        {
+          task_t index;
+          task_iterator_t iterator;
+
+          current_credentials.username = g_strdup ((const char*) name
+                                                   + strlen ("tasks_"));
+          init_task_iterator (&iterator);
+          while (next_task (&iterator, &index))
+            {
+              switch (task_run_status (index))
+                {
+                  case TASK_STATUS_DELETE_REQUESTED:
+                  case TASK_STATUS_REQUESTED:
+                  case TASK_STATUS_RUNNING:
+                  case TASK_STATUS_STOP_REQUESTED:
+                    set_task_run_status (index, TASK_STATUS_STOPPED);
+                    break;
+                  default:
+                    break;
+                }
+            }
+          cleanup_task_iterator (&iterator);
+          g_free (current_credentials.username);
+          current_credentials.username = NULL;
+        }
+    }
+  sqlite3_finalize (stmt);
+
+  return 0;
+}
+
+/**
+ * @brief Cleanup the manage library.
+ */
+void
+cleanup_manage_process ()
+{
+  if (task_db)
+    {
+      if (current_server_task)
+        {
+          if (task_run_status (current_server_task) == TASK_STATUS_REQUESTED)
+            set_task_run_status (current_server_task, TASK_STATUS_STOPPED);
+        }
+      sqlite3_close (task_db);
+      task_db = NULL;
+    }
+}
+
+/**
+ * @brief Authenticate credentials.
+ *
+ * @param[in]  credentials  Credentials.
+ *
+ * @return 1 if credentials are authentic, else 0.
+ */
+int
+authenticate (credentials_t credentials)
+{
+  if (credentials.username)
+    {
+      sql ("CREATE TABLE IF NOT EXISTS tasks_%s (uuid, name, time, comment, description, run_status, start_time, end_time, report_count, attack_state, current_port, max_port, debugs_size, holes_size, infos_size, logs_size, notes_size)",
+           current_credentials.username);
+      return 1;
+    }
+  return 0;
+}
+
+/**
+ * @brief Return the number of tasks associated with the current user.
+ *
+ * @return The number of tasks associated with the current user.
+ */
+unsigned int
+task_count ()
+{
+  return (unsigned int) sql_int (0, 0,
+                                 "SELECT count(*) FROM tasks_%s;",
+                                 current_credentials.username);
+}
+
+/**
  * @brief Return the identifier of a task.
  *
  * @param[in]  task  Task.



More information about the Openvas-commits mailing list