[Openvas-commits] r6186 - in trunk/openvas-manager: . src
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Fri Dec 18 12:52:43 CET 2009
Author: mattm
Date: 2009-12-18 12:52:40 +0100 (Fri, 18 Dec 2009)
New Revision: 6186
Modified:
trunk/openvas-manager/ChangeLog
trunk/openvas-manager/src/manage.h
trunk/openvas-manager/src/omp.c
trunk/openvas-manager/src/tasks_sql.h
Log:
Add agent support.
* src/tasks_sql.h (agent_t): New type.
(create_tables): Added "agents" table.
(create_agent, delete_agent, init_agent_iterator, agent_iterator_name)
(agent_iterator_comment, agent_iterator_installer)
(agent_iterator_howto_install, agent_iterator_howto_use, agent_name): New
functions.
* src/manage.h: Added protos accordingly.
* src/omp.c (help_text, client_state_t): Extended with agent
commands/states.
(omp_xml_handle_start_element, omp_xml_handle_end_element)
(omp_xml_handle_text): Added handling for agent commands.
Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog 2009-12-18 10:58:28 UTC (rev 6185)
+++ trunk/openvas-manager/ChangeLog 2009-12-18 11:52:40 UTC (rev 6186)
@@ -1,3 +1,21 @@
+2009-12-18 Jan-Oliver Wagner <jan-oliver.wagner at greenbone.net>
+
+ Add agent support.
+
+ * src/tasks_sql.h (agent_t): New type.
+ (create_tables): Added "agents" table.
+ (create_agent, delete_agent, init_agent_iterator, agent_iterator_name)
+ (agent_iterator_comment, agent_iterator_installer)
+ (agent_iterator_howto_install, agent_iterator_howto_use, agent_name): New
+ functions.
+
+ * src/manage.h: Added protos accordingly.
+
+ * src/omp.c (help_text, client_state_t): Extended with agent
+ commands/states.
+ (omp_xml_handle_start_element, omp_xml_handle_end_element)
+ (omp_xml_handle_text): Added handling for agent commands.
+
2009-12-18 Felix Wolfsteller <felix.wolfsteller at intevation.de>
Changed installation destination for openvasmd_report_html.xsl as
Modified: trunk/openvas-manager/src/manage.h
===================================================================
--- trunk/openvas-manager/src/manage.h 2009-12-18 10:58:28 UTC (rev 6185)
+++ trunk/openvas-manager/src/manage.h 2009-12-18 11:52:40 UTC (rev 6186)
@@ -809,6 +809,36 @@
lsc_credential_name (lsc_credential_t);
+/* Agents. */
+
+int
+create_agent (const char*, const char*);
+
+int
+delete_agent (const char*);
+
+void
+init_agent_iterator (iterator_t*, const char*, int, const char*);
+
+const char*
+agent_iterator_name (iterator_t*);
+
+const char*
+agent_iterator_comment (iterator_t*);
+
+const char*
+agent_iterator_installer (iterator_t*);
+
+const char*
+agent_iterator_howto_install (iterator_t*);
+
+const char*
+agent_iterator_howto_use (iterator_t*);
+
+char*
+agent_name (lsc_credential_t);
+
+
/* Scanner messaging. */
int
Modified: trunk/openvas-manager/src/omp.c
===================================================================
--- trunk/openvas-manager/src/omp.c 2009-12-18 10:58:28 UTC (rev 6185)
+++ trunk/openvas-manager/src/omp.c 2009-12-18 11:52:40 UTC (rev 6186)
@@ -229,14 +229,17 @@
" AUTHENTICATE Authenticate with the manager.\n"
" COMMANDS Run a list of commands.\n"
" CREATE_CONFIG Create a config.\n"
+" CREATE_AGENT Create a agent.\n"
" CREATE_LSC_CREDENTIAL Create a local security check credential.\n"
" CREATE_TARGET Create a target.\n"
" CREATE_TASK Create a task.\n"
+" DELETE_AGENT Delete a agent.\n"
" DELETE_CONFIG Delete a config.\n"
" DELETE_LSC_CREDENTIAL Delete a local security check credential.\n"
" DELETE_REPORT Delete a report.\n"
" DELETE_TARGET Delete a target.\n"
" DELETE_TASK Delete a task.\n"
+" GET_AGENTS Get all agents.\n"
" GET_CERTIFICATES Get all available certificates.\n"
" GET_CONFIGS Get all configs.\n"
" GET_DEPENDENCIES Get dependencies for all available NVTs.\n"
@@ -502,6 +505,12 @@
CLIENT_AUTHENTICATE,
CLIENT_AUTHENTIC_COMMANDS,
CLIENT_COMMANDS,
+ CLIENT_CREATE_AGENT,
+ CLIENT_CREATE_AGENT_NAME,
+ CLIENT_CREATE_AGENT_COMMENT,
+ CLIENT_CREATE_AGENT_INSTALLER,
+ CLIENT_CREATE_AGENT_HOWTO_INSTALL,
+ CLIENT_CREATE_AGENT_HOWTO_USE,
CLIENT_CREATE_CONFIG,
CLIENT_CREATE_CONFIG_COMMENT,
CLIENT_CREATE_CONFIG_COPY,
@@ -526,6 +535,8 @@
CLIENT_CREDENTIALS,
CLIENT_CREDENTIALS_PASSWORD,
CLIENT_CREDENTIALS_USERNAME,
+ CLIENT_DELETE_AGENT,
+ CLIENT_DELETE_AGENT_NAME,
CLIENT_DELETE_CONFIG,
CLIENT_DELETE_CONFIG_NAME,
CLIENT_DELETE_LSC_CREDENTIAL,
@@ -534,6 +545,7 @@
CLIENT_DELETE_TASK,
CLIENT_DELETE_TARGET,
CLIENT_DELETE_TARGET_NAME,
+ CLIENT_GET_AGENTS,
CLIENT_GET_CERTIFICATES,
CLIENT_GET_CONFIGS,
CLIENT_GET_DEPENDENCIES,
@@ -1015,6 +1027,12 @@
" status=\"" STATUS_OK "\" status_text=\"" STATUS_OK_TEXT "\">");
set_client_state (CLIENT_AUTHENTIC_COMMANDS);
}
+ else if (strcasecmp ("DELETE_AGENT", element_name) == 0)
+ {
+ assert (modify_task_name == NULL);
+ openvas_append_string (&modify_task_name, "");
+ set_client_state (CLIENT_DELETE_AGENT);
+ }
else if (strcasecmp ("DELETE_CONFIG", element_name) == 0)
{
assert (modify_task_name == NULL);
@@ -1049,6 +1067,25 @@
openvas_append_string (¤t_uuid, attribute);
set_client_state (CLIENT_DELETE_TASK);
}
+ else if (strcasecmp ("GET_AGENTS", element_name) == 0)
+ {
+ const gchar* attribute;
+ if (find_attribute (attribute_names, attribute_values,
+ "name", &attribute))
+ openvas_append_string (¤t_uuid, attribute);
+ if (find_attribute (attribute_names, attribute_values,
+ "format", &attribute))
+ openvas_append_string (¤t_format, attribute);
+ if (find_attribute (attribute_names, attribute_values,
+ "sort_field", &attribute))
+ openvas_append_string (¤t_name, attribute);
+ if (find_attribute (attribute_names, attribute_values,
+ "sort_order", &attribute))
+ current_int_2 = strcmp (attribute, "descending");
+ else
+ current_int_2 = 1;
+ set_client_state (CLIENT_GET_AGENTS);
+ }
else if (strcasecmp ("GET_CERTIFICATES", element_name) == 0)
set_client_state (CLIENT_GET_CERTIFICATES);
else if (strcasecmp ("GET_CONFIGS", element_name) == 0)
@@ -1225,6 +1262,14 @@
openvas_append_string (¤t_uuid, attribute);
set_client_state (CLIENT_MODIFY_TASK);
}
+ else if (strcasecmp ("CREATE_AGENT", element_name) == 0)
+ {
+ assert (modify_task_comment == NULL);
+ assert (modify_task_name == NULL);
+ openvas_append_string (&modify_task_comment, "");
+ openvas_append_string (&modify_task_name, "");
+ set_client_state (CLIENT_CREATE_AGENT);
+ }
else if (strcasecmp ("CREATE_CONFIG", element_name) == 0)
{
assert (modify_task_comment == NULL);
@@ -1345,6 +1390,25 @@
}
break;
+ case CLIENT_DELETE_AGENT:
+ if (strcasecmp ("NAME", element_name) == 0)
+ set_client_state (CLIENT_DELETE_AGENT_NAME);
+ else
+ {
+ if (send_element_error_to_client ("delete_agent",
+ element_name))
+ {
+ error_send_to_client (error);
+ return;
+ }
+ set_client_state (CLIENT_AUTHENTIC);
+ g_set_error (error,
+ G_MARKUP_ERROR,
+ G_MARKUP_ERROR_UNKNOWN_ELEMENT,
+ "Error");
+ }
+ break;
+
case CLIENT_DELETE_CONFIG:
if (strcasecmp ("NAME", element_name) == 0)
set_client_state (CLIENT_DELETE_CONFIG_NAME);
@@ -1426,6 +1490,22 @@
"Error");
break;
+ case CLIENT_GET_AGENTS:
+ {
+ if (send_element_error_to_client ("get_agents",
+ element_name))
+ {
+ error_send_to_client (error);
+ return;
+ }
+ set_client_state (CLIENT_AUTHENTIC);
+ g_set_error (error,
+ G_MARKUP_ERROR,
+ G_MARKUP_ERROR_UNKNOWN_ELEMENT,
+ "Error");
+ }
+ break;
+
case CLIENT_GET_CERTIFICATES:
{
if (send_element_error_to_client ("get_certificates", element_name))
@@ -1854,6 +1934,27 @@
}
break;
+ case CLIENT_CREATE_AGENT:
+ if (strcasecmp ("COMMENT", element_name) == 0)
+ set_client_state (CLIENT_CREATE_AGENT_COMMENT);
+ else if (strcasecmp ("NAME", element_name) == 0)
+ set_client_state (CLIENT_CREATE_AGENT_NAME);
+ else
+ {
+ if (send_element_error_to_client ("create_agent",
+ element_name))
+ {
+ error_send_to_client (error);
+ return;
+ }
+ set_client_state (CLIENT_AUTHENTIC);
+ g_set_error (error,
+ G_MARKUP_ERROR,
+ G_MARKUP_ERROR_UNKNOWN_ELEMENT,
+ "Error");
+ }
+ break;
+
case CLIENT_CREATE_CONFIG:
if (strcasecmp ("COMMENT", element_name) == 0)
set_client_state (CLIENT_CREATE_CONFIG_COMMENT);
@@ -4453,6 +4554,44 @@
set_client_state (CLIENT_AUTHENTIC);
break;
+ case CLIENT_DELETE_AGENT:
+ {
+ assert (strcasecmp ("DELETE_AGENT", element_name) == 0);
+ assert (modify_task_name != NULL);
+
+ if (strlen (modify_task_name) == 0)
+ {
+ openvas_free_string_var (&modify_task_name);
+ SEND_TO_CLIENT_OR_FAIL
+ (XML_ERROR_SYNTAX ("delete_agent",
+ "DELETE_AGENT name must be at least"
+ " one character long"));
+ }
+ else switch (delete_agent (modify_task_name))
+ {
+ case 0:
+ openvas_free_string_var (&modify_task_name);
+ SEND_TO_CLIENT_OR_FAIL (XML_OK ("delete_agent"));
+ break;
+ case 1:
+ openvas_free_string_var (&modify_task_name);
+ SEND_TO_CLIENT_OR_FAIL
+ (XML_ERROR_SYNTAX ("delete_agent",
+ "Agent is in use"));
+ break;
+ default:
+ openvas_free_string_var (&modify_task_name);
+ SEND_TO_CLIENT_OR_FAIL
+ (XML_INTERNAL_ERROR ("delete_agent"));
+ }
+ set_client_state (CLIENT_AUTHENTIC);
+ break;
+ }
+ case CLIENT_DELETE_AGENT_NAME:
+ assert (strcasecmp ("NAME", element_name) == 0);
+ set_client_state (CLIENT_DELETE_AGENT);
+ break;
+
case CLIENT_DELETE_CONFIG:
{
assert (strcasecmp ("DELETE_CONFIG", element_name) == 0);
@@ -5108,6 +5247,56 @@
set_client_state (CLIENT_MODIFY_TASK);
break;
+ case CLIENT_CREATE_AGENT:
+ {
+ assert (strcasecmp ("CREATE_AGENT", element_name) == 0);
+ assert (modify_task_name != NULL);
+
+ if (strlen (modify_task_name) == 0)
+ {
+ SEND_TO_CLIENT_OR_FAIL
+ (XML_ERROR_SYNTAX ("create_agent",
+ "CREATE_AGENT name must be at"
+ " least one character long"));
+ }
+ else switch (create_agent (modify_task_name,
+ modify_task_comment))
+ {
+ case 0:
+ SEND_TO_CLIENT_OR_FAIL (XML_OK_CREATED ("create_agent"));
+ break;
+ case 1:
+ SEND_TO_CLIENT_OR_FAIL
+ (XML_ERROR_SYNTAX ("create_agent",
+ "Agent exists already"));
+ break;
+ case 2:
+ SEND_TO_CLIENT_OR_FAIL
+ (XML_ERROR_SYNTAX ("create_agent",
+ "Name may only contain alphanumeric"
+ " characters"));
+ break;
+ default:
+ assert (0);
+ case -1:
+ SEND_TO_CLIENT_OR_FAIL
+ (XML_INTERNAL_ERROR ("create_agent"));
+ break;
+ }
+ openvas_free_string_var (&modify_task_comment);
+ openvas_free_string_var (&modify_task_name);
+ set_client_state (CLIENT_AUTHENTIC);
+ break;
+ }
+ case CLIENT_CREATE_AGENT_COMMENT:
+ assert (strcasecmp ("COMMENT", element_name) == 0);
+ set_client_state (CLIENT_CREATE_AGENT);
+ break;
+ case CLIENT_CREATE_AGENT_NAME:
+ assert (strcasecmp ("NAME", element_name) == 0);
+ set_client_state (CLIENT_CREATE_AGENT);
+ break;
+
case CLIENT_CREATE_CONFIG:
{
assert (strcasecmp ("CREATE_CONFIG", element_name) == 0);
@@ -5238,7 +5427,7 @@
{
SEND_TO_CLIENT_OR_FAIL
(XML_ERROR_SYNTAX ("create_lsc_credential",
- "CREATE_LSC_CREDENTIAL name must both be at"
+ "CREATE_LSC_CREDENTIAL name must be at"
" least one character long"));
}
else if (strlen (current_name) == 0)
@@ -6336,6 +6525,107 @@
set_client_state (CLIENT_AUTHENTIC);
break;
+ case CLIENT_GET_AGENTS:
+ {
+ iterator_t targets;
+ int format;
+ assert (strcasecmp ("GET_AGENTS", element_name) == 0);
+
+ if (current_format)
+ {
+ if (strlen (current_format))
+ {
+ if (strcasecmp (current_format, "installer") == 0)
+ format = 1;
+ else if (strcasecmp (current_format, "howto_install") == 0)
+ format = 2;
+ else if (strcasecmp (current_format, "howto_use") == 0)
+ format = 3;
+ else
+ format = -1;
+ }
+ else
+ format = 0;
+ openvas_free_string_var (¤t_format);
+ }
+ else
+ format = 0;
+ if (format == -1)
+ SEND_TO_CLIENT_OR_FAIL
+ (XML_ERROR_SYNTAX ("get_agents",
+ "GET_AGENTS format attribute should"
+ " be \"installer\", \"howto_install\" or \"howto_use\"."));
+ else
+ {
+ SEND_TO_CLIENT_OR_FAIL ("<get_agents_response"
+ " status=\"" STATUS_OK "\""
+ " status_text=\"" STATUS_OK_TEXT "\">");
+ init_agent_iterator (&targets,
+ current_uuid,
+ /* Attribute sort_order. */
+ current_int_2,
+ /* Attribute sort_field. */
+ current_name);
+ while (next (&targets))
+ {
+ switch (format)
+ {
+ case 1: /* installer */
+ SENDF_TO_CLIENT_OR_FAIL
+ ("<agent>"
+ "<name>%s</name>"
+ "<comment>%s</comment>"
+ "<package format=\"installer\">%s</package>"
+ "<in_use>0</in_use>"
+ "</agent>",
+ agent_iterator_name (&targets),
+ agent_iterator_comment (&targets),
+ agent_iterator_installer (&targets));
+ break;
+ case 2: /* howto_install */
+ SENDF_TO_CLIENT_OR_FAIL
+ ("<agent>"
+ "<name>%s</name>"
+ "<comment>%s</comment>"
+ "<package format=\"howto_install\">%s</package>"
+ "<in_use>0</in_use>"
+ "</agent>",
+ agent_iterator_name (&targets),
+ agent_iterator_comment (&targets),
+ agent_iterator_howto_install (&targets));
+ break;
+ case 3: /* howto_use */
+ SENDF_TO_CLIENT_OR_FAIL
+ ("<agent>"
+ "<name>%s</name>"
+ "<comment>%s</comment>"
+ "<package format=\"howto_use\">%s</package>"
+ "<in_use>0</in_use>"
+ "</agent>",
+ agent_iterator_name (&targets),
+ agent_iterator_comment (&targets),
+ agent_iterator_howto_use (&targets));
+ break;
+ default:
+ SENDF_TO_CLIENT_OR_FAIL
+ ("<agent>"
+ "<name>%s</name>"
+ "<comment>%s</comment>"
+ "<in_use>0</in_use>"
+ "</agent>",
+ agent_iterator_name (&targets),
+ agent_iterator_comment (&targets));
+ break;
+ }
+ }
+ cleanup_iterator (&targets);
+ SEND_TO_CLIENT_OR_FAIL ("</get_agents_response>");
+ }
+ openvas_free_string_var (¤t_name);
+ set_client_state (CLIENT_AUTHENTIC);
+ break;
+ }
+
case CLIENT_GET_CONFIGS:
{
iterator_t configs;
@@ -6819,6 +7109,13 @@
append_to_credentials_password (¤t_credentials, text, text_len);
break;
+ case CLIENT_CREATE_AGENT_COMMENT:
+ openvas_append_text (&modify_task_comment, text, text_len);
+ break;
+ case CLIENT_CREATE_AGENT_NAME:
+ openvas_append_text (&modify_task_name, text, text_len);
+ break;
+
case CLIENT_CREATE_CONFIG_COMMENT:
openvas_append_text (&modify_task_comment, text, text_len);
break;
@@ -6878,6 +7175,7 @@
append_to_task_target (current_client_task, text, text_len);
break;
+ case CLIENT_DELETE_AGENT_NAME:
case CLIENT_DELETE_CONFIG_NAME:
case CLIENT_DELETE_LSC_CREDENTIAL_NAME:
case CLIENT_DELETE_TARGET_NAME:
Modified: trunk/openvas-manager/src/tasks_sql.h
===================================================================
--- trunk/openvas-manager/src/tasks_sql.h 2009-12-18 10:58:28 UTC (rev 6185)
+++ trunk/openvas-manager/src/tasks_sql.h 2009-12-18 11:52:40 UTC (rev 6186)
@@ -62,6 +62,7 @@
/* Types. */
typedef long long int config_t;
+typedef long long int agent_t;
/* Static headers. */
@@ -457,6 +458,7 @@
sql ("CREATE TABLE IF NOT EXISTS config_preferences (id INTEGER PRIMARY KEY, config INTEGER, type, name, value);");
sql ("CREATE TABLE IF NOT EXISTS configs (id INTEGER PRIMARY KEY, name UNIQUE, nvt_selector, comment, family_count INTEGER, nvt_count INTEGER, families_growing INTEGER, nvts_growing INTEGER);");
sql ("CREATE TABLE IF NOT EXISTS lsc_credentials (id INTEGER PRIMARY KEY, name, login, password, comment, public_key TEXT, private_key TEXT, rpm TEXT, deb TEXT, exe TEXT);");
+ sql ("CREATE TABLE IF NOT EXISTS agents (id INTEGER PRIMARY KEY, name UNIQUE, comment, installer TEXT, howto_install TEXT, howto_use TEXT);");
sql ("CREATE TABLE IF NOT EXISTS meta (id INTEGER PRIMARY KEY, name UNIQUE, value);");
sql ("CREATE TABLE IF NOT EXISTS nvt_preferences (id INTEGER PRIMARY KEY, name, value);");
/* nvt_selectors types: 0 all, 1 family, 2 NVT (NVT_SELECTOR_TYPE_* above). */
@@ -8503,4 +8505,277 @@
lsc_credential);
}
+/**
+ * @brief Create an agent entry.
+ *
+ * @param[in] name Name of agent. Must be at least one character long.
+ * @param[in] comment Comment on agent.
+ *
+ * @return 0 success, 1 agent exists already, -1 error.
+ */
+int
+create_agent (const char* name, const char* comment)
+{
+ gchar *quoted_name = sql_nquote (name, strlen (name));
+ gchar *quoted_comment, *base64;
+ void *installer = NULL, *howto_install = NULL, *howto_use = NULL;
+ gsize installer_size, howto_install_size, howto_use_size;
+
+ assert (strlen (name) > 0);
+
+ sql ("BEGIN IMMEDIATE;");
+
+ if (sql_int (0, 0, "SELECT COUNT(*) FROM agents WHERE name = '%s';",
+ quoted_name))
+ {
+ g_free (quoted_name);
+ sql ("END;");
+ return 1;
+ }
+
+ /* Insert the packages. */
+
+ {
+ const char* tail;
+ int ret;
+ sqlite3_stmt* stmt;
+ gchar* formatted;
+
+ if (comment)
+ {
+ quoted_comment = sql_nquote (comment, strlen (comment));
+ formatted = g_strdup_printf ("INSERT INTO agents"
+ " (name, comment, installer,"
+ " howto_install, howto_use)"
+ " VALUES"
+ " ('%s', '%s',"
+ " $installer, $howto_install,"
+ " $howto_use);",
+ quoted_name,
+ quoted_comment);
+ g_free (quoted_comment);
+ }
+ else
+ {
+ formatted = g_strdup_printf ("INSERT INTO agents"
+ " (name, comment, installer,"
+ " howto_install, howto_use)"
+ " VALUES"
+ " ('%s', '',"
+ " $installer, $howto_install,"
+ " $howto_use);",
+ quoted_name);
+ }
+
+ g_free (quoted_name);
+
+ tracef (" sql: %s\n", formatted);
+
+ /* Prepare statement. */
+
+ while (1)
+ {
+ ret = sqlite3_prepare (task_db, (char*) formatted, -1, &stmt, &tail);
+ if (ret == SQLITE_BUSY) continue;
+ g_free (formatted);
+ if (ret == SQLITE_OK)
+ {
+ if (stmt == NULL)
+ {
+ g_warning ("%s: sqlite3_prepare failed with NULL stmt: %s\n",
+ __FUNCTION__,
+ sqlite3_errmsg (task_db));
+ sql ("END;");
+ g_free (installer);
+ g_free (howto_install);
+ g_free (howto_use);
+ return -1;
+ }
+ break;
+ }
+ g_warning ("%s: sqlite3_prepare failed: %s\n",
+ __FUNCTION__,
+ sqlite3_errmsg (task_db));
+ sql ("END;");
+ g_free (installer);
+ g_free (howto_install);
+ g_free (howto_use);
+ return -1;
+ }
+
+ /* Bind the packages to the "$values" in the SQL statement. */
+
+ base64 = (installer && strlen (installer))
+ ? g_base64_encode (installer, installer_size)
+ : g_strdup ("");
+ g_free (installer);
+ while (1)
+ {
+ ret = sqlite3_bind_text (stmt,
+ 1,
+ base64,
+ strlen (base64),
+ SQLITE_TRANSIENT);
+ if (ret == SQLITE_BUSY) continue;
+ if (ret == SQLITE_OK) break;
+ g_warning ("%s: sqlite3_prepare failed: %s\n",
+ __FUNCTION__,
+ sqlite3_errmsg (task_db));
+ sql ("END;");
+ g_free (base64);
+ g_free (howto_install);
+ g_free (howto_use);
+ return -1;
+ }
+ g_free (base64);
+
+ base64 = (howto_install && strlen (howto_install))
+ ? g_base64_encode (howto_install, howto_install_size)
+ : g_strdup ("");
+ g_free (howto_install);
+ while (1)
+ {
+ ret = sqlite3_bind_text (stmt,
+ 2,
+ base64,
+ strlen (base64),
+ SQLITE_TRANSIENT);
+ if (ret == SQLITE_BUSY) continue;
+ if (ret == SQLITE_OK) break;
+ g_warning ("%s: sqlite3_prepare failed: %s\n",
+ __FUNCTION__,
+ sqlite3_errmsg (task_db));
+ sql ("END;");
+ g_free (base64);
+ g_free (howto_use);
+ return -1;
+ }
+ g_free (base64);
+
+ base64 = (howto_use && strlen (howto_use))
+ ? g_base64_encode (howto_use, howto_use_size)
+ : g_strdup ("");
+ g_free (howto_use);
+ while (1)
+ {
+ ret = sqlite3_bind_blob (stmt,
+ 3,
+ base64,
+ strlen (base64),
+ SQLITE_TRANSIENT);
+ if (ret == SQLITE_BUSY) continue;
+ if (ret == SQLITE_OK) break;
+ g_warning ("%s: sqlite3_prepare failed: %s\n",
+ __FUNCTION__,
+ sqlite3_errmsg (task_db));
+ sql ("END;");
+ g_free (base64);
+ return -1;
+ }
+ g_free (base64);
+
+ /* Run the statement. */
+
+ while (1)
+ {
+ ret = sqlite3_step (stmt);
+ if (ret == SQLITE_BUSY) continue;
+ if (ret == SQLITE_DONE) break;
+ if (ret == SQLITE_ERROR || ret == SQLITE_MISUSE)
+ {
+ if (ret == SQLITE_ERROR) ret = sqlite3_reset (stmt);
+ g_warning ("%s: sqlite3_step failed: %s\n",
+ __FUNCTION__,
+ sqlite3_errmsg (task_db));
+ sql ("END;");
+ return -1;
+ }
+ }
+
+ sqlite3_finalize (stmt);
+ }
+
+ sql ("COMMIT;");
+
+ return 0;
+}
+
+/**
+ * @brief Delete an agent.
+ *
+ * @param[in] name Name of agent.
+ *
+ * @return 0 success, -1 error.
+ */
+int
+delete_agent (const char* name)
+{
+ gchar* quoted_name = sql_quote (name);
+ sql ("BEGIN IMMEDIATE;");
+
+ sql ("DELETE FROM agents WHERE name = '%s';", quoted_name);
+ sql ("COMMIT;");
+ g_free (quoted_name);
+ return 0;
+}
+
+/**
+ * @brief Initialise an agent iterator.
+ *
+ * @param[in] iterator Iterator.
+ * @param[in] name Name of single agent to iterate, NULL for all.
+ * @param[in] ascending Whether to sort ascending or descending.
+ * @param[in] sort_field Field to sort on, or NULL for "ROWID".
+ */
+void
+init_agent_iterator (iterator_t* iterator, const char *name,
+ int ascending, const char* sort_field)
+{
+ if (name && strlen (name))
+ {
+ gchar *quoted_name = sql_quote (name);
+ init_iterator (iterator,
+ "SELECT name, comment, installer,"
+ " howto_install, howto_use,"
+ " FROM agents"
+ " WHERE name = '%s'"
+ " ORDER BY %s %s;",
+ quoted_name,
+ sort_field ? sort_field : "ROWID",
+ ascending ? "ASC" : "DESC");
+ g_free (quoted_name);
+ }
+ else
+ init_iterator (iterator,
+ "SELECT name, comment, installer,"
+ " howto_install, howto_use"
+ " FROM agents"
+ " ORDER BY %s %s;",
+ sort_field ? sort_field : "ROWID",
+ ascending ? "ASC" : "DESC");
+}
+
+DEF_ACCESS (agent_iterator_name, 0);
+
+const char*
+agent_iterator_comment (iterator_t* iterator)
+{
+ const char *ret;
+ if (iterator->done) return "";
+ ret = (const char*) sqlite3_column_text (iterator->stmt, 1);
+ return ret ? ret : "";
+}
+
+DEF_ACCESS (agent_iterator_installer, 2);
+DEF_ACCESS (agent_iterator_howto_install, 3);
+DEF_ACCESS (agent_iterator_howto_use, 4);
+
+char*
+agent_name (agent_t agent)
+{
+ return sql_string (0, 0,
+ "SELECT name FROM agents WHERE ROWID = %llu;",
+ agent);
+}
+
#undef DEF_ACCESS
More information about the Openvas-commits
mailing list