18#include "../util/serverutils.h"
28#define G_LOG_DOMAIN "libgvm gmp"
30#define GMP_FMT_BOOL_ATTRIB(var, attrib) \
31 (var.attrib == 0 ? " " #attrib "=\"0\"" : " " #attrib "=\"1\"")
33#define GMP_FMT_STRING_ATTRIB(var, attrib) \
34 (var.attrib ? " " #attrib "= \"" : ""), (var.attrib ? var.attrib : ""), \
35 (var.attrib ? "\"" : "")
88 if (strlen (status) == 0)
97 ret = (int) strtol (status, NULL, 10);
135 if (strlen (status) == 0)
140 if (status[0] ==
'2')
145 ret = (int) strtol (status, NULL, 10);
220 if (strlen (status) == 0)
251 if (version && *version)
281 if (strlen (status) == 0)
286 if (status[0] ==
'2')
292 if (omp_version == NULL)
318 const char *password)
325 "<authenticate><credentials>"
326 "<username>%s</username>"
327 "<password>%s</password>"
328 "</credentials></authenticate>",
329 username ? username :
"",
330 password ? password :
"");
373 "<authenticate><credentials>"
374 "<username>%s</username>"
375 "<password>%s</password>"
376 "</credentials></authenticate>",
402 if (strlen (status) == 0)
410 entity_t timezone_entity, role_entity, pw_warn_entity;
418 pw_warn_entity =
entity_child (entity,
"password_warning");
457 "<username>%s</username>"
458 "<password>%s</password>"
486 if (strlen (status) == 0)
494 entity_t timezone_entity, role_entity;
497 if (timezone_entity && opts.
timezone)
500 if (role_entity && opts.
role)
505 pw_warn_entity =
entity_child (entity,
"password_warning");
538 gchar *prefs, *start, *hosts_ordering, *scanner, *schedule, *slave;
539 GString *alerts, *observers;
545 start = g_markup_printf_escaped (
547 "<config id=\"%s\"/>"
548 "<target id=\"%s\"/>"
550 "<comment>%s</comment>"
551 "<alterable>%d</alterable>",
556 hosts_ordering = g_strdup_printf (
"<hosts_ordering>%s</hosts_ordering>",
559 hosts_ordering = NULL;
562 scanner = g_strdup_printf (
"<scanner id=\"%s\"/>", opts.
scanner_id);
567 schedule = g_strdup_printf (
"<schedule id=\"%s\"/>"
568 "<schedule_periods>%d</schedule_periods>",
574 slave = g_strdup_printf (
"<slave id=\"%s\"/>", opts.
slave_id);
580 gchar *in_assets, *checks, *hosts, *source_iface;
582 in_assets = checks = hosts = source_iface = NULL;
585 in_assets = g_markup_printf_escaped (
"<preference>"
596 hosts = g_markup_printf_escaped (
"<preference>"
607 checks = g_markup_printf_escaped (
"<preference>"
618 source_iface = g_markup_printf_escaped (
"<preference>"
629 g_strdup_printf (
"<preferences>%s%s%s%s</preferences>",
630 in_assets ? in_assets :
"", checks ? checks :
"",
631 hosts ? hosts :
"", source_iface ? source_iface :
"");
635 g_free (source_iface);
641 alerts = g_string_new (
"");
642 for (i = 0; i < opts.
alert_ids->len; i++)
644 char *alert = (
char *) g_ptr_array_index (opts.
alert_ids, i);
645 g_string_append_printf (alerts,
"<alert id=\"%s\"/>", alert);
649 alerts = g_string_new (
"");
653 observers = g_string_new (
"<observers>");
656 g_string_append (observers, opts.
observers);
665 g_string_append_printf (observers,
"<group id=\"%s\"/>", group);
668 g_string_append (observers,
"</observers>");
671 observers = g_string_new (
"");
675 session,
"%s%s%s%s%s%s%s%s</create_task>", start, prefs ? prefs :
"",
676 hosts_ordering ? hosts_ordering :
"", scanner ? scanner :
"",
677 schedule ? schedule :
"", slave ? slave :
"", alerts ? alerts->str :
"",
678 observers ? observers->str :
"");
681 g_free (hosts_ordering);
685 g_string_free (alerts, TRUE);
686 g_string_free (observers, TRUE);
714 const char *config,
const char *target,
const char *comment,
721 "<config id=\"%s\"/>"
722 "<target id=\"%s\"/>"
724 "<comment>%s</comment>"
726 config, target, name, comment);
753 if (
gvm_server_sendf (session,
"<start_task task_id=\"%s\"/>", task_id) == -1)
767 *report_id = g_strdup (
entity_text (report_id_xml));
818 if (strlen (status) == 0)
830 *report_id = g_strdup (
entity_text (report_id_xml));
904 if (strlen (status) == 0)
920 if (strlen (
id) == 0)
925 *uuid = g_strdup (
id);
1005 *report_id = g_strdup (
entity_text (report_id_xml));
1052 if (strlen (status) == 0)
1057 char first = status[0];
1064 *report_id = g_strdup (
entity_text (report_id_xml));
1095 "<delete_task task_id=\"%s\" ultimate=\"%d\"/>",
id,
1121 int include_rcfile,
entity_t *status)
1123 (void) include_rcfile;
1135 " details=\"%i\"/>",
1158 if ((response == NULL) || (opts.
task_id == NULL))
1196 const char *status_code;
1199 if (response == NULL)
1202 cmd = g_markup_printf_escaped (
"<get_tasks"
1228 if (status_code == NULL)
1233 if (strlen (status_code) == 0)
1238 if (status_code[0] ==
'2')
1240 ret = (int) strtol (status_code, NULL, 10);
1242 if (errno == ERANGE)
1260 const char *name,
const void *content, gsize content_len)
1279 gchar *base64_content =
1280 g_base64_encode ((guchar *) content, content_len);
1282 g_free (base64_content);
1345 int include_rcfile,
entity_t *target)
1347 (void) include_rcfile;
1386 const char *status_code;
1388 if (response == NULL)
1397 " host_first_result=\"%i\""
1398 " host_max_results=\"%i\""
1451 if (status_code == NULL)
1456 if (strlen (status_code) == 0)
1461 if (status_code[0] ==
'2')
1463 ret = (int) strtol (status_code, NULL, 10);
1465 if (errno == ERANGE)
1487 session,
"<delete_port_list port_list_id=\"%s\" ultimate=\"%d\"/>",
id,
1541 gchar *comment, *ssh, *smb, *esxi, *snmp, *port_range, *start;
1542 gchar *exclude_hosts, *alive_tests;
1547 if (opts.
hosts == NULL)
1551 g_markup_printf_escaped (
"<create_target>"
1553 "<hosts>%s</hosts>",
1557 exclude_hosts = g_markup_printf_escaped (
"<exclude_hosts>"
1562 exclude_hosts = NULL;
1565 alive_tests = g_markup_printf_escaped (
"<alive_tests>"
1573 comment = g_markup_printf_escaped (
"<comment>"
1583 ssh = g_markup_printf_escaped (
"<ssh_lsc_credential id=\"%s\">"
1585 "</ssh_lsc_credential>",
1589 ssh = g_markup_printf_escaped (
"<ssh_lsc_credential id=\"%s\"/>",
1596 smb = g_markup_printf_escaped (
"<smb_lsc_credential id=\"%s\"/>",
1602 esxi = g_markup_printf_escaped (
"<esxi_lsc_credential id=\"%s\"/>",
1608 snmp = g_markup_printf_escaped (
"<snmp_credential id=\"%s\"/>",
1615 g_markup_printf_escaped (
"<port_range>%s</port_range>", opts.
port_range);
1621 "%s%s%s%s%s%s%s%s%s"
1622 "<reverse_lookup_only>%d</reverse_lookup_only>"
1623 "<reverse_lookup_unify>%d</reverse_lookup_unify>"
1625 start, exclude_hosts ? exclude_hosts :
"",
1626 alive_tests ? alive_tests :
"", ssh ? ssh :
"",
1627 smb ? smb :
"", esxi ? esxi :
"", snmp ? snmp :
"",
1628 port_range ? port_range :
"", comment ? comment :
"",
1631 g_free (exclude_hosts);
1632 g_free (alive_tests);
1636 g_free (port_range);
1666 "<delete_target target_id=\"%s\" ultimate=\"%d\"/>",
id,
1695 "<delete_config config_id=\"%s\" ultimate=\"%d\"/>",
id,
1721 const char *login,
const char *password,
1722 const char *comment, gchar **uuid)
1730 "<create_credential>"
1733 "<password>%s</password>"
1734 "<comment>%s</comment>"
1735 "</create_credential>",
1736 name, login, password, comment);
1739 "<create_credential>"
1742 "<password>%s</password>"
1743 "</create_credential>",
1744 name, login, password);
1750 "<create_credential>"
1753 "<comment>%s</comment>"
1754 "</create_credential>",
1755 name, login, comment);
1758 "<create_credential>"
1761 "</create_credential>",
1789 const char *login,
const char *passphrase,
1790 const char *private_key,
const char *comment,
1797 "<create_credential>"
1801 "<phrase>%s</phrase>"
1802 "<private>%s</private>"
1804 "<comment>%s</comment>"
1805 "</create_credential>",
1806 name, login, passphrase ? passphrase :
"",
1807 private_key, comment);
1810 "<create_credential>"
1814 "<phrase>%s</phrase>"
1815 "<private>%s</private>"
1817 "</create_credential>",
1818 name, login, passphrase ? passphrase :
"",
1845 gchar *comment, *pass, *start, *snmp_elems;
1850 if (opts.
login == NULL)
1854 g_markup_printf_escaped (
"<create_credential>"
1856 "<login>%s</login>",
1860 comment = g_markup_printf_escaped (
"<comment>"
1868 pass = g_markup_printf_escaped (
"<key>"
1869 "<phrase>%s</phrase>"
1870 "<private>%s</private>"
1877 pass = g_markup_printf_escaped (
"<password>"
1888 g_markup_printf_escaped (
"<community>"
1895 "<password>%s</password>"
1896 "<algorithm>%s</algorithm>"
1906 comment ? comment :
"", pass ? pass :
"",
1907 snmp_elems ? snmp_elems :
"");
1940 "<delete_credential credential_id=\"%s\""
1941 " ultimate=\"%d\"/>",
1971 "<get_system_reports name=\"%s\" brief=\"%i\"/>",
2002 request = g_string_new (
"<get_system_reports");
2019 g_string_append (request,
"/>");
2025 g_string_free (request, 1);
2028 g_string_free (request, 1);
int gmp_get_system_reports(gnutls_session_t *session, const char *name, int brief, entity_t *reports)
Get system reports.
int gmp_start_task_report(gnutls_session_t *session, const char *task_id, char **report_id)
Start a task and read the manager response.
int gmp_ping_c(gvm_connection_t *connection, int timeout, gchar **version)
"Ping" the manager.
#define GMP_FMT_BOOL_ATTRIB(var, attrib)
int gmp_delete_target_ext(gnutls_session_t *session, const char *id, gmp_delete_opts_t opts)
Delete a target.
int gmp_get_tasks_ext(gnutls_session_t *session, gmp_get_tasks_opts_t opts, entity_t *response)
Get all tasks (generic version).
const char * gmp_task_status(entity_t response)
Get the task status from a GMP GET_TASKS response.
int gmp_get_task_ext(gnutls_session_t *session, gmp_get_task_opts_t opts, entity_t *response)
Get a task (generic version).
int gmp_create_lsc_credential_ext(gnutls_session_t *session, gmp_create_lsc_credential_opts_t opts, gchar **id)
Create an LSC credential.
int gmp_create_target_ext(gnutls_session_t *session, gmp_create_target_opts_t opts, gchar **id)
Create a target.
int gmp_ping(gnutls_session_t *session, int timeout)
"Ping" the manager.
int gmp_create_task_ext(gnutls_session_t *session, gmp_create_task_opts_t opts, gchar **id)
Create a task.
int gmp_create_lsc_credential(gnutls_session_t *session, const char *name, const char *login, const char *password, const char *comment, gchar **uuid)
Create an LSC Credential.
int gmp_delete_report(gnutls_session_t *session, const char *id)
Remove a report.
int gmp_authenticate(gnutls_session_t *session, const char *username, const char *password)
Authenticate with the manager.
int gmp_stop_task_c(gvm_connection_t *connection, const char *id)
Stop a task and read the manager response.
int gmp_resume_task_report(gnutls_session_t *session, const char *task_id, char **report_id)
Resume a task and read the manager response.
int gmp_create_task(gnutls_session_t *session, const char *name, const char *config, const char *target, const char *comment, gchar **id)
Create a task given a config and target.
int gmp_modify_task_file(gnutls_session_t *session, const char *id, const char *name, const void *content, gsize content_len)
Modify a file on a task.
int gmp_start_task_report_c(gvm_connection_t *connection, const char *task_id, char **report_id)
Start a task and read the manager response.
static int gmp_check_response_c(gvm_connection_t *connection)
Read response and convert status of response to a return value.
int gmp_delete_task(gnutls_session_t *session, const char *id)
Delete a task and read the manager response.
int gmp_delete_port_list_ext(gnutls_session_t *session, const char *id, gmp_delete_opts_t opts)
Delete a port list.
static int check_response_c(gvm_connection_t *connection, int convert_99)
Read response and convert status of response to a return value.
int gmp_authenticate_info_ext_c(gvm_connection_t *connection, gmp_authenticate_info_opts_t opts)
Authenticate with the manager.
int gmp_delete_task_ext(gnutls_session_t *session, const char *id, gmp_delete_opts_t opts)
Delete a task and read the manager response.
int gmp_resume_task_report_c(gvm_connection_t *connection, const char *task_id, char **report_id)
Resume a task and read the manager response.
int gmp_delete_config_ext(gnutls_session_t *session, const char *id, gmp_delete_opts_t opts)
Delete a config.
int gmp_read_create_response(gnutls_session_t *session, gchar **uuid)
Read response status and resource UUID.
int gmp_get_report_ext(gnutls_session_t *session, gmp_get_report_opts_t opts, entity_t *response)
Get a report (generic version).
int gmp_delete_lsc_credential_ext(gnutls_session_t *session, const char *id, gmp_delete_opts_t opts)
Delete a LSC credential.
int gmp_authenticate_info_ext(gnutls_session_t *session, gmp_authenticate_info_opts_t opts)
Authenticate with the manager.
int gmp_start_task_ext_c(gvm_connection_t *connection, gmp_start_task_opts_t opts)
Start a task and read the manager response.
int gmp_get_tasks(gnutls_session_t *session, const char *id, int details, int include_rcfile, entity_t *status)
Get the status of a task.
#define GMP_FMT_STRING_ATTRIB(var, attrib)
int gmp_get_system_reports_ext(gnutls_session_t *session, gmp_get_system_reports_opts_t opts, entity_t *reports)
Get system reports.
int gmp_get_targets(gnutls_session_t *session, const char *id, int tasks, int include_rcfile, entity_t *target)
Get a target.
int gmp_create_lsc_credential_key(gnutls_session_t *session, const char *name, const char *login, const char *passphrase, const char *private_key, const char *comment, gchar **uuid)
Create an LSC Credential with a key.
int gmp_stop_task(gnutls_session_t *session, const char *id)
Stop a task and read the manager response.
static int gmp_check_response(gnutls_session_t *session, entity_t *entity)
Read response and convert status of response to a return value.
API for Greenbone Management Protocol communication.
int gvm_connection_sendf(gvm_connection_t *connection, const char *format,...)
Format and send a string to the server.
int gvm_server_sendf_xml_quiet(gnutls_session_t *session, const char *format,...)
Format and send an XML string to the server.
int gvm_server_sendf_xml(gnutls_session_t *session, const char *format,...)
Format and send an XML string to the server.
int gvm_server_sendf(gnutls_session_t *session, const char *format,...)
Format and send a string to the server.
int gvm_connection_sendf_xml_quiet(gvm_connection_t *connection, const char *format,...)
Format and send an XML string to the server.
Struct holding options for authentication.
const char * password
Username.
int timeout
Timeout for authentication.
char ** pw_warning
[out] Password warning, NULL if password is okay.
char ** timezone
[out] Timezone if any, else NULL.
const char * username
Password.
Struct holding options for gmp create_lsc_credential command.
const char * privacy_password
SNMP privacy password.
const char * privacy_algorithm
SNMP privacy algorithm.
const char * community
SNMP community.
const char * passphrase
Passphrase.
const char * comment
Comment on LSC credential.
const char * private_key
Private key.
const char * auth_algorithm
SNMP authentication algorithm.
const char * name
Name of LSC credential.
Struct holding options for gmp create_target command.
const char * exclude_hosts
Hosts to exclude.
const char * comment
Comment on target.
const char * smb_credential_id
ID of SMB credential.
int reverse_lookup_only
Scanner pref reverse_lookup_only.
int reverse_lookup_unify
Scanner pref reverse_lookup_unify.
const char * esxi_credential_id
ID of ESXi credential.
const char * snmp_credential_id
ID of SNMP credential.
const char * hosts
Name of target.
const char * port_range
Port range.
int ssh_credential_port
Port for SSH access.
const char * alive_tests
Alive tests.
const char * ssh_credential_id
ID of SSH credential.
const char * name
Name of target.
Struct holding options for gmp create_task command.
int schedule_periods
Number of periods the schedule must run for.
const char * max_checks
Max checks preference.
const char * slave_id
ID of task schedule.
array_t * alert_ids
Array of alert IDs.
const char * config_id
ID of config.
const char * in_assets
In assets preference.
const char * schedule_id
ID of task schedule.
const char * source_iface
Source iface preference.
array_t * observer_groups
IDs of observer groups.
const char * observers
Comma-separated string of observer users.
const char * name
Name of task.
const char * target_id
ID of target.
const char * scanner_id
ID of task scanner.
int alterable
Whether the task is alterable.
const char * max_hosts
Max hosts preference.
const char * hosts_ordering
Order for scanning target hosts.
const char * comment
Comment on task.
Struct holding options for various gmp delete_[...] commands.
Struct holding options for gmp get_report command.
int timeout
Timeout for GMP response.
const char * format_id
ID of required report format.
int host_max_results
Maximum number of results to return.
const char * report_id
ID of single report to get.
int host_first_result
Skip over results before this result number.
Struct holding options for gmp get_system_reports command.
const char * duration
Duration.
const char * slave_id
ID of the slave to get report from.
const char * end_time
Time of last data point.
const char * name
Name of report.
const char * start_time
Time of first data point.
Struct holding options for gmp get_tasks command.
const char * task_id
ID of single task to get.
const char * actions
Actions argument.
Struct holding options for gmp get_tasks command.
int timeout
Timeout for GMP response.
const char * filter
Filter argument.
Struct holding options for gmp start_task command.
const char * task_id
ID of task.
int read_entity_c(gvm_connection_t *connection, entity_t *entity)
Read an XML entity tree from the manager.
const char * entity_attribute(entity_t entity, const char *name)
Get an attribute of an entity.
char * entity_text(entity_t entity)
Get the text an entity.
int read_entity(gnutls_session_t *session, entity_t *entity)
Read an XML entity tree from the manager.
int try_read_entity(gnutls_session_t *session, int timeout, entity_t *entity)
Try read an XML entity tree from the manager.
void free_entity(entity_t entity)
Free an entity, recursively.
entity_t entity_child(entity_t entity, const char *name)
Get a child of an entity.
int try_read_entity_c(gvm_connection_t *connection, int timeout, entity_t *entity)
Try read an XML entity tree from the manager.
void xml_string_append(GString *xml, const char *format,...)
Append formatted escaped XML to a string.