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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Oct 5 23:54:24 CEST 2009


Author: mattm
Date: 2009-10-05 23:54:23 +0200 (Mon, 05 Oct 2009)
New Revision: 5377

Modified:
   trunk/openvas-manager/ChangeLog
   trunk/openvas-manager/src/manage.h
   trunk/openvas-manager/src/omp.c
   trunk/openvas-manager/src/ompd.c
   trunk/openvas-manager/src/otp.c
   trunk/openvas-manager/src/otp.h
   trunk/openvas-manager/src/tasks_sql.h
Log:
	Move caching of NVT preferences to the database.

	* src/otp.h (scanner_init_state_t): Add SCANNER_INIT_DONE_CACHE_MODE.

	* src/ompd.c (init_ompd): In SCANNER_INIT_GOT_PLUGINS set scanner init to
	special cache done state when in cache mode.  Add
	SCANNER_INIT_DONE_CACHE_MODE to error check.
	(serve_omp): Add SCANNER_INIT_DONE_CACHE_MODE to select setup.

	* src/otp.c (current_scanner_preferences, make_scanner_preferences)
	(add_scanner_preference): Remove.
	(init_otp_data): Remove scanner.preferences init.
	(parse_scanner_preference_value): Call manage_nvt_preference_add instead
	of add_scanner_preference.
	(process_otp_scanner_input): Add SCANNER_INIT_DONE_CACHE_MODE alongside
	SCANNER_INIT_DONE.  In NVT cache mode, instead of exiting when the
	NVTs are cached, continue to read the preferences and exit after that.

	* src/tasks_sql.h (init_manage_process): Also clear NVT prefs.
	(init_manage): Add outline for database initialisation check.  Add table
	nvt_preferences.
	(manage_nvt_preference_add, manage_nvt_preferences_enable)
	(init_nvt_preference_iterator, nvt_preference_iterator_name)
	(nvt_preference_iterator_value): New functions.

	* src/manage.h: Add headers.
	(scanner_t): Remove preferences slot.

	* src/omp.c (send_preference): Remove.
	(omp_xml_handle_end_element): In CLIENT_GET_PREFERENCES use new
	preferences iterator to send preferences.

Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog	2009-10-05 17:43:01 UTC (rev 5376)
+++ trunk/openvas-manager/ChangeLog	2009-10-05 21:54:23 UTC (rev 5377)
@@ -1,5 +1,39 @@
 2009-10-05  Matthew Mundell <matthew.mundell at intevation.de>
 
+	Move caching of NVT preferences to the database.
+
+	* src/otp.h (scanner_init_state_t): Add SCANNER_INIT_DONE_CACHE_MODE.
+
+	* src/ompd.c (init_ompd): In SCANNER_INIT_GOT_PLUGINS set scanner init to
+	special cache done state when in cache mode.  Add
+	SCANNER_INIT_DONE_CACHE_MODE to error check.
+	(serve_omp): Add SCANNER_INIT_DONE_CACHE_MODE to select setup.
+
+	* src/otp.c (current_scanner_preferences, make_scanner_preferences)
+	(add_scanner_preference): Remove.
+	(init_otp_data): Remove scanner.preferences init.
+	(parse_scanner_preference_value): Call manage_nvt_preference_add instead
+	of add_scanner_preference.
+	(process_otp_scanner_input): Add SCANNER_INIT_DONE_CACHE_MODE alongside
+	SCANNER_INIT_DONE.  In NVT cache mode, instead of exiting when the
+	NVTs are cached, continue to read the preferences and exit after that.
+
+	* src/tasks_sql.h (init_manage_process): Also clear NVT prefs.
+	(init_manage): Add outline for database initialisation check.  Add table
+	nvt_preferences.
+	(manage_nvt_preference_add, manage_nvt_preferences_enable)
+	(init_nvt_preference_iterator, nvt_preference_iterator_name)
+	(nvt_preference_iterator_value): New functions.
+
+	* src/manage.h: Add headers.
+	(scanner_t): Remove preferences slot.
+
+	* src/omp.c (send_preference): Remove.
+	(omp_xml_handle_end_element): In CLIENT_GET_PREFERENCES use new
+	preferences iterator to send preferences.
+
+2009-10-05  Matthew Mundell <matthew.mundell at intevation.de>
+
 	* src/ompd.c (recreate_session): Add openvas_server_free to fnctl fail.
 	(serve_omp): Put the credential args the right way round.
 

Modified: trunk/openvas-manager/src/manage.h
===================================================================
--- trunk/openvas-manager/src/manage.h	2009-10-05 17:43:01 UTC (rev 5376)
+++ trunk/openvas-manager/src/manage.h	2009-10-05 21:54:23 UTC (rev 5377)
@@ -41,7 +41,6 @@
   certificates_t* certificates;      ///< List of certificates.
   char* plugins_md5;                 ///< MD5 sum over all tests.
   GHashTable* plugins_dependencies;  ///< Dependencies between plugins.
-  GHashTable* preferences;           ///< Scanner preference.
   GPtrArray* rules;                  ///< Scanner rules.
   int rules_size;                    ///< Number of rules.
 } scanner_t;
@@ -635,8 +634,26 @@
 nvt_selector_nvt_count (const char*, const char*);
 
 
-/* Lsc_Credentials. */
+/* NVT preferences. */
 
+void
+manage_nvt_preference_add (char*, char*);
+
+void
+manage_nvt_preferences_enable ();
+
+void
+init_nvt_preference_iterator (iterator_t*);
+
+const char*
+nvt_preference_iterator_name (iterator_t*);
+
+const char*
+nvt_preference_iterator_value (iterator_t*);
+
+
+/* LSC credentials. */
+
 int
 create_lsc_credential (const char*, const char*);
 

Modified: trunk/openvas-manager/src/omp.c
===================================================================
--- trunk/openvas-manager/src/omp.c	2009-10-05 17:43:01 UTC (rev 5376)
+++ trunk/openvas-manager/src/omp.c	2009-10-05 21:54:23 UTC (rev 5377)
@@ -1690,38 +1690,6 @@
 }
 
 /**
- * @brief Send XML for a preference.
- *
- * @param[in]  key    The preferences hashtable key.
- * @param[in]  value  The preferences hashtable value.
- * @param[in]  dummy  Dummy variable for g_hash_table_find.
- *
- * @return TRUE if out of space in to_client buffer, else FALSE.
- */
-static gboolean
-send_preference (gpointer key, gpointer value, /*@unused@*/ gpointer dummy)
-{
-  /* \todo Do these reallocations affect performance? */
-  gchar* key_text = g_markup_escape_text ((char*) key,
-                                          strlen ((char*) key));
-  gchar* value_text = g_markup_escape_text ((char*) value,
-                                            strlen ((char*) value));
-  gchar* msg = g_strdup_printf ("<preference>"
-                                "<name>%s</name><value>%s</value>"
-                                "</preference>",
-                                key_text, value_text);
-  g_free (key_text);
-  g_free (value_text);
-  if (send_to_client (msg))
-    {
-      g_free (msg);
-      return TRUE;
-    }
-  g_free (msg);
-  return FALSE;
-}
-
-/**
  * @brief Send XML for a rule.
  *
  * @param[in]  rule  The rule.
@@ -2550,22 +2518,26 @@
         break;
 
       case CLIENT_GET_PREFERENCES:
-        if (scanner.preferences)
-          {
-            SEND_TO_CLIENT_OR_FAIL ("<get_preferences_response"
-                                    " status=\"" STATUS_OK "\""
-                                    " status_text=\"" STATUS_OK_TEXT "\">");
-            if (g_hash_table_find (scanner.preferences, send_preference, NULL))
-              {
-                error_send_to_client (error);
-                return;
-              }
-            SEND_TO_CLIENT_OR_FAIL ("</get_preferences_response>");
-          }
-        else
-          SEND_TO_CLIENT_OR_FAIL (XML_SERVICE_DOWN ("get_preferences"));
-        set_client_state (CLIENT_AUTHENTIC);
-        break;
+        {
+          iterator_t prefs;
+          SEND_TO_CLIENT_OR_FAIL ("<get_preferences_response"
+                                  " status=\"" STATUS_OK "\""
+                                  " status_text=\"" STATUS_OK_TEXT "\">");
+          init_nvt_preference_iterator (&prefs);
+          while (next (&prefs))
+            {
+              SENDF_TO_CLIENT_OR_FAIL ("<preference>"
+                                       "<name>%s</name>"
+                                       "<value>%s</value>"
+                                       "</preference>",
+                                       nvt_preference_iterator_name (&prefs),
+                                       nvt_preference_iterator_value (&prefs));
+            }
+          cleanup_iterator (&prefs);
+          SEND_TO_CLIENT_OR_FAIL ("</get_preferences_response>");
+          set_client_state (CLIENT_AUTHENTIC);
+          break;
+        }
 
       case CLIENT_GET_CERTIFICATES:
         if (scanner.certificates)
@@ -5203,7 +5175,8 @@
  *
  * @param[in]  log_config  Logging configuration list.
  *
- * @return 0 success, -1 error, -2 database is wrong version.
+ * @return 0 success, -1 error, -2 database is wrong version, -3 database
+ *         needs to be initialized from server.
  */
 int
 init_omp (GSList *log_config)

Modified: trunk/openvas-manager/src/ompd.c
===================================================================
--- trunk/openvas-manager/src/ompd.c	2009-10-05 17:43:01 UTC (rev 5376)
+++ trunk/openvas-manager/src/ompd.c	2009-10-05 21:54:23 UTC (rev 5377)
@@ -95,7 +95,8 @@
  *
  * @param[in]  log_config  Log configuration
  *
- * @return 0 success, -1 error, -2 database is wrong version.
+ * @return 0 success, -1 error, -2 database is wrong version, -3 database
+ *         needs to be initialized from server.
  */
 int
 init_ompd (GSList *log_config)
@@ -385,7 +386,12 @@
                                  (scanner_session,
                                   ack + scanner_init_offset);
           if (scanner_init_offset == 0)
-            set_scanner_init_state (SCANNER_INIT_DONE);
+            {
+              if (ompd_nvt_cache_mode)
+                set_scanner_init_state (SCANNER_INIT_DONE_CACHE_MODE);
+              else
+                set_scanner_init_state (SCANNER_INIT_DONE);
+            }
           else if (scanner_init_offset == -1)
             {
               scanner_init_offset = 0;
@@ -396,6 +402,7 @@
         }
         /*@fallthrough@*/
       case SCANNER_INIT_DONE:
+      case SCANNER_INIT_DONE_CACHE_MODE:
         while (1)
           switch (write_to_server_buffer (scanner_session))
             {
@@ -675,6 +682,7 @@
         }
 
       if ((scanner_init_state == SCANNER_INIT_DONE
+           || scanner_init_state == SCANNER_INIT_DONE_CACHE_MODE
            || scanner_init_state == SCANNER_INIT_GOT_VERSION
            || scanner_init_state == SCANNER_INIT_SENT_COMPLETE_LIST
            || scanner_init_state == SCANNER_INIT_SENT_PASSWORD
@@ -687,7 +695,8 @@
         }
 
       if (((scanner_init_state == SCANNER_INIT_TOP
-            || scanner_init_state == SCANNER_INIT_DONE)
+            || scanner_init_state == SCANNER_INIT_DONE
+            || scanner_init_state == SCANNER_INIT_DONE_CACHE_MODE)
            && to_server_buffer_space () > 0)
           || scanner_init_state == SCANNER_INIT_CONNECT_INTR
           || scanner_init_state == SCANNER_INIT_CONNECTED

Modified: trunk/openvas-manager/src/otp.c
===================================================================
--- trunk/openvas-manager/src/otp.c	2009-10-05 17:43:01 UTC (rev 5376)
+++ trunk/openvas-manager/src/otp.c	2009-10-05 21:54:23 UTC (rev 5377)
@@ -417,40 +417,6 @@
 /*@null@*/ /*@only@*/
 static char* current_scanner_preference = NULL;
 
-/**
- * @brief The current scanner preferences, during reading of scanner preferences.
- */
-static GHashTable* current_scanner_preferences = NULL;
-
-/**
- * @brief Create the scanner preferences.
- */
-static GHashTable*
-make_scanner_preferences ()
-{
-  return g_hash_table_new_full (g_str_hash,
-                                g_str_equal,
-                                g_free,
-                                g_free);
-}
-
-/**
- * @brief Add a preference to the scanner preferences.
- *
- * Both parameters are used directly, and are freed when the
- * preferences are freed.
- *
- * @param[in]  preference  The preference.
- * @param[in]  value       The value of the preference.
- */
-static void
-add_scanner_preference (GHashTable* preferences,
-                        /*@keep@*/ char* preference,
-                        /*@keep@*/ char* value)
-{
-  g_hash_table_insert (preferences, preference, value);
-}
-
 
 /* Scanner plugins. */
 
@@ -616,7 +582,6 @@
 init_otp_data ()
 {
   scanner.certificates = NULL;
-  scanner.preferences = NULL;
   scanner.rules = NULL;
   scanner.plugins_md5 = NULL;
 }
@@ -1016,9 +981,7 @@
     {
       match[0] = '\0';
       value = g_strdup (*messages);
-      add_scanner_preference (current_scanner_preferences,
-                              current_scanner_preference,
-                              value);
+      manage_nvt_preference_add (current_scanner_preference, value);
       set_scanner_state (SCANNER_PREFERENCE_NAME);
       from_scanner_start += match + 1 - *messages;
       *messages = match + 1;
@@ -1251,7 +1214,7 @@
  *
  * This includes updating the scanner state with \ref set_scanner_state
  * and \ref set_scanner_init_state, and updating scanner records with functions
- * like \ref add_scanner_preference and \ref append_task_open_port.
+ * like \ref manage_nvt_preference_add and \ref append_task_open_port.
  *
  * \endif
  *
@@ -1373,6 +1336,7 @@
       case SCANNER_INIT_SENT_COMPLETE_LIST:
       case SCANNER_INIT_SENT_PASSWORD:
       case SCANNER_INIT_DONE:
+      case SCANNER_INIT_DONE_CACHE_MODE:
       case SCANNER_INIT_TOP:
         if (scanner_state == SCANNER_TOP)
           switch (parse_scanner_bad_login (&messages))
@@ -1968,13 +1932,7 @@
                             if (scanner_init_state == SCANNER_INIT_SENT_COMPLETE_LIST)
                               {
                                 set_scanner_init_state (SCANNER_INIT_GOT_PLUGINS);
-                                /* Initialisation only sends COMPLETE_LIST when
-                                 * caching plugins, so return 1 (as though the
-                                 * scanner sent BYE). */
-                                // FIX should perhaps exit more formally with
-                                //     scanner
                                 set_nvts_md5sum (scanner.plugins_md5);
-                                return 1;
                               }
                             break;
                           case -1: return -1;
@@ -2142,10 +2100,15 @@
                             if (sync_buffer ()) return -1;
                             return 0;
                         }
-                      if (scanner.preferences)
-                        g_hash_table_destroy (scanner.preferences);
-                      scanner.preferences = current_scanner_preferences;
-                      current_scanner_preferences = NULL;
+                      if (scanner_init_state == SCANNER_INIT_DONE_CACHE_MODE)
+                        {
+                          set_scanner_init_state (SCANNER_INIT_DONE);
+                          manage_nvt_preferences_enable ();
+                          /* Return 1, as though the scanner sent BYE. */
+                          // FIX should perhaps exit more formally with scanner
+                          scanner_active = 0;
+                          return 1;
+                        }
                       break;
                     }
                   {
@@ -2216,7 +2179,6 @@
                 else if (strcasecmp ("PREFERENCES", field) == 0)
                   {
                     assert (current_scanner_preference == NULL);
-                    current_scanner_preferences = make_scanner_preferences ();
                     set_scanner_state (SCANNER_PREFERENCE_NAME);
                   }
                 else if (strcasecmp ("RULES", field) == 0)

Modified: trunk/openvas-manager/src/otp.h
===================================================================
--- trunk/openvas-manager/src/otp.h	2009-10-05 17:43:01 UTC (rev 5376)
+++ trunk/openvas-manager/src/otp.h	2009-10-05 21:54:23 UTC (rev 5377)
@@ -44,6 +44,7 @@
   SCANNER_INIT_CONNECT_INTR,    /* `connect' to scanner was interrupted. */
   SCANNER_INIT_CONNECTED,
   SCANNER_INIT_DONE,
+  SCANNER_INIT_DONE_CACHE_MODE, /* Done state when in NVT caching mode. */
   SCANNER_INIT_GOT_MD5SUM,
   SCANNER_INIT_GOT_PASSWORD,
   SCANNER_INIT_GOT_PLUGINS,

Modified: trunk/openvas-manager/src/tasks_sql.h
===================================================================
--- trunk/openvas-manager/src/tasks_sql.h	2009-10-05 17:43:01 UTC (rev 5376)
+++ trunk/openvas-manager/src/tasks_sql.h	2009-10-05 21:54:23 UTC (rev 5377)
@@ -715,6 +715,7 @@
         {
           sql ("BEGIN EXCLUSIVE;");
           sql ("DELETE FROM nvts;");
+          sql ("DELETE FROM nvt_preferences;");
           sql ("DELETE FROM meta WHERE name = 'nvts_checksum';");
           sql ("COMMIT;");
         }
@@ -746,6 +747,7 @@
     {
       sql ("BEGIN EXCLUSIVE;");
       sql ("DELETE FROM nvts;");
+      sql ("DELETE FROM nvt_preferences;");
       sql ("DELETE FROM meta WHERE name = 'nvts_checksum';");
       sql ("COMMIT;");
     }
@@ -887,7 +889,8 @@
  * Beware that calling this function while tasks are running may lead to
  * problems.
  *
- * @return 0 success, -1 error, -2 database is wrong version.
+ * @return 0 success, -1 error, -2 database is wrong version, -3 database needs
+ *         to be initialised from server.
  */
 int
 init_manage (GSList *log_config)
@@ -912,6 +915,22 @@
       && strcmp (database_version, G_STRINGIFY (DATABASE_VERSION)))
     return -2;
 
+#if 0
+  /** @todo Skip this when in NVT caching mode. */
+
+  /* Check that the database was initialised from the scanner. */
+
+  {
+    long long int count;
+    if (sql_int64 (&count, 0, 0,
+                   "SELECT count(*) FROM meta"
+                   " WHERE name = 'nvt_md5sum'"
+                   " OR name = 'nvt_preferences_enabled';")
+        || count < 2)
+      return -3;
+  }
+#endif
+
   /* Ensure the tables exist. */
 
   sql ("CREATE TABLE IF NOT EXISTS meta    (name UNIQUE, value);");
@@ -927,6 +946,7 @@
   sql ("CREATE TABLE IF NOT EXISTS report_hosts (report INTEGER, host, start_time, end_time, attack_state, current_port, max_port);");
   sql ("CREATE TABLE IF NOT EXISTS report_results (report INTEGER, result INTEGER);");
   sql ("CREATE TABLE IF NOT EXISTS nvts (oid, version, name, summary, description, copyright, cve, bid, xref, tag, sign_key_ids, category, family);");
+  sql ("CREATE TABLE IF NOT EXISTS nvt_preferences (name, value);");
   sql ("CREATE TABLE IF NOT EXISTS lsc_credentials (name, comment, rpm, deb, dog);");
 
   /* Ensure the version is set. */
@@ -4432,6 +4452,54 @@
 }
 
 
+/* NVT preferences. */
+
+/**
+ * @brief Add an NVT preference.
+ *
+ * @param[in]  name   The name of the preference.
+ * @param[in]  value  The value of the preference.
+ */
+void
+manage_nvt_preference_add (char* name, char* value)
+{
+  gchar* quoted_name = sql_quote (name);
+  gchar* quoted_value = sql_quote (value);
+  sql ("INSERT into nvt_preferences (name, value)"
+       " VALUES ('%s', '%s');",
+       quoted_name, quoted_value);
+  g_free (quoted_name);
+  g_free (quoted_value);
+}
+
+/**
+ * @brief Add an NVT preference.
+ *
+ * @param[in]  name   The name of the preference.
+ * @param[in]  value  The value of the preference.
+ */
+void
+manage_nvt_preferences_enable ()
+{
+  sql ("INSERT OR REPLACE INTO meta (name, value)"
+       " VALUES ('nvt_preferences_enabled', 1);");
+}
+
+/**
+ * @brief Initialise an NVT preference iterator.
+ *
+ * @param[in]  iterator  Iterator.
+ */
+void
+init_nvt_preference_iterator (iterator_t* iterator)
+{
+  init_table_iterator (iterator, "nvt_preferences");
+}
+
+DEF_ACCESS (nvt_preference_iterator_name, 0);
+DEF_ACCESS (nvt_preference_iterator_value, 1);
+
+
 /* LSC Credentials. */
 
 /**



More information about the Openvas-commits mailing list