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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Dec 25 20:39:38 CET 2009


Author: mattm
Date: 2009-12-25 20:39:36 +0100 (Fri, 25 Dec 2009)
New Revision: 6256

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 new condition "Threat level at least".

	* src/manage.c (escalator_data): Declare.
	(escalator_condition_name, escalator_condition_from_name): Add new
	condition.
	(escalator_condition_description): New function.

	* src/manage.h (escalator_condition_t): Add new condition.
	(escalator_condition_description): New header.

	* src/tasks_sql.h (escalate): Use condition description instead of name
	in email body.
	(condition_met): Add escalator arg.  Add check for new condition.  Update
	caller.
	(task_last_report_id, task_second_last_report_id): Correct doc.
	(task_threat_level): New function.

Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog	2009-12-25 16:13:27 UTC (rev 6255)
+++ trunk/openvas-manager/ChangeLog	2009-12-25 19:39:36 UTC (rev 6256)
@@ -1,5 +1,24 @@
 2009-12-25  Matthew Mundell <matthew.mundell at intevation.de>
 
+	Add new condition "Threat level at least".
+
+	* src/manage.c (escalator_data): Declare.
+	(escalator_condition_name, escalator_condition_from_name): Add new
+	condition.
+	(escalator_condition_description): New function.
+
+	* src/manage.h (escalator_condition_t): Add new condition.
+	(escalator_condition_description): New header.
+
+	* src/tasks_sql.h (escalate): Use condition description instead of name
+	in email body.
+	(condition_met): Add escalator arg.  Add check for new condition.  Update
+	caller.
+	(task_last_report_id, task_second_last_report_id): Correct doc.
+	(task_threat_level): New function.
+
+2009-12-25  Matthew Mundell <matthew.mundell at intevation.de>
+
 	* src/tasks_sql.h (email): Add simple implementation.
 	(escalate): Add newline to body.
 

Modified: trunk/openvas-manager/src/manage.c
===================================================================
--- trunk/openvas-manager/src/manage.c	2009-12-25 16:13:27 UTC (rev 6255)
+++ trunk/openvas-manager/src/manage.c	2009-12-25 19:39:36 UTC (rev 6256)
@@ -86,6 +86,18 @@
 void
 dec_task_report_count (task_t task);
 
+/**
+ * @brief Return data associated with an escalator.
+ *
+ * @param[in]  escalator  Escalator.
+ * @param[in]  type       Type of data: "condition", "event" or "method".
+ * @param[in]  name       Name of the data.
+ *
+ * @return Freshly allocated data if it exists, else NULL.
+ */
+static char *
+escalator_data (escalator_t, const char *, const char *);
+
 
 /* Credentials. */
 
@@ -255,8 +267,12 @@
 {
   switch (condition)
     {
-      case ESCALATOR_CONDITION_ALWAYS: return "Always";
-      default:                         return "Internal Error";
+      case ESCALATOR_CONDITION_ALWAYS:
+        return "Always";
+      case ESCALATOR_CONDITION_THREAT_LEVEL_AT_LEAST:
+        return "Threat level at least";
+      default:
+        return "Internal Error";
     }
 }
 
@@ -278,6 +294,36 @@
 }
 
 /**
+ * @brief Get a description of an escalator condition.
+ *
+ * @param[in]  condition  Condition.
+ * @param[in]  escalator  Escalator.
+ *
+ * @return Freshly allocated description of condition.
+ */
+gchar*
+escalator_condition_description (escalator_condition_t condition,
+                                 escalator_t escalator)
+{
+  switch (condition)
+    {
+      case ESCALATOR_CONDITION_ALWAYS:
+        return g_strdup ("Always");
+      case ESCALATOR_CONDITION_THREAT_LEVEL_AT_LEAST:
+        {
+          char *level = escalator_data (escalator, "condition", "level");
+          gchar *ret = g_strdup_printf ("Task threat level is at least '%s'",
+                                        level);
+          free (level);
+          return ret;
+          break;
+        }
+      default:
+        return g_strdup ("Internal Error");
+    }
+}
+
+/**
  * @brief Get a description of an escalator event.
  *
  * @param[in]  event       Event.
@@ -328,6 +374,8 @@
 {
   if (strcasecmp (name, "Always") == 0)
     return ESCALATOR_CONDITION_ALWAYS;
+  if (strcasecmp (name, "Threat level at least") == 0)
+    return ESCALATOR_CONDITION_THREAT_LEVEL_AT_LEAST;
   return ESCALATOR_CONDITION_ERROR;
 }
 

Modified: trunk/openvas-manager/src/manage.h
===================================================================
--- trunk/openvas-manager/src/manage.h	2009-12-25 16:13:27 UTC (rev 6255)
+++ trunk/openvas-manager/src/manage.h	2009-12-25 19:39:36 UTC (rev 6256)
@@ -219,7 +219,8 @@
 typedef enum
 {
   ESCALATOR_CONDITION_ERROR,
-  ESCALATOR_CONDITION_ALWAYS
+  ESCALATOR_CONDITION_ALWAYS,
+  ESCALATOR_CONDITION_THREAT_LEVEL_AT_LEAST
 } escalator_condition_t;
 
 int
@@ -260,6 +261,9 @@
 const char*
 escalator_condition_name (escalator_condition_t);
 
+gchar*
+escalator_condition_description (escalator_condition_t, escalator_t);
+
 const char*
 event_name (event_t);
 

Modified: trunk/openvas-manager/src/tasks_sql.h
===================================================================
--- trunk/openvas-manager/src/tasks_sql.h	2009-12-25 16:13:27 UTC (rev 6255)
+++ trunk/openvas-manager/src/tasks_sql.h	2009-12-25 19:39:36 UTC (rev 6256)
@@ -125,6 +125,9 @@
 int
 family_count ();
 
+const char*
+task_threat_level (task_t);
+
 
 /* Variables. */
 
@@ -2380,10 +2383,12 @@
               name = task_name (task);
               if (notice && strcmp (notice, "0") == 0)
                 {
-                  gchar *event_desc;
+                  gchar *event_desc, *condition_desc;
 
                   /* Summary message. */
                   event_desc = event_description (event, event_data);
+                  condition_desc = escalator_condition_description (condition,
+                                                                    escalator);
                   subject = g_strdup_printf ("[OpenVAS-Manager] Task '%s': %s",
                                              name ? name : "Internal Error",
                                              event_desc);
@@ -2395,8 +2400,9 @@
                                           " task and condition.\n",
                                           name ? name : "Internal Error",
                                           event_desc,
-                                          escalator_condition_name (condition));
+                                          condition_desc);
                   g_free (event_desc);
+                  g_free (condition_desc);
                 }
               else
                 {
@@ -2465,25 +2471,52 @@
 }
 
 /**
- * @brief Return whether a condition is met.
+ * @brief Return whether the condition of an escalator is met by a task.
  *
  * @param[in]  task       Task.
+ * @param[in]  escalator  Escalator.
  * @param[in]  condition  Condition.
  *
  * @return 1 if met, else 0.
  */
 static int
-condition_met (task_t task, escalator_condition_t condition)
+condition_met (task_t task, escalator_t escalator,
+               escalator_condition_t condition)
 {
   switch (condition)
     {
       case ESCALATOR_CONDITION_ALWAYS:
         return 1;
         break;
+      case ESCALATOR_CONDITION_THREAT_LEVEL_AT_LEAST:
+        {
+          char *condition_level;
+          const char *report_level;
+
+          /* True if the threat level of the last finished report is at
+           * least the given level. */
+
+          condition_level = escalator_data (escalator, "condition", "level");
+          report_level = task_threat_level (task);
+          if (condition_level
+              && report_level
+              && (collate_message_type (NULL,
+                                        strlen (report_level),
+                                        report_level,
+                                        strlen (condition_level),
+                                        condition_level)
+                  > -1))
+            {
+              free (condition_level);
+              return 1;
+            }
+          free (condition_level);
+          break;
+        }
       default:
-        return 0;
         break;
     }
+  return 0;
 }
 
 /**
@@ -2507,7 +2540,7 @@
           escalator_condition_t condition;
 
           condition = escalator_iterator_condition (&escalators);
-          if (condition_met (task, condition))
+          if (condition_met (task, escalator, condition))
             escalate (escalator,
                       task,
                       event,
@@ -3562,7 +3595,7 @@
  *
  * @param[in]  task  The task.
  *
- * @return The UUID of the task as a newly allocated string.
+ * @return The UUID of the report as a newly allocated string.
  */
 gchar*
 task_last_report_id (task_t task)
@@ -3580,7 +3613,7 @@
  *
  * @param[in]  task  The task.
  *
- * @return The UUID of the task as a newly allocated string.
+ * @return The UUID of the report as a newly allocated string.
  */
 gchar*
 task_second_last_report_id (task_t task)
@@ -3629,6 +3662,65 @@
 }
 
 /**
+ * @brief Return the threat level of a task.
+ *
+ * @param[in]  task  Task.
+ *
+ * @return Threat level of last report on task if there is one, as a static
+ *         string, else NULL.
+ */
+const char*
+task_threat_level (task_t task)
+{
+  char *type;
+
+  type = sql_string (0, 0,
+                     " SELECT results.type FROM results, report_results"
+                     " WHERE report_results.report ="
+                     " (SELECT ROWID FROM reports WHERE reports.task = %llu"
+                     "  AND reports.scan_run_status = %u"
+                     "  ORDER BY reports.date DESC LIMIT 1)"
+                     " AND results.ROWID = report_results.result"
+                     " ORDER BY type COLLATE collate_message_type DESC"
+                     " LIMIT 1",
+                     task,
+                     TASK_STATUS_DONE);
+
+  if (strcmp (type, "Security Hole") == 0)
+    {
+      free (type);
+      return "High";
+    }
+
+  if (strcmp (type, "Security Warning") == 0)
+    {
+      free (type);
+      return "Medium";
+    }
+
+  if (strcmp (type, "Security Note") == 0)
+    {
+      free (type);
+      return "Low";
+    }
+
+  if (strcmp (type, "Log Message") == 0)
+    {
+      free (type);
+      return "Log";
+    }
+
+  if (strcmp (type, "Debug Message") == 0)
+    {
+      free (type);
+      return "Debug";
+    }
+
+  free (type);
+  return NULL;
+}
+
+/**
  * @brief Generate rcfile in task from config and target.
  *
  * @param[in]  task  The task.



More information about the Openvas-commits mailing list