Greenbone Vulnerability Management Libraries
11.0.0
|
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"
111 __attribute__ ((__format__ (__printf__, 3, 4)));
126 const
char *cert, const
char *key)
130 if (host && *host ==
'/')
132 struct sockaddr_un addr;
135 connection = g_malloc0 (
sizeof (*connection));
136 connection->
socket = socket (AF_UNIX, SOCK_STREAM, 0);
137 if (connection->
socket == -1)
140 addr.sun_family = AF_UNIX;
141 strncpy (addr.sun_path, host, sizeof (addr.sun_path) - 1);
142 len = strlen (addr.sun_path) +
sizeof (addr.sun_family);
143 if (connect (connection->
socket, (
struct sockaddr *) &addr, len) == -1)
145 close (connection->
socket);
151 if (port <= 0 || port > 65535)
155 if (!cert || !key || !cacert)
158 connection = g_malloc0 (
sizeof (*connection));
160 &connection->
session, host, port, cacert, cert, key);
162 if (connection->
socket == -1)
168 connection->
host = g_strdup (host);
169 connection->
port = port;
184 const char *fmt, ...)
191 if (!connection || !fmt || !response)
194 if (*connection->
host ==
'/')
228 if (*connection->
host ==
'/')
229 close (connection->
socket);
232 g_free (connection->
host);
251 char **d_name,
char **d_version,
char **p_name,
264 goto err_get_version;
267 goto err_get_version;
272 goto err_get_version;
278 goto err_get_version;
281 goto err_get_version;
286 goto err_get_version;
292 goto err_get_version;
295 goto err_get_version;
300 goto err_get_version;
308 g_warning (
"Erroneous OSP <get_version/> response.");
347 g_warning (
"%s: element VTS missing.", __FUNCTION__);
355 g_warning (
"%s: element VERSION missing.", __FUNCTION__);
446 if (strcmp (status,
"200"))
475 *error = g_strdup (
"Couldn't send get_performance command "
476 "to scanner. Not valid connection");
483 || opts.
start > now || opts.
end < 0 || opts.
end > now)
486 *error = g_strdup (
"Couldn't send get_performance command "
487 "to scanner. Bad or missing parameters.");
492 "<get_performance start='%d' "
493 "end='%d' titles='%s'/>",
499 *error = g_strdup (
"Couldn't send get_performance command to scanner");
511 *error = g_strdup (text);
540 *error = g_strdup (
"Couldn't send get_scans command "
541 "to scanner. Not valid connection");
547 "<get_scans scan_id='%s'"
549 " pop_results='0'/>",
555 *error = g_strdup (
"Couldn't send get_scans command to scanner");
566 *error = g_strdup (text);
598 char **report_xml,
int details,
int pop_results,
608 *error = g_strdup (
"Couldn't send get_scan command "
609 "to scanner. Not valid connection");
614 "<get_scans scan_id='%s'"
616 " pop_results='%d'/>",
623 *error = g_strdup (
"Couldn't send get_scans command to scanner");
634 *error = g_strdup (text);
643 string = g_string_new (
"");
645 *report_xml = g_string_free (
string, FALSE);
664 char **report_xml,
int details,
char **error)
666 return osp_get_scan_pop (connection, scan_id, report_xml, details, 0, error);
687 *error = g_strdup (
"Couldn't send stop_scan command "
688 "to scanner. Not valid connection");
697 *error = g_strdup (
"Couldn't send stop_scan command to scanner");
713 *error = g_strdup (text);
730 char *options_str, *tmp, *key_escaped, *value_escaped;
732 options_str = *(
char **) pstr;
734 key_escaped = g_markup_escape_text ((
char *) key, -1);
735 value_escaped = g_markup_escape_text ((
char *) value, -1);
736 tmp = g_strdup_printf (
"%s<%s>%s</%s>", options_str ? options_str :
"",
737 key_escaped, value_escaped, key_escaped);
739 g_free (options_str);
740 g_free (key_escaped);
741 g_free (value_escaped);
742 *(
char **) pstr = tmp;
759 const char *ports, GHashTable *options,
const char *scan_id,
763 char *options_str = NULL;
770 *error = g_strdup (
"Couldn't send start_scan command "
771 "to scanner. Not valid connection");
781 "<start_scan target='%s' ports='%s' scan_id='%s'>"
782 "<scanner_params>%s</scanner_params></start_scan>",
783 target, ports ? ports :
"", scan_id ? scan_id :
"",
784 options_str ? options_str :
"");
785 g_free (options_str);
789 *error = g_strdup (
"Couldn't send start_scan command to scanner");
805 *error = g_strdup (text);
823 GHashTableIter auth_data_iter;
824 gchar *auth_data_name, *auth_data_value;
827 "<credential type=\"%s\" service=\"%s\" port=\"%s\">",
828 credential->
type ? credential->
type :
"",
830 credential->
port ? credential->
port :
"");
832 g_hash_table_iter_init (&auth_data_iter, credential->
auth_data);
833 while (g_hash_table_iter_next (&auth_data_iter,
834 (gpointer*)&auth_data_name,
835 (gpointer*)&auth_data_value))
860 "<exclude_hosts>%s</exclude_hosts>"
868 g_string_append (xml_string,
"<credentials>");
872 g_string_append (xml_string,
"</credentials>");
888 "<vt_group filter=\"%s\"/>",
904 "<vt_value id=\"%s\">%s</vt_value>",
919 "<vt_single id=\"%s\">",
921 g_hash_table_foreach (vt_single->
vt_values,
942 gchar *scanner_params_xml = NULL;
949 char filename[] =
"/tmp/osp-cmd-XXXXXX";
955 *error = g_strdup (
"Couldn't send start_scan command "
956 "to scanner. Not valid connection");
960 fd = mkstemp (filename);
961 FILE *file = fdopen (fd,
"w");
963 xml = g_string_sized_new (10240);
964 g_string_append (xml,
"<start_scan");
971 g_string_append (xml,
"<targets>");
973 g_string_append (xml,
"</targets>");
975 g_string_append (xml,
"<scanner_params>");
978 scanner_params_xml = NULL;
981 &scanner_params_xml);
982 if (scanner_params_xml)
983 g_string_append (xml, scanner_params_xml);
984 g_free (scanner_params_xml);
986 g_string_append (xml,
"</scanner_params>");
988 g_string_append (xml,
"<vt_selection>");
991 fprintf (file,
"%s", xml->str);
993 g_string_free (xml, TRUE);
995 xml = g_string_new (
"");
996 list_item = opts.
vts;
1003 list_item = list_item->next;
1005 if (list_count == 1000)
1007 fprintf (file,
"%s", xml->str);
1009 g_string_free (xml, TRUE);
1010 xml = g_string_new (
"");
1015 g_string_append (xml,
"</vt_selection>");
1016 g_string_append (xml,
"</start_scan>");
1018 fprintf (file,
"%s", xml->str);
1021 g_string_free (xml, TRUE);
1023 g_file_get_contents (filename, &cmd, NULL, NULL);
1033 *error = g_strdup (
"Could not send start_scan command to scanner");
1049 *error = g_strdup (text);
1071 if (!strcmp (str,
"integer"))
1073 else if (!strcmp (str,
"string"))
1075 else if (!strcmp (str,
"password"))
1077 else if (!strcmp (str,
"file"))
1079 else if (!strcmp (str,
"boolean"))
1081 else if (!strcmp (str,
"ovaldef_file"))
1083 else if (!strcmp (str,
"selection"))
1085 else if (!strcmp (str,
"credential_up"))
1116 return "ovaldef_file";
1120 return "credential_up";
1141 assert (connection);
1158 child = entities->data;
1170 *params = g_slist_append (*params, param);
1282 g_free (param->
name);
1283 g_free (param->
desc);
1284 g_free (param->
def);
1305 new_credential->
type = type ? g_strdup (type) : NULL;
1306 new_credential->
service = service ? g_strdup (service) : NULL;
1307 new_credential->
port = port ? g_strdup (port) : NULL;
1308 new_credential->
auth_data = g_hash_table_new_full (g_str_hash,
1313 return new_credential;
1327 g_free (credential->
type);
1329 g_free (credential->
port);
1330 g_hash_table_destroy (credential->
auth_data);
1331 g_free (credential);
1346 if (credential == NULL || name == NULL)
1348 return g_hash_table_lookup (credential->
auth_data, name);
1363 if (credential == NULL || name == NULL)
1366 if (g_regex_match_simple (
"^[[:alpha:]][[:alnum:]_]*$", name, 0, 0))
1369 g_hash_table_replace (credential->
auth_data,
1373 g_hash_table_remove (credential->
auth_data,
1378 g_warning (
"%s: Invalid auth data name: %s", __FUNCTION__, name);
1394 const char *exclude_hosts)
1399 new_target->
exclude_hosts = exclude_hosts ? g_strdup (exclude_hosts) : NULL;
1400 new_target->
hosts = hosts ? g_strdup (hosts) : NULL;
1401 new_target->
ports = ports ? g_strdup (ports) : NULL;
1420 g_free (target->
hosts);
1421 g_free (target->
ports);
1434 if (!target || !credential)
1453 new_vt_group->
filter = filter ? g_strdup (filter) : NULL;
1455 return new_vt_group;
1469 g_free (vt_group->
filter);
1486 new_vt_single->
vt_id = vt_id ? g_strdup (vt_id) : NULL;
1487 new_vt_single->
vt_values = g_hash_table_new_full (g_str_hash, g_str_equal,
1490 return new_vt_single;
1504 g_hash_table_destroy (vt_single->
vt_values);
1506 g_free (vt_single->
vt_id);
1520 const char *name,
const char *value)
1522 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 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.