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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Apr 6 21:25:31 CEST 2012


Author: mattm
Date: 2012-04-06 21:25:31 +0200 (Fri, 06 Apr 2012)
New Revision: 13207

Modified:
   trunk/openvas-manager/ChangeLog
   trunk/openvas-manager/src/manage.h
   trunk/openvas-manager/src/manage_sql.c
   trunk/openvas-manager/src/omp.c
Log:
	Add OMP GET_SETTINGS.

	* src/manage_sql.c (create_tables): Add settings.
	(init_manage): Ensure default settings exist.
	(target_count): Remove dangling SQL clause.
	(SETTING_ITERATOR_EXTRA_COLS): New define.
	(setting_count, init_setting_iterator, setting_iterator_uuid)
	(setting_iterator_name, setting_iterator_comment)
	(setting_iterator_value): New function.

	* src/omp.c (help_text): Add GET_SETTINGS.
	(get_settings_data_t): New type.
	(get_settings_data_reset): New function.
	(command_data_t, client_state_t): Add get_settings.
	(get_settings_data): New variable.
	(xml_handle_start_element, xml_handle_end_element): Add GET_SETTINGS.

	* src/manage.h: Add headers accordingly.
	(setting_t): New type.

Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog	2012-04-06 12:59:45 UTC (rev 13206)
+++ trunk/openvas-manager/ChangeLog	2012-04-06 19:25:31 UTC (rev 13207)
@@ -1,3 +1,25 @@
+2012-04-06  Matthew Mundell <matthew.mundell at greenbone.net>
+
+	Add OMP GET_SETTINGS.
+
+	* src/manage_sql.c (create_tables): Add settings.
+	(init_manage): Ensure default settings exist.
+	(target_count): Remove dangling SQL clause.
+	(SETTING_ITERATOR_EXTRA_COLS): New define.
+	(setting_count, init_setting_iterator, setting_iterator_uuid)
+	(setting_iterator_name, setting_iterator_comment)
+	(setting_iterator_value): New function.
+
+	* src/omp.c (help_text): Add GET_SETTINGS.
+	(get_settings_data_t): New type.
+	(get_settings_data_reset): New function.
+	(command_data_t, client_state_t): Add get_settings.
+	(get_settings_data): New variable.
+	(xml_handle_start_element, xml_handle_end_element): Add GET_SETTINGS.
+
+	* src/manage.h: Add headers accordingly.
+	(setting_t): New type.
+
 2012-04-04  Matthew Mundell <matthew.mundell at greenbone.net>
 
 	Add the option to serve OMP on a second address.

Modified: trunk/openvas-manager/src/manage.h
===================================================================
--- trunk/openvas-manager/src/manage.h	2012-04-06 12:59:45 UTC (rev 13206)
+++ trunk/openvas-manager/src/manage.h	2012-04-06 19:25:31 UTC (rev 13207)
@@ -179,6 +179,7 @@
 typedef long long int port_range_t;
 typedef long long int lsc_credential_t;
 typedef long long int schedule_t;
+typedef long long int setting_t;
 
 #include <sqlite3.h>
 
@@ -2162,6 +2163,25 @@
 /* Settings. */
 
 int
+setting_count (const char *);
+
+void
+init_setting_iterator (iterator_t *, const char *, const char *, int, int, int,
+                       const char *);
+
+const char*
+setting_iterator_uuid (iterator_t*);
+
+const char*
+setting_iterator_name (iterator_t*);
+
+const char*
+setting_iterator_comment (iterator_t*);
+
+const char*
+setting_iterator_value (iterator_t*);
+
+int
 manage_set_setting (const gchar *, const gchar *);
 
 


Modified: trunk/openvas-manager/src/manage_sql.c
===================================================================
--- trunk/openvas-manager/src/manage_sql.c	2012-04-06 12:59:45 UTC (rev 13206)
+++ trunk/openvas-manager/src/manage_sql.c	2012-04-06 19:25:31 UTC (rev 13207)
@@ -1006,6 +1006,7 @@
   sql ("CREATE TABLE IF NOT EXISTS schedules_trash (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment, first_time, period, period_months, duration);");
   sql ("CREATE TABLE IF NOT EXISTS slaves (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment, host, port, login, password);");
   sql ("CREATE TABLE IF NOT EXISTS slaves_trash (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment, host, port, login, password);");
+  sql ("CREATE TABLE IF NOT EXISTS settings (id INTEGER PRIMARY KEY, uuid, owner INTEGER, name, comment, value);");
   /* port_range in the following two is actually a port list.  Migrating a
    * column rename is lots of work. */
   sql ("CREATE TABLE IF NOT EXISTS targets (id INTEGER PRIMARY KEY, uuid UNIQUE, owner INTEGER, name, hosts, comment, lsc_credential INTEGER, ssh_port, smb_lsc_credential INTEGER, port_range);");
@@ -9976,6 +9977,19 @@
     g_free (dir);
   }
 
+  /* Ensure the default settings exist. */
+
+  if (sql_int (0, 0,
+               "SELECT count(*) FROM settings"
+               " WHERE uuid = '5f5a8712-8017-11e1-8556-406186ea4fc5'"
+               " AND owner IS NULL;")
+      == 0)
+    sql ("INSERT into settings (uuid, owner, name, comment, value)"
+         " VALUES"
+         " ('5f5a8712-8017-11e1-8556-406186ea4fc5', NULL, 'Rows Per Page',"
+         "  'The default number of rows displayed in any listing.',"
+         "  10);");
+
   if (nvt_cache_mode == 0)
     {
       iterator_t tasks;
@@ -22991,8 +23005,7 @@
                  "   (SELECT ROWID FROM users"
                  "    WHERE users.uuid = '%s')"
                  "   AND actions & %u = %u))"
-                 "%s%s"
-                 " ORDER BY %s %s;",
+                 "%s%s;",
                  current_credentials.uuid,
                  current_credentials.uuid,
                  actions,
@@ -37127,6 +37140,159 @@
 /* Settings. */
 
 /**
+ * @brief Extra columns for setting iterator.
+ */
+#define SETTING_ITERATOR_EXTRA_COLS \
+ { NULL }
+
+/**
+ * @brief Count number of settings.
+ *
+ * @param[in]  filter           Filter term.
+ *
+ * @return Total number of settings in filtered set.
+ */
+int
+setting_count (const char *filter)
+{
+  static const char *extra_columns[] = SETTING_ITERATOR_EXTRA_COLS;
+  gchar *clause;
+  int ret;
+
+  assert (current_credentials.uuid);
+
+  clause = filter_clause ("setting", filter, extra_columns);
+
+  ret = sql_int (0, 0,
+                 "SELECT count (*)"
+                 " FROM settings"
+                 " WHERE"
+                 " (owner == (SELECT ROWID FROM users WHERE uuid = '%s')"
+                 "  OR (owner IS NULL"
+                 "      AND uuid"
+                 "      NOT IN (SELECT uuid FROM settings"
+                 "              WHERE owner == (SELECT ROWID FROM users"
+                 "                              WHERE uuid = '%s'))))"
+                 "%s%s;",
+                 current_credentials.uuid,
+                 current_credentials.uuid,
+                 clause ? " AND " : "",
+                 clause ? clause : "");
+
+  g_free (clause);
+
+  return ret;
+}
+
+/**
+ * @brief Initialise a setting iterator, including observed settings.
+ *
+ * @param[in]  iterator    Iterator.
+ * @param[in]  uuid        UUID of setting to limit iteration to.  0 for all.
+ * @param[in]  filter      Filter term.
+ * @param[in]  first       First setting.
+ * @param[in]  max         Maximum number of settings returned.
+ * @param[in]  ascending   Whether to sort ascending or descending.
+ * @param[in]  sort_field  Field to sort on, or NULL for "ROWID".
+ */
+void
+init_setting_iterator (iterator_t *iterator, const char *uuid,
+                       const char *filter, int first, int max, int ascending,
+                       const char *sort_field)
+{
+  static const char *extra_columns[] = SETTING_ITERATOR_EXTRA_COLS;
+  gchar *clause, *quoted_uuid;
+
+  assert (current_credentials.uuid);
+
+  if (first < 0)
+    first = 0;
+  if (max < 1)
+    max = -1;
+
+  clause = filter_clause ("setting", filter, extra_columns);
+
+  quoted_uuid = uuid ? sql_quote (uuid) : NULL;
+
+  if (quoted_uuid)
+    init_iterator (iterator,
+                   "SELECT ROWID, uuid, name, comment, value"
+                   " FROM settings"
+                   " WHERE uuid = '%s'"
+                   " AND (owner IS NULL)"
+                   " OR (owner ="
+                   "     (SELECT ROWID FROM users WHERE users.uuid = '%s'))"
+                   /* Force the user's setting to come before the default. */
+                   " ORDER BY owner DESC;",
+                   quoted_uuid,
+                   current_credentials.uuid);
+  else
+    init_iterator (iterator,
+                   "SELECT ROWID, uuid, name, comment, value"
+                   " FROM settings"
+                   " WHERE"
+                   " (owner == (SELECT ROWID FROM users WHERE uuid = '%s')"
+                   "  OR (owner IS NULL"
+                   "      AND uuid"
+                   "      NOT IN (SELECT uuid FROM settings"
+                   "              WHERE owner == (SELECT ROWID FROM users"
+                   "                              WHERE uuid = '%s'))))"
+                   "%s%s"
+                   " ORDER BY %s %s"
+                   " LIMIT %i OFFSET %i;",
+                   current_credentials.uuid,
+                   current_credentials.uuid,
+                   clause ? " AND " : "",
+                   clause ? clause : "",
+                   sort_field ? sort_field : "ROWID",
+                   ascending ? "ASC" : "DESC",
+                   max,
+                   first);
+
+  g_free (clause);
+}
+
+/**
+ * @brief Get the UUID from a setting iterator.
+ *
+ * @param[in]  iterator  Iterator.
+ *
+ * @return The UUID of the setting, or NULL if iteration is complete.  Freed by
+ *         cleanup_iterator.
+ */
+DEF_ACCESS (setting_iterator_uuid, 1);
+
+/**
+ * @brief Get the name from a setting iterator.
+ *
+ * @param[in]  iterator  Iterator.
+ *
+ * @return The name of the setting, or NULL if iteration is complete.  Freed by
+ *         cleanup_iterator.
+ */
+DEF_ACCESS (setting_iterator_name, 2);
+
+/**
+ * @brief Get the comment from a setting iterator.
+ *
+ * @param[in]  iterator  Iterator.
+ *
+ * @return The comment of the setting, or NULL if iteration is complete.  Freed by
+ *         cleanup_iterator.
+ */
+DEF_ACCESS (setting_iterator_comment, 3);
+
+/**
+ * @brief Get the value from a setting iterator.
+ *
+ * @param[in]  iterator  Iterator.
+ *
+ * @return The value of the setting, or NULL if iteration is complete.  Freed by
+ *         cleanup_iterator.
+ */
+DEF_ACCESS (setting_iterator_value, 4);
+
+/**
  * @brief Set the value of a setting.
  *
  * @param[in]  name   Setting name.

Modified: trunk/openvas-manager/src/omp.c
===================================================================
--- trunk/openvas-manager/src/omp.c	2012-04-06 12:59:45 UTC (rev 13206)
+++ trunk/openvas-manager/src/omp.c	2012-04-06 19:25:31 UTC (rev 13207)
@@ -383,6 +383,7 @@
 "    GET_REPORT_FORMATS     Get all report formats.\n"
 "    GET_RESULTS            Get results.\n"
 "    GET_SCHEDULES          Get all schedules.\n"
+"    GET_SETTINGS           Get all settings.\n"
 "    GET_SLAVES             Get all slaves.\n"
 "    GET_SYSTEM_REPORTS     Get all system reports.\n"
 "    GET_TARGET_LOCATORS    Get configured target locators.\n"
@@ -2084,6 +2085,34 @@
 }
 
 /**
+ * @brief Command data.
+ */
+typedef struct
+{
+  char *filter;        ///< Filter term.
+  int first;           ///< Skip over rows before this number.
+  int max;             ///< Maximum number of rows returned.
+  char *sort_field;    ///< Field to sort results on.
+  int sort_order;      ///< Result sort order: 0 descending, else ascending.
+  char *setting_id;    ///< UUID of single setting to get.
+} get_settings_data_t;
+
+/**
+ * @brief Reset command data.
+ *
+ * @param[in]  data  Command data.
+ */
+static void
+get_settings_data_reset (get_settings_data_t *data)
+{
+  free (data->filter);
+  free (data->setting_id);
+  free (data->sort_field);
+
+  memset (data, 0, sizeof (get_settings_data_t));
+}
+
+/**
  * @brief Command data for the get_slaves command.
  */
 typedef struct
@@ -2775,6 +2804,7 @@
   get_report_formats_data_t get_report_formats;       ///< get_report_formats
   get_results_data_t get_results;                     ///< get_results
   get_schedules_data_t get_schedules;                 ///< get_schedules
+  get_settings_data_t get_settings;                   ///< get_settings
   get_slaves_data_t get_slaves;                       ///< get_slaves
   get_system_reports_data_t get_system_reports;       ///< get_system_reports
   get_targets_data_t get_targets;                     ///< get_targets
@@ -3086,6 +3116,12 @@
  = &(command_data.get_schedules);
 
 /**
+ * @brief Parser callback data for GET_SETTINGS.
+ */
+get_settings_data_t *get_settings_data
+ = &(command_data.get_settings);
+
+/**
  * @brief Parser callback data for GET_SLAVES.
  */
 get_slaves_data_t *get_slaves_data
@@ -3520,6 +3556,7 @@
   CLIENT_GET_REPORT_FORMATS,
   CLIENT_GET_RESULTS,
   CLIENT_GET_SCHEDULES,
+  CLIENT_GET_SETTINGS,
   CLIENT_GET_SLAVES,
   CLIENT_GET_SYSTEM_REPORTS,
   CLIENT_GET_TARGET_LOCATORS,
@@ -4832,6 +4869,44 @@
 
             set_client_state (CLIENT_GET_SCHEDULES);
           }
+        else if (strcasecmp ("GET_SETTINGS", element_name) == 0)
+          {
+            const gchar* attribute;
+
+            append_attribute (attribute_names, attribute_values, "setting_id",
+                              &get_settings_data->setting_id);
+
+            append_attribute (attribute_names, attribute_values, "filter",
+                              &get_settings_data->filter);
+
+            if (find_attribute (attribute_names, attribute_values,
+                                "first", &attribute))
+              /* Subtract 1 to switch from 1 to 0 indexing. */
+              get_settings_data->first = atoi (attribute) - 1;
+            else
+              get_settings_data->first = 0;
+            if (get_settings_data->first < 0)
+              get_settings_data->first = 0;
+
+            if (find_attribute (attribute_names, attribute_values,
+                                "max", &attribute))
+              get_settings_data->max = atoi (attribute);
+            else
+              get_settings_data->max = -1;
+            if (get_settings_data->max < 1)
+              get_settings_data->max = -1;
+
+            append_attribute (attribute_names, attribute_values, "sort_field",
+                              &get_settings_data->sort_field);
+
+            if (find_attribute (attribute_names, attribute_values,
+                                "sort_order", &attribute))
+              get_settings_data->sort_order = strcmp (attribute, "descending");
+            else
+              get_settings_data->sort_order = 1;
+
+            set_client_state (CLIENT_GET_SETTINGS);
+          }
         else if (strcasecmp ("GET_SLAVES", element_name) == 0)
           {
             const gchar* attribute;
@@ -14715,6 +14790,66 @@
           break;
         }
 
+      case CLIENT_GET_SETTINGS:
+        {
+          setting_t setting = 0;
+          iterator_t settings;
+          int count, filtered;
+
+          assert (strcasecmp ("GET_SETTINGS", element_name) == 0);
+
+          init_setting_iterator (&settings,
+                                 get_settings_data->setting_id,
+                                 get_settings_data->filter,
+                                 get_settings_data->first,
+                                 get_settings_data->max,
+                                 get_settings_data->sort_order,
+                                 get_settings_data->sort_field);
+
+          SEND_TO_CLIENT_OR_FAIL ("<get_settings_response"
+                                  " status=\"" STATUS_OK "\""
+                                  " status_text=\"" STATUS_OK_TEXT "\">");
+          SENDF_TO_CLIENT_OR_FAIL ("<filters>"
+                                   "<term>%s</term>"
+                                   "</filters>"
+                                   "<settings start=\"%i\" max=\"%i\"/>",
+                                   get_settings_data->filter
+                                    ? get_settings_data->filter
+                                    : "",
+                                   /* Add 1 for 1 indexing. */
+                                   get_settings_data->first + 1,
+                                   get_settings_data->max);
+          count = 0;
+          while (next (&settings))
+            {
+              SENDF_TO_CLIENT_OR_FAIL ("<setting id=\"%s\">"
+                                       "<name>%s</name>"
+                                       "<comment>%s</comment>"
+                                       "<value>%s</value>"
+                                       "</setting>",
+                                       setting_iterator_uuid (&settings),
+                                       setting_iterator_name (&settings),
+                                       setting_iterator_comment (&settings),
+                                       setting_iterator_value (&settings));
+              count++;
+            }
+          filtered = setting
+                      ? 1
+                      : setting_count (get_settings_data->filter);
+          SENDF_TO_CLIENT_OR_FAIL ("<setting_count>"
+                                   "<filtered>%i</filtered>"
+                                   "<page>%i</page>"
+                                   "</setting_count>",
+                                   filtered,
+                                   count);
+          cleanup_iterator (&settings);
+          SEND_TO_CLIENT_OR_FAIL ("</get_settings_response>");
+
+          get_settings_data_reset (get_settings_data);
+          set_client_state (CLIENT_AUTHENTIC);
+          break;
+        }
+
       case CLIENT_GET_SLAVES:
         {
           slave_t slave = 0;



More information about the Openvas-commits mailing list