[Openvas-commits] r3552 - in trunk/openvas-libraries: . libopenvascommon

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun May 31 00:22:33 CEST 2009


Author: jan
Date: 2009-05-31 00:22:33 +0200 (Sun, 31 May 2009)
New Revision: 3552

Modified:
   trunk/openvas-libraries/ChangeLog
   trunk/openvas-libraries/libopenvascommon/nvti.c
   trunk/openvas-libraries/libopenvascommon/nvti.h
Log:
* libopenvascommon/nvti.h: Added data structure nvtpref_t and
associated functionprotos for handling NVT preferences.
(nvti_t): Extended to handle its preferences.

* libopenvascommon/nvti.c (nvtpref_new, nvtpref_free,
nvtpref_name, nvtpref_type, nvtpref_default): New. For
handling nvtpref objects.
(nvti_free, nvti_from_keyfile, nvti_to_keyfile): Handle
preferences accordingly.
(nvti_pref_len, nvti_pref, nvti_add_pref,
nvtpref_add_to_keyfile): New.
(nvti_to_keyfile): Fix bug: release text only when it is
really allocated.



Modified: trunk/openvas-libraries/ChangeLog
===================================================================
--- trunk/openvas-libraries/ChangeLog	2009-05-29 22:54:00 UTC (rev 3551)
+++ trunk/openvas-libraries/ChangeLog	2009-05-30 22:22:33 UTC (rev 3552)
@@ -1,3 +1,19 @@
+2009-05-31  Jan-Oliver Wagner <jan-oliver.wagner at greenbone.net>
+
+	* libopenvascommon/nvti.h: Added data structure nvtpref_t and
+	associated functionprotos for handling NVT preferences.
+	(nvti_t): Extended to handle its preferences.
+
+	* libopenvascommon/nvti.c (nvtpref_new, nvtpref_free,
+	nvtpref_name, nvtpref_type, nvtpref_default): New. For
+	handling nvtpref objects.
+	(nvti_free, nvti_from_keyfile, nvti_to_keyfile): Handle
+	preferences accordingly.
+	(nvti_pref_len, nvti_pref, nvti_add_pref,
+	nvtpref_add_to_keyfile): New.
+	(nvti_to_keyfile): Fix bug: release text only when it is
+	really allocated.
+
 2009-05-29  Matthew Mundell <mmundell at intevation.de>
 
 	Add certificate facilities.

Modified: trunk/openvas-libraries/libopenvascommon/nvti.c
===================================================================
--- trunk/openvas-libraries/libopenvascommon/nvti.c	2009-05-29 22:54:00 UTC (rev 3551)
+++ trunk/openvas-libraries/libopenvascommon/nvti.c	2009-05-30 22:22:33 UTC (rev 3552)
@@ -40,6 +40,89 @@
 #include "nvti.h"
 
 /**
+ * @brief Create a new nvtpref structure filled with the given values.
+ *
+ * @param name The name to be set. A copy will created of this.
+ *
+ * @param type The type to be set. A copy will created of this.
+ *
+ * @param dflt The default to be set. A copy will created of this.
+ *
+ * @return NULL in case the memory could not be allocated.
+ *         Else a nvtpref structure which needs to be
+ *         released using @ref nvtpref_free .
+ */
+nvtpref_t *
+nvtpref_new (gchar * name, gchar * type, gchar * dflt)
+{
+  nvtpref_t * np = g_malloc0 (sizeof (nvtpref_t));
+
+  if (! np) return NULL;
+
+  if (name) np->name = g_strdup (name);
+  if (type) np->type = g_strdup (type);
+  if (dflt) np->dflt = g_strdup (dflt);
+
+  return (np);
+}
+
+/**
+ * @brief Free memory of a nvtpref structure.
+ *
+ * @param n The structure to be freed.
+ */
+void
+nvtpref_free (nvtpref_t * np)
+{
+  if (np->name) g_free (np->name);
+  if (np->type) g_free (np->type);
+  if (np->dflt) g_free (np->dflt);
+  g_free (np);
+}
+
+/**
+ * @brief Get the Name of a NVT Preference.
+ *
+ * @param np The NVT Pref structure of which the Name should
+ *           be returned.
+ *
+ * @return The name string. Don't free this.
+ */
+gchar *
+nvtpref_name (const nvtpref_t * np)
+{
+  return (np->name);
+}
+
+/**
+ * @brief Get the Type of a NVT Preference.
+ *
+ * @param np The NVT Pref structure of which the Type should
+ *           be returned.
+ *
+ * @return The type string. Don't free this.
+ */
+gchar *
+nvtpref_type (const nvtpref_t * np)
+{
+  return (np->type);
+}
+
+/**
+ * @brief Get the Default of a NVT Preference.
+ *
+ * @param np The NVT Pref structure of which the Default should
+ *           be returned.
+ *
+ * @return The default string. Don't free this.
+ */
+gchar *
+nvtpref_default (const nvtpref_t * np)
+{
+  return (np->dflt);
+}
+
+/**
  * @brief Create a new (empty) nvti structure.
  *
  * @return NULL in case the memory could not be allocated.
@@ -79,6 +162,13 @@
   if (n->sign_key_ids) g_free (n->sign_key_ids);
   if (n->family) g_free (n->family);
   if (n->src) g_free (n->src);
+  if (n->prefs) {
+    guint len = g_slist_length(n->prefs);
+    int i;
+    for (i = 0;i < len;i ++)
+      nvtpref_free(g_slist_nth_data(n->prefs, i));
+    g_slist_free(n->prefs);
+  }
   g_free (n);
 }
 
@@ -321,6 +411,34 @@
 }
 
 /**
+ * @brief Get the number of preferences of the NVT.
+ *
+ * @param n The NVT Info structure.
+ *
+ * @return The number of preferences.
+ */
+guint
+nvti_pref_len (const nvti_t * n)
+{
+  return(g_slist_length(n->prefs));
+}
+
+/**
+ * @brief Get the n'th preferences of the NVT.
+ *
+ * @param n The NVT Info structure.
+ *
+ * @param p The position of the preference to return.
+ *
+ * @return The number of preferences. NULL if 
+ */
+nvtpref_t *
+nvti_pref (const nvti_t * n, guint p)
+{
+  return(g_slist_nth_data(n->prefs, p));
+}
+
+/**
  * @brief Get the source URI of a NVT Info.
  *
  * @param n The NVT Info structure of which the source URI should
@@ -745,6 +863,22 @@
 }
 
 /**
+ * @brief Add a preference to the NVT Info.
+ *
+ * @param n The NVT Info structure.
+ *
+ * @param np The NVT preference to add.
+ *
+ * @return 0 for success. Anything else indicates an error.
+ */
+int
+nvti_add_pref (nvti_t * n, nvtpref_t * np)
+{
+  n->prefs = g_slist_append(n->prefs, np);
+  return (0);
+}
+
+/**
  * @brief Create a human readable text representation of a NVT Info.
  *        This is mainly for debug purposes.
  *
@@ -809,6 +943,8 @@
   GKeyFile *keyfile = g_key_file_new ();
   nvti_t *n;
   GError *error = NULL;
+  gchar **keys;
+  int i;
 
   if (!g_key_file_load_from_file (keyfile, fn, G_KEY_FILE_NONE, &error))
     {
@@ -838,12 +974,45 @@
   nvti_set_timeout (n, g_key_file_get_integer (keyfile, "NVT Info", "Timeout", NULL));
   nvti_set_category (n, g_key_file_get_integer (keyfile, "NVT Info", "Category", NULL));
 
+  if (g_key_file_has_group(keyfile, "NVT Prefs")) {
+    keys = g_key_file_get_keys(keyfile, "NVT Prefs", NULL, NULL);
+    for (i = 0;keys[i];i ++) {
+      gsize len;
+      gchar ** items = g_key_file_get_string_list(keyfile, "NVT Prefs", keys[i], &len, NULL);
+      if (len != 3) continue; // format error for this pref.
+      nvtpref_t *np = nvtpref_new(items[0], items[1], items[2]);
+      nvti_add_pref(n, np);
+      g_strfreev(items);
+    }
+    g_strfreev(keys);
+  }
+
   g_key_file_free (keyfile);
 
   return (n);
 }
 
 /**
+ * @brief Callback function for adding a nvtpref to a keyfile.
+ *
+ * @param key The key of the pref.
+ *
+ * @param np The nvtipref object.
+ *
+ * @param keyfile The keyfile where the pref data are added.
+ */
+static void
+nvtpref_add_to_keyfile(gpointer key, gpointer np, gpointer keyfile )
+{
+  gchar * lst[3];
+  lst[0] = ((nvtpref_t *)np)->name;
+  lst[1] = ((nvtpref_t *)np)->type;
+  lst[2] = ((nvtpref_t *)np)->dflt;
+
+  g_key_file_set_string_list((GKeyFile *)keyfile, "NVT Prefs", (gchar *)key, (const gchar **)lst, 3);
+}
+
+/**
  * @brief Store NVT Info into a keyfile.
  *
  * @param n The NVT Info object to store.
@@ -900,6 +1069,17 @@
   if (n->category > 0)
     g_key_file_set_integer (keyfile, "NVT Info", "Category", n->category);
 
+  int i;
+  for (i=0;i < nvti_pref_len(n);i ++) {
+    nvtpref_t * np = nvti_pref(n, i);
+    gchar * lst[3];
+    lst[0] = ((nvtpref_t *)np)->name;
+    lst[1] = ((nvtpref_t *)np)->type;
+    lst[2] = ((nvtpref_t *)np)->dflt;
+
+    g_key_file_set_string_list((GKeyFile *)keyfile, "NVT Prefs", (gchar *)lst[0], (const gchar **)lst, 3);
+  }
+
   text = g_key_file_to_data (keyfile, NULL, &error);
   if (error != NULL)
     {
@@ -912,10 +1092,10 @@
       FILE *fp = fopen (fn, "w");
       fprintf (fp, text);
       fclose (fp);
+      g_free(text);
     }
 
   g_key_file_free (keyfile);
-  g_free (text);
 
   return (0);
 }

Modified: trunk/openvas-libraries/libopenvascommon/nvti.h
===================================================================
--- trunk/openvas-libraries/libopenvascommon/nvti.h	2009-05-29 22:54:00 UTC (rev 3551)
+++ trunk/openvas-libraries/libopenvascommon/nvti.h	2009-05-30 22:22:33 UTC (rev 3552)
@@ -37,6 +37,25 @@
 #include <glib.h>
 
 /**
+ * @brief The structure for a preference of a NVT.
+ *
+ * The elements of this structure should never be accessed directly.
+ * Only the functions corresponding to this module should be used.
+ */
+typedef struct nvtpref
+{
+  gchar * type;  // preference type
+  gchar * name;  // name of the preference
+  gchar * dflt; // default value of the preference
+} nvtpref_t;
+
+nvtpref_t *nvtpref_new (gchar *, gchar *, gchar *);
+void nvtpref_free (nvtpref_t *);
+gchar * nvtpref_name(const nvtpref_t *);
+gchar * nvtpref_type(const nvtpref_t *);
+gchar * nvtpref_default(const nvtpref_t *);
+
+/**
  * @brief The structure of a information record that corresponds to a NVT.
  *
  * The elements of this structure should never be accessed directly.
@@ -66,6 +85,8 @@
 
   gchar *src;                   // the source of the corresponding script, can be filename or other URI
 
+  GSList * prefs;           // Collection of NVT preferences
+
   // The following are not settled yet.
   gint timeout;                 // default timeout time for this NVT
   gint category;                // The category, this NVT belongs to.
@@ -95,6 +116,8 @@
 gint nvti_timeout (const nvti_t *);
 gint nvti_category (const nvti_t *);
 gchar *nvti_family (const nvti_t *);
+guint nvti_pref_len (const nvti_t *);
+nvtpref_t * nvti_pref (const nvti_t *, guint);
 
 int nvti_set_oid (nvti_t *, const gchar *);
 int nvti_set_version (nvti_t *, const gchar *);
@@ -116,6 +139,7 @@
 int nvti_set_timeout (nvti_t *, const gint);
 int nvti_set_category (nvti_t *, const gint);
 int nvti_set_family (nvti_t *, const gchar *);
+int nvti_add_pref (nvti_t *, nvtpref_t *);
 
 gchar *nvti_as_text (const nvti_t *);
 gchar *nvti_as_openvas_nvt_cache_entry (const nvti_t *);



More information about the Openvas-commits mailing list