openvasd.c File Reference

API for Openvas Daemon communication. More...

`#include "openvasd.h"`
`#include "../base/array.h"`
`#include "../base/networking.h"`
`#include "../http/httputils.h"`
`#include "../util/json.h"`
`#include <cjson/cJSON.h>`
`#include <netinet/in.h>`
`#include <stddef.h>`
`#include <stdio.h>`
`#include <stdlib.h>`
`#include <unistd.h>`

Go to the source code of this file.

## Data Structures

struct | openvasd_connector
| Struct holding the data for connecting with openvasd. More...
struct | openvasd_param
| Struct holding options for openvasd parameters. More...
struct | openvasd_credential
| Struct credential information for openvasd. More...
struct | openvasd_target
| Struct holding target information. More...
struct | openvasd_vt_single
| Struct holding vt information. More...

## Macros

#define | G_LOG_DOMAIN "libgvm ovd"
| GLib log domain.
#define | RESP_CODE_ERR -1
#define | RESP_CODE_OK 0

## Functions

openvasd_connector_t | openvasd_connector_new (void)
| Initialize an openvasd connector.
openvasd_error_t | openvasd_connector_builder (openvasd_connector_t conn, openvasd_conn_opt_t opt, const void *val)
| Build a openvasd connector.
openvasd_error_t | openvasd_connector_free (openvasd_connector_t conn)
| Build a openvasd connector.
void | openvasd_response_cleanup (openvasd_resp_t resp)
| Free an openvasd response struct.
static gvm_http_headers_t * | init_customheader (const gchar *apikey, gboolean contenttype)
static openvasd_resp_t | openvasd_send_request (openvasd_connector_t conn, gvm_http_method_t method, const gchar *path, const gchar *data, const gchar *header_name)
| Sends an HTTP(S) request to the OpenVAS daemon using the specified parameters.
openvasd_resp_t | openvasd_get_version (openvasd_connector_t conn)
| Request HEAD.
openvasd_resp_t | openvasd_get_vt_stream_init (openvasd_connector_t conn)
| Initialized an curl multiperform handler which allows fetch feed metadata chunk by chunk.
void | openvasd_reset_vt_stream (openvasd_connector_t conn)
gchar * | openvasd_vt_stream_str (openvasd_connector_t conn)
size_t | openvasd_vt_stream_len (openvasd_connector_t conn)
int | openvasd_get_vt_stream (openvasd_connector_t conn)
| Get a new feed metadata chunk.
openvasd_resp_t | openvasd_get_vts (openvasd_connector_t conn)
| Get VT's metadata.
openvasd_resp_t | openvasd_start_scan (openvasd_connector_t conn, gchar *data)
openvasd_resp_t | openvasd_stop_scan (openvasd_connector_t conn)
openvasd_resp_t | openvasd_get_scan_results (openvasd_connector_t conn, long first, long last)
openvasd_result_t | openvasd_result_new (unsigned long id, gchar *type, gchar *ip_address, gchar *hostname, gchar *oid, int port, gchar *protocol, gchar *message, gchar *detail_name, gchar *detail_value, gchar *detail_source_type, gchar *detail_source_name, gchar *detail_source_description)
char * | openvasd_get_result_member_str (openvasd_result_t result, openvasd_result_member_string_t member)
int | openvasd_get_result_member_int (openvasd_result_t result, openvasd_result_member_int_t member)
void | openvasd_result_free (openvasd_result_t result)
static int | parse_results (const gchar *body, GSList **results)
int | openvasd_parsed_results (openvasd_connector_t conn, unsigned long first, unsigned long last, GSList **results)
openvasd_resp_t | openvasd_get_scan_status (openvasd_connector_t conn)
static int | get_member_value_or_fail (cJSON *reader, const gchar *member)
| Get the value from an object or error.
static int | openvasd_get_scan_progress_ext (openvasd_connector_t conn, openvasd_resp_t response)
int | openvasd_get_scan_progress (openvasd_connector_t conn)
static openvasd_status_t | get_status_code_from_openvas (const gchar *status_val)
static int | parse_status (const gchar *body, openvasd_scan_status_t status_info)
openvasd_scan_status_t | openvasd_parsed_scan_status (openvasd_connector_t conn)
| Return a struct with the general scan status.
openvasd_resp_t | openvasd_delete_scan (openvasd_connector_t conn)
openvasd_resp_t | openvasd_get_health_alive (openvasd_connector_t conn)
openvasd_resp_t | openvasd_get_health_ready (openvasd_connector_t conn)
openvasd_resp_t | openvasd_get_health_started (openvasd_connector_t conn)
openvasd_resp_t | openvasd_get_performance (openvasd_connector_t conn, openvasd_get_performance_opts_t opts)
int | openvasd_parsed_performance (openvasd_connector_t conn, openvasd_get_performance_opts_t opts, gchar **graph, gchar **err)
openvasd_resp_t | openvasd_get_scan_preferences (openvasd_connector_t conn)
static openvasd_param_t * | openvasd_param_new (char *id, gchar *name, gchar *defval, gchar *description, gchar *type, int mandatory)
| Create a new openvasd parameter.
void | openvasd_param_free (openvasd_param_t *param)
| Free an openvasd parameter.
char * | openvasd_param_id (openvasd_param_t *param)
| Get the parameter id.
char * | openvasd_param_name (openvasd_param_t *param)
| Get the parameter default.
char * | openvasd_param_desc (openvasd_param_t *param)
| Get the parameter description.
char * | openvasd_param_type (openvasd_param_t *param)
| Get the parameter type.
char * | openvasd_param_default (openvasd_param_t *param)
| Get the parameter default.
int | openvasd_param_mandatory (openvasd_param_t *param)
| If the parameter is mandatory.
int | openvasd_parsed_scans_preferences (openvasd_connector_t conn, GSList **params)
static void | add_port_to_scan_json (gpointer range, gpointer p_array)
static void | add_credential_to_scan_json (gpointer credentials, gpointer cred_array)
static void | add_scan_preferences_to_scan_json (gpointer key, gpointer val, gpointer scan_prefs_array)
static void | add_vts_to_scan_json (gpointer single_vt, gpointer vts_array)
char * | openvasd_build_scan_config_json (openvasd_target_t *target, GHashTable *scan_preferences, GSList *vts)
| Build a json object with data necessary to start a scan.
openvasd_credential_t * | openvasd_credential_new (const gchar *type, const gchar *service, const gchar *port)
| Allocate and initialize a new openvasd credential.
void | openvasd_credential_free (openvasd_credential_t *credential)
| Free an openvasd credential.
void | openvasd_credential_set_auth_data (openvasd_credential_t *credential, const gchar *name, const gchar *value)
| Get authentication data from an openvasd credential.
openvasd_target_t * | openvasd_target_new (const gchar *scanid, const gchar *hosts, const gchar *ports, const gchar *exclude_hosts, int reverse_lookup_unify, int reverse_lookup_only)
| Create a new openvasd target.
void | openvasd_target_set_finished_hosts (openvasd_target_t *target, const gchar *finished_hosts)
| Set the finished hosts of an openvasd target.
void | openvasd_target_free (openvasd_target_t *target)
| Free an openvasd target, including all added credentials.
void | openvasd_target_add_alive_test_methods (openvasd_target_t *target, gboolean icmp, gboolean tcp_syn, gboolean tcp_ack, gboolean arp, gboolean consider_alive)
| Add alive test methods to openvasd target.
void | openvasd_target_add_credential (openvasd_target_t *target, openvasd_credential_t *credential)
| Add a credential to an openvasd target.
openvasd_vt_single_t * | openvasd_vt_single_new (const gchar *vt_id)
| Create a new single openvasd VT.
void | openvasd_vt_single_free (openvasd_vt_single_t *vt_single)
| Free a single openvasd VT, including all preference values.
void | openvasd_vt_single_add_value (openvasd_vt_single_t *vt_single, const gchar *name, const gchar *value)
| Add a preference value to an openvasd VT.

## Detailed Description

API for Openvas Daemon communication.

## Macro Definition Documentation

## G_LOG_DOMAIN

#define G_LOG_DOMAIN "libgvm ovd"

GLib log domain.

## RESP_CODE_ERR

#define RESP_CODE_ERR -1

## RESP_CODE_OK

#define RESP_CODE_OK 0

## Function Documentation

## add_credential_to_scan_json()

static void add_credential_to_scan_json | ( | gpointer | credentials,
|  | gpointer | cred_array
| ) |  |

static

## add_port_to_scan_json()

static void add_port_to_scan_json | ( | gpointer | range,
|  | gpointer | p_array
| ) |  |

static

## add_scan_preferences_to_scan_json()

static void add_scan_preferences_to_scan_json | ( | gpointer | key,
|  | gpointer | val,
|  | gpointer | scan_prefs_array
| ) |  |

static

## add_vts_to_scan_json()

static void add_vts_to_scan_json | ( | gpointer | single_vt,
|  | gpointer | vts_array
| ) |  |

static

## get_member_value_or_fail()

static int get_member_value_or_fail | ( | cJSON * | reader,
|  | const gchar * | member
| ) |  |

static

Get the value from an object or error.

Returns
  0 on success, -1 on error.

## get_status_code_from_openvas()

static openvasd_status_t get_status_code_from_openvas | ( | const gchar * | status_val | ) |

static

## init_customheader()

static gvm_http_headers_t * init_customheader | ( | const gchar * | apikey,
|  | gboolean | contenttype
| ) |  |

static

## openvasd_build_scan_config_json()

char * openvasd_build_scan_config_json | ( | openvasd_target_t * | target,
|  | GHashTable * | scan_preferences,
|  | GSList * | vts
| ) |  |

Build a json object with data necessary to start a scan.

JSON result consists of scan_id, message type, host ip, hostname, port, together with proto, OID, result message and uri.

Parameters

  target | target
  scan_preferences | Scan preferences to be added to the scan config
  vts | VTS collection to be added to the scan config.

Returns
  JSON string on success. Must be freed by caller. NULL on error.

## openvasd_connector_builder()

openvasd_error_t openvasd_connector_builder | ( | openvasd_connector_t | conn,
|  | openvasd_conn_opt_t | opt,
|  | const void * | val
| ) |  |

Build a openvasd connector.

Receive option name and value to build the openvasd connector

Parameters

  conn | struct holding the openvasd connector information
  opt | option to set
  val | value to set

Returns
  Return OK on success, otherwise error;

## openvasd_connector_free()

openvasd_error_t openvasd_connector_free | ( | openvasd_connector_t | conn | ) |

Build a openvasd connector.

Receive option name and value to build the openvasd connector

Parameters

  conn | struct holding the openvasd connector information

Returns
  Return OPENVASD_OK

## openvasd_connector_new()

openvasd_connector_t openvasd_connector_new | ( | void |  | ) |

Initialize an openvasd connector.

Returns
  An openvasd connector struct. It must be freed with openvasd_connector_free()

## openvasd_credential_free()

void openvasd_credential_free | ( | openvasd_credential_t * | credential | ) |

Free an openvasd credential.

Parameters

  credential | The credential to free.

## openvasd_credential_new()

openvasd_credential_t * openvasd_credential_new | ( | const gchar * | type,
|  | const gchar * | service,
|  | const gchar * | port
| ) |  |

Allocate and initialize a new openvasd credential.

Parameters

  type | The credential type.
  service | The service the credential is for.
  port | The port.

Returns
  New openvasd credential.

## openvasd_credential_set_auth_data()

void openvasd_credential_set_auth_data | ( | openvasd_credential_t * | credential,
|  | const gchar * | name,
|  | const gchar * | value
| ) |  |

Get authentication data from an openvasd credential.

Parameters

  credential | The credential to get the data from.
  name | The name of the data item to get.
  value | The authentication data or NULL to unset.

## openvasd_delete_scan()

openvasd_resp_t openvasd_delete_scan | ( | openvasd_connector_t | conn | ) |

## openvasd_get_health_alive()

openvasd_resp_t openvasd_get_health_alive | ( | openvasd_connector_t | conn | ) |

## openvasd_get_health_ready()

openvasd_resp_t openvasd_get_health_ready | ( | openvasd_connector_t | conn | ) |

## openvasd_get_health_started()

openvasd_resp_t openvasd_get_health_started | ( | openvasd_connector_t | conn | ) |

## openvasd_get_performance()

openvasd_resp_t openvasd_get_performance | ( | openvasd_connector_t | conn,
|  | openvasd_get_performance_opts_t | opts
| ) |  |

## openvasd_get_result_member_int()

int openvasd_get_result_member_int | ( | openvasd_result_t | result,
|  | openvasd_result_member_int_t | member
| ) |  |

## openvasd_get_result_member_str()

char * openvasd_get_result_member_str | ( | openvasd_result_t | result,
|  | openvasd_result_member_string_t | member
| ) |  |

## openvasd_get_scan_preferences()

openvasd_resp_t openvasd_get_scan_preferences | ( | openvasd_connector_t | conn | ) |

## openvasd_get_scan_progress()

int openvasd_get_scan_progress | ( | openvasd_connector_t | conn | ) |

## openvasd_get_scan_progress_ext()

static int openvasd_get_scan_progress_ext | ( | openvasd_connector_t | conn,
|  | openvasd_resp_t | response
| ) |  |

static

## openvasd_get_scan_results()

openvasd_resp_t openvasd_get_scan_results | ( | openvasd_connector_t | conn,
|  | long | first,
|  | long | last
| ) |  |

## openvasd_get_scan_status()

openvasd_resp_t openvasd_get_scan_status | ( | openvasd_connector_t | conn | ) |

## openvasd_get_version()

openvasd_resp_t openvasd_get_version | ( | openvasd_connector_t | conn | ) |

Request HEAD.

Parameters

  conn | Connector struct with the data necessary for the connection

Returns
  Response containing the header information

## openvasd_get_vt_stream()

int openvasd_get_vt_stream | ( | openvasd_connector_t | conn | ) |

Get a new feed metadata chunk.

This function must be call until the return value is 0, meaning there is no more data to fetch.

Parameters

  mhnd | Curl multiperfom for requesting the feed metadata

Returns
  greather than 0 if the handler is still getting data. 0 if the transmision finished. -1 on error

## openvasd_get_vt_stream_init()

openvasd_resp_t openvasd_get_vt_stream_init | ( | openvasd_connector_t | conn | ) |

Initialized an curl multiperform handler which allows fetch feed metadata chunk by chunk.

Parameters

  conn | Connector struct with the data necessary for the connection
  mhnd | The curl multiperform handler. It the caller doesn't provide it initialized, it will be initialized. The caller has to free it with gvm_http_multi_free().
  resp | The stringstream struct for the write callback function.

Returns
  The response.

## openvasd_get_vts()

openvasd_resp_t openvasd_get_vts | ( | openvasd_connector_t | conn | ) |

Get VT's metadata.

Parameters

  conn | Connector struct with the data necessary for the connection

Returns
  Response Struct containing the feed metadata in json format in the body.

## openvasd_param_default()

char * openvasd_param_default | ( | openvasd_param_t * | param | ) |

Get the parameter default.

Parameters

  param | openvasd parameter

## openvasd_param_desc()

char * openvasd_param_desc | ( | openvasd_param_t * | param | ) |

Get the parameter description.

Parameters

  param | openvasd parameter

## openvasd_param_free()

void openvasd_param_free | ( | openvasd_param_t * | param | ) |

Free an openvasd parameter.

Parameters

  param | openvasd parameter to destroy.

## openvasd_param_id()

char * openvasd_param_id | ( | openvasd_param_t * | param | ) |

Get the parameter id.

Parameters

  param | openvasd parameter

## openvasd_param_mandatory()

int openvasd_param_mandatory | ( | openvasd_param_t * | param | ) |

If the parameter is mandatory.

Parameters

  param | openvasd parameter

## openvasd_param_name()

char * openvasd_param_name | ( | openvasd_param_t * | param | ) |

Get the parameter default.

Parameters

  param | openvasd parameter

## openvasd_param_new()

static openvasd_param_t * openvasd_param_new | ( | char * | id,
|  | gchar * | name,
|  | gchar * | defval,
|  | gchar * | description,
|  | gchar * | type,
|  | int | mandatory
| ) |  |

static

Create a new openvasd parameter.

Returns
  New openvasd parameter.

## openvasd_param_type()

char * openvasd_param_type | ( | openvasd_param_t * | param | ) |

Get the parameter type.

Parameters

  param | openvasd parameter

## openvasd_parsed_performance()

int openvasd_parsed_performance | ( | openvasd_connector_t | conn,
|  | openvasd_get_performance_opts_t | opts,
|  | gchar ** | graph,
|  | gchar ** | err
| ) |  |

## openvasd_parsed_results()

int openvasd_parsed_results | ( | openvasd_connector_t | conn,
|  | unsigned long | first,
|  | unsigned long | last,
|  | GSList ** | results
| ) |  |

## openvasd_parsed_scan_status()

openvasd_scan_status_t openvasd_parsed_scan_status | ( | openvasd_connector_t | conn | ) |

Return a struct with the general scan status.

Parameters

  conn | openvasd connector data

Returns
  The data in a struct. The struct must be freed by the caller.

## openvasd_parsed_scans_preferences()

int openvasd_parsed_scans_preferences | ( | openvasd_connector_t | conn,
|  | GSList ** | params
| ) |  |

## openvasd_reset_vt_stream()

void openvasd_reset_vt_stream | ( | openvasd_connector_t | conn | ) |

## openvasd_response_cleanup()

void openvasd_response_cleanup | ( | openvasd_resp_t | resp | ) |

Free an openvasd response struct.

Parameters

  resp | Response to be freed

## openvasd_result_free()

void openvasd_result_free | ( | openvasd_result_t | result | ) |

## openvasd_result_new()

openvasd_result_t openvasd_result_new | ( | unsigned long | id,
|  | gchar * | type,
|  | gchar * | ip_address,
|  | gchar * | hostname,
|  | gchar * | oid,
|  | int | port,
|  | gchar * | protocol,
|  | gchar * | message,
|  | gchar * | detail_name,
|  | gchar * | detail_value,
|  | gchar * | detail_source_type,
|  | gchar * | detail_source_name,
|  | gchar * | detail_source_description
| ) |  |

## openvasd_send_request()

static openvasd_resp_t openvasd_send_request | ( | openvasd_connector_t | conn,
|  | gvm_http_method_t | method,
|  | const gchar * | path,
|  | const gchar * | data,
|  | const gchar * | header_name
| ) |  |

static

Sends an HTTP(S) request to the OpenVAS daemon using the specified parameters.

Parameters

  conn | The `openvasd_connector_t` containing server and certificate details.
  method | The HTTP method (GET, POST, etc.).
  path | The resource path (e.g., `/vts`).
  data | The request payload (if applicable).
  custom_headers | Additional request headers.
  header_name | The header key to extract from the response.

Returns
  `openvasd_resp_t` containing response status, body, and header value.

## openvasd_start_scan()

openvasd_resp_t openvasd_start_scan | ( | openvasd_connector_t | conn,
|  | gchar * | data
| ) |  |

@Brief Get VT's metadata

Parameters

  conn | Connector struct with the data necessary for the connection
  data | String containing the scan config in JSON format.

Returns
  Response Struct containing the resonse.

## openvasd_stop_scan()

openvasd_resp_t openvasd_stop_scan | ( | openvasd_connector_t | conn | ) |

## openvasd_target_add_alive_test_methods()

void openvasd_target_add_alive_test_methods | ( | openvasd_target_t * | target,
|  | gboolean | icmp,
|  | gboolean | tcp_syn,
|  | gboolean | tcp_ack,
|  | gboolean | arp,
|  | gboolean | consider_alive
| ) |  |

Add alive test methods to openvasd target.

Parameters

  target | The openvasd target to add the methods to.
  icmp | Use ICMP ping.
  tcp_syn | Use TCP-SYN ping.
  tcp_ack | Use TCP-ACK ping.
  arp | Use ARP ping.
  consider_alive | Consider host to be alive.

## openvasd_target_add_credential()

void openvasd_target_add_credential | ( | openvasd_target_t * | target,
|  | openvasd_credential_t * | credential
| ) |  |

Add a credential to an openvasd target.

Parameters

  target | The openvasd target to add the credential to.
  credential | The credential to add. Will be freed with target.

## openvasd_target_free()

void openvasd_target_free | ( | openvasd_target_t * | target | ) |

Free an openvasd target, including all added credentials.

Parameters

  target | The openvasd target to free.

## openvasd_target_new()

openvasd_target_t * openvasd_target_new | ( | const gchar * | scanid,
|  | const gchar * | hosts,
|  | const gchar * | ports,
|  | const gchar * | exclude_hosts,
|  | int | reverse_lookup_unify,
|  | int | reverse_lookup_only
| ) |  |

Create a new openvasd target.

Parameters

  scanid | Scan ID.
  hosts | The hostnames of the target.
  ports | The ports of the target.
  exclude_hosts | The excluded hosts of the target.
  reverse_lookup_unify | Lookup flag.
  reverse_lookup_only | Lookup flag.

Returns
  The newly allocated openvasd_target_t.

## openvasd_target_set_finished_hosts()

void openvasd_target_set_finished_hosts | ( | openvasd_target_t * | target,
|  | const gchar * | finished_hosts
| ) |  |

Set the finished hosts of an openvasd target.

Parameters

  target | The openvasd target to modify.
  finished_hosts | The hostnames to consider finished.

## openvasd_vt_single_add_value()

void openvasd_vt_single_add_value | ( | openvasd_vt_single_t * | vt_single,
|  | const gchar * | name,
|  | const gchar * | value
| ) |  |

Add a preference value to an openvasd VT.

This creates a copy of the name and value.

Parameters

  vt_single | The VT to add the preference to.
  name | The name / identifier of the preference.
  value | The value of the preference.

## openvasd_vt_single_free()

void openvasd_vt_single_free | ( | openvasd_vt_single_t * | vt_single | ) |

Free a single openvasd VT, including all preference values.

Parameters

  vt_single | The openvasd VT to free.

## openvasd_vt_single_new()

openvasd_vt_single_t * openvasd_vt_single_new | ( | const gchar * | vt_id | ) |

Create a new single openvasd VT.

Parameters

  vt_id | The id of the VT.

Returns
  The newly allocated single VT.

## openvasd_vt_stream_len()

size_t openvasd_vt_stream_len | ( | openvasd_connector_t | conn | ) |

## openvasd_vt_stream_str()

gchar * openvasd_vt_stream_str | ( | openvasd_connector_t | conn | ) |

## parse_results()

static int parse_results | ( | const gchar * | body,
|  | GSList ** | results
| ) |  |

static

## parse_status()

static int parse_status | ( | const gchar * | body,
|  | openvasd_scan_status_t | status_info
| ) |  |

static