Greenbone Vulnerability Management Libraries
11.0.1
|
Go to the documentation of this file.
27 #include "../base/hosts.h"
28 #include "../util/serverutils.h"
31 #include <gnutls/gnutls.h>
36 #include <sys/socket.h>
44 #define G_LOG_DOMAIN "lib osp"
112 __attribute__ ((__format__ (__printf__, 3, 4)));
127 const
char *cert, const
char *key)
131 if (host && *host ==
'/')
133 struct sockaddr_un addr;
136 connection = g_malloc0 (
sizeof (*connection));
137 connection->
socket = socket (AF_UNIX, SOCK_STREAM, 0);
138 if (connection->
socket == -1)
141 addr.sun_family = AF_UNIX;
142 strncpy (addr.sun_path, host, sizeof (addr.sun_path) - 1);
143 len = strlen (addr.sun_path) +
sizeof (addr.sun_family);
144 if (connect (connection->
socket, (
struct sockaddr *) &addr, len) == -1)
146 close (connection->
socket);
152 if (port <= 0 || port > 65535)
156 if (!cert || !key || !cacert)
159 connection = g_malloc0 (
sizeof (*connection));
161 &connection->
session, host, port, cacert, cert, key);
163 if (connection->
socket == -1)
169 connection->
host = g_strdup (host);
170 connection->
port = port;
185 const char *fmt, ...)
192 if (!connection || !fmt || !response)
195 if (*connection->
host ==
'/')
229 if (*connection->
host ==
'/')
230 close (connection->
socket);
233 g_free (connection->
host);
252 char **d_name,
char **d_version,
char **p_name,
265 goto err_get_version;
268 goto err_get_version;
273 goto err_get_version;
279 goto err_get_version;
282 goto err_get_version;
287 goto err_get_version;
293 goto err_get_version;
296 goto err_get_version;
301 goto err_get_version;
309 g_warning (
"Erroneous OSP <get_version/> response.");
348 g_warning (
"%s: element VTS missing.", __FUNCTION__);
356 g_warning (
"%s: element VERSION missing.", __FUNCTION__);
447 if (strcmp (status,
"200"))
476 *error = g_strdup (
"Couldn't send get_performance command "
477 "to scanner. Not valid connection");
484 || opts.
start > now || opts.
end < 0 || opts.
end > now)
487 *error = g_strdup (
"Couldn't send get_performance command "
488 "to scanner. Bad or missing parameters.");
493 "<get_performance start='%d' "
494 "end='%d' titles='%s'/>",
500 *error = g_strdup (
"Couldn't send get_performance command to scanner");
512 *error = g_strdup (text);
541 *error = g_strdup (
"Couldn't send get_scans command "
542 "to scanner. Not valid connection");
548 "<get_scans scan_id='%s'"
550 " pop_results='0'/>",
556 *error = g_strdup (
"Couldn't send get_scans command to scanner");
567 *error = g_strdup (text);
599 char **report_xml,
int details,
int pop_results,
609 *error = g_strdup (
"Couldn't send get_scan command "
610 "to scanner. Not valid connection");
615 "<get_scans scan_id='%s'"
617 " pop_results='%d'/>",
624 *error = g_strdup (
"Couldn't send get_scans command to scanner");
635 *error = g_strdup (text);
644 string = g_string_new (
"");
646 *report_xml = g_string_free (
string, FALSE);
665 char **report_xml,
int details,
char **error)
667 return osp_get_scan_pop (connection, scan_id, report_xml, details, 0, error);
688 *error = g_strdup (
"Couldn't send stop_scan command "
689 "to scanner. Not valid connection");
698 *error = g_strdup (
"Couldn't send stop_scan command to scanner");
714 *error = g_strdup (text);
731 char *options_str, *tmp, *key_escaped, *value_escaped;
733 options_str = *(
char **) pstr;
735 key_escaped = g_markup_escape_text ((
char *) key, -1);
736 value_escaped = g_markup_escape_text ((
char *) value, -1);
737 tmp = g_strdup_printf (
"%s<%s>%s</%s>", options_str ? options_str :
"",
738 key_escaped, value_escaped, key_escaped);
740 g_free (options_str);
741 g_free (key_escaped);
742 g_free (value_escaped);
743 *(
char **) pstr = tmp;
760 const char *ports, GHashTable *options,
const char *scan_id,
764 char *options_str = NULL;
771 *error = g_strdup (
"Couldn't send start_scan command "
772 "to scanner. Not valid connection");
782 "<start_scan target='%s' ports='%s' scan_id='%s'>"
783 "<scanner_params>%s</scanner_params></start_scan>",
784 target, ports ? ports :
"", scan_id ? scan_id :
"",
785 options_str ? options_str :
"");
786 g_free (options_str);
790 *error = g_strdup (
"Couldn't send start_scan command to scanner");
806 *error = g_strdup (text);
824 GHashTableIter auth_data_iter;
825 gchar *auth_data_name, *auth_data_value;
828 "<credential type=\"%s\" service=\"%s\" port=\"%s\">",
829 credential->
type ? credential->
type :
"",
831 credential->
port ? credential->
port :
"");
833 g_hash_table_iter_init (&auth_data_iter, credential->
auth_data);
834 while (g_hash_table_iter_next (&auth_data_iter,
835 (gpointer*)&auth_data_name,
836 (gpointer*)&auth_data_value))
861 "<exclude_hosts>%s</exclude_hosts>"
862 "<finished_hosts>%s</finished_hosts>"
871 g_string_append (xml_string,
"<credentials>");
875 g_string_append (xml_string,
"</credentials>");
891 "<vt_group filter=\"%s\"/>",
907 "<vt_value id=\"%s\">%s</vt_value>",
922 "<vt_single id=\"%s\">",
924 g_hash_table_foreach (vt_single->
vt_values,
945 gchar *scanner_params_xml = NULL;
952 char filename[] =
"/tmp/osp-cmd-XXXXXX";
958 *error = g_strdup (
"Couldn't send start_scan command "
959 "to scanner. Not valid connection");
963 fd = mkstemp (filename);
964 FILE *file = fdopen (fd,
"w");
966 xml = g_string_sized_new (10240);
967 g_string_append (xml,
"<start_scan");
974 g_string_append (xml,
"<targets>");
976 g_string_append (xml,
"</targets>");
978 g_string_append (xml,
"<scanner_params>");
981 scanner_params_xml = NULL;
984 &scanner_params_xml);
985 if (scanner_params_xml)
986 g_string_append (xml, scanner_params_xml);
987 g_free (scanner_params_xml);
989 g_string_append (xml,
"</scanner_params>");
991 g_string_append (xml,
"<vt_selection>");
994 fprintf (file,
"%s", xml->str);
996 g_string_free (xml, TRUE);
998 xml = g_string_new (
"");
999 list_item = opts.
vts;
1006 list_item = list_item->next;
1008 if (list_count == 1000)
1010 fprintf (file,
"%s", xml->str);
1012 g_string_free (xml, TRUE);
1013 xml = g_string_new (
"");
1018 g_string_append (xml,
"</vt_selection>");
1019 g_string_append (xml,
"</start_scan>");
1021 fprintf (file,
"%s", xml->str);
1024 g_string_free (xml, TRUE);
1026 g_file_get_contents (filename, &cmd, NULL, NULL);
1036 *error = g_strdup (
"Could not send start_scan command to scanner");
1052 *error = g_strdup (text);
1074 if (!strcmp (str,
"integer"))
1076 else if (!strcmp (str,
"string"))
1078 else if (!strcmp (str,
"password"))
1080 else if (!strcmp (str,
"file"))
1082 else if (!strcmp (str,
"boolean"))
1084 else if (!strcmp (str,
"ovaldef_file"))
1086 else if (!strcmp (str,
"selection"))
1088 else if (!strcmp (str,
"credential_up"))
1119 return "ovaldef_file";
1123 return "credential_up";
1144 assert (connection);
1161 child = entities->data;
1173 *params = g_slist_append (*params, param);
1285 g_free (param->
name);
1286 g_free (param->
desc);
1287 g_free (param->
def);
1308 new_credential->
type = type ? g_strdup (type) : NULL;
1309 new_credential->
service = service ? g_strdup (service) : NULL;
1310 new_credential->
port = port ? g_strdup (port) : NULL;
1311 new_credential->
auth_data = g_hash_table_new_full (g_str_hash,
1316 return new_credential;
1330 g_free (credential->
type);
1332 g_free (credential->
port);
1333 g_hash_table_destroy (credential->
auth_data);
1334 g_free (credential);
1349 if (credential == NULL || name == NULL)
1351 return g_hash_table_lookup (credential->
auth_data, name);
1366 if (credential == NULL || name == NULL)
1369 if (g_regex_match_simple (
"^[[:alpha:]][[:alnum:]_]*$", name, 0, 0))
1372 g_hash_table_replace (credential->
auth_data,
1376 g_hash_table_remove (credential->
auth_data,
1381 g_warning (
"%s: Invalid auth data name: %s", __FUNCTION__, name);
1397 const char *exclude_hosts)
1402 new_target->
exclude_hosts = exclude_hosts ? g_strdup (exclude_hosts) : NULL;
1403 new_target->
hosts = hosts ? g_strdup (hosts) : NULL;
1404 new_target->
ports = ports ? g_strdup (ports) : NULL;
1418 const char *finished_hosts)
1421 target->
finished_hosts = finished_hosts ? g_strdup (finished_hosts) : NULL;
1438 g_free (target->
hosts);
1439 g_free (target->
ports);
1452 if (!target || !credential)
1471 new_vt_group->
filter = filter ? g_strdup (filter) : NULL;
1473 return new_vt_group;
1487 g_free (vt_group->
filter);
1504 new_vt_single->
vt_id = vt_id ? g_strdup (vt_id) : NULL;
1505 new_vt_single->
vt_values = g_hash_table_new_full (g_str_hash, g_str_equal,
1508 return new_vt_single;
1522 g_hash_table_destroy (vt_single->
vt_values);
1524 g_free (vt_single->
vt_id);
1538 const char *name,
const char *value)
1540 g_hash_table_replace (vt_single->
vt_values,
@ OSP_SCAN_STATUS_FINISHED
int parallel
Number of parallel scans.
static void vt_value_append_as_xml(gpointer id, gchar *value, GString *xml_string)
Append VT values as XML to a string buffer.
osp_param_t * osp_param_new(void)
Create a new OSP parameter.
int osp_get_vts_ext(osp_connection_t *connection, osp_get_vts_opts_t opts, entity_t *vts)
Get filtered set of VTs from an OSP server.
const char * entity_attribute(entity_t entity, const char *name)
Get an attribute of an entity.
osp_scan_status_t
OSP scan status.
entity_t entity_child(entity_t entity, const char *name)
Get a child of an entity.
void osp_vt_single_add_value(osp_vt_single_t *, const char *, const char *)
@ OSP_SCAN_STATUS_RUNNING
@ OSP_PARAM_TYPE_SELECTION
int gvm_server_close(int socket, gnutls_session_t session)
Close a server connection and its socket.
@ OSP_PARAM_TYPE_OVALDEF_FILE
osp_connection_t * osp_connection_new(const char *, int, const char *, const char *, const char *)
API for Open Scanner Protocol communication.
int osp_get_scanner_details(osp_connection_t *connection, char **desc, GSList **params)
Get an OSP scanner's details.
GHashTable * scanner_params
Table of scanner parameters.
int osp_start_scan_ext(osp_connection_t *connection, osp_start_scan_opts_t opts, char **error)
Start an OSP scan against a target.
entities_t entities
Children.
static int osp_send_command(osp_connection_t *, entity_t *, const char *,...)
Send a command to an OSP server.
char * entity_text(entity_t entity)
Get the text an entity.
static void vt_group_append_as_xml(osp_vt_group_t *vt_group, GString *xml_string)
Append VT groups as XML to a string buffer.
void osp_param_free(osp_param_t *param)
Free an OSP parameter.
static void credential_append_as_xml(osp_credential_t *credential, GString *xml_string)
Concatenate a credential as XML.
const char * scan_id
UUID of the scan which get the status from.
const char * scan_id
UUID to set for scan, null otherwise.
const char * osp_param_default(const osp_param_t *param)
Get an OSP parameter's default value.
int read_entity(gnutls_session_t *session, entity_t *entity)
Read an XML entity tree from the manager.
osp_scan_status_t osp_get_scan_status_ext(osp_connection_t *connection, osp_get_scan_status_opts_t opts, char **error)
Get a scan status from an OSP server.
int osp_param_mandatory(const osp_param_t *param)
Get an OSP parameter's mandatory value.
void osp_target_set_finished_hosts(osp_target_t *, const char *)
void free_entity(entity_t entity)
Free an entity, recursively.
@ OSP_PARAM_TYPE_PASSWORD
void osp_vt_group_free(osp_vt_group_t *)
void xml_string_append(GString *xml, const char *format,...)
Append formatted escaped XML to a string.
osp_credential_t * osp_credential_new(const char *type, const char *service, const char *port)
Allocate and initialize a new OSP credential.
char * filter
the filter to apply for a vt sub-selection.
entities_t next_entities(entities_t entities)
Return all the entities from an entities_t after the first.
GSList * vt_groups
VT groups to use for the scan.
void osp_vt_single_free(osp_vt_single_t *)
void osp_target_free(osp_target_t *)
int gvm_get_host_type(const gchar *str_stripped)
Determines the host type in a buffer.
int gvm_server_open_with_cert(gnutls_session_t *session, const char *host, int port, const char *ca_mem, const char *pub_mem, const char *priv_mem)
Connect to the server using a given host, port and cert.
int osp_get_vts(osp_connection_t *connection, entity_t *vts)
Get all VTs from an OSP server.
const char * osp_param_type_str(const osp_param_t *param)
Get an OSP parameter in string format form its type.
static void target_append_as_xml(osp_target_t *target, GString *xml_string)
Concatenate a target as XML.
int osp_delete_scan(osp_connection_t *connection, const char *scan_id)
Delete a scan from an OSP server.
int osp_get_version(osp_connection_t *connection, char **s_name, char **s_version, char **d_name, char **d_version, char **p_name, char **p_version)
Get the scanner version from an OSP server.
static osp_param_type_t osp_param_str_to_type(const char *str)
Get an OSP parameter's type from its string format.
int osp_get_scan_pop(osp_connection_t *connection, const char *scan_id, char **report_xml, int details, int pop_results, char **error)
Get a scan from an OSP server, optionally removing the results.
int gvm_server_vsendf(gnutls_session_t *session, const char *fmt, va_list ap)
Send a string to the server.
Struct holding options for OSP parameters.
const char * osp_param_desc(const osp_param_t *param)
Get an OSP parameter's description.
void osp_connection_close(osp_connection_t *connection)
Close a connection to an OSP server.
void osp_credential_set_auth_data(osp_credential_t *credential, const char *name, const char *value)
Get authentication data from an OSP credential.
void osp_credential_free(osp_credential_t *credential)
Free an OSP credential.
osp_vt_single_t * osp_vt_single_new(const char *)
int osp_get_vts_version(osp_connection_t *connection, char **vts_version)
Get the VTs version from an OSP server.
osp_target_t * osp_target_new(const char *, const char *, const char *)
static void option_concat_as_xml(gpointer key, gpointer value, gpointer pstr)
Concatenate options as xml.
osp_param_type_t
OSP parameter types.
int gvm_socket_vsendf(int socket, const char *fmt, va_list ap)
Send a string to the server.
Struct credential information for OSP.
int read_entity_s(int socket, entity_t *entity)
Read an XML entity tree from the socket.
GSList * targets
Target hosts to scan.
int osp_start_scan(osp_connection_t *connection, const char *target, const char *ports, GHashTable *options, const char *scan_id, char **error)
Start an OSP scan against a target.
const char * osp_param_name(const osp_param_t *param)
Get an OSP parameter's name.
Struct holding vt_group information.
static void vt_single_append_as_xml(osp_vt_single_t *vt_single, GString *xml_string)
Append single VTs as XML to a string buffer.
void print_entity_to_string(entity_t entity, GString *string)
Print an XML entity tree to a GString, appending it if string is not.
Struct holding target information.
@ OSP_SCAN_STATUS_STOPPED
int osp_stop_scan(osp_connection_t *connection, const char *scan_id, char **error)
Stop a scan on an OSP server.
Struct holding options for OSP connection.
const char * osp_param_id(const osp_param_t *param)
Get an OSP parameter's id.
int osp_get_performance_ext(osp_connection_t *connection, osp_get_performance_opts_t opts, char **graph, char **error)
Get performance graphics from an OSP server.
GSList * vts
Single VTs to use for the scan.
osp_vt_group_t * osp_vt_group_new(const char *)
const gchar * osp_credential_get_auth_data(osp_credential_t *credential, const char *name)
Get authentication data from an OSP credential.
Struct holding vt_group information.
void osp_target_add_credential(osp_target_t *, osp_credential_t *)
GSList * entities_t
Entities.
int osp_get_scan(osp_connection_t *connection, const char *scan_id, char **report_xml, int details, char **error)
Get a scan from an OSP server.