[Openvas-commits] r5574 - in trunk/openvas-client: . src
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Fri Oct 16 11:41:16 CEST 2009
Author: felix
Date: 2009-10-16 11:41:15 +0200 (Fri, 16 Oct 2009)
New Revision: 5574
Modified:
trunk/openvas-client/ChangeLog
trunk/openvas-client/src/omp-cli.c
Log:
Refactored omp-cli for easier extendability.
* src/omp-cli.c (omp_cli_connection_t): New, struct bundles connection
information.
(ompcli_connect): New. Connects and authenticates.
(ompcli_disconnect): New. Disconnects.
(ompcli_cmd_get_report): New, extracted from main.
(main): Fill values of omp_cli_connection_t and (dis)connect by using
new functions.
Added TODO about freeing omp_cli_connection_t.
Modified: trunk/openvas-client/ChangeLog
===================================================================
--- trunk/openvas-client/ChangeLog 2009-10-16 09:30:20 UTC (rev 5573)
+++ trunk/openvas-client/ChangeLog 2009-10-16 09:41:15 UTC (rev 5574)
@@ -1,3 +1,16 @@
+2009-10-16 Felix Wolfsteller <felix.wolfsteller at intevation.de>
+
+ Refactored omp-cli for easier extendability.
+
+ * src/omp-cli.c (omp_cli_connection_t): New, struct bundles connection
+ information.
+ (ompcli_connect): New. Connects and authenticates.
+ (ompcli_disconnect): New. Disconnects.
+ (ompcli_cmd_get_report): New, extracted from main.
+ (main): Fill values of omp_cli_connection_t and (dis)connect by using
+ new functions.
+ Added TODO about freeing omp_cli_connection_t.
+
2009-10-16 Matthew Mundell <matthew.mundell at intevation.de>
* openvas/prefs_dialog/prefs_scope_tree.c [USE_OMP]
Modified: trunk/openvas-client/src/omp-cli.c
===================================================================
--- trunk/openvas-client/src/omp-cli.c 2009-10-16 09:30:20 UTC (rev 5573)
+++ trunk/openvas-client/src/omp-cli.c 2009-10-16 09:41:15 UTC (rev 5574)
@@ -67,7 +67,70 @@
*/
#define OPENVASMD_PORT 9390
+
/**
+ * @brief Structure used to bundle the information needed to (dis)connect to
+ * @brief an openvas-manager.
+ */
+typedef struct {
+ gnutls_session_t session; ///< GnuTLS Session to use.
+ int socket; ///< Socket to manager.
+ gchar* omp_username; ///< Username with which to connect.
+ gchar* omp_password; ///< Password for user with which to connect.
+ gchar* manager_host_string; ///< OpenVAS-Manager host string.
+ gchar* manager_port_string; ///< OpenVAS-Manager port string.
+ gint manager_port; ///< Port of manager.
+} omp_cli_connection_t;
+
+/**
+ * @brief Connect to an openvas-manager, exit (\ref EXIT_FAILURE) if connection
+ * @brief could not be established or authentication failed.
+ *
+ * Exits in the fail case and prints a message to stderr.
+ *
+ * @return TRUE. Does not return in fail case.
+ */
+static gboolean
+ompcli_connect (omp_cli_connection_t* connection)
+{
+ connection->socket = openvas_server_open (&connection->session,
+ connection->manager_host_string,
+ connection->manager_port);
+
+ if (connection->socket == -1)
+ {
+ fprintf (stderr, "Failed to acquire socket.\n");
+ exit (EXIT_FAILURE);
+ }
+
+ if (omp_authenticate (&connection->session,
+ connection->omp_username,
+ connection->omp_password))
+ {
+ openvas_server_close (connection->socket, connection->session);
+ fprintf (stderr, "Failed to authenticate.\n");
+ exit (EXIT_FAILURE);
+ }
+
+ return TRUE;
+}
+
+/**
+ * @brief Closes the connection to an openvas-manager.
+ *
+ * @return Always TRUE.
+ *
+ * @todo Consider freeing the connection here. If not here, ensure its freed
+ * in all caller places.
+ */
+static gboolean
+ompcli_disconnect (omp_cli_connection_t* connection)
+{
+ openvas_server_close (connection->socket, connection->session);
+ return TRUE;
+}
+
+/**
* @brief Print tasks.
*
* @param[in] tasks Tasks.
@@ -150,12 +213,77 @@
return 0;
}
+/**
+ * @brief Performs the omp get_report command.
+ *
+ * This function exit (\ref EXIT_SUCCES) in case of success and
+ * exit (\ref EXIT_FAILURE) in case of failure.
+ *
+ * @param connection Connection to manager to use.
+ * @param point Pointer to task_uuid id.
+ * @param format Queried report format.
+ */
+static void
+ompcli_cmd_get_report (omp_cli_connection_t* connection, gchar** point,
+ gchar* format)
+{
+ if (format == NULL || strcasecmp (format, "xml") == 0)
+ {
+ entity_t entity, report_xml;
+
+ if (omp_get_report (&(connection->session),
+ *point,
+ "xml",
+ &entity))
+ {
+ fprintf (stderr, "Failed to get report.\n");
+ ompcli_disconnect (connection);
+ exit (EXIT_FAILURE);
+ }
+
+ report_xml = entity_child (entity, "report");
+ if (report_xml == NULL)
+ {
+ free_entity (entity);
+ fprintf (stderr, "Failed to get report.\n");
+ ompcli_disconnect (connection);
+ exit (EXIT_FAILURE);
+ }
+
+ print_entity (stdout, report_xml);
+ }
+ else
+ {
+ void *report;
+ gsize report_size;
+
+ if (omp_get_report_format (&(connection->session),
+ *point,
+ format,
+ &report,
+ &report_size))
+ {
+ fprintf (stderr, "Failed to get report.\n");
+ ompcli_disconnect (connection);
+ exit (EXIT_FAILURE);
+ }
+
+ if (fwrite (report, 1, report_size, stdout) < report_size)
+ {
+ fprintf (stderr, "Failed to write entire report.\n");
+ ompcli_disconnect (connection);
+ exit (EXIT_FAILURE);
+ }
+ }
+
+ ompcli_disconnect (connection);
+ exit (EXIT_SUCCESS);
+}
+
int
main (int argc, char** argv)
{
- unsigned int manager_port;
- int socket;
- gnutls_session_t session;
+ omp_cli_connection_t* connection = NULL;
/* Global options. */
static gboolean prompt = FALSE;
@@ -279,55 +407,45 @@
exit (EXIT_SUCCESS);
}
+ connection = g_malloc0 (sizeof (omp_cli_connection_t));
+
if (manager_host_string == NULL)
manager_host_string = OPENVASMD_ADDRESS;
+ connection->manager_host_string = manager_host_string;
- if (manager_port_string)
- {
- manager_port = atoi (manager_port_string);
- if (manager_port <= 0 || manager_port >= 65536)
- {
- fprintf (stderr, "Manager port must be a number between 0 and 65536.\n");
- exit (EXIT_FAILURE);
- }
- }
+ if (manager_port_string != NULL)
+ connection->manager_port = atoi (manager_port_string);
else
+ connection->manager_port = OPENVASMD_PORT;
+
+ if (connection->manager_port <= 0 || connection->manager_port >= 65536)
{
- manager_port = OPENVASMD_PORT;
+ fprintf (stderr, "Manager port must be a number between 0 and 65536.\n");
+ exit (EXIT_FAILURE);
}
+ if (omp_username == NULL)
+ omp_username = g_strdup (g_get_user_name ());
+ connection->omp_username = omp_username;
+
+ if (omp_password == NULL)
+ omp_password = g_strdup (omp_username);
+ connection->omp_password = omp_password;
+
if (be_verbose)
{
- printf ("Will try to connect to host %s, port %d...\n", manager_host_string,
- manager_port);
+ printf ("Will try to connect to host %s, port %d...\n",
+ connection->manager_host_string,
+ connection->manager_port);
}
else
{
g_log_set_default_handler (openvas_log_silent, NULL);
}
- 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)
{
- 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);
- }
-
+ ompcli_connect (connection);
if (prompt)
{
fprintf (stderr, "Connected, press a key to continue.\n");
@@ -336,9 +454,9 @@
printf ("Sending to manager: %s\n", cmd_xml);
- if (openvas_server_send (&session, cmd_xml) == -1)
+ if (openvas_server_send (&(connection->session), cmd_xml) == -1)
{
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
fprintf (stderr, "Failed to send_to_manager.\n");
exit (EXIT_FAILURE);
}
@@ -346,10 +464,10 @@
/* Read the response. */
entity_t entity = NULL;
- if (read_entity (&session, &entity))
+ if (read_entity (&(connection->session), &entity))
{
fprintf (stderr, "Failed to read response.\n");
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_FAILURE);
}
@@ -365,7 +483,7 @@
getchar ();
}
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
free_entity (entity);
exit (EXIT_SUCCESS);
@@ -381,20 +499,8 @@
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);
- }
+ ompcli_connect (connection);
- 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;
@@ -414,7 +520,7 @@
exit (EXIT_FAILURE);
}
- if (omp_create_task_rc (&session,
+ if (omp_create_task_rc (&(connection->session),
content,
content_len,
name ? name : "unnamed task",
@@ -423,13 +529,13 @@
{
g_free (content);
fprintf (stderr, "Failed to create task.\n");
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_FAILURE);
}
}
else
{
- if (omp_create_task (&session,
+ if (omp_create_task (&(connection->session),
name ? name : "unnamed task",
config ? config : "Full and fast",
target ? target : "Localhost",
@@ -437,7 +543,7 @@
&id))
{
fprintf (stderr, "Failed to create task.\n");
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_FAILURE);
}
}
@@ -445,7 +551,7 @@
printf (id);
putchar ('\n');
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_SUCCESS);
}
@@ -459,34 +565,22 @@
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);
- }
+ ompcli_connect (connection);
- 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_report (&session, *point))
+ if (omp_delete_report (&(connection->session), *point))
{
fprintf (stderr,
"Failed to delete report %s, exiting.\n",
*point);
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_FAILURE);
}
point++;
}
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_SUCCESS);
}
@@ -500,32 +594,20 @@
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);
- }
+ ompcli_connect (connection);
- 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))
+ if (omp_delete_task (&(connection->session), *point))
{
fprintf (stderr, "Failed to delete task.\n");
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_FAILURE);
}
point++;
}
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_SUCCESS);
}
@@ -534,34 +616,22 @@
gchar **point = rest;
entity_t status;
- socket = openvas_server_open (&session, manager_host_string, manager_port);
- if (socket == -1)
- {
- fprintf (stderr, "Failed to acquire socket.\n");
- exit (EXIT_FAILURE);
- }
+ ompcli_connect (connection);
- 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))
+ if (omp_get_status (&(connection->session), *point, 0, &status))
{
fprintf (stderr, "Failed to get status of task %s.\n", *point);
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_FAILURE);
}
else
{
if (print_tasks (status->entities))
{
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_FAILURE);
}
}
@@ -570,25 +640,25 @@
}
else
{
- if (omp_get_status (&session, NULL, 0, &status))
+ if (omp_get_status (&(connection->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);
+ ompcli_disconnect (connection);
exit (EXIT_FAILURE);
}
}
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_SUCCESS);
}
if (cmd_get_report)
{
- gchar **point = rest;
+ gchar** point = rest;
if (point == NULL || *point == NULL)
{
@@ -596,71 +666,8 @@
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_SUCCESS);
+ ompcli_connect (connection);
+ ompcli_cmd_get_report (connection, point, format);
}
if (cmd_modify_task)
@@ -678,7 +685,7 @@
if (name == NULL)
{
- fprintf (stderr, "modify-task requires the name option.\n");
+ fprintf (stderr, "modify-task requires the name option (path to file).\n");
exit (EXIT_FAILURE);
}
@@ -690,20 +697,8 @@
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);
- }
+ ompcli_connect (connection);
- 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));
@@ -717,16 +712,16 @@
exit (EXIT_FAILURE);
}
- if (omp_modify_task_file (&session, *point, name,
+ if (omp_modify_task_file (&(connection->session), *point, name,
content, content_len))
{
g_free (content);
fprintf (stderr, "Failed to modify task.\n");
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_FAILURE);
}
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_SUCCESS);
}
@@ -743,32 +738,20 @@
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);
- }
+ ompcli_connect (connection);
- 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))
+ if (omp_start_task (&(connection->session), *point))
{
fprintf (stderr, "Failed to start task.\n");
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_FAILURE);
}
point++;
}
- openvas_server_close (socket, session);
+ ompcli_disconnect (connection);
exit (EXIT_SUCCESS);
}
More information about the Openvas-commits
mailing list