Greenbone Vulnerability Management Libraries  10.0.0
osp.h File Reference

API for Open Scanner Protocol communication. More...

#include <glib.h>
Include dependency graph for osp.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct osp_connection osp_connection_t
 
typedef struct osp_param osp_param_t
 

Enumerations

enum  osp_param_type_t {
  OSP_PARAM_TYPE_INT = 0, OSP_PARAM_TYPE_STR, OSP_PARAM_TYPE_PASSWORD, OSP_PARAM_TYPE_FILE,
  OSP_PARAM_TYPE_BOOLEAN, OSP_PARAM_TYPE_OVALDEF_FILE, OSP_PARAM_TYPE_SELECTION, OSP_PARAM_TYPE_CRD_UP
}
 OSP parameter types. More...
 

Functions

osp_connection_tosp_connection_new (const char *, int, const char *, const char *, const char *)
 
int osp_get_version (osp_connection_t *, char **, char **, char **, char **, char **, char **)
 Get the scanner version from an OSP server. More...
 
int osp_start_scan (osp_connection_t *, const char *, const char *, GHashTable *, const char *, char **)
 Start an OSP scan against a target. More...
 
int osp_get_scan (osp_connection_t *, const char *, char **, int, char **)
 Get a scan from an OSP server. More...
 
int osp_delete_scan (osp_connection_t *, const char *)
 Delete a scan from an OSP server. More...
 
int osp_stop_scan (osp_connection_t *, const char *, char **)
 Stop a scan on an OSP server. More...
 
int osp_get_scanner_details (osp_connection_t *, char **, GSList **)
 Get an OSP scanner's details. More...
 
osp_param_tosp_param_new (void)
 Create a new OSP parameter. More...
 
const char * osp_param_id (const osp_param_t *)
 Get an OSP parameter's id. More...
 
const char * osp_param_name (const osp_param_t *)
 Get an OSP parameter's name. More...
 
const char * osp_param_desc (const osp_param_t *)
 Get an OSP parameter's description. More...
 
const char * osp_param_default (const osp_param_t *)
 Get an OSP parameter's default value. More...
 
const char * osp_param_type_str (const osp_param_t *)
 Get an OSP parameter in string format form its type. More...
 
int osp_param_mandatory (const osp_param_t *)
 Get an OSP parameter's mandatory value. More...
 
void osp_param_free (osp_param_t *)
 Free an OSP parameter. More...
 
void osp_connection_close (osp_connection_t *)
 Close a connection to an OSP server. More...
 

Detailed Description

API for Open Scanner Protocol communication.

Definition in file osp.h.

Typedef Documentation

◆ osp_connection_t

Definition at line 30 of file osp.h.

◆ osp_param_t

typedef struct osp_param osp_param_t

Definition at line 47 of file osp.h.

Enumeration Type Documentation

◆ osp_param_type_t

OSP parameter types.

Enumerator
OSP_PARAM_TYPE_INT 

Integer type.

OSP_PARAM_TYPE_STR 

String type.

OSP_PARAM_TYPE_PASSWORD 

Password type.

OSP_PARAM_TYPE_FILE 

File type.

OSP_PARAM_TYPE_BOOLEAN 

Boolean type.

OSP_PARAM_TYPE_OVALDEF_FILE 

Oval definition type.

OSP_PARAM_TYPE_SELECTION 

Selection type.

OSP_PARAM_TYPE_CRD_UP 

Credential user/pass type.

Definition at line 35 of file osp.h.

Function Documentation

◆ osp_connection_close()

void osp_connection_close ( osp_connection_t connection)

Close a connection to an OSP server.

Parameters
[in]connectionConnection to OSP server to close.

Definition at line 184 of file osp.c.

185 {
186  if (!connection)
187  return;
188 
189  if (*connection->host == '/')
190  close (connection->socket);
191  else
192  gvm_server_close (connection->socket, connection->session);
193  g_free (connection->host);
194  g_free (connection);
195 }
int socket
Definition: osp.c:52
int gvm_server_close(int socket, gnutls_session_t session)
Close a server connection and its socket.
Definition: serverutils.c:507
gnutls_session_t session
Definition: osp.c:51
char * host
Definition: osp.c:53

References gvm_server_close(), osp_connection::host, osp_connection::session, and osp_connection::socket.

Here is the call graph for this function:

◆ osp_connection_new()

osp_connection_t* osp_connection_new ( const char *  ,
int  ,
const char *  ,
const char *  ,
const char *   
)

◆ osp_delete_scan()

int osp_delete_scan ( osp_connection_t connection,
const char *  scan_id 
)

Delete a scan from an OSP server.

Parameters
[in]connectionConnection to an OSP server.
[in]scan_idID of scan to delete.
Returns
0 if success, 1 if error.

Definition at line 295 of file osp.c.

296 {
297  entity_t entity;
298  int ret = 0;
299  const char *status;
300 
301  if (!connection)
302  return 1;
303 
304  ret = osp_send_command (connection, &entity, "<delete_scan scan_id='%s'/>",
305  scan_id);
306  if (ret)
307  return 1;
308 
309  /* Check response status. */
310  status = entity_attribute (entity, "status");
311  assert (status);
312  if (strcmp (status, "200"))
313  ret = 1;
314 
315  free_entity (entity);
316  return ret;
317 }
static int osp_send_command(osp_connection_t *, entity_t *, const char *,...)
Send a command to an OSP server.
Definition: osp.c:71
XML element.
Definition: xmlutils.h:65
const char * entity_attribute(entity_t entity, const char *name)
Get an attribute of an entity.
Definition: xmlutils.c:228
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:127

References entity_attribute(), free_entity(), and osp_send_command().

Here is the call graph for this function:

◆ osp_get_scan()

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.

Parameters
[in]connectionConnection to an OSP server.
[in]scan_idID of scan to get.
[out]report_xmlScans report.
[in]details0 for no scan details, 1 otherwise.
[out]errorPointer to error, if any.
Returns
Scan progress if success, -1 if error.

Definition at line 331 of file osp.c.

333 {
334  entity_t entity, child;
335  int progress;
336  int rc;
337 
338  assert (connection);
339  assert (scan_id);
340  rc = osp_send_command (connection, &entity,
341  "<get_scans scan_id='%s' details='%d'/>", scan_id,
342  details ? 1 : 0);
343  if (rc)
344  {
345  if (error)
346  *error = g_strdup ("Couldn't send get_scans command to scanner");
347  return -1;
348  }
349 
350  child = entity_child (entity, "scan");
351  if (!child)
352  {
353  const char *text = entity_attribute (entity, "status_text");
354 
355  assert (text);
356  if (error)
357  *error = g_strdup (text);
358  free_entity (entity);
359  return -1;
360  }
361  progress = atoi (entity_attribute (child, "progress"));
362  if (report_xml)
363  {
364  GString *string;
365 
366  string = g_string_new ("");
367  print_entity_to_string (child, string);
368  *report_xml = g_string_free (string, FALSE);
369  }
370  free_entity (entity);
371  return progress;
372 }
void print_entity_to_string(entity_t entity, GString *string)
Print an XML entity tree to a GString, appending it if string is not.
Definition: xmlutils.c:1197
static int osp_send_command(osp_connection_t *, entity_t *, const char *,...)
Send a command to an OSP server.
Definition: osp.c:71
XML element.
Definition: xmlutils.h:65
entity_t entity_child(entity_t entity, const char *name)
Get a child of an entity.
Definition: xmlutils.c:205
const char * entity_attribute(entity_t entity, const char *name)
Get an attribute of an entity.
Definition: xmlutils.c:228
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:127

References entity_attribute(), entity_child(), free_entity(), osp_send_command(), and print_entity_to_string().

Here is the call graph for this function:

◆ osp_get_scanner_details()

int osp_get_scanner_details ( osp_connection_t connection,
char **  desc,
GSList **  params 
)

Get an OSP scanner's details.

Parameters
[in]connectionConnection to an OSP server.
[out]descScanner's description.
[out]paramsScanner's parameters.
Returns
0 if success, 1 if failure.

Definition at line 578 of file osp.c.

580 {
581  entity_t entity, child;
582  entities_t entities;
583 
584  assert (connection);
585 
586  if (osp_send_command (connection, &entity, "<get_scanner_details/>"))
587  return 1;
588  if (params)
589  {
590  child = entity_child (entity, "scanner_params");
591  if (!child)
592  {
593  free_entity (entity);
594  return 1;
595  }
596  entities = child->entities;
597  while (entities)
598  {
599  osp_param_t *param;
600 
601  child = entities->data;
602  param = osp_param_new ();
603  param->id = g_strdup (entity_attribute (child, "id"));
604  param->type =
605  osp_param_str_to_type (entity_attribute (child, "type"));
606  param->name = g_strdup (entity_text (entity_child (child, "name")));
607  param->desc =
608  g_strdup (entity_text (entity_child (child, "description")));
609  param->def = g_strdup (entity_text (entity_child (child, "default")));
610  if (entity_child (child, "mandatory"))
611  param->mandatory =
612  atoi (entity_text (entity_child (child, "mandatory")));
613  *params = g_slist_append (*params, param);
614  entities = next_entities (entities);
615  }
616  }
617  if (desc)
618  {
619  child = entity_child (entity, "description");
620  assert (child);
621  *desc = g_strdup (entity_text (child));
622  }
623 
624  free_entity (entity);
625  return 0;
626 }
Struct holding options for OSP parameters.
Definition: osp.c:60
char * desc
Definition: osp.c:64
osp_param_t * osp_param_new(void)
Create a new OSP parameter.
Definition: osp.c:634
static int osp_send_command(osp_connection_t *, entity_t *, const char *,...)
Send a command to an OSP server.
Definition: osp.c:71
XML element.
Definition: xmlutils.h:65
entities_t entities
Children.
Definition: xmlutils.h:70
entity_t entity_child(entity_t entity, const char *name)
Get a child of an entity.
Definition: xmlutils.c:205
char * name
Definition: osp.c:63
static osp_param_type_t osp_param_str_to_type(const char *str)
Get an OSP parameter's type from its string format.
Definition: osp.c:511
const char * entity_attribute(entity_t entity, const char *name)
Get an attribute of an entity.
Definition: xmlutils.c:228
osp_param_type_t type
Definition: osp.c:66
char * def
Definition: osp.c:65
char * entity_text(entity_t entity)
Get the text an entity.
Definition: xmlutils.c:157
int mandatory
Definition: osp.c:67
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:127
char * id
Definition: osp.c:62
GSList * entities_t
Entities.
Definition: xmlutils.h:60
entities_t next_entities(entities_t entities)
Return all the entities from an entities_t after the first.
Definition: xmlutils.c:79

References osp_param::def, osp_param::desc, entity_s::entities, entity_attribute(), entity_child(), entity_text(), free_entity(), osp_param::id, osp_param::mandatory, osp_param::name, next_entities(), osp_param_new(), osp_param_str_to_type(), osp_send_command(), and osp_param::type.

Here is the call graph for this function:

◆ osp_get_version()

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.

Parameters
[in]connectionConnection to an OSP server.
[out]s_nameParsed scanner name.
[out]s_versionParsed scanner version.
[out]d_nameParsed scanner name.
[out]d_versionParsed scanner version.
[out]p_nameParsed scanner name.
[out]p_versionParsed scanner version.
Returns
0 if success, 1 if error.

Definition at line 211 of file osp.c.

214 {
215  entity_t entity, child, gchild;
216 
217  if (!connection)
218  return 1;
219 
220  if (osp_send_command (connection, &entity, "<get_version/>"))
221  return 1;
222 
223  child = entity_child (entity, "scanner");
224  if (!child)
225  goto err_get_version;
226  gchild = entity_child (child, "name");
227  if (!gchild)
228  goto err_get_version;
229  if (s_name)
230  *s_name = g_strdup (entity_text (gchild));
231  gchild = entity_child (child, "version");
232  if (!gchild)
233  goto err_get_version;
234  if (s_version)
235  *s_version = g_strdup (entity_text (gchild));
236 
237  child = entity_child (entity, "daemon");
238  if (!child)
239  goto err_get_version;
240  gchild = entity_child (child, "name");
241  if (!gchild)
242  goto err_get_version;
243  if (d_name)
244  *d_name = g_strdup (entity_text (gchild));
245  gchild = entity_child (child, "version");
246  if (!gchild)
247  goto err_get_version;
248  if (d_version)
249  *d_version = g_strdup (entity_text (gchild));
250 
251  child = entity_child (entity, "protocol");
252  if (!child)
253  goto err_get_version;
254  gchild = entity_child (child, "name");
255  if (!gchild)
256  goto err_get_version;
257  if (p_name)
258  *p_name = g_strdup (entity_text (gchild));
259  gchild = entity_child (child, "version");
260  if (!gchild)
261  goto err_get_version;
262  if (p_version)
263  *p_version = g_strdup (entity_text (gchild));
264 
265  free_entity (entity);
266  return 0;
267 
268 err_get_version:
269  g_warning ("Erroneous OSP <get_version/> response.");
270  if (s_name)
271  g_free (*s_name);
272  if (s_version)
273  g_free (*s_version);
274  if (d_name)
275  g_free (*d_name);
276  if (d_version)
277  g_free (*d_version);
278  if (p_name)
279  g_free (*p_name);
280  if (p_version)
281  g_free (*p_version);
282  free_entity (entity);
283  return 1;
284 }
static int osp_send_command(osp_connection_t *, entity_t *, const char *,...)
Send a command to an OSP server.
Definition: osp.c:71
XML element.
Definition: xmlutils.h:65
entity_t entity_child(entity_t entity, const char *name)
Get a child of an entity.
Definition: xmlutils.c:205
char * entity_text(entity_t entity)
Get the text an entity.
Definition: xmlutils.c:157
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:127

References entity_child(), entity_text(), free_entity(), and osp_send_command().

Here is the call graph for this function:

◆ osp_param_default()

const char* osp_param_default ( const osp_param_t param)

Get an OSP parameter's default value.

Parameters
[in]paramOSP parameter.
Returns
Default value of OSP parameter.

Definition at line 692 of file osp.c.

693 {
694  assert (param);
695 
696  return param->def;
697 }
char * def
Definition: osp.c:65

References osp_param::def.

◆ osp_param_desc()

const char* osp_param_desc ( const osp_param_t param)

Get an OSP parameter's description.

Parameters
[in]paramOSP parameter.
Returns
Description of OSP parameter.

Definition at line 677 of file osp.c.

678 {
679  assert (param);
680 
681  return param->desc;
682 }
char * desc
Definition: osp.c:64

References osp_param::desc.

◆ osp_param_free()

void osp_param_free ( osp_param_t param)

Free an OSP parameter.

Parameters
[in]paramOSP parameter to destroy.

Definition at line 720 of file osp.c.

721 {
722  if (!param)
723  return;
724  g_free (param->id);
725  g_free (param->name);
726  g_free (param->desc);
727  g_free (param->def);
728  g_free (param);
729 }
char * desc
Definition: osp.c:64
char * name
Definition: osp.c:63
char * def
Definition: osp.c:65
char * id
Definition: osp.c:62

References osp_param::def, osp_param::desc, osp_param::id, and osp_param::name.

◆ osp_param_id()

const char* osp_param_id ( const osp_param_t param)

Get an OSP parameter's id.

Parameters
[in]paramOSP parameter.
Returns
ID of OSP parameter.

Definition at line 647 of file osp.c.

648 {
649  assert (param);
650 
651  return param->id;
652 }
char * id
Definition: osp.c:62

References osp_param::id.

◆ osp_param_mandatory()

int osp_param_mandatory ( const osp_param_t param)

Get an OSP parameter's mandatory value.

Parameters
[in]paramOSP parameter.
Returns
Mandatory value of OSP parameter.

Definition at line 707 of file osp.c.

708 {
709  assert (param);
710 
711  return param->mandatory;
712 }
int mandatory
Definition: osp.c:67

References osp_param::mandatory.

◆ osp_param_name()

const char* osp_param_name ( const osp_param_t param)

Get an OSP parameter's name.

Parameters
[in]paramOSP parameter.
Returns
Name of OSP parameter.

Definition at line 662 of file osp.c.

663 {
664  assert (param);
665 
666  return param->name;
667 }
char * name
Definition: osp.c:63

References osp_param::name.

◆ osp_param_new()

osp_param_t* osp_param_new ( void  )

Create a new OSP parameter.

Returns
New OSP parameter.

Definition at line 634 of file osp.c.

635 {
636  return g_malloc0 (sizeof (osp_param_t));
637 }
Struct holding options for OSP parameters.
Definition: osp.c:60

Referenced by osp_get_scanner_details().

Here is the caller graph for this function:

◆ osp_param_type_str()

const char* osp_param_type_str ( const osp_param_t param)

Get an OSP parameter in string format form its type.

Parameters
[in]paramOSP parameter.
Returns
OSP parameter in string format.

Definition at line 542 of file osp.c.

543 {
544  osp_param_type_t type;
545 
546  assert (param);
547  type = param->type;
548  if (type == OSP_PARAM_TYPE_INT)
549  return "integer";
550  else if (type == OSP_PARAM_TYPE_STR)
551  return "string";
552  else if (type == OSP_PARAM_TYPE_PASSWORD)
553  return "password";
554  else if (type == OSP_PARAM_TYPE_FILE)
555  return "file";
556  else if (type == OSP_PARAM_TYPE_BOOLEAN)
557  return "boolean";
558  else if (type == OSP_PARAM_TYPE_OVALDEF_FILE)
559  return "ovaldef_file";
560  else if (type == OSP_PARAM_TYPE_SELECTION)
561  return "selection";
562  else if (type == OSP_PARAM_TYPE_CRD_UP)
563  return "credential_up";
564  assert (0);
565  return NULL;
566 }
osp_param_type_t
OSP parameter types.
Definition: osp.h:35
osp_param_type_t type
Definition: osp.c:66

References OSP_PARAM_TYPE_BOOLEAN, OSP_PARAM_TYPE_CRD_UP, OSP_PARAM_TYPE_FILE, OSP_PARAM_TYPE_INT, OSP_PARAM_TYPE_OVALDEF_FILE, OSP_PARAM_TYPE_PASSWORD, OSP_PARAM_TYPE_SELECTION, OSP_PARAM_TYPE_STR, and osp_param::type.

◆ osp_start_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.

Parameters
[in]connectionConnection to an OSP server.
[in]targetTarget host to scan.
[in]portsList of ports to scan.
[in]optionsTable of scan options.
[in]scan_iduuid to set for scan, null otherwise.
[out]errorPointer to error, if any.
Returns
0 on success, -1 otherwise.

Definition at line 457 of file osp.c.

460 {
461  entity_t entity;
462  char *options_str = NULL;
463  int status;
464  int rc;
465 
466  assert (connection);
467  assert (target);
468  /* Construct options string. */
469  if (options)
470  g_hash_table_foreach (options, option_concat_as_xml, &options_str);
471 
472  rc = osp_send_command (connection, &entity,
473  "<start_scan target='%s' ports='%s' scan_id='%s'>"
474  "<scanner_params>%s</scanner_params></start_scan>",
475  target, ports ? ports : "", scan_id ? scan_id : "",
476  options_str ? options_str : "");
477  g_free (options_str);
478  if (rc)
479  {
480  if (error)
481  *error = g_strdup ("Couldn't send start_scan command to scanner");
482  return -1;
483  }
484 
485  status = atoi (entity_attribute (entity, "status"));
486  if (status == 200)
487  {
488  free_entity (entity);
489  return 0;
490  }
491  else
492  {
493  const char *text = entity_attribute (entity, "status_text");
494 
495  assert (text);
496  if (error)
497  *error = g_strdup (text);
498  free_entity (entity);
499  return -1;
500  }
501 }
static int osp_send_command(osp_connection_t *, entity_t *, const char *,...)
Send a command to an OSP server.
Definition: osp.c:71
XML element.
Definition: xmlutils.h:65
static void option_concat_as_xml(gpointer key, gpointer value, gpointer pstr)
Concatenate options as xml.
Definition: osp.c:427
const char * entity_attribute(entity_t entity, const char *name)
Get an attribute of an entity.
Definition: xmlutils.c:228
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:127

References entity_attribute(), free_entity(), option_concat_as_xml(), and osp_send_command().

Here is the call graph for this function:

◆ osp_stop_scan()

int osp_stop_scan ( osp_connection_t connection,
const char *  scan_id,
char **  error 
)

Stop a scan on an OSP server.

Parameters
[in]connectionConnection to an OSP server.
[in]scan_idID of scan to delete.
[out]errorPointer to error, if any.
Returns
Scan progress if success, -1 if error.

Definition at line 384 of file osp.c.

385 {
386  entity_t entity;
387  int rc;
388 
389  assert (connection);
390  assert (scan_id);
391  rc = osp_send_command (connection, &entity, "<stop_scan scan_id='%s'/>",
392  scan_id);
393  if (rc)
394  {
395  if (error)
396  *error = g_strdup ("Couldn't send stop_scan command to scanner");
397  return -1;
398  }
399 
400  rc = atoi (entity_attribute (entity, "status"));
401  if (rc == 200)
402  {
403  free_entity (entity);
404  return 0;
405  }
406  else
407  {
408  const char *text = entity_attribute (entity, "status_text");
409 
410  assert (text);
411  if (error)
412  *error = g_strdup (text);
413  free_entity (entity);
414  return -1;
415  }
416 }
static int osp_send_command(osp_connection_t *, entity_t *, const char *,...)
Send a command to an OSP server.
Definition: osp.c:71
XML element.
Definition: xmlutils.h:65
const char * entity_attribute(entity_t entity, const char *name)
Get an attribute of an entity.
Definition: xmlutils.c:228
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:127

References entity_attribute(), free_entity(), and osp_send_command().

Here is the call graph for this function: