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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Oct 23 00:31:10 CEST 2009


Author: mattm
Date: 2009-10-23 00:31:08 +0200 (Fri, 23 Oct 2009)
New Revision: 5677

Modified:
   trunk/openvas-manager/ChangeLog
   trunk/openvas-manager/src/manage.h
   trunk/openvas-manager/src/omp.c
   trunk/openvas-manager/src/tasks_sql.h
Log:
	Extend OMP GET_CONFIGS for retrieving a single config with a summary
	of the selected families.

	* src/tasks_sql.h (DATABASE_VERSION): Increase to 4, for change to
	nvt_selectors.
	(init_manage): Add row family to nvt_selectors.
	(init_config_iterator): Add name arg.
	(family_nvt_count, init_family_iterator, family_iterator_name)
	(nvt_selector_family_growing, nvt_selector_family_selected_count): New
	functions.

	* src/manage.h: Update headers.

	* src/omp.c (omp_xml_handle_start_element, omp_xml_handle_end_element):
	Add name and families attributes to GET_CONFIGS.

Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog	2009-10-22 20:13:23 UTC (rev 5676)
+++ trunk/openvas-manager/ChangeLog	2009-10-22 22:31:08 UTC (rev 5677)
@@ -1,3 +1,21 @@
+2009-10-23  Matthew Mundell <matthew.mundell at intevation.de>
+
+	Extend OMP GET_CONFIGS for retrieving a single config with a summary
+	of the selected families.
+
+	* src/tasks_sql.h (DATABASE_VERSION): Increase to 4, for change to
+	nvt_selectors.
+	(init_manage): Add row family to nvt_selectors.
+	(init_config_iterator): Add name arg.
+	(family_nvt_count, init_family_iterator, family_iterator_name)
+	(nvt_selector_family_growing, nvt_selector_family_selected_count): New
+	functions.
+
+	* src/manage.h: Update headers.
+
+	* src/omp.c (omp_xml_handle_start_element, omp_xml_handle_end_element):
+	Add name and families attributes to GET_CONFIGS.
+
 2009-10-22  Matthew Mundell <matthew.mundell at intevation.de>
 
 	Add generation of LSC user keys and packages (RPM and Debian).  Extend OMP

Modified: trunk/openvas-manager/src/manage.h
===================================================================
--- trunk/openvas-manager/src/manage.h	2009-10-22 20:13:23 UTC (rev 5676)
+++ trunk/openvas-manager/src/manage.h	2009-10-22 22:31:08 UTC (rev 5677)
@@ -170,7 +170,7 @@
   sqlite3_stmt* stmt;
   gboolean done;
 } iterator_t;
-#endif
+#endif /* TASKS_SQL */
 
 
 /* Task global variables. */
@@ -550,7 +550,7 @@
 delete_config (const char*);
 
 void
-init_config_iterator (iterator_t*);
+init_config_iterator (iterator_t*, const char*);
 
 const char*
 config_iterator_name (iterator_t*);
@@ -633,6 +633,9 @@
 const char*
 nvt_iterator_family (iterator_t*);
 
+int
+family_nvt_count (const char *);
+
 
 /* NVT selectors. */
 
@@ -648,6 +651,18 @@
 int
 nvt_selector_nvt_count (const char*, const char*);
 
+void
+init_family_iterator (iterator_t*, int, const char*);
+
+const char*
+family_iterator_name (iterator_t*);
+
+int
+nvt_selector_family_growing (const char *, const char *, int);
+
+int
+nvt_selector_family_selected_count (const char *, const char *, int);
+
 
 /* NVT preferences. */
 

Modified: trunk/openvas-manager/src/omp.c
===================================================================
--- trunk/openvas-manager/src/omp.c	2009-10-22 20:13:23 UTC (rev 5676)
+++ trunk/openvas-manager/src/omp.c	2009-10-22 22:31:08 UTC (rev 5677)
@@ -884,7 +884,19 @@
         else if (strcasecmp ("GET_CERTIFICATES", element_name) == 0)
           set_client_state (CLIENT_GET_CERTIFICATES);
         else if (strcasecmp ("GET_CONFIGS", element_name) == 0)
-          set_client_state (CLIENT_GET_CONFIGS);
+          {
+            const gchar* attribute;
+            assert (current_name == NULL);
+            if (find_attribute (attribute_names, attribute_values,
+                                "name", &attribute))
+              openvas_append_string (&current_name, attribute);
+            if (find_attribute (attribute_names, attribute_values,
+                                "families", &attribute))
+              current_int_1 = atoi (attribute);
+            else
+              current_int_1 = 0;
+            set_client_state (CLIENT_GET_CONFIGS);
+          }
         else if (strcasecmp ("GET_DEPENDENCIES", element_name) == 0)
           set_client_state (CLIENT_GET_DEPENDENCIES);
         else if (strcasecmp ("GET_LSC_CREDENTIALS", element_name) == 0)
@@ -5115,12 +5127,16 @@
           SEND_TO_CLIENT_OR_FAIL ("<get_configs_response"
                                   " status=\"" STATUS_OK "\""
                                   " status_text=\"" STATUS_OK_TEXT "\">");
-          init_config_iterator (&configs);
+          init_config_iterator (&configs, current_name);
           while (next (&configs))
             {
+              int config_nvts_growing;
               const char *selector, *config_name;
+
               selector = config_iterator_nvt_selector (&configs);
               config_name = config_iterator_name (&configs);
+              config_nvts_growing = config_iterator_nvts_growing (&configs);
+
               SENDF_TO_CLIENT_OR_FAIL ("<config>"
                                        "<name>%s</name>"
                                        "<comment>%s</comment>"
@@ -5130,8 +5146,7 @@
                                        "<nvt_count>"
                                        "%i<growing>%i</growing>"
                                        "</nvt_count>"
-                                       "<in_use>%i</in_use>"
-                                       "</config>",
+                                       "<in_use>%i</in_use>",
                                        config_name,
                                        config_iterator_comment (&configs),
                                        nvt_selector_family_count (selector,
@@ -5139,9 +5154,68 @@
                                        config_iterator_families_growing (&configs),
                                        nvt_selector_nvt_count (selector,
                                                                config_name),
-                                       config_iterator_nvts_growing (&configs),
+                                       config_nvts_growing,
                                        config_in_use (config_name));
+
+              if (current_int_1)
+                {
+                  iterator_t families;
+                  int selected_count = 0;
+
+                  /* The "families" attribute was true. */
+
+                  SENDF_TO_CLIENT_OR_FAIL ("<families>");
+                  init_family_iterator (&families,
+                                        config_nvts_growing,
+                                        selector);
+                  while (next (&families))
+                    {
+                      int family_growing, nvt_count, family_selected_count;
+                      const char *family;
+
+                      family = family_iterator_name (&families);
+                      /* family can be NULL if the selector was created
+                       * from an RC file, as it's currently too slow to lookup
+                       * each family when inserting the selector. */
+                      if (family == NULL) continue;
+
+                      family_growing = nvt_selector_family_growing
+                                        (selector,
+                                         family,
+                                         config_nvts_growing);
+                      nvt_count = family_nvt_count (family);
+                      if (family_growing)
+                        family_selected_count = nvt_count;
+                      else
+                        family_selected_count
+                          = nvt_selector_family_selected_count
+                             (selector,
+                              family,
+                              family_growing);
+
+                      SENDF_TO_CLIENT_OR_FAIL
+                       ("<family>"
+                        "<name>%s</name>"
+                        "<selected_count>%i</selected_count>"
+                        "<nvt_count>%i</nvt_count>"
+                        "<growing>%i</growing>"
+                        "</family>",
+                        family,
+                        family_selected_count,
+                        nvt_count,
+                        family_growing);
+                      selected_count += family_selected_count;
+                    }
+                  cleanup_iterator (&families);
+                  SENDF_TO_CLIENT_OR_FAIL ("</families>"
+                                           "<selected_count>%i</selected_count>"
+                                           "</config>",
+                                           selected_count);
+                }
+              else
+                SENDF_TO_CLIENT_OR_FAIL ("</config>");
             }
+          openvas_free_string_var (&current_name);
           cleanup_iterator (&configs);
           SEND_TO_CLIENT_OR_FAIL ("</get_configs_response>");
           set_client_state (CLIENT_AUTHENTIC);

Modified: trunk/openvas-manager/src/tasks_sql.h
===================================================================
--- trunk/openvas-manager/src/tasks_sql.h	2009-10-22 20:13:23 UTC (rev 5676)
+++ trunk/openvas-manager/src/tasks_sql.h	2009-10-22 22:31:08 UTC (rev 5677)
@@ -31,7 +31,7 @@
 /**
  * @brief Version of the database schema.
  */
-#define DATABASE_VERSION 3
+#define DATABASE_VERSION 4
 
 /**
  * @brief NVT selector type for "all" rule.
@@ -1078,7 +1078,7 @@
   sql ("CREATE TABLE IF NOT EXISTS meta    (name UNIQUE, value);");
   sql ("CREATE TABLE IF NOT EXISTS users   (name UNIQUE, password);");
   /* nvt_selectors types: 0 all, 1 family, 2 NVT (NVT_SELECTOR_TYPE_* above). */
-  sql ("CREATE TABLE IF NOT EXISTS nvt_selectors (name, exclude INTEGER, type INTEGER, family_or_nvt);");
+  sql ("CREATE TABLE IF NOT EXISTS nvt_selectors (name, exclude INTEGER, type INTEGER, family_or_nvt, family);");
   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 task_files (task INTEGER, name, content);");
@@ -4105,11 +4105,48 @@
  * @brief Initialise a config iterator.
  *
  * @param[in]  iterator  Iterator.
+ * @param[in]  name      Name of config.  NULL for all.
  */
 void
-init_config_iterator (iterator_t* iterator)
+init_config_iterator (iterator_t* iterator, const char *name)
 {
-  init_table_iterator (iterator, "configs");
+  int ret;
+  const char* tail;
+  gchar* formatted;
+  sqlite3_stmt* stmt;
+
+  iterator->done = FALSE;
+  if (name)
+    {
+      gchar *quoted_name = sql_quote (name);
+      formatted = g_strdup_printf ("SELECT * FROM configs WHERE name = '%s';",
+                                   quoted_name);
+      g_free (quoted_name);
+    }
+  else
+    formatted = g_strdup ("SELECT * FROM configs;");
+  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_iterator_name, 0);
@@ -4506,6 +4543,24 @@
   return ret;
 }
 
+/**
+ * @brief Get the number of NVTs in a family.
+ *
+ * @param[in]  family  Family name.
+ *
+ * @return Number of NVTs in family.
+ */
+int
+family_nvt_count (const char *family)
+{
+  gchar *quoted_family = sql_quote (family);
+  int ret = sql_int (0, 0,
+                     "SELECT COUNT(*) FROM nvts WHERE family = '%s';",
+                     quoted_family);
+  g_free (quoted_family);
+  return ret;
+}
+
 
 /* NVT selectors. */
 
@@ -4761,6 +4816,130 @@
                     config);
 }
 
+/**
+ * @brief Initialise an NVT selector family iterator.
+ *
+ * @param[in]  iterator  Iterator.
+ * @param[in]  all       True for an "all" selector, else 0.
+ * @param[in]  selector  Name of NVT selector.
+ */
+void
+init_family_iterator (iterator_t* iterator, int all, const char* selector)
+{
+  int ret;
+  const char* tail;
+  gchar* formatted;
+  sqlite3_stmt* stmt;
+
+  iterator->done = FALSE;
+  if (all)
+    formatted = g_strdup_printf ("SELECT distinct family FROM nvts;");
+  else
+    {
+      gchar *quoted_selector = sql_quote (selector);
+      formatted = g_strdup_printf ("SELECT distinct family FROM nvt_selectors"
+                                   " WHERE (type = 1 OR type = 2) AND name = '%s';",
+                                   quoted_selector);
+      g_free (quoted_selector);
+    }
+
+  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 (family_iterator_name, 0);
+
+/**
+ * @brief Get whether an NVT selector family is growing.
+ *
+ * @param[in]  selector  NVT selector.
+ * @param[in]  family    Family name.
+ * @param[in]  all       True if selector is an "all" selector, else 0.
+ *
+ * @return 1 growing, 0 static.
+ */
+int
+nvt_selector_family_growing (const char *selector,
+                             const char *family,
+                             int all)
+{
+  int ret;
+
+  if (all) return 1;
+
+  gchar *quoted_selector = sql_quote (selector);
+  gchar *quoted_family = sql_quote (family);
+
+  ret = sql_int (0, 0,
+                 "SELECT COUNT(*) FROM nvt_selectors"
+                 " WHERE name = '%s' AND type = 1 AND nvt_or_family = '%s'"
+                 " LIMIT 1;",
+                 quoted_selector, quoted_family);
+
+  g_free (quoted_selector);
+  g_free (quoted_family);
+
+  return ret == 0 ? 0 : 1;
+}
+
+/**
+ * @brief Get the number of NVTs selected in an NVT selector family.
+ *
+ * @param[in]  selector  NVT selector.
+ * @param[in]  family    Family name.
+ * @param[in]  growing   True if the family is growing, else 0.
+ *
+ * @return Number of NVTs selected in family.
+ */
+int
+nvt_selector_family_selected_count (const char *selector,
+                                    const char *family,
+                                    int growing)
+{
+  int ret;
+
+  gchar *quoted_family = sql_quote (family);
+
+  if (growing)
+    ret = sql_int (0, 0,
+                   "SELECT COUNT(*) FROM nvts WHERE family = '%s';",
+                   quoted_family);
+  else
+    {
+      gchar *quoted_selector = sql_quote (selector);
+      ret = sql_int (0, 0,
+                     "SELECT COUNT(*) FROM nvt_selectors"
+                     " WHERE name = '%s' AND type = 2 AND family = '%s';",
+                     quoted_selector,
+                     quoted_family);
+      g_free (quoted_selector);
+    }
+
+  g_free (quoted_family);
+
+  return ret;
+}
+
 
 /* NVT preferences. */
 



More information about the Openvas-commits mailing list