[Openvas-commits] r12224 - in trunk/gsa: . src

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Nov 30 15:26:23 CET 2011


Author: mattm
Date: 2011-11-30 15:26:22 +0100 (Wed, 30 Nov 2011)
New Revision: 12224

Modified:
   trunk/gsa/ChangeLog
   trunk/gsa/src/validator.c
   trunk/gsa/src/validator.h
Log:
	In the validator, also store the rule name for aliases.

	* src/validator.c (openvas_validator_rule_new)
	(openvas_validator_rule_free, openvas_validator_alias_for): New functions.
	(openvas_validator_new, openvas_validator_add, openvas_validator_alias)
	(openvas_validate): Use validator_rule_t instead of regexp as hashtable
	value.

	* src/validator.h: Add header accordingly.
	(struct validator_rule): New structure.
	(validator_rule_t): New type.

Modified: trunk/gsa/ChangeLog
===================================================================
--- trunk/gsa/ChangeLog	2011-11-30 12:43:41 UTC (rev 12223)
+++ trunk/gsa/ChangeLog	2011-11-30 14:26:22 UTC (rev 12224)
@@ -1,5 +1,19 @@
 2011-11-30  Matthew Mundell <matthew.mundell at greenbone.net>
 
+	In the validator, also store the rule name for aliases.
+
+	* src/validator.c (openvas_validator_rule_new)
+	(openvas_validator_rule_free, openvas_validator_alias_for): New functions.
+	(openvas_validator_new, openvas_validator_add, openvas_validator_alias)
+	(openvas_validate): Use validator_rule_t instead of regexp as hashtable
+	value.
+
+	* src/validator.h: Add header accordingly.
+	(struct validator_rule): New structure.
+	(validator_rule_t): New type.
+
+2011-11-30  Matthew Mundell <matthew.mundell at greenbone.net>
+
 	* src/gsad.c (exec_omp_post): Allow empty passwords.  If login is empty
 	go to the login page with fail message, instead of an internal error page.
 

Modified: trunk/gsa/src/validator.c
===================================================================
--- trunk/gsa/src/validator.c	2011-11-30 12:43:41 UTC (rev 12223)
+++ trunk/gsa/src/validator.c	2011-11-30 14:26:22 UTC (rev 12224)
@@ -24,6 +24,8 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include <assert.h>
+
 #include "tracef.h"
 #include "validator.h"
 
@@ -46,6 +48,39 @@
  */
 
 /**
+ * @brief Create a new validator rule.
+ *
+ * The validator must be freed with \ref validator_rule_free.
+ *
+ * @return A newly allocated validator.
+ */
+validator_rule_t *
+openvas_validator_rule_new (const char *regex)
+{
+  validator_rule_t *rule;
+  rule = g_malloc (sizeof (validator_rule_t));
+  rule->regex = g_strdup (regex);
+  rule->alias_for = NULL;
+  return rule;
+}
+
+
+/**
+ * @brief Free a validator rule.
+ *
+ * @param  rule  Validator rule.
+ */
+void
+openvas_validator_rule_free (validator_rule_t *rule)
+{
+  if (rule)
+    {
+      g_free (rule->alias_for);
+      g_free (rule->regex);
+    }
+}
+
+/**
  * @brief Create a new validator.
  *
  * The validator must be freed with \ref openvas_validator_free.
@@ -58,7 +93,7 @@
   return g_hash_table_new_full (g_str_hash,
                                 g_str_equal,
                                 g_free,
-                                g_free);
+                                (void (*) (gpointer)) openvas_validator_rule_free);
 }
 
 /**
@@ -75,7 +110,7 @@
 {
   g_hash_table_insert (validator,
                        (gpointer) g_strdup (name),
-                       (gpointer) g_strdup (regex));
+                       (gpointer) openvas_validator_rule_new (regex));
 }
 
 /**
@@ -92,19 +127,49 @@
                          const char *alias,
                          const char *name)
 {
-  gpointer key, regex;
+  gpointer key, value_rule;
 
-  if (g_hash_table_lookup_extended (validator, name, &key, &regex))
+  if (g_hash_table_lookup_extended (validator, name, &key, &value_rule))
     {
+      validator_rule_t *alias_rule, *rule;
+      rule = (validator_rule_t*) value_rule;
+      alias_rule = openvas_validator_rule_new (rule->regex
+                                                ? g_strdup (rule->regex)
+                                                : NULL);
+      alias_rule->alias_for = g_strdup (name);
       g_hash_table_insert (validator,
                            (gpointer) g_strdup (alias),
-                           (gpointer) (regex ? g_strdup (regex) : NULL));
+                           (gpointer) alias_rule);
       return 0;
     }
   return -1;
 }
 
 /**
+ * @brief Get the name of the rule for which a rule is an alias.
+ *
+ * @param  validator  Validator.
+ * @param  alias      Name of alias.
+ *
+ * @return Rule name if \p alias is an alias, else NULL.  Freed by
+ *         openvas_validator_free.
+ */
+gchar *
+openvas_validator_alias_for (validator_t validator, const char *alias)
+{
+  gpointer key, value_rule;
+
+  if (g_hash_table_lookup_extended (validator, alias, &key, &value_rule))
+    {
+      validator_rule_t *rule;
+      assert (value_rule);
+      rule = (validator_rule_t*) value_rule;
+      return rule->alias_for;
+    }
+  return NULL;
+}
+
+/**
  * @brief Validate a string for a given rule.
  *
  * @param  validator  Validator to validate from.
@@ -117,13 +182,19 @@
 int
 openvas_validate (validator_t validator, const char *name, const char *value)
 {
-  gpointer key, regex;
+  gpointer key, value_rule;
 
   tracef ("%s: name %s value %s", __FUNCTION__, name, value);
 
-  if (g_hash_table_lookup_extended (validator, name, &key, &regex))
+  if (g_hash_table_lookup_extended (validator, name, &key, &value_rule))
     {
-      if (regex == NULL)
+      validator_rule_t *rule;
+
+      assert (value_rule);
+
+      rule = (validator_rule_t*) value_rule;
+
+      if (rule->regex == NULL)
         {
           if (value == NULL)
             {
@@ -140,8 +211,8 @@
           return 2;
         }
 
-      tracef ("matching <%s> against <%s>: ", (char *) regex, value);
-      if (g_regex_match_simple ((const gchar *) regex,
+      tracef ("matching <%s> against <%s>: ", (char *) rule->regex, value);
+      if (g_regex_match_simple (rule->regex,
                                 (const gchar *) value,
                                 0,
                                 0))

Modified: trunk/gsa/src/validator.h
===================================================================
--- trunk/gsa/src/validator.h	2011-11-30 12:43:41 UTC (rev 12223)
+++ trunk/gsa/src/validator.h	2011-11-30 14:26:22 UTC (rev 12224)
@@ -35,13 +35,28 @@
 #include <glib.h>
 
 /**
- * @brief A set of name validator pairs.
+ * @brief A set of name rule pairs.
  */
 typedef GHashTable *validator_t;
 
+/**
+ * @brief A validator rule.
+ */
+struct validator_rule
+{
+  gchar *alias_for;   ///< Name of the rule for which this is an alias.
+  gchar *regex;       ///< Regular expression.
+};
+
+/**
+ * @brief A validator rule.
+ */
+typedef struct validator_rule validator_rule_t;
+
 validator_t openvas_validator_new ();
 void openvas_validator_add (validator_t, const char *, const char *);
 int openvas_validator_alias (validator_t, const char *, const char *);
+gchar *openvas_validator_alias_for (validator_t, const char *);
 int openvas_validate (validator_t, const char *, const char *);
 void openvas_validator_free (validator_t);
 



More information about the Openvas-commits mailing list