[Openvas-commits] r2699 - in trunk/openvas-client: . src/util

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Mar 6 22:20:14 CET 2009


Author: jan
Date: 2009-03-06 22:20:12 +0100 (Fri, 06 Mar 2009)
New Revision: 2699

Modified:
   trunk/openvas-client/ChangeLog
   trunk/openvas-client/src/util/priority_filter.c
Log:
* src/util/priority_filter.c (priority_filter_xml_elem,
priority_override_xml_elem, priority_filter_xml_start_element,
priority_override_xml_reason_text): New.
Handling for parsing xml file. Main author is Felix Wolfsteller.
(priority_filter_from_xml): Added implementation. Main author
is Felix again.



Modified: trunk/openvas-client/ChangeLog
===================================================================
--- trunk/openvas-client/ChangeLog	2009-03-06 15:46:37 UTC (rev 2698)
+++ trunk/openvas-client/ChangeLog	2009-03-06 21:20:12 UTC (rev 2699)
@@ -1,5 +1,14 @@
 2009-03-06  Jan-Oliver Wagner <jan-oliver.wagner at intevation.de>
 
+	* src/util/priority_filter.c (priority_filter_xml_elem,
+	priority_override_xml_elem, priority_filter_xml_start_element,
+	priority_override_xml_reason_text): New.
+	Handling for parsing xml file. Main author is Felix Wolfsteller.
+	(priority_filter_from_xml): Added implementation. Main author
+	is Felix again.
+
+2009-03-06  Jan-Oliver Wagner <jan-oliver.wagner at intevation.de>
+
 	* src/util/priority_filter.c: Added file doc string and various macros for
 	XML keywords.
 	(priority_override_new): Added parameter for directly setting the active

Modified: trunk/openvas-client/src/util/priority_filter.c
===================================================================
--- trunk/openvas-client/src/util/priority_filter.c	2009-03-06 15:46:37 UTC (rev 2698)
+++ trunk/openvas-client/src/util/priority_filter.c	2009-03-06 21:20:12 UTC (rev 2699)
@@ -72,6 +72,7 @@
 */
 
 #include "priority_filter.h"
+#include "nessus_i18n.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -294,6 +295,165 @@
 
 
 /**
+ * @brief Creates a priority_filter from lists of (xml) attribute names and
+ * @brief values.
+ * 
+ * A line in an xml file describing a filter looks like:
+ * @verbatim <priority_filter name="My Priority Filter"> @endverbatim
+ * Thus, only one attribute- value pair is needed (\"name\"), others will be
+ * ignored.
+ * 
+ * @param attr_name   Attribute names, e.g. {"name", NULL}.
+ * @param attr_values Attribute values, e.g. {"My Name", NULL}.
+ * 
+ * @return A fresh priority_filter_t the name attributes and value were found
+ *         in the parameter lists.
+ */
+static const priority_filter_t*
+priority_filter_xml_elem (const gchar** attr_names, const gchar** attr_values)
+{
+  const priority_filter_t* filter = NULL;
+  while (*attr_names != NULL) {
+    if (!strcmp (*attr_names, XML_ATTR_NAME) && *attr_values != NULL)
+      filter = priority_filter_new (*attr_values);
+    ++attr_values;
+    ++attr_names;
+  }
+
+  return filter;
+}
+
+
+/**
+ * @brief Creates a override from lists of (xml) attribute names and values.
+ * 
+ * The reason will be the empty string, the source xml should look like:
+ * @verbatim
+ * <priority_override name="PriorityOverride2" host="192.168.11.35" port="general/tcp" OID="1.3.6.1.4.1.25623.1.0.900505"
+      prio_from="NOTE" prio_to="FALSE" active="true"> @endverbatim
+ * If any attribute is missing, NULL will be returned.
+ * 
+ * @return A fresh priority_override_t if all attributes and values were found
+ *         in the parameter lists.
+ */
+static const priority_override_t*
+priority_override_xml_elem (const gchar** attr_names, const gchar** attr_values)
+{
+  const priority_override_t* override = NULL;
+  const gchar *name = NULL,
+              *host = NULL,
+              *port = NULL,
+              *oid  = NULL,
+              *prio_from = NULL,
+              *prio_to = NULL;
+  gboolean active = FALSE;
+
+  while (*attr_names != NULL) {
+    if (!strcmp (*attr_names, XML_ATTR_NAME) && *attr_values != NULL)
+      name = *attr_values;
+    else if (!strcmp (*attr_names, XML_ATTR_HOST) && *attr_values != NULL)
+      host = *attr_values;
+    else if (!strcmp (*attr_names, XML_ATTR_PORT) && *attr_values != NULL)
+      port = *attr_values;
+    else if (!strcmp (*attr_names, XML_ATTR_OID) && *attr_values != NULL)
+      oid = *attr_values;
+    else if (!strcmp (*attr_names, XML_ATTR_PRIO_FROM) && *attr_values != NULL)
+      prio_from = *attr_values;
+    else if (!strcmp (*attr_names, XML_ATTR_PRIO_TO) && *attr_values != NULL)
+      prio_to = *attr_values;
+    else if (!strcmp (*attr_names, XML_ATTR_ACTIVE) && *attr_values != NULL)
+      active = (!strcmp (*attr_values, XML_ATTR_ACTIVE_TRUE)) ? TRUE : FALSE;
+
+    ++attr_values;
+    ++attr_names;
+  }
+
+  override = priority_override_new (name, host, port, oid, "", prio_from,
+                                    prio_to, active);
+
+  return override;
+}
+
+
+/**
+ * @brief Used as an GMarkupParsers start_element function.
+ * 
+ * We are interested in filter and override elements, as they contain
+ * the important attributes.
+ * 
+ * When parsing went fine, (*user_data) will point to a priority_filter
+ * that contains all the priority_overrides defined in an xml file.
+ * 
+ * @param user_data[in,out] Double-pointer to a priority_filter_t.
+ */
+static void
+priority_filter_xml_start_element (GMarkupParseContext *context,
+                                   const gchar *element_name,
+                                   const gchar **attribute_names,
+                                   const gchar **attribute_values,
+                                   gpointer user_data, GError **error)
+{
+  priority_filter_t** filter = user_data;
+
+  /* Its a: <priority_filter name="My priority Filter"> */
+  if (!strcmp (element_name, XML_ELEM_PRIORITY_FILTER)) {
+    if (*filter != NULL)
+      printf (_("XML parser error: second filter specified in file\n"));
+
+    *filter = priority_filter_xml_elem (attribute_names, attribute_values);
+
+    if (*filter == NULL)
+      printf (_("XML parser error: error parsing filter element\n"));
+  }
+  /* Its a: <priority_override name="PriorityOverride2" host="192.168.11.35"
+                  port="general/tcp" OID="1.3.6.1.4.1.25623.1.0.900505"
+                  prio_from="NOTE" prio_to="FALSE" active="true"> ...  */
+  else if (!strcmp (element_name, XML_ELEM_PRIORITY_OVERRIDE))
+  {
+    if (*filter != NULL) {
+      const priority_override_t* override =
+        priority_override_xml_elem (attribute_names, attribute_values);
+      if (override != NULL)
+        priority_filter_add (*filter, override);
+      else printf (_("XML Parser Error: override parsing error\n"));
+    } else
+      printf (_("XML Parser Error: override without filter\n"));
+  }
+}
+
+
+/**
+ * @brief Used as a GMarkupParser text- function.
+ * 
+ * We are just interested in the text between the reason elements, ignore
+ * everything else.
+ * The text between reason start/end elements will be set as reason for the
+ * last added override in the filter that is parsed.
+ * 
+ * @param [in,out] user_data The priority_filter that is currently parsed.
+ */
+static void
+priority_override_xml_reason_text (GMarkupParseContext *context,
+                                   const gchar *text,
+                                   gsize text_len, gpointer user_data,
+                                   GError **error)
+{
+  priority_filter_t** filter = user_data;
+  if (!strcmp (g_markup_parse_context_get_element (context), XML_ELEM_REASON)
+      && filter != NULL && *filter != NULL
+      && (*filter)->overrides != NULL
+      && (*filter)->overrides->data != NULL)
+  {
+    priority_override_t* override = (*filter)->overrides->data;
+    g_free (override->reason);
+    override->reason = g_strdup (text);
+  }
+  // Else either not in a reason element or no override to set reason for.
+  // Later case should be handled as an error.
+}
+
+
+/**
  * @brief Imports a priority_filter from an xml file that has been written by
  * @brief priority_filter_to_xml.
  * 
@@ -307,5 +467,41 @@
 const priority_filter_t *
 priority_filter_from_xml (const gchar * filename)
 {
-  return NULL;
+  GMarkupParser parser;
+  GMarkupParseContext *context = NULL;
+  gchar *filebuffer = NULL;
+  gsize length = 0;
+  priority_filter_t** filter = malloc (sizeof (void*));
+  *filter = NULL;
+
+  if (!g_file_get_contents (filename, &filebuffer, &length, NULL))
+    return NULL;
+
+  // Init XML subset parser
+  parser.start_element = priority_filter_xml_start_element;
+  parser.end_element   = NULL;
+  parser.text          = priority_override_xml_reason_text;
+  parser.passthrough   = NULL;
+  parser.error         = NULL;
+
+  // Setup context and parse it
+  context = g_markup_parse_context_new (&parser, 0, filter, NULL);
+  if (g_markup_parse_context_parse (context, filebuffer, length, NULL) == TRUE)
+  {
+#ifdef DEBUG
+    if (*filter) {
+      printf ("Parsing of priorities- XML (%s) done.\n", (*filter)->name);
+      if ((*filter)->overrides) {
+        priority_override_t* override = (*filter)->overrides->data;
+        printf ("\tFirst override: %s\n", override->name);
+        printf ("\treason %s\n", override->reason);
+      }
+    } else
+      printf ("Parsing priorities- XML succeeded but no valid filter found.\n");
+#endif
+  } else printf(_("XML Parser error: Parsing failed"));
+
+  g_free (filebuffer);
+  g_markup_parse_context_free (context);
+  return *filter;
 }



More information about the Openvas-commits mailing list