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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Oct 9 11:51:31 CEST 2009


Author: mattm
Date: 2009-10-09 11:51:29 +0200 (Fri, 09 Oct 2009)
New Revision: 5455

Modified:
   trunk/openvas-manager/ChangeLog
   trunk/openvas-manager/src/manage.c
   trunk/openvas-manager/src/manage.h
   trunk/openvas-manager/src/tasks_sql.h
Log:
	Add support for storing files referenced in preferences, and sending
	of the files to the scanner.

	* src/tasks_sql.h (init_manage): Add table config_files.
	(init_config_file_iterator, config_file_iterator_content)
	(config_file_iterator_length): New functions.

	* src/manage.c (preference_value): Add file type parsing, which pushes
	file onto new arg "files".
	(send_config_preferences): Pass files list through.
	(send_config_file): New function.
	(start_task): Send files gathered with sending preferences.

	* src/manage.h: Add new headers.

Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog	2009-10-09 09:44:54 UTC (rev 5454)
+++ trunk/openvas-manager/ChangeLog	2009-10-09 09:51:29 UTC (rev 5455)
@@ -1,3 +1,20 @@
+2009-10-09  Matthew Mundell <matthew.mundell at intevation.de>
+
+	Add support for storing files referenced in preferences, and sending
+	of the files to the scanner.
+
+	* src/tasks_sql.h (init_manage): Add table config_files.
+	(init_config_file_iterator, config_file_iterator_content)
+	(config_file_iterator_length): New functions.
+
+	* src/manage.c (preference_value): Add file type parsing, which pushes
+	file onto new arg "files".
+	(send_config_preferences): Pass files list through.
+	(send_config_file): New function.
+	(start_task): Send files gathered with sending preferences.
+
+	* src/manage.h: Add new headers.
+
 2009-10-08  Matthew Mundell <matthew.mundell at intevation.de>
 
 	* src/manage.c (preference_value): New function.  Handles radio

Modified: trunk/openvas-manager/src/manage.c
===================================================================
--- trunk/openvas-manager/src/manage.c	2009-10-09 09:44:54 UTC (rev 5454)
+++ trunk/openvas-manager/src/manage.c	2009-10-09 09:51:29 UTC (rev 5455)
@@ -497,14 +497,21 @@
  * @return Real value of the preference.
  */
 static gchar*
-preference_value (const char* name, const char* full_value)
+preference_value (const char* name, const char* full_value, GSList** files)
 {
   char *bracket = strchr (name, '[');
-  if (bracket && strncmp (bracket, "[radio]:", strlen ("[radio]:")) == 0)
+  if (bracket)
     {
-      char *semicolon = strchr (full_value, ';');
-      if (semicolon)
-        return g_strndup (full_value, semicolon - full_value);
+      if (strncmp (bracket, "[radio]:", strlen ("[radio]:")) == 0)
+        {
+          char *semicolon = strchr (full_value, ';');
+          if (semicolon)
+            return g_strndup (full_value, semicolon - full_value);
+        }
+      else if ((strncmp (bracket, "[file]:", strlen ("[file]:")) == 0)
+               && full_value
+               && strlen (full_value))
+        *files = g_slist_append (*files, g_strdup (full_value));
     }
   return g_strdup (full_value);
 }
@@ -518,7 +525,9 @@
  * @return 0 on success, -1 on failure.
  */
 static int
-send_config_preferences (const char* config, const char* name)
+send_config_preferences (const char* config,
+                         const char* name,
+                         GSList** files)
 {
   iterator_t prefs;
 
@@ -540,7 +549,9 @@
           return -1;
         }
 
-      value = preference_value (name, preference_iterator_value (&prefs));
+      value = preference_value (name,
+                                preference_iterator_value (&prefs),
+                                files);
       if (send_to_server (value))
         {
           g_free (value);
@@ -590,6 +601,50 @@
 }
 
 /**
+ * @brief Send a file from a config to the scanner.
+ *
+ * @param[in]  config  Config.
+ * @param[in]  file    File name.
+ *
+ * @return 0 on success, -1 on failure.
+ */
+static int
+send_config_file (const char* config, const char* file)
+{
+  iterator_t files;
+
+  init_config_file_iterator (&files, config, file);
+  while (next (&files))
+    {
+      int length = config_file_iterator_length (&files);
+
+      if (length == -1)
+        {
+          cleanup_iterator (&files);
+          return -1;
+        }
+
+      if (sendf_to_server ("CLIENT <|> ATTACHED_FILE\n"
+                           "name: %s\n"
+                           "content: octet/stream\n"
+                           "bytes: %i\n",
+                           file,
+                           length))
+        {
+          cleanup_iterator (&files);
+          return -1;
+        }
+      if (sendn_to_server (config_file_iterator_content (&files), length))
+        {
+          cleanup_iterator (&files);
+          return -1;
+        }
+    }
+  cleanup_iterator (&files);
+  return 0;
+}
+
+/**
  * @brief Start a task.
  *
  * Use \ref send_to_server to queue the task start sequence in the scanner
@@ -611,6 +666,7 @@
   char *hosts, *target, *config, *selector;
   gchar *plugins;
   int fail;
+  GSList *files = NULL;
 
   tracef ("   start task %u\n", task_id (task));
 
@@ -706,12 +762,12 @@
 
   /* Send the scanner and plugins preferences. */
 
-  if (send_config_preferences (config, "SERVER_PREFS"))
+  if (send_config_preferences (config, "SERVER_PREFS", &files))
     {
       free (config);
       return -1;
     }
-  if (send_config_preferences (config, "PLUGINS_PREFS"))
+  if (send_config_preferences (config, "PLUGINS_PREFS", &files))
     {
       free (config);
       return -1;
@@ -723,6 +779,29 @@
       return -1;
     }
 
+  /* Send any files. */
+
+  while (files)
+    {
+      GSList *last = files;
+      if (send_config_file (config, files->data))
+        {
+          free (config);
+          /* Free the data. */
+          while (files)
+            {
+              g_free (files->data);
+              files = g_slist_next (files);
+            }
+          /* Free the list. */
+          g_slist_free (last);
+          return -1;
+        }
+      files = g_slist_next (files);
+      g_free (last->data);
+      g_slist_free_1 (last);
+    }
+
   /* Send the rules. */
 
   if (send_to_server ("CLIENT <|> RULES <|>\n"))

Modified: trunk/openvas-manager/src/manage.h
===================================================================
--- trunk/openvas-manager/src/manage.h	2009-10-09 09:44:54 UTC (rev 5454)
+++ trunk/openvas-manager/src/manage.h	2009-10-09 09:51:29 UTC (rev 5455)
@@ -558,6 +558,15 @@
 int
 config_in_use (const char*);
 
+void
+init_config_file_iterator (iterator_t*, const char*, const char*);
+
+const char*
+config_file_iterator_content (iterator_t*);
+
+int
+config_file_iterator_length (iterator_t*);
+
 
 /* NVT's. */
 

Modified: trunk/openvas-manager/src/tasks_sql.h
===================================================================
--- trunk/openvas-manager/src/tasks_sql.h	2009-10-09 09:44:54 UTC (rev 5454)
+++ trunk/openvas-manager/src/tasks_sql.h	2009-10-09 09:51:29 UTC (rev 5455)
@@ -969,6 +969,7 @@
   sql ("CREATE TABLE IF NOT EXISTS nvt_selectors (name, exclude INTEGER, type INTEGER, family_or_nvt);");
   sql ("CREATE TABLE IF NOT EXISTS targets (name, hosts, comment);");
   sql ("CREATE TABLE IF NOT EXISTS configs (name UNIQUE, nvt_selector, comment, family_count INTEGER, nvt_count INTEGER, families_growing INTEGER, nvts_growing INTEGER);");
+  sql ("CREATE TABLE IF NOT EXISTS config_files (config INTEGER, name, content);");
   sql ("CREATE TABLE IF NOT EXISTS config_preferences (config INTEGER, type, name, value);");
   sql ("CREATE TABLE IF NOT EXISTS tasks   (uuid, name, hidden INTEGER, time, comment, description, owner, run_status INTEGER, start_time, end_time, config, target);");
   sql ("CREATE TABLE IF NOT EXISTS results (task INTEGER, subnet, host, port, nvt, type, description)");
@@ -4007,6 +4008,73 @@
   return FALSE;
 }
 
+/**
+ * @brief Initialise a config file iterator.
+ *
+ * @param[in]  iterator  Iterator.
+ * @param[in]  config    Config name.
+ * @param[in]  file      File name, NULL for all files.
+ */
+void
+init_config_file_iterator (iterator_t* iterator, const char* config, const char* file)
+{
+  int ret;
+  const char* tail;
+  gchar* formatted;
+  sqlite3_stmt* stmt;
+  gchar *quoted_config = sql_nquote (config, strlen (config));
+
+  iterator->done = FALSE;
+  if (file)
+    {
+      gchar *quoted_file = sql_nquote (file, strlen (file));
+      formatted = g_strdup_printf ("SELECT *, length(content) FROM config_files"
+                                   " WHERE config = (SELECT ROWID FROM configs WHERE name = '%s')"
+                                   " AND name = '%s';",
+                                   quoted_config, quoted_file);
+      g_free (quoted_file);
+    }
+  else
+    formatted = g_strdup_printf ("SELECT *, length(content) FROM config_files"
+                                 " WHERE config = (SELECT ROWID FROM configs WHERE name = '%s')",
+                                 quoted_config);
+  g_free (quoted_config);
+
+  while (1)
+    {
+      ret = sqlite3_prepare (task_db, (char*) formatted, -1, &stmt, &tail);
+      if (ret == SQLITE_BUSY) continue;
+      g_free (formatted);
+      iterator->stmt = stmt;
+      if (ret == SQLITE_OK)
+        {
+          if (stmt == NULL)
+            {
+              g_warning ("%s: sqlite3_prepare failed with NULL stmt: %s\n",
+                         __FUNCTION__,
+                         sqlite3_errmsg (task_db));
+              abort ();
+            }
+          break;
+        }
+      g_warning ("%s: sqlite3_prepare failed: %s\n",
+                 __FUNCTION__,
+                 sqlite3_errmsg (task_db));
+      abort ();
+    }
+}
+
+DEF_ACCESS (config_file_iterator_content, 2);
+
+int
+config_file_iterator_length (iterator_t* iterator)
+{
+  int ret;
+  if (iterator->done) return -1;
+  ret = (int) sqlite3_column_int (iterator->stmt, 3);
+  return ret;
+}
+
 
 /* NVT's. */
 



More information about the Openvas-commits mailing list