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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sat Mar 21 15:58:50 CET 2009


Author: mattm
Date: 2009-03-21 15:58:48 +0100 (Sat, 21 Mar 2009)
New Revision: 2867

Modified:
   trunk/openvas-manager/ChangeLog
   trunk/openvas-manager/src/CMakeLists.txt
   trunk/openvas-manager/src/manage.c
   trunk/openvas-manager/src/manage.h
   trunk/openvas-manager/src/omp.c
   trunk/openvas-manager/src/otp.c
Log:
Switch to universal report identifiers.

Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog	2009-03-21 09:04:15 UTC (rev 2866)
+++ trunk/openvas-manager/ChangeLog	2009-03-21 14:58:48 UTC (rev 2867)
@@ -1,3 +1,22 @@
+2009-03-21  Matthew Mundell <matt at mundell.ukfsn.org>
+
+	Switch to universal report identifiers.
+
+	* src/manage.h (make_report_id, report_path_task_name, report_task): New
+	headers.
+
+	* src/manage.c (make_report_id, report_path_task_name, report_task): New
+	functions.
+	(delete_report, set_report_parameter): Switch to UUIDs.
+
+	* src/omp.c (send_reports): Switch to UUIDs.  Free all names on	send fail.
+	(omp_xml_handle_end_element): Switch to UUIDs for reports.  Correct
+	response tag in CLIENT_GET_REPORT.
+
+	* src/otp.c (save_report): Switch to UUIDs.
+
+	* src/CMakeLists.txt: Add -lossp-uuid to openvasmd.
+
 2009-03-20  Matthew Mundell <matt at mundell.ukfsn.org>
 
 	Add a task status enumeration.  Rename task_t running slot to run_status.

Modified: trunk/openvas-manager/src/CMakeLists.txt
===================================================================
--- trunk/openvas-manager/src/CMakeLists.txt	2009-03-21 09:04:15 UTC (rev 2866)
+++ trunk/openvas-manager/src/CMakeLists.txt	2009-03-21 14:58:48 UTC (rev 2867)
@@ -98,7 +98,7 @@
 endif (OPENVAS_LIB_INSTALL_DIR)
 
 set_target_properties (openvasmd PROPERTIES LINK_FLAGS
-                       "${TEMP} -lpcap -unessus_get_socket_from_connection -lopenvas -lgnutls ${GLIB_LDFLAGS}")
+                       "${TEMP} -lpcap -unessus_get_socket_from_connection -lopenvas -lgnutls -lossp-uuid ${GLIB_LDFLAGS}")
 
 if (OPENVAS_HEADER_INSTALL_DIR)
   set (TEMP "-I${OPENVAS_HEADER_INSTALL_DIR}")

Modified: trunk/openvas-manager/src/manage.c
===================================================================
--- trunk/openvas-manager/src/manage.c	2009-03-21 09:04:15 UTC (rev 2866)
+++ trunk/openvas-manager/src/manage.c	2009-03-21 14:58:48 UTC (rev 2867)
@@ -48,6 +48,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <dirent.h>
+#include <ossp/uuid.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -134,74 +135,171 @@
 /* Reports. */
 
 /**
+ * @brief Make a new universal identifier for a report.
+ *
+ * @return A newly allocated string holding the identifier, which the
+ *         caller must free.
+ */
+char*
+make_report_id ()
+{
+  uuid_t *uuid;
+
+  // FIX check errs
+  uuid_create (&uuid);
+  uuid_make (uuid, UUID_MAKE_V1);
+  char* id = NULL;
+  uuid_export (uuid, UUID_FMT_STR, (void**) &id, NULL);
+  uuid_destroy (uuid);
+  return id;
+}
+
+/**
+ * @brief Get the name of the task from the pathname of a report.
+ *
+ * @param[in]  path  Absolute path of report in task directory.
+ *
+ * @return The name of the task as a newly allocated string, which the
+ *         caller must free.
+ */
+gchar*
+report_path_task_name (gchar* path)
+{
+#if 0
+  gchar* task_dir = g_build_filename (path, "..", "..", NULL);
+  // FIX how to do this (expand .. (and resolve links))?
+  gchar* task_actual_dir = g_truename (task_dir);
+  gchar* basename = g_path_get_basename (task_actual_dir);
+  g_free (task_actual_dir);
+  return basename;
+#else
+  gchar* path2 = g_strdup (path);
+  /* mgr/users/user/tasks/ID/reports/report_id */
+  gchar* last = g_path_get_basename (path2);
+  int path2_length = strlen (path2);
+  path2_length -= strlen (last);
+  path2_length--; /* In case trailing slash. */
+  path2[path2_length] = '\0';
+  g_free (last);
+  /* mgr/users/user/tasks/ID/reports/ */
+  last = g_path_get_basename (path2);
+  path2_length -= strlen (last);
+  path2_length--; /* Trailing slash. */
+  path2[path2_length] = '\0';
+  g_free (last);
+  /* mgr/users/user/tasks/ID/ */
+  gchar* basename = g_path_get_basename (path2);
+  g_free (path2);
+  return basename;
+#endif
+}
+
+/**
+ * @brief Get the task associated with a report.
+ *
+ * @param[in]  report_id  ID of report.
+ *
+ * @return Pointer to task on success, else NULL.
+ */
+task_t*
+report_task (const char* report_id)
+{
+  gchar* link_name;
+  link_name = g_build_filename (PREFIX
+                                "/var/lib/openvas/mgr/users/",
+                                current_credentials.username,
+                                "reports",
+                                report_id,
+                                NULL);
+  // FIX glib access setuid note
+  if (g_file_test (link_name, G_FILE_TEST_IS_SYMLINK)
+      && g_file_test (link_name, G_FILE_TEST_IS_DIR))
+    {
+      GError* error = NULL;
+      gchar* report_path = g_file_read_link (link_name, &error);
+      if (error)
+        {
+          fprintf (stderr,
+                   "Failed to read report symlink: %s.\n",
+                   error->message);
+          g_error_free (error);
+          g_free (report_path);
+          g_free (link_name);
+          return NULL;
+        }
+      gchar* task_name = report_path_task_name (report_path);
+      unsigned int id;
+      int ret = sscanf (task_name, "%u", &id);
+      g_free (task_name);
+      if (ret == 1) return find_task (id);
+    }
+  return NULL;
+}
+
+/**
  * @brief Delete a report.
  *
  * @param[in]  report_id  ID of report.
  *
- * @return 0 success, -1 failed to parse ID, -2 report file missing,
+ * @return 0 success, -1 failed to find task, -2 report file missing,
  *         -3 failed to read link, -4 failed to remove report.
  */
 int
 delete_report (const char* report_id)
 {
-  unsigned int id;
-  if (sscanf (report_id, "%u", &id) == 1)
+  task_t* task = report_task (report_id);
+  if (task == NULL) return -1;
+
+  gchar* link_name;
+  link_name = g_build_filename (PREFIX
+                                "/var/lib/openvas/mgr/users/",
+                                current_credentials.username,
+                                "reports",
+                                report_id,
+                                NULL);
+  // FIX glib access setuid note
+  if (g_file_test (link_name, G_FILE_TEST_IS_SYMLINK)
+      && g_file_test (link_name, G_FILE_TEST_IS_DIR))
     {
-      static char buffer[11]; /* (expt 2 32) => 4294967296 */
-
-      sprintf (buffer, "%010u", id);
-      gchar* link_name;
-      link_name = g_build_filename (PREFIX
-                                    "/var/lib/openvas/mgr/users/",
-                                    current_credentials.username,
-                                    "reports",
-                                    buffer,
-                                    NULL);
-      // FIX glib access setuid note
-      if (g_file_test (link_name, G_FILE_TEST_IS_SYMLINK)
-          && g_file_test (link_name, G_FILE_TEST_IS_DIR))
+      GError* error = NULL;
+      gchar* name = g_file_read_link (link_name, &error);
+      if (error)
         {
-          GError* error = NULL;
-          gchar* name = g_file_read_link (link_name, &error);
-          if (error)
-            {
-              fprintf (stderr,
-                       "Failed to read report symlink: %s.\n",
-                       error->message);
-              g_error_free (error);
-              g_free (name);
-              g_free (link_name);
-              return -3;
-            }
-          else if (rmdir_recursively (name, &error) == FALSE)
-            {
-              fprintf (stderr,
-                       "Failed to remove %s: %s.\n",
-                       name,
-                       error->message);
-              g_error_free (error);
-              g_free (name);
-              g_free (link_name);
-              return -4;
-            }
-          else
-            {
-              if (unlink (link_name))
-                /* Just log the error. */
-                fprintf (stderr,
-                         "Failed to remove report symlink %s: %s.\n",
-                         link_name,
-                         strerror (errno));
-              g_free (name);
-              g_free (link_name);
-              return 0;
-            }
+          fprintf (stderr,
+                   "Failed to read report symlink: %s.\n",
+                   error->message);
+          g_error_free (error);
+          g_free (name);
+          g_free (link_name);
+          return -3;
         }
+      else if (rmdir_recursively (name, &error) == FALSE)
+        {
+          fprintf (stderr,
+                   "Failed to remove %s: %s.\n",
+                   name,
+                   error->message);
+          g_error_free (error);
+          g_free (name);
+          g_free (link_name);
+          return -4;
+        }
       else
-        return -2;
+        {
+          if (unlink (link_name))
+            /* Just log the error. */
+            fprintf (stderr,
+                     "Failed to remove report symlink %s: %s.\n",
+                     link_name,
+                     strerror (errno));
+          g_free (name);
+          g_free (link_name);
+          task->report_count--;
+          return 0;
+        }
     }
   else
-    return -1;
+    return -2;
 }
 
 /**
@@ -211,7 +309,7 @@
  * @param[in]  parameter  The name of the parameter (in any case): COMMENT.
  * @param[in]  value      The value of the parameter.
  *
- * @return 0 success, -1 failed to scan ID, -2 parameter name error,
+ * @return 0 success, -2 parameter name error,
  *         -3 failed to write parameter to disk.
  */
 int
@@ -220,35 +318,26 @@
   tracef ("   set_report_parameter %s %s\n", report_id, parameter);
   if (strncasecmp ("COMMENT", parameter, 7) == 0)
     {
-      unsigned int id;
-      if (sscanf (report_id, "%u", &id) == 1)
+      gchar* name;
+      name = g_build_filename (PREFIX
+                               "/var/lib/openvas/mgr/users/",
+                               current_credentials.username,
+                               "reports",
+                               report_id,
+                               "comment",
+                               NULL);
+      GError* error = NULL;
+      g_file_set_contents (name, value, -1, &error);
+      if (error)
         {
-          static char buffer[11]; /* (expt 2 32) => 4294967296 */
-
-          sprintf (buffer, "%010u", id);
-          gchar* name;
-          name = g_build_filename (PREFIX
-                                   "/var/lib/openvas/mgr/users/",
-                                   current_credentials.username,
-                                   "reports",
-                                   buffer,
-                                   "comment",
-                                   NULL);
-          GError* error = NULL;
-          g_file_set_contents (name, value, -1, &error);
-          if (error)
-            {
-              fprintf (stderr,
-                       "Failed to save comment to %s: %s.\n",
-                       name,
-                       error->message);
-              g_free (name);
-              return -3;
-            }
+          fprintf (stderr,
+                   "Failed to save comment to %s: %s.\n",
+                   name,
+                   error->message);
           g_free (name);
+          return -3;
         }
-      else
-        return -1;
+      g_free (name);
     }
   else
     return -2;

Modified: trunk/openvas-manager/src/manage.h
===================================================================
--- trunk/openvas-manager/src/manage.h	2009-03-21 09:04:15 UTC (rev 2866)
+++ trunk/openvas-manager/src/manage.h	2009-03-21 14:58:48 UTC (rev 2867)
@@ -27,6 +27,7 @@
 #define OPENVAS_MANAGER_MANAGE_H
 
 #include <glib.h>
+#include <ossp/uuid.h>
 
 
 /* Credentials. */
@@ -43,7 +44,7 @@
 extern credentials_t current_credentials;
 
 void
-free_credentials (credentials_t* credentials);
+free_credentials (credentials_t*);
 
 void
 append_to_credentials_username (credentials_t*, const char*, int);
@@ -55,15 +56,6 @@
 authenticate (credentials_t);
 
 
-/* Reports. */
-
-int
-delete_report (const char*);
-
-int
-set_report_parameter (char*, const char*, char*);
-
-
 /* Tasks. */
 
 // FIX should be in otp.c/h
@@ -199,4 +191,25 @@
 void
 append_task_open_port (task_t*, unsigned int, char*);
 
+
+/* Reports. */
+
+// FIX how is this doc'd?
+#define OVAS_MANAGE_REPORT_ID_LENGTH UUID_LEN_STR
+
+char*
+make_report_id ();
+
+gchar*
+report_path_task_name (gchar*);
+
+task_t*
+report_task (const char*);
+
+int
+delete_report (const char*);
+
+int
+set_report_parameter (char*, const char*, char*);
+
 #endif

Modified: trunk/openvas-manager/src/omp.c
===================================================================
--- trunk/openvas-manager/src/omp.c	2009-03-21 09:04:15 UTC (rev 2866)
+++ trunk/openvas-manager/src/omp.c	2009-03-21 14:58:48 UTC (rev 2867)
@@ -741,42 +741,43 @@
     {
       const char* report_name = names[index]->d_name;
 
-      if (report_name[0] == '.'
-          || strlen (report_name) < 5
-          || strcmp (report_name + strlen (report_name) - 4, ".nbe"))
-        continue;
+      if (report_name[0] == '.') continue;
 
+      if (strlen (report_name) == OVAS_MANAGE_REPORT_ID_LENGTH)
+        {
 #if 0
-      report_dir_name = g_build_filename (dir_name, report_name, NULL);
+          report_dir_name = g_build_filename (dir_name, report_name, NULL);
 #endif
 
-      tracef ("     %s\n", report_name);
+          tracef ("     %s\n", report_name);
 
-      msg = g_strdup_printf ("<report>"
-                             "<id>%.*s</id>"
-                             "<timestamp>FIX</timestamp>"
-                             "<messages>"
-                             "<hole>0</hole>"
-                             "<info>0</info>"
-                             "<log>0</log>"
-                             "<debug>0</debug>"
-                             "</messages>"
-                             "</report>",
-                             strlen (report_name) - 4,
-                             report_name);
-      free (names[index]);
-      SEND_TO_CLIENT (msg);
+          msg = g_strdup_printf ("<report>"
+                                 "<id>%s</id>"
+                                 "<timestamp>FIX</timestamp>"
+                                 "<messages>"
+                                 // FIX
+                                 "<hole>0</hole>"
+                                 "<info>0</info>"
+                                 "<log>0</log>"
+                                 "<debug>0</debug>"
+                                 "</messages>"
+                                 "</report>",
+                                 report_name);
+          free (names[index]);
+          SEND_TO_CLIENT (msg);
+        }
     }
 
-#if 0
   g_free (dir_name);
-#endif
   free (names);
 
   SEND_TO_CLIENT (msg);
   g_free (msg);
   return FALSE;
+
  send_to_client_fail:
+  while (++index < count) { free (names[index]); }
+  g_free (dir_name);
   g_free (names);
   g_free (msg);
   return TRUE;
@@ -1009,11 +1010,11 @@
                   SEND_TO_CLIENT ("<delete_report_response>"
                                   "<status>200</status>");
                   break;
+                case -1: /* Failed to find associated task. */
                 case -2: /* Report file missing. */
                   SEND_TO_CLIENT ("<delete_report_response>"
                                   "<status>40x</status>");
                   break;
-                case -1: /* Failed to parse id. */
                 case -3: /* Failed to read link. */
                 case -4: /* Failed to remove report. */
                 default:
@@ -1033,22 +1034,16 @@
 
       case CLIENT_GET_REPORT:
         assert (strncasecmp ("GET_REPORT", element_name, 10) == 0);
-        unsigned int id;
-        if (current_task_task_id
-            && sscanf (current_task_task_id, "%u", &id) == 1)
+        if (current_task_task_id)
           {
-            static char buffer[11]; /* (expt 2 32) => 4294967296 */
-
-            free_string_var (&current_task_task_id);
-
-            sprintf (buffer, "%010u", id);
             gchar* name = g_build_filename (PREFIX
                                             "/var/lib/openvas/mgr/users/",
                                             current_credentials.username,
                                             "reports",
-                                            buffer,
+                                            current_task_task_id,
                                             "report.nbe",
                                             NULL);
+            free_string_var (&current_task_task_id);
             // FIX glib access setuid note
             if (g_file_test (name, G_FILE_TEST_EXISTS))
               {
@@ -1089,7 +1084,7 @@
         else
           {
             free_string_var (&current_task_task_id);
-            SEND_TO_CLIENT ("<get_report_reponse>"
+            SEND_TO_CLIENT ("<get_report_response>"
                             "<status>500</status>");
           }
         SEND_TO_CLIENT ("</get_report_response>");
@@ -1178,7 +1173,6 @@
                   SEND_TO_CLIENT ("<modify_report_response>"
                                   "<status>200</status>");
                   break;
-                case -1: /* Failed to scan ID. */
                 case -2: /* Parameter name error. */
                   SEND_TO_CLIENT ("<modify_report_response>"
                                   "<status>40x</status>");

Modified: trunk/openvas-manager/src/otp.c
===================================================================
--- trunk/openvas-manager/src/otp.c	2009-03-21 09:04:15 UTC (rev 2866)
+++ trunk/openvas-manager/src/otp.c	2009-03-21 14:58:48 UTC (rev 2867)
@@ -283,7 +283,7 @@
 
   if (current_credentials.username == NULL) return -1;
 
-  tracef ("   Saving report %s on task %u\n", task->start_time, task->id);
+  tracef ("   Saving report (%s) on task %u\n", task->start_time, task->id);
 
   if (task_id_string (task, &id)) return -1;
 
@@ -306,16 +306,7 @@
 
   /* Generate report directory name. */
 
-  // FIX OID
-  static char buffer[11]; /* (expt 2 32) => 4294967296 */
-  int length = sprintf (buffer, "%010u", task->report_count);
-  assert (length < 15);
-  if (length < 4)
-    {
-      fprintf (stderr, "Failed to generate report id.\n");
-      g_free (user_dir_name);
-      return -5;
-    }
+  char* report_id = make_report_id ();
 
   gchar* dir_name = g_build_filename (PREFIX
                                       "/var/lib/openvas/mgr/users/",
@@ -323,10 +314,11 @@
                                       "tasks",
                                       id,
                                       "reports",
-                                      buffer,
+                                      report_id,
                                       NULL);
 
-  gchar* symlink_name = g_build_filename (user_dir_name, buffer, NULL);
+  gchar* symlink_name = g_build_filename (user_dir_name, report_id, NULL);
+  free (report_id);
   g_free (user_dir_name);
 
   /* Ensure task report directory exists. */



More information about the Openvas-commits mailing list