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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Oct 28 17:40:34 CET 2009


Author: mattm
Date: 2009-10-28 17:40:33 +0100 (Wed, 28 Oct 2009)
New Revision: 5748

Modified:
   trunk/openvas-manager/ChangeLog
   trunk/openvas-manager/src/omp.c
   trunk/openvas-manager/src/tasks_sql.h
Log:
	* src/tasks_sql.h (config_preference): Add the config to the query.
	(init_preference_iterator): Use "is NULL" instead of "= NULL" in query.
	(nvt_selector_family_count): Add the growing case where there is an "all"
	selector with only "include" selectors.
	(nvt_selector_nvt_count): Add naive handling of the growing case where
	there is an "all" selector with other selectors.
	(nvt_selector_family_selected_count): Handle NULL family for static
	selection.

	* src/omp.c (omp_xml_handle_end_element): In CLIENT_GET_CONFIGS handle the
	NULL family, rename FAMILY/SELECTED_COUNT to FAMILY/NVT_COUNT and
	FAMILY/NVT_COUNT to FAMILY/MAX_NVT_COUNT, and add MAX_NVT_COUNT and
	KNOWN_NVT_COUNT to the config.

Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog	2009-10-28 15:45:47 UTC (rev 5747)
+++ trunk/openvas-manager/ChangeLog	2009-10-28 16:40:33 UTC (rev 5748)
@@ -1,5 +1,21 @@
 2009-10-28  Matthew Mundell <matthew.mundell at intevation.de>
 
+	* src/tasks_sql.h (config_preference): Add the config to the query.
+	(init_preference_iterator): Use "is NULL" instead of "= NULL" in query.
+	(nvt_selector_family_count): Add the growing case where there is an "all"
+	selector with only "include" selectors.
+	(nvt_selector_nvt_count): Add naive handling of the growing case where
+	there is an "all" selector with other selectors.
+	(nvt_selector_family_selected_count): Handle NULL family for static
+	selection.
+
+	* src/omp.c (omp_xml_handle_end_element): In CLIENT_GET_CONFIGS handle the
+	NULL family, rename FAMILY/SELECTED_COUNT to FAMILY/NVT_COUNT and
+	FAMILY/NVT_COUNT to FAMILY/MAX_NVT_COUNT, and add MAX_NVT_COUNT and
+	KNOWN_NVT_COUNT to the config.
+
+2009-10-28  Matthew Mundell <matthew.mundell at intevation.de>
+
 	* src/manage.c (start_task): Set current_scanner_task before task run
 	status, so that report run status is set with task run status.
 

Modified: trunk/openvas-manager/src/omp.c
===================================================================
--- trunk/openvas-manager/src/omp.c	2009-10-28 15:45:47 UTC (rev 5747)
+++ trunk/openvas-manager/src/omp.c	2009-10-28 16:40:33 UTC (rev 5748)
@@ -5167,6 +5167,8 @@
                                        "<family_count>"
                                        "%i<growing>%i</growing>"
                                        "</family_count>"
+                                       /* The number of NVT's selected by
+                                        * the selector. */
                                        "<nvt_count>"
                                        "%i<growing>%i</growing>"
                                        "</nvt_count>"
@@ -5184,7 +5186,7 @@
               if (current_int_1)
                 {
                   iterator_t families;
-                  int selected_count = 0;
+                  int max_nvt_count = 0, known_nvt_count = 0;
 
                   /* The "families" attribute was true. */
 
@@ -5194,51 +5196,70 @@
                                         selector);
                   while (next (&families))
                     {
-                      int family_growing, nvt_count, family_selected_count;
+                      int family_growing, family_max, 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;
+                      if (family)
+                        {
+                          family_growing = nvt_selector_family_growing
+                                            (selector,
+                                             family,
+                                             config_nvts_growing);
+                          family_max = family_nvt_count (family);
+                          if (family_growing)
+                            family_selected_count = family_max;
+                          else
+                            family_selected_count
+                              = nvt_selector_family_selected_count
+                                 (selector,
+                                  family,
+                                  family_growing);
+                          known_nvt_count += family_selected_count;
+                        }
                       else
-                        family_selected_count
-                          = nvt_selector_family_selected_count
-                             (selector,
-                              family,
-                              family_growing);
+                        {
+                          /* The family can be NULL if an RC adds an NVT to a
+                           * config and the NVT is missing from the NVT
+                           * cache. */
+                          family_growing = 0;
+                          family_max = -1;
+                          family_selected_count
+                            = nvt_selector_family_selected_count
+                               (selector, NULL, 0);
+                        }
 
                       SENDF_TO_CLIENT_OR_FAIL
                        ("<family>"
                         "<name>%s</name>"
-                        "<selected_count>%i</selected_count>"
-                        /**
-                         * @todo This is total NVTs in family, whereas in
-                         * config above it's total number of selected NVTs.
-                         */
+                        /* The number of selected NVT's. */
                         "<nvt_count>%i</nvt_count>"
+                        /* The total number of NVT's in the family. */
+                        "<max_nvt_count>%i</max_nvt_count>"
                         "<growing>%i</growing>"
                         "</family>",
-                        family,
+                        family ? family : "",
                         family_selected_count,
-                        nvt_count,
+                        family_max,
                         family_growing);
-                      selected_count += family_selected_count;
+                      if (family_max > 0)
+                        max_nvt_count += family_max;
                     }
                   cleanup_iterator (&families);
                   SENDF_TO_CLIENT_OR_FAIL ("</families>"
-                                           "<selected_count>%i</selected_count>"
+                                           /* The total number of NVT's in all
+                                            * the families for which the
+                                            * selector selects at least one
+                                            * NVT. */
+                                           "<max_nvt_count>%i</max_nvt_count>"
+                                           /* Total number of selected known
+                                            * NVT's. */
+                                           "<known_nvt_count>"
+                                           "%i"
+                                           "</known_nvt_count>"
                                            "</config>",
-                                           selected_count);
+                                           max_nvt_count,
+                                           known_nvt_count);
                 }
               else
                 SENDF_TO_CLIENT_OR_FAIL ("</config>");

Modified: trunk/openvas-manager/src/tasks_sql.h
===================================================================
--- trunk/openvas-manager/src/tasks_sql.h	2009-10-28 15:45:47 UTC (rev 5747)
+++ trunk/openvas-manager/src/tasks_sql.h	2009-10-28 16:40:33 UTC (rev 5748)
@@ -3937,16 +3937,17 @@
 static char *
 config_preference (config_t config, const char *type, const char *preference)
 {
+  /** @todo Quote type and preference. */
   if (type)
     return sql_string (0, 0,
                        "SELECT value FROM config_preferences"
-                       " WHERE type = '%s' AND name = '%s';",
-                       type, preference);
+                       " WHERE ROWID = %llu AND  type = '%s' AND name = '%s';",
+                       config, type, preference);
   else
     return sql_string (0, 0,
                        "SELECT value FROM config_preferences"
-                       " WHERE type = NULL AND name = '%s';",
-                       preference);
+                       " WHERE ROWID = %llu AND type is NULL AND name = '%s';",
+                       config, preference);
 }
 
 /**
@@ -4549,7 +4550,7 @@
   else
     formatted = g_strdup_printf ("SELECT * FROM config_preferences"
                                  " WHERE config = (SELECT ROWID FROM configs WHERE name = '%s')"
-                                 " AND type = NULL;",
+                                 " AND type is NULL;",
                                  quoted_config);
   g_free (quoted_config);
 
@@ -5083,21 +5084,45 @@
     {
       if (config_families_growing (config))
         {
-          if ((sql_int (0, 0,
+          /* The number of families can grow. */
+
+          if (sql_int (0, 0,
                         "SELECT COUNT(*) FROM nvt_selectors WHERE name = '%s';",
                         selector)
-               == 1)
-              && (sql_int (0, 0,
+              == 1)
+            {
+              /* There is only one selector. */
+              if (sql_int (0, 0,
                            "SELECT COUNT(*) FROM nvt_selectors"
                            " WHERE name = '%s'"
                            " AND type = " G_STRINGIFY (NVT_SELECTOR_TYPE_ALL)
                            ";",
                            selector)
-                  == 1))
-            return sql_int (0, 0, "SELECT COUNT(DISTINCT family) FROM nvts;");
-          return -1;
+                  == 1)
+                /* It is the all selector. */
+                return sql_int (0, 0,
+                                "SELECT COUNT(DISTINCT family) FROM nvts;");
+              /* An error somewhere. */
+              return -1;
+            }
+          else
+            {
+              /* There are multiple selectors. */
+              if (sql_int (0, 0,
+                           "SELECT COUNT(*) FROM nvt_selectors"
+                           " WHERE name = '%s' AND exclude = 1;",
+                           selector))
+                {
+                  /* There are excludes, so give up. */
+                  return -1;
+                }
+              /* It is equivalent to the all selector. */
+              return sql_int (0, 0,
+                              "SELECT COUNT(DISTINCT family) FROM nvts;");
+            }
         }
       else
+        /* The number of families is static. */
         return sql_int (0, 0,
                         "SELECT family_count FROM configs"
                         " WHERE name = '%s'"
@@ -5118,26 +5143,62 @@
 int
 nvt_selector_nvt_count (const char* selector, const char* config)
 {
+  /** @todo sql_quote. */
   if (config_nvts_growing (config))
     {
+      /* The number of NVT's can increase. */
+
       if (nvt_cache_present ())
         {
-          if ((sql_int (0, 0,
+          int alls = sql_int (0, 0,
+                              "SELECT COUNT(*) FROM nvt_selectors"
+                              " WHERE name = '%s'"
+                              " AND type = " G_STRINGIFY (NVT_SELECTOR_TYPE_ALL)
+                              ";",
+                              selector);
+          if (sql_int (0, 0,
                         "SELECT COUNT(*) FROM nvt_selectors WHERE name = '%s';",
                         selector)
-               == 1)
-              && (sql_int (0, 0,
-                           "SELECT COUNT(*) FROM nvt_selectors"
-                           " WHERE name = '%s'"
-                           " AND type = " G_STRINGIFY (NVT_SELECTOR_TYPE_ALL)
-                           ";",
-                           selector)
-                  == 1))
-            return sql_int (0, 0, "SELECT COUNT(*) FROM nvts;");
+              == 1)
+            {
+              /* There is one selector. */
+              if (alls == 1)
+                /* It is the all selector. */
+                return sql_int (0, 0, "SELECT COUNT(*) FROM nvts;");
+              /* An error somewhere. */
+              return -1;
+            }
+          else
+            {
+              int excludes, includes;
+
+              /* There are multiple selectors. */
+
+              excludes = sql_int (0, 0,
+                                  "SELECT COUNT(*) FROM nvt_selectors"
+                                  " WHERE name = '%s' AND exclude = 1"
+                                  " AND type = "
+                                  G_STRINGIFY (NVT_SELECTOR_TYPE_NVT)
+                                  ";",
+                                  selector);
+              includes = sql_int (0, 0,
+                                  "SELECT COUNT(*) FROM nvt_selectors"
+                                  " WHERE name = '%s' AND exclude = 0"
+                                  " AND type = "
+                                  G_STRINGIFY (NVT_SELECTOR_TYPE_NVT)
+                                  ";",
+                                  selector);
+
+              return MAX ((alls ? sql_int (0, 0, "SELECT COUNT(*) FROM nvts;")
+                                  - excludes
+                                : includes - excludes),
+                          0);
+            }
         }
       return -1;
     }
   else
+    /* The number of NVT's is static. */
     return sql_int (0, 0,
                     "SELECT nvt_count FROM configs"
                     " WHERE name = '%s'"
@@ -5247,25 +5308,37 @@
 {
   int ret;
 
-  gchar *quoted_family = sql_quote (family);
-
   if (growing)
-    ret = sql_int (0, 0,
-                   "SELECT COUNT(*) FROM nvts WHERE family = '%s';",
-                   quoted_family);
+    {
+      gchar *quoted_family = sql_quote (family);
+      ret = sql_int (0, 0,
+                     "SELECT COUNT(*) FROM nvts WHERE family = '%s';",
+                     quoted_family);
+      g_free (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);
+      if (family)
+        {
+          gchar *quoted_family = sql_quote (family);
+          ret = sql_int (0, 0,
+                         "SELECT COUNT(*) FROM nvt_selectors"
+                         " WHERE exclude = 0 AND type = 2"
+                         " AND name = '%s' AND family = '%s';",
+                         quoted_selector,
+                         quoted_family);
+          g_free (quoted_family);
+        }
+      else
+        ret = sql_int (0, 0,
+                       "SELECT COUNT(*) FROM nvt_selectors"
+                       " WHERE exclude = 0 AND type = 2"
+                       " AND name = '%s' AND family is NULL;",
+                       quoted_selector);
       g_free (quoted_selector);
     }
 
-  g_free (quoted_family);
-
   return ret;
 }
 



More information about the Openvas-commits mailing list