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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Oct 14 22:54:51 CEST 2009


Author: mattm
Date: 2009-10-14 22:54:49 +0200 (Wed, 14 Oct 2009)
New Revision: 5545

Modified:
   trunk/openvas-client/ChangeLog
   trunk/openvas-client/src/omp-cli.c
Log:
	* src/omp-cli.c (print_tasks): New function.
	(main): Rename the --command option to --xml.  Add options for creating
	a task, removing a task, getting reports, getting task status, starting
	a task and attaching a file to a task.

Modified: trunk/openvas-client/ChangeLog
===================================================================
--- trunk/openvas-client/ChangeLog	2009-10-14 16:36:58 UTC (rev 5544)
+++ trunk/openvas-client/ChangeLog	2009-10-14 20:54:49 UTC (rev 5545)
@@ -1,5 +1,12 @@
 2009-10-14  Matthew Mundell <matthew.mundell at intevation.de>
 
+	* src/omp-cli.c (print_tasks): New function.
+	(main): Rename the --command option to --xml.  Add options for creating
+	a task, removing a task, getting reports, getting task status, starting
+	a task and attaching a file to a task.
+
+2009-10-14  Matthew Mundell <matthew.mundell at intevation.de>
+
 	Remove reports in scope tree before refreshing.
 
 	* openvas/prefs_dialog/prefs_scope_tree.c (copy_plugins): New function.

Modified: trunk/openvas-client/src/omp-cli.c
===================================================================
--- trunk/openvas-client/src/omp-cli.c	2009-10-14 16:36:58 UTC (rev 5544)
+++ trunk/openvas-client/src/omp-cli.c	2009-10-14 20:54:49 UTC (rev 5545)
@@ -32,13 +32,16 @@
  * TODO: Add documentation
  */
 
+#include <assert.h>
 #include <glib.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "corevers.h"
 
-#include <openvas_server.h>
+#include <openvas/openvas_server.h>
+#include <openvas/openvas_logging.h>
 #include <openvas/omp/omp.h>
 #include <openvas/omp/xml.h>
 
@@ -64,6 +67,89 @@
  */
 #define OPENVASMD_PORT 9390
 
+/**
+ * @brief Print tasks.
+ *
+ * @param[in]  tasks  Tasks.
+ *
+ * @return 0 success, -1 error.
+ */
+static int
+print_tasks (entities_t tasks)
+{
+  entity_t task;
+  while ((task = first_entity (tasks)))
+    {
+      if (strcmp (entity_name (task), "task") == 0)
+        {
+          entity_t entity;
+          const char *id, *name, *status, *progress, *last_report_id;
+
+          id = entity_attribute (task, "id");
+          if (id == NULL)
+            {
+              fprintf (stderr, "Failed to parse task ID.\n");
+              return -1;
+            }
+
+          entity = entity_child (task, "name");
+          if (entity == NULL)
+            {
+              fprintf (stderr, "Failed to parse task name.\n");
+              return -1;
+            }
+          name = entity_text (entity);
+
+          entity = entity_child (task, "status");
+          if (entity == NULL)
+            {
+              fprintf (stderr, "Failed to parse task status.\n");
+              return -1;
+            }
+          status = entity_text (entity);
+
+          entity = entity_child (task, "progress");
+          if (entity == NULL)
+            {
+              fprintf (stderr, "Failed to parse task progress.\n");
+              return -1;
+            }
+          progress = entity_text (entity);
+
+          entity = entity_child (task, "last_report");
+          if (entity == NULL)
+            last_report_id = "";
+          else
+            {
+              entity_t report = entity_child (entity, "report");
+              if (report == NULL)
+                {
+                  fprintf (stderr, "Failed to parse last_report.\n");
+                  return -1;
+                }
+              last_report_id = entity_attribute (report, "id");
+              if (last_report_id == NULL)
+                {
+                  fprintf (stderr, "Failed to parse last_report ID.\n");
+                  return -1;
+                }
+            }
+
+          printf ("%s  %-7s", id, status);
+          if (strcmp (status, "Running") == 0)
+            printf (" %2s%%  %s", progress, name);
+          else
+            printf ("      %s", name);
+          if (last_report_id && strlen (last_report_id))
+            printf (" %s\n", last_report_id);
+          else
+            putchar ('\n');
+        }
+      tasks = next_entities (tasks);
+    }
+  return 0;
+}
+
 int
 main (int argc, char** argv)
 {
@@ -71,21 +157,45 @@
   int socket;
   gnutls_session_t session;
 
+  /* Global options. */
   static gboolean prompt = FALSE;
   static gboolean print_version = FALSE;
   static gboolean be_verbose = FALSE;
   static gchar *manager_host_string = NULL;
   static gchar *manager_port_string = NULL;
-  static gchar *command = NULL;
-  static gchar *omp_command = NULL;
-  static gchar *omp_username = "dummy";
-  static gchar *omp_password = "dummy";
+  static gchar *omp_username = NULL;
+  static gchar *omp_password = NULL;
+  /* Shared command options. */
+  static gchar *name = NULL;
+  /* Command create-task. */
+  static gboolean cmd_create_task = FALSE;
+  static gchar *comment = NULL;
+  static gchar *config = NULL;
+  static gboolean rc = FALSE;
+  static gchar *target = NULL;
+  /* Command delete-task. */
+  static gboolean cmd_delete_task = FALSE;
+  /* Command get-report. */
+  static gboolean cmd_get_report = FALSE;
+  static gchar *format = NULL;
+  /* Command get-status. */
+  static gboolean cmd_get_status = FALSE;
+  /* Command modify-task. */
+  static gboolean cmd_modify_task = FALSE;
+  static gboolean file = FALSE;
+  /* Command start-task. */
+  static gboolean cmd_start_task = FALSE;
+  /* Command given as XML. */
+  static gchar *cmd_xml = NULL;
+  /* The rest of the args. */
+  static gchar **rest = NULL;
 
   GError *error = NULL;
 
   GOptionContext *option_context;
   static GOptionEntry option_entries[]
     = {
+        /* Global options. */
         { "host", 'h', 0, G_OPTION_ARG_STRING, &manager_host_string,
           "Connect to manager on host <host>", "<host>" },
         { "port", 'p', 0, G_OPTION_ARG_STRING, &manager_port_string,
@@ -94,15 +204,56 @@
           "Print version.", NULL },
         { "verbose", 'v', 0, G_OPTION_ARG_NONE, &be_verbose,
           "Verbose messages.", NULL },
-        { "command", 'c', 0, G_OPTION_ARG_STRING, &command,
-          "OMP command (e.g. \"<help/>\", \"<get_version/>\")",
-          "<command>" },
         { "username", 'u', 0, G_OPTION_ARG_STRING, &omp_username,
           "OMP username", "<username>" },
         { "password", 'w', 0, G_OPTION_ARG_STRING, &omp_password,
           "OMP password", "<password>" },
         { "prompt", 'P', 0, G_OPTION_ARG_NONE, &prompt,
           "Prompt to exit.", NULL },
+        /* Shared command options. */
+        { "name", 'n', 0, G_OPTION_ARG_STRING, &name,
+          "Name for create-task.",
+          "<name>" },
+        /* Command create-task. */
+        { "create-task", 'C', 0, G_OPTION_ARG_NONE, &cmd_create_task,
+          "Create a task.", NULL },
+        { "comment", 'm', 0, G_OPTION_ARG_STRING, &comment,
+          "Comment for create-task.",
+          "<name>" },
+        { "config", 'c', 0, G_OPTION_ARG_STRING, &config,
+          "Config for create-task.",
+          "<config>" },
+        { "rc", 'r', 0, G_OPTION_ARG_NONE, &rc,
+          "Create task with RC read from stdin.", NULL },
+        { "target", 't', 0, G_OPTION_ARG_STRING, &target,
+          "Target for create-task.",
+          "<target>" },
+        /* Command delete-task. */
+        { "delete-task", 'D', 0, G_OPTION_ARG_NONE, &cmd_delete_task,
+          "Delete a task.", NULL },
+        /* Command get-report. */
+        { "get-report", 'R', 0, G_OPTION_ARG_NONE, &cmd_get_report,
+          "Get report of one task.", NULL },
+        { "format", 'f', 0, G_OPTION_ARG_STRING, &format,
+          "Format for get-report.",
+          "<format>" },
+        /* Command get-status. */
+        { "get-status", 'G', 0, G_OPTION_ARG_NONE, &cmd_get_status,
+          "Get status of one, many or all tasks.", NULL },
+        /* Command start-task. */
+        { "start-task", 'S', 0, G_OPTION_ARG_NONE, &cmd_start_task,
+          "Start one or more tasks.", NULL },
+        /* Command modify-task. */
+        { "modify-task", 'M', 0, G_OPTION_ARG_NONE, &cmd_modify_task,
+          "Modify a task.", NULL },
+        { "file", 0, 0, G_OPTION_ARG_NONE, &file,
+          "Add text in stdin as file on task.", NULL },
+        /* Command as XML. */
+        { "xml", 'X', 0, G_OPTION_ARG_STRING, &cmd_xml,
+          "XML command (e.g. \"<help/>\", \"<get_version/>\")",
+          "<command>" },
+        { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &rest,
+          NULL, NULL },
         { NULL }
       };
 
@@ -140,75 +291,440 @@
       manager_port = OPENVASMD_PORT;
     }
 
-  if (command != NULL)
+  if (be_verbose)
     {
-      omp_command = g_strconcat (command, "\n", NULL);
+      printf ("Will try to connect to host %s, port %d...\n", manager_host_string,
+              manager_port);
     }
   else
     {
-      printf ("No OMP command supplied, exiting.\n");
-      exit (EXIT_FAILURE);
+      g_log_set_default_handler (openvas_log_silent, NULL);
     }
 
-  if (be_verbose)
+  if (omp_username == NULL)
+    omp_username = g_strdup (g_get_user_name ());
+
+  if (omp_password == NULL)
+    omp_password = g_strdup (omp_username);
+
+  if (cmd_xml)
     {
-      printf ("Will try to connect to host %s, port %d...\n", manager_host_string,
-              manager_port);
+      socket = openvas_server_open (&session, manager_host_string, manager_port);
+      if (socket == -1)
+        {
+          fprintf (stderr, "Failed to acquire socket.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      if (omp_authenticate (&session, omp_username, omp_password))
+        {
+          openvas_server_close (socket, session);
+          fprintf (stderr, "Failed to authenticate.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      if (prompt)
+        {
+          fprintf (stderr, "Connected, press a key to continue.\n");
+          getchar ();
+        }
+
+      printf ("Sending to manager: %s\n", cmd_xml);
+
+      if (openvas_server_send (&session, cmd_xml) == -1)
+        {
+          openvas_server_close (socket, session);
+          fprintf (stderr, "Failed to send_to_manager.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      /* Read the response. */
+
+      entity_t entity = NULL;
+      if (read_entity (&session, &entity))
+        {
+          fprintf (stderr, "Failed to read response.\n");
+          openvas_server_close (socket, session);
+          exit (EXIT_FAILURE);
+        }
+
+      printf ("Got response:\n");
+      print_entity (stdout, entity);
+      printf ("\n");
+
+      /* Cleanup. */
+
+      if (prompt)
+        {
+          fprintf (stderr, "Press a key when done.\n");
+          getchar ();
+        }
+
+      openvas_server_close (socket, session);
+      free_entity (entity);
+
+      exit (EXIT_SUCCESS);
     }
 
-  socket = openvas_server_open (&session, manager_host_string, manager_port);
-  if (socket == -1)
+  if (cmd_create_task)
     {
-      fprintf (stderr, "Failed to acquire socket.\n");
-      exit (EXIT_FAILURE);
+      char *id = NULL;
+
+      if (rc && (config || target))
+        {
+          fprintf (stderr, "create-task rc given with config or target.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      socket = openvas_server_open (&session, manager_host_string, manager_port);
+      if (socket == -1)
+        {
+          fprintf (stderr, "Failed to acquire socket.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      if (omp_authenticate (&session, omp_username, omp_password))
+        {
+          openvas_server_close (socket, session);
+          fprintf (stderr, "Failed to authenticate.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      if (rc)
+        {
+          gchar *content;
+          gsize content_len;
+          GIOChannel *stdin_channel;
+
+          /* Mixing stream and file descriptor IO might lead to trouble. */
+          error = NULL;
+          stdin_channel = g_io_channel_unix_new (fileno (stdin));
+          g_io_channel_read_to_end (stdin_channel, &content, &content_len, &error);
+          g_io_channel_shutdown (stdin_channel, TRUE, NULL);
+          g_io_channel_unref (stdin_channel);
+          if (error)
+            {
+              fprintf (stderr, "failed to read from stdin: %s\n", error->message);
+              g_error_free (error);
+              exit (EXIT_FAILURE);
+            }
+
+          if (omp_create_task_rc (&session,
+                                  content,
+                                  content_len,
+                                  name ? name : "unnamed task",
+                                  comment ? comment : "",
+                                  &id))
+            {
+              g_free (content);
+              fprintf (stderr, "Failed to create task.\n");
+              openvas_server_close (socket, session);
+              exit (EXIT_FAILURE);
+            }
+        }
+      else
+        {
+          if (omp_create_task (&session,
+                               name ? name : "unnamed task",
+                               config ? config : "Full and fast",
+                               target ? target : "Localhost",
+                               comment ? comment : "",
+                               &id))
+            {
+              fprintf (stderr, "Failed to create task.\n");
+              openvas_server_close (socket, session);
+              exit (EXIT_FAILURE);
+            }
+        }
+
+      printf (id);
+      putchar ('\n');
+
+      openvas_server_close (socket, session);
+      exit (EXIT_SUCCESS);
     }
 
-  if (omp_authenticate (&session, omp_username, omp_password))
+  if (cmd_delete_task)
     {
+      gchar **point = rest;
+
+      if (point == NULL || *point == NULL)
+        {
+          fprintf (stderr, "delete-task requires at least one argument.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      socket = openvas_server_open (&session, manager_host_string, manager_port);
+      if (socket == -1)
+        {
+          fprintf (stderr, "Failed to acquire socket.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      if (omp_authenticate (&session, omp_username, omp_password))
+        {
+          openvas_server_close (socket, session);
+          fprintf (stderr, "Failed to authenticate.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      while (*point)
+        {
+          if (omp_delete_task (&session, *point))
+            {
+              fprintf (stderr, "Failed to delete task.\n");
+              openvas_server_close (socket, session);
+              exit (EXIT_FAILURE);
+            }
+          point++;
+        }
+
       openvas_server_close (socket, session);
-      fprintf (stderr, "Failed to authenticate.\n");
-      exit (EXIT_FAILURE);
+      exit (EXIT_SUCCESS);
     }
 
-  if (prompt)
+  if (cmd_get_status)
     {
-      fprintf (stderr, "Connected, press a key to continue.\n");
-      getchar ();
-    }
+      gchar **point = rest;
+      entity_t status;
 
-  fprintf (stderr, "Sending to manager: %s", omp_command);
+      socket = openvas_server_open (&session, manager_host_string, manager_port);
+      if (socket == -1)
+        {
+          fprintf (stderr, "Failed to acquire socket.\n");
+          exit (EXIT_FAILURE);
+        }
 
-  if (openvas_server_send (&session, omp_command) == -1)
-    {
+      if (omp_authenticate (&session, omp_username, omp_password))
+        {
+          openvas_server_close (socket, session);
+          fprintf (stderr, "Failed to authenticate.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      if (point)
+        while (*point)
+          {
+            if (omp_get_status (&session, *point, 0, &status))
+              {
+                fprintf (stderr, "Failed to get status of task %s.\n", *point);
+                openvas_server_close (socket, session);
+                exit (EXIT_FAILURE);
+              }
+            else
+              {
+                if (print_tasks (status->entities))
+                  {
+                    openvas_server_close (socket, session);
+                    exit (EXIT_FAILURE);
+                  }
+              }
+
+            point++;
+          }
+      else
+        {
+          if (omp_get_status (&session, NULL, 0, &status))
+            {
+              fprintf (stderr, "Failed to get status of all tasks.\n");
+              exit (EXIT_FAILURE);
+            }
+          if (print_tasks (status->entities))
+            {
+              openvas_server_close (socket, session);
+              exit (EXIT_FAILURE);
+            }
+        }
+
       openvas_server_close (socket, session);
-      fprintf (stderr, "Failed to send_to_manager.\n");
-      exit (EXIT_FAILURE);
+      exit (EXIT_SUCCESS);
     }
 
-  /* Read the response. */
+  if (cmd_get_report)
+    {
+      gchar **point = rest;
 
-  entity_t entity = NULL;
-  if (read_entity (&session, &entity))
-    {
-      fprintf (stderr, "Failed to read response.\n");
+      if (point == NULL || *point == NULL)
+        {
+          fprintf (stderr, "get-report requires one argument.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      socket = openvas_server_open (&session, manager_host_string, manager_port);
+      if (socket == -1)
+        {
+          fprintf (stderr, "Failed to acquire socket.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      if (omp_authenticate (&session, omp_username, omp_password))
+        {
+          openvas_server_close (socket, session);
+          fprintf (stderr, "Failed to authenticate.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      if (format == NULL || strcasecmp (format, "xml") == 0)
+        {
+          entity_t entity, report_xml;
+
+          if (omp_get_report (&session,
+                              *point,
+                              "xml",
+                              &entity))
+            {
+              fprintf (stderr, "Failed to get report.\n");
+              openvas_server_close (socket, session);
+              exit (EXIT_FAILURE);
+            }
+
+          report_xml = entity_child (entity, "report");
+          if (report_xml == NULL)
+            {
+              free_entity (entity);
+              fprintf (stderr, "Failed to get report.\n");
+              openvas_server_close (socket, session);
+              exit (EXIT_FAILURE);
+            }
+
+          print_entity (stdout, report_xml);
+        }
+      else
+        {
+          void *report;
+          gsize report_size;
+
+          if (omp_get_report_format (&session,
+                                     *point,
+                                     format,
+                                     &report,
+                                     &report_size))
+            {
+              fprintf (stderr, "Failed to get report.\n");
+              openvas_server_close (socket, session);
+              exit (EXIT_FAILURE);
+            }
+
+          if (fwrite (report, 1, report_size, stdout) < report_size)
+            {
+              fprintf (stderr, "Failed to write entire report.\n");
+              openvas_server_close (socket, session);
+              exit (EXIT_FAILURE);
+            }
+        }
+
       openvas_server_close (socket, session);
-      exit (EXIT_FAILURE);
+      exit (EXIT_SUCCESS);
     }
 
-  fprintf (stderr, "Got response:\n");
-  print_entity (stderr, entity);
-  fprintf (stderr, "\n");
+  if (cmd_modify_task)
+    {
+      gchar **point = rest;
+      gchar *content;
+      gsize content_len;
+      GIOChannel *stdin_channel;
 
-  /* Cleanup. */
+      if (point == NULL || *point == NULL)
+        {
+          fprintf (stderr, "modify-task requires one argument.\n");
+          exit (EXIT_FAILURE);
+        }
 
-  if (prompt)
+      if (name == NULL)
+        {
+          fprintf (stderr, "modify-task requires the name option.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      if (file == FALSE)
+        {
+          fprintf (stderr, "modify-task requires the file option.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      if (file)
+        {
+          socket = openvas_server_open (&session, manager_host_string, manager_port);
+          if (socket == -1)
+            {
+              fprintf (stderr, "Failed to acquire socket.\n");
+              exit (EXIT_FAILURE);
+            }
+
+          if (omp_authenticate (&session, omp_username, omp_password))
+            {
+              openvas_server_close (socket, session);
+              fprintf (stderr, "Failed to authenticate.\n");
+              exit (EXIT_FAILURE);
+            }
+
+          /* Mixing stream and file descriptor IO might lead to trouble. */
+          error = NULL;
+          stdin_channel = g_io_channel_unix_new (fileno (stdin));
+          g_io_channel_read_to_end (stdin_channel, &content, &content_len, &error);
+          g_io_channel_shutdown (stdin_channel, TRUE, NULL);
+          g_io_channel_unref (stdin_channel);
+          if (error)
+            {
+              fprintf (stderr, "failed to read from stdin: %s\n", error->message);
+              g_error_free (error);
+              exit (EXIT_FAILURE);
+            }
+
+          if (omp_modify_task_file (&session, *point, name, content))
+            {
+              g_free (content);
+              fprintf (stderr, "Failed to modify task.\n");
+              openvas_server_close (socket, session);
+              exit (EXIT_FAILURE);
+            }
+
+          openvas_server_close (socket, session);
+          exit (EXIT_SUCCESS);
+        }
+
+      assert (0);
+    }
+
+  if (cmd_start_task)
     {
-      fprintf (stderr, "Press a key when done.\n");
-      getchar ();
+      gchar **point = rest;
+
+      if (point == NULL || *point == NULL)
+        {
+          fprintf (stderr, "start-task requires at least one argument.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      socket = openvas_server_open (&session, manager_host_string, manager_port);
+      if (socket == -1)
+        {
+          fprintf (stderr, "Failed to acquire socket.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      if (omp_authenticate (&session, omp_username, omp_password))
+        {
+          openvas_server_close (socket, session);
+          fprintf (stderr, "Failed to authenticate.\n");
+          exit (EXIT_FAILURE);
+        }
+
+      while (*point)
+        {
+          if (omp_start_task (&session, *point))
+            {
+              fprintf (stderr, "Failed to start task.\n");
+              openvas_server_close (socket, session);
+              exit (EXIT_FAILURE);
+            }
+          point++;
+        }
+
+      openvas_server_close (socket, session);
+      exit (EXIT_SUCCESS);
     }
 
-  openvas_server_close (socket, session);
-  free_entity (entity);
-
-  exit (EXIT_SUCCESS);
+  fprintf (stderr, "One of the command options must be present.\n");
+  exit (EXIT_FAILURE);
 }



More information about the Openvas-commits mailing list