OpenVAS Libraries  9.0.3
xml.h File Reference
#include <glib.h>
#include <gnutls/gnutls.h>
#include <stdio.h>
#include "../misc/openvas_server.h"
Include dependency graph for xml.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  entity_s
 XML element. More...
 
struct  xml_search_data_t
 Data for xml search functions. More...
 

Typedefs

typedef GSList * entities_t
 
typedef struct entity_sentity_t
 

Functions

entities_t next_entities (entities_t)
 Return all the entities from an entities_t after the first. More...
 
entity_t first_entity (entities_t)
 Return the first entity from an entities_t. More...
 
entity_t add_entity (entities_t *, const char *, const char *)
 Add an XML entity to a tree of entities. More...
 
int compare_entities (entity_t, entity_t)
 Compare two XML entity. More...
 
entity_t entity_child (entity_t, const char *)
 Get a child of an entity. More...
 
const char * entity_attribute (entity_t, const char *)
 Get an attribute of an entity. More...
 
char * entity_name (entity_t entity)
 Get the name an entity. More...
 
char * entity_text (entity_t entity)
 Get the text an entity. More...
 
void free_entity (entity_t)
 Free an entity, recursively. More...
 
void print_entity (FILE *, entity_t)
 Print an XML entity. More...
 
void print_entity_format (entity_t, gpointer indentation)
 Print an XML entity to stdout, recusively printing its children. More...
 
int try_read_entity_and_string (gnutls_session_t *, int, entity_t *, GString **)
 Try read an XML entity tree from the manager. More...
 
int try_read_entity_and_string_c (openvas_connection_t *, int, entity_t *, GString **)
 Try read an XML entity tree from the manager. More...
 
int read_entity_and_string (gnutls_session_t *, entity_t *, GString **)
 Try read an XML entity tree from the manager. More...
 
int read_entity_and_string_c (openvas_connection_t *, entity_t *, GString **)
 Try read an XML entity tree from the manager. More...
 
int read_entity_and_text (gnutls_session_t *, entity_t *, char **)
 Read an XML entity tree from the manager. More...
 
int read_entity_and_text_c (openvas_connection_t *, entity_t *, char **)
 Read an XML entity tree from the manager. More...
 
int try_read_entity (gnutls_session_t *, int, entity_t *)
 Try read an XML entity tree from the manager. More...
 
int try_read_entity_c (openvas_connection_t *, int, entity_t *)
 Try read an XML entity tree from the manager. More...
 
int read_entity (gnutls_session_t *, entity_t *)
 Read an XML entity tree from the manager. More...
 
int read_entity_c (openvas_connection_t *, entity_t *)
 Read an XML entity tree from the manager. More...
 
int read_string (gnutls_session_t *, GString **)
 Read entity and text. Free the entity immediately. More...
 
int read_string_c (openvas_connection_t *, GString **)
 Read entity and text. Free the entity immediately. More...
 
int parse_entity (const char *, entity_t *)
 Read an XML entity tree from a string. More...
 
void print_entity_to_string (entity_t entity, GString *string)
 Print an XML entity tree to a GString, appending it if string is not. More...
 
int xml_count_entities (entities_t)
 Count the number of entities. More...
 
void xml_string_append (GString *, const char *,...)
 Append formatted escaped XML to a string. More...
 
int find_element_in_xml_file (gchar *, gchar *, GHashTable *)
 Tests if an XML file contains an element with given attributes. More...
 

Typedef Documentation

◆ entities_t

typedef GSList* entities_t

Definition at line 43 of file xml.h.

◆ entity_t

typedef struct entity_s* entity_t

Definition at line 55 of file xml.h.

Function Documentation

◆ add_entity()

entity_t add_entity ( entities_t entities,
const char *  name,
const char *  text 
)

Add an XML entity to a tree of entities.

Parameters
[in]entitiesThe tree of entities
[in]nameName of the entity. Copied, copy is freed by free_entity.
[in]textText of the entity. Copied, copy is freed by free_entity.
Returns
The new entity.

Definition at line 134 of file xml.c.

References make_entity(), and name.

Referenced by handle_start_element().

135 {
136  entity_t entity = make_entity (name, text);
137  if (entities)
138  *entities = g_slist_append (entities ? *entities : NULL, entity);
139  return entity;
140 }
XML element.
Definition: xml.h:48
const char * name
Definition: nasl_init.c:524
entity_t make_entity(const char *name, const char *text)
Create an entity.
Definition: xml.c:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ compare_entities()

int compare_entities ( entity_t  entity1,
entity_t  entity2 
)

Compare two XML entity.

Parameters
[in]entity1First entity.
[in]entity2First entity.
Returns
0 if equal, 1 otherwise.

Definition at line 1279 of file xml.c.

References entity_s::attributes, compare_entities(), compare_find_attribute(), entity_s::entities, entity_s::name, and entity_s::text.

Referenced by compare_entities().

1280 {
1281  if (entity1 == NULL)
1282  return entity2 == NULL ? 0 : 1;
1283  if (entity2 == NULL)
1284  return 1;
1285 
1286  if (strcmp (entity1->name, entity2->name))
1287  {
1288  g_debug (" compare failed name: %s vs %s\n", entity1->name,
1289  entity2->name);
1290  return 1;
1291  }
1292  if (strcmp (entity1->text, entity2->text))
1293  {
1294  g_debug (" compare failed text %s vs %s (%s)\n", entity1->text,
1295  entity2->text, entity1->name);
1296  return 1;
1297  }
1298 
1299  if (entity1->attributes == NULL)
1300  {
1301  if (entity2->attributes)
1302  return 1;
1303  }
1304  else
1305  {
1306  if (entity2->attributes == NULL)
1307  return 1;
1308  if (g_hash_table_find
1310  (gpointer) entity2->attributes))
1311  {
1312  g_debug (" compare failed attributes\n");
1313  return 1;
1314  }
1315  }
1316 
1317  // FIX entities can be in any order
1318  GSList *list1 = entity1->entities;
1319  GSList *list2 = entity2->entities;
1320  while (list1 && list2)
1321  {
1322  if (compare_entities (list1->data, list2->data))
1323  {
1324  g_debug (" compare failed subentity\n");
1325  return 1;
1326  }
1327  list1 = g_slist_next (list1);
1328  list2 = g_slist_next (list2);
1329  }
1330  if (list1 == list2)
1331  return 0;
1332  /* More entities in one of the two. */
1333  g_debug (" compare failed number of entities (%s)\n", entity1->name);
1334  return 1;
1335 }
GHashTable * attributes
Attributes.
Definition: xml.h:52
int compare_entities(entity_t entity1, entity_t entity2)
Compare two XML entity.
Definition: xml.c:1279
gboolean compare_find_attribute(gpointer key, gpointer value, gpointer attributes2)
Look for a key-value pair in a hash table.
Definition: xml.c:1261
entities_t entities
Children.
Definition: xml.h:53
char * name
Name.
Definition: xml.h:50
char * text
Text.
Definition: xml.h:51
Here is the call graph for this function:
Here is the caller graph for this function:

◆ entity_attribute()

const char* entity_attribute ( entity_t  entity,
const char *  name 
)

Get an attribute of an entity.

Parameters
[in]entityEntity.
[in]nameName of the attribute.
Returns
Attribute if found, else NULL.

Definition at line 241 of file xml.c.

References entity_s::attributes, and name.

Referenced by check_response(), check_response_c(), omp_authenticate(), omp_authenticate_info_ext(), omp_authenticate_info_ext_c(), omp_get_report_ext(), omp_get_system_reports(), omp_get_system_reports_ext(), omp_get_targets(), omp_get_task_ext(), omp_get_tasks(), omp_get_tasks_ext(), omp_ping(), omp_ping_c(), omp_read_create_response(), omp_resume_task_report(), omp_resume_task_report_c(), omp_start_task_report(), and omp_start_task_report_c().

242 {
243  if (entity->attributes)
244  return (const char *) g_hash_table_lookup (entity->attributes, name);
245  return NULL;
246 }
GHashTable * attributes
Attributes.
Definition: xml.h:52
const char * name
Definition: nasl_init.c:524
Here is the caller graph for this function:

◆ entity_child()

entity_t entity_child ( entity_t  entity,
const char *  name 
)

Get a child of an entity.

Parameters
[in]entityEntity.
[in]nameName of the child.
Returns
Entity if found, else NULL.

Definition at line 220 of file xml.c.

References compare_entity_with_name(), entity_s::entities, and name.

Referenced by omp_authenticate_info_ext(), omp_authenticate_info_ext_c(), omp_ping_c(), omp_resume_task_report(), omp_resume_task_report_c(), omp_start_task_report(), omp_start_task_report_c(), and omp_task_status().

221 {
222  if (entity->entities)
223  {
224  entities_t match = g_slist_find_custom (entity->entities,
225  name,
227  return match ? (entity_t) match->data : NULL;
228  }
229  return NULL;
230 }
int compare_entity_with_name(gconstpointer entity, gconstpointer name)
Compare a given name with the name of a given entity.
Definition: xml.c:206
entities_t entities
Children.
Definition: xml.h:53
struct entity_s * entity_t
Definition: xml.h:55
const char * name
Definition: nasl_init.c:524
GSList * entities_t
Definition: xml.h:43
Here is the call graph for this function:
Here is the caller graph for this function:

◆ entity_name()

char* entity_name ( entity_t  entity)

Get the name an entity.

Parameters
[in]entityEntity.
Returns
Entity name, which is freed by free_entity.

Definition at line 191 of file xml.c.

References entity_s::name.

Referenced by compare_entity_with_name().

192 {
193  return entity->name;
194 }
char * name
Name.
Definition: xml.h:50
Here is the caller graph for this function:

◆ entity_text()

char* entity_text ( entity_t  entity)

Get the text an entity.

Parameters
[in]entityEntity.
Returns
Entity text, which is freed by free_entity.

Definition at line 178 of file xml.c.

References entity_s::text.

Referenced by omp_authenticate_info_ext(), omp_authenticate_info_ext_c(), omp_ping_c(), omp_resume_task_report(), omp_resume_task_report_c(), omp_start_task_report(), omp_start_task_report_c(), and omp_task_status().

179 {
180  return entity->text;
181 }
char * text
Text.
Definition: xml.h:51
Here is the caller graph for this function:

◆ find_element_in_xml_file()

int find_element_in_xml_file ( gchar *  file_path,
gchar *  find_element,
GHashTable *  find_attributes 
)

Tests if an XML file contains an element with given attributes.

Parameters
[in]file_pathPath of the XML file.
[in]find_elementName of the element to find.
[in]find_attributesGHashTable of attributes to find.
Returns
1 if element was found, 0 if not.

Definition at line 1463 of file xml.c.

References xml_search_data_t::find_attributes, xml_search_data_t::find_element, xml_search_data_t::found, and XML_FILE_BUFFER_SIZE.

1465 {
1466  gchar buffer[XML_FILE_BUFFER_SIZE];
1467  FILE *file;
1468  int read_len;
1469  GMarkupParser xml_parser;
1470  GMarkupParseContext *xml_context;
1471  xml_search_data_t search_data;
1472  GError *error = NULL;
1473 
1474  search_data.find_element = find_element;
1475  search_data.find_attributes = find_attributes;
1476  search_data.found = 0;
1477 
1478  /* Create the XML parser. */
1479  xml_parser.start_element = xml_search_handle_start_element;
1480  xml_parser.end_element = NULL;
1481  xml_parser.text = NULL;
1482  xml_parser.passthrough = NULL;
1483  xml_parser.error = NULL;
1484  xml_context = g_markup_parse_context_new
1485  (&xml_parser,
1486  0,
1487  &search_data,
1488  NULL);
1489 
1490  file = fopen (file_path, "r");
1491  if (file == NULL)
1492  {
1493  g_markup_parse_context_free (xml_context);
1494  g_warning ("%s: Failed to open '%s':", __FUNCTION__, strerror (errno));
1495  return 0;
1496  }
1497 
1498  while ((read_len = fread (&buffer, sizeof(char), XML_FILE_BUFFER_SIZE, file))
1499  && g_markup_parse_context_parse
1500  (xml_context, buffer, read_len, &error)
1501  && error == NULL)
1502  {
1503  }
1504  g_markup_parse_context_end_parse (xml_context, &error);
1505 
1506  fclose (file);
1507 
1508  g_markup_parse_context_free (xml_context);
1509  return search_data.found;
1510 }
Data for xml search functions.
Definition: xml.h:60
#define XML_FILE_BUFFER_SIZE
Definition: xml.c:1452
GHashTable * find_attributes
Definition: xml.h:64
gchar * find_element
Definition: xml.h:63

◆ first_entity()

entity_t first_entity ( entities_t  entities)

Return the first entity from an entities_t.

Parameters
[in]entitiesThe list of entities.
Returns
The first entity.

Definition at line 115 of file xml.c.

Referenced by xml_count_entities().

116 {
117  if (entities)
118  return (entity_t) entities->data;
119  return NULL;
120 }
XML element.
Definition: xml.h:48
Here is the caller graph for this function:

◆ free_entity()

void free_entity ( entity_t  entity)

Free an entity, recursively.

Parameters
[in]entityThe entity, can be NULL.

Definition at line 148 of file xml.c.

References entity_s::attributes, entity_s::entities, free_entity(), entity_s::name, list::next, and entity_s::text.

Referenced by check_response(), check_response_c(), free_entity(), omp_authenticate(), omp_authenticate_info_ext(), omp_authenticate_info_ext_c(), omp_get_report_ext(), omp_get_system_reports(), omp_get_system_reports_ext(), omp_get_targets(), omp_get_task_ext(), omp_get_tasks(), omp_get_tasks_ext(), omp_ping(), omp_ping_c(), omp_read_create_response(), omp_resume_task_report(), omp_resume_task_report_c(), omp_start_task_report(), omp_start_task_report_c(), parse_entity(), read_string(), read_string_c(), try_read_entity_and_string(), and try_read_entity_and_string_c().

149 {
150  if (entity)
151  {
152  g_free (entity->name);
153  g_free (entity->text);
154  if (entity->attributes)
155  g_hash_table_destroy (entity->attributes);
156  if (entity->entities)
157  {
158  GSList *list = entity->entities;
159  while (list)
160  {
161  free_entity (list->data);
162  list = list->next;
163  }
164  g_slist_free (entity->entities);
165  }
166  g_free (entity);
167  }
168 }
GHashTable * attributes
Attributes.
Definition: xml.h:52
entities_t entities
Children.
Definition: xml.h:53
char * name
Name.
Definition: xml.h:50
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xml.c:148
struct list * next
char * text
Text.
Definition: xml.h:51
Here is the call graph for this function:
Here is the caller graph for this function:

◆ next_entities()

entities_t next_entities ( entities_t  entities)

Return all the entities from an entities_t after the first.

Parameters
[in]entitiesThe list of entities.
Returns
All the entities that follow the first.

Definition at line 100 of file xml.c.

Referenced by xml_count_entities().

101 {
102  if (entities)
103  return (entities_t) entities->next;
104  return NULL;
105 }
GSList * entities_t
Definition: xml.h:43
Here is the caller graph for this function:

◆ parse_entity()

int parse_entity ( const char *  string,
entity_t entity 
)

Read an XML entity tree from a string.

Parameters
[in]stringInput string.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 XML ended prematurely.

Definition at line 1032 of file xml.c.

References context_data_t::current, context_data_t::done, context_data_t::first, free_entity(), handle_end_element(), handle_error(), handle_start_element(), and handle_text().

1033 {
1034  GMarkupParser xml_parser;
1035  GError *error = NULL;
1036  GMarkupParseContext *xml_context;
1037  context_data_t context_data;
1038 
1039  /* Create the XML parser. */
1040 
1041  xml_parser.start_element = handle_start_element;
1042  xml_parser.end_element = handle_end_element;
1043  xml_parser.text = handle_text;
1044  xml_parser.passthrough = NULL;
1045  xml_parser.error = handle_error;
1046 
1047  context_data.done = FALSE;
1048  context_data.first = NULL;
1049  context_data.current = NULL;
1050 
1051  /* Setup the XML context. */
1052 
1053  xml_context =
1054  g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
1055 
1056  /* Parse the string. */
1057 
1058  g_markup_parse_context_parse (xml_context, string, strlen (string), &error);
1059  if (error)
1060  {
1061  g_error_free (error);
1062  if (context_data.first && context_data.first->data)
1063  {
1064  free_entity (context_data.first->data);
1065  g_slist_free_1 (context_data.first);
1066  }
1067  return -2;
1068  }
1069  if (context_data.done)
1070  {
1071  g_markup_parse_context_end_parse (xml_context, &error);
1072  if (error)
1073  {
1074  g_warning (" End error: %s\n", error->message);
1075  g_error_free (error);
1076  if (context_data.first && context_data.first->data)
1077  {
1078  free_entity (context_data.first->data);
1079  g_slist_free_1 (context_data.first);
1080  }
1081  return -2;
1082  }
1083  *entity = (entity_t) context_data.first->data;
1084  g_slist_free_1 (context_data.first);
1085  return 0;
1086  }
1087  if (context_data.first && context_data.first->data)
1088  {
1089  free_entity (context_data.first->data);
1090  g_slist_free_1 (context_data.first);
1091  }
1092  return -3;
1093 }
GSList * first
The name of the very first entity.
Definition: xml.c:67
void handle_error(GMarkupParseContext *context, GError *error, gpointer user_data)
Handle an OMP XML parsing error.
Definition: xml.c:386
void handle_start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error)
Handle the start of an OMP XML element.
Definition: xml.c:285
void handle_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
Handle additional text of an XML element.
Definition: xml.c:359
GSList * current
The element currently being parsed.
Definition: xml.c:68
void handle_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
Handle the end of an XML element.
Definition: xml.c:321
XML context.
Definition: xml.c:65
struct entity_s * entity_t
Definition: xml.h:55
gboolean done
Flag which is true when the first element is closed.
Definition: xml.c:69
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xml.c:148
Here is the call graph for this function:

◆ print_entity()

void print_entity ( FILE *  stream,
entity_t  entity 
)

Print an XML entity.

Parameters
[in]entityThe entity.
[in]streamThe stream to which to print.

Definition at line 1177 of file xml.c.

References entity_s::attributes, and entity_s::name.

1178 {
1179  gchar *text_escaped = NULL;
1180  fprintf (stream, "<%s", entity->name);
1181  if (entity->attributes && g_hash_table_size (entity->attributes))
1182  g_hash_table_foreach (entity->attributes, foreach_print_attribute, stream);
1183  fprintf (stream, ">");
1184  text_escaped = g_markup_escape_text (entity->text, -1);
1185  fprintf (stream, "%s", text_escaped);
1186  g_free (text_escaped);
1187  g_slist_foreach (entity->entities, foreach_print_entity, stream);
1188  fprintf (stream, "</%s>", entity->name);
1189  fflush (stream);
1190 }
GHashTable * attributes
Attributes.
Definition: xml.h:52
entities_t entities
Children.
Definition: xml.h:53
char * name
Name.
Definition: xml.h:50
char * text
Text.
Definition: xml.h:51

◆ print_entity_format()

void print_entity_format ( entity_t  entity,
gpointer  indent 
)

Print an XML entity to stdout, recusively printing its children.

Does very basic indentation for pretty printing.

This function is used as the (callback) GFunc in g_slist_foreach.

Parameters
[in]entityThe entity.
[in]indentIndentation level, indentation width is 2 spaces. Use GINT_TO_POINTER to convert a integer value when passing this parameter.

Definition at line 1220 of file xml.c.

References entity_s::attributes, and entity_s::name.

1221 {
1222  int i = 0;
1223  int indentation = GPOINTER_TO_INT (indent);
1224  gchar *text_escaped = NULL;
1225 
1226  for (i = 0; i < indentation; i++)
1227  printf (" ");
1228 
1229  printf ("<%s", entity->name);
1230  if (entity->attributes && g_hash_table_size (entity->attributes))
1231  g_hash_table_foreach (entity->attributes, foreach_print_attribute_format,
1232  indent);
1233  printf (">");
1234 
1235  text_escaped = g_markup_escape_text (entity->text, -1);
1236  printf ("%s", text_escaped);
1237  g_free (text_escaped);
1238 
1239  if (entity->entities)
1240  {
1241  printf ("\n");
1242  g_slist_foreach (entity->entities, (GFunc) print_entity_format,
1243  GINT_TO_POINTER (indentation + 1));
1244  for (i = 0; i < indentation; i++)
1245  printf (" ");
1246  }
1247 
1248  printf ("</%s>\n", entity->name);
1249 }
GHashTable * attributes
Attributes.
Definition: xml.h:52
void print_entity_format(entity_t entity, gpointer indent)
Print an XML entity to stdout, recusively printing its children.
Definition: xml.c:1220
entities_t entities
Children.
Definition: xml.h:53
char * name
Name.
Definition: xml.h:50
char * text
Text.
Definition: xml.h:51

◆ print_entity_to_string()

void print_entity_to_string ( entity_t  entity,
GString *  string 
)

Print an XML entity tree to a GString, appending it if string is not.

empty.

Parameters
[in]entityEntity tree to print to string.
[in,out]stringString to write to (will be created if NULL).

Definition at line 1130 of file xml.c.

References entity_s::attributes, and entity_s::name.

1131 {
1132  gchar *text_escaped = NULL;
1133  g_string_append_printf (string, "<%s", entity->name);
1134  if (entity->attributes && g_hash_table_size (entity->attributes))
1135  g_hash_table_foreach (entity->attributes, foreach_print_attribute_to_string,
1136  string);
1137  g_string_append_printf (string, ">");
1138  text_escaped = g_markup_escape_text (entity->text, -1);
1139  g_string_append_printf (string, "%s", text_escaped);
1140  g_free (text_escaped);
1141  g_slist_foreach (entity->entities, foreach_print_entity_to_string, string);
1142  g_string_append_printf (string, "</%s>", entity->name);
1143 }
GHashTable * attributes
Attributes.
Definition: xml.h:52
entities_t entities
Children.
Definition: xml.h:53
char * name
Name.
Definition: xml.h:50
char * text
Text.
Definition: xml.h:51

◆ read_entity()

int read_entity ( gnutls_session_t *  session,
entity_t entity 
)

Read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1004 of file xml.c.

References try_read_entity().

Referenced by check_response(), omp_authenticate(), omp_get_system_reports(), omp_get_system_reports_ext(), omp_get_targets(), omp_get_task_ext(), omp_get_tasks(), omp_read_create_response(), omp_resume_task_report(), and omp_start_task_report().

1005 {
1006  return try_read_entity (session, 0, entity);
1007 }
int try_read_entity(gnutls_session_t *session, int timeout, entity_t *entity)
Try read an XML entity tree from the manager.
Definition: xml.c:973
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity_and_string()

int read_entity_and_string ( gnutls_session_t *  session,
entity_t entity,
GString **  string_return 
)

Try read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[out]entityPointer to an entity tree.
[out]string_returnAn optional return location for the text read from the session. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 831 of file xml.c.

References try_read_entity_and_string().

Referenced by read_entity_and_text(), and read_string().

833 {
834  return try_read_entity_and_string (session, 0, entity, string_return);
835 }
int try_read_entity_and_string(gnutls_session_t *session, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xml.c:410
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity_and_string_c()

int read_entity_and_string_c ( openvas_connection_t connection,
entity_t entity,
GString **  string_return 
)

Try read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[out]entityPointer to an entity tree.
[out]string_returnAn optional return location for the text read from the session. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 852 of file xml.c.

References try_read_entity_and_string_c().

Referenced by read_entity_and_text_c(), and read_string_c().

854 {
855  return try_read_entity_and_string_c (connection, 0, entity, string_return);
856 }
int try_read_entity_and_string_c(openvas_connection_t *connection, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xml.c:625
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity_and_text()

int read_entity_and_text ( gnutls_session_t *  session,
entity_t entity,
char **  text 
)

Read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[out]entityPointer to an entity tree.
[out]textA pointer to a pointer, at which to store the address of a newly allocated string holding the text read from the session, if the text is required, else NULL.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 871 of file xml.c.

References read_entity_and_string().

873 {
874  if (text)
875  {
876  GString *string = NULL;
877  int ret = read_entity_and_string (session, entity, &string);
878  if (ret)
879  {
880  if (string)
881  g_string_free (string, TRUE);
882  return ret;
883  }
884  *text = g_string_free (string, FALSE);
885  return 0;
886  }
887  return read_entity_and_string (session, entity, NULL);
888 }
int read_entity_and_string(gnutls_session_t *session, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xml.c:831
Here is the call graph for this function:

◆ read_entity_and_text_c()

int read_entity_and_text_c ( openvas_connection_t connection,
entity_t entity,
char **  text 
)

Read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[out]entityEntity tree.
[out]textA pointer to a pointer, at which to store the address of a newly allocated string holding the text read from the session, if the text is required, else NULL.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 903 of file xml.c.

References read_entity_and_string_c().

905 {
906  if (text)
907  {
908  GString *string = NULL;
909  int ret = read_entity_and_string_c (connection, entity, &string);
910  if (ret)
911  {
912  if (string)
913  g_string_free (string, TRUE);
914  return ret;
915  }
916  *text = g_string_free (string, FALSE);
917  return 0;
918  }
919  return read_entity_and_string_c (connection, entity, NULL);
920 }
int read_entity_and_string_c(openvas_connection_t *connection, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xml.c:852
Here is the call graph for this function:

◆ read_entity_c()

int read_entity_c ( openvas_connection_t connection,
entity_t entity 
)

Read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1018 of file xml.c.

References try_read_entity_c().

Referenced by check_response_c(), omp_resume_task_report_c(), and omp_start_task_report_c().

1019 {
1020  return try_read_entity_c (connection, 0, entity);
1021 }
int try_read_entity_c(openvas_connection_t *connection, int timeout, entity_t *entity)
Try read an XML entity tree from the manager.
Definition: xml.c:989
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_string()

int read_string ( gnutls_session_t *  session,
GString **  string 
)

Read entity and text. Free the entity immediately.

Parameters
[in]sessionPointer to GNUTLS session to read from.
[out]stringReturn location for the string.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 931 of file xml.c.

References free_entity(), and read_entity_and_string().

932 {
933  int ret = 0;
934  entity_t entity;
935 
936  if (!(ret = read_entity_and_string (session, &entity, string)))
937  free_entity (entity);
938 
939  return ret;
940 }
XML element.
Definition: xml.h:48
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xml.c:148
int read_entity_and_string(gnutls_session_t *session, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xml.c:831
Here is the call graph for this function:

◆ read_string_c()

int read_string_c ( openvas_connection_t connection,
GString **  string 
)

Read entity and text. Free the entity immediately.

Parameters
[in]connectionConnection.
[out]stringReturn location for the string.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 951 of file xml.c.

References free_entity(), and read_entity_and_string_c().

952 {
953  int ret = 0;
954  entity_t entity;
955 
956  if (!(ret = read_entity_and_string_c (connection, &entity, string)))
957  free_entity (entity);
958 
959  return ret;
960 }
XML element.
Definition: xml.h:48
int read_entity_and_string_c(openvas_connection_t *connection, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xml.c:852
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xml.c:148
Here is the call graph for this function:

◆ try_read_entity()

int try_read_entity ( gnutls_session_t *  session,
int  timeout,
entity_t entity 
)

Try read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout.

Definition at line 973 of file xml.c.

References try_read_entity_and_string().

Referenced by omp_authenticate_info_ext(), omp_get_report_ext(), omp_get_tasks_ext(), omp_ping(), and read_entity().

974 {
975  return try_read_entity_and_string (session, timeout, entity, NULL);
976 }
int try_read_entity_and_string(gnutls_session_t *session, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xml.c:410
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read_entity_and_string()

int try_read_entity_and_string ( gnutls_session_t *  session,
int  timeout,
entity_t entity,
GString **  string_return 
)

Try read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]entityPointer to an entity tree.
[out]stringAn optional return location for the text read from the session. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout.

Definition at line 410 of file xml.c.

References BUFFER_SIZE, context_data_t::current, context_data_t::done, context_data_t::first, free_entity(), handle_end_element(), handle_error(), handle_start_element(), and handle_text().

Referenced by read_entity_and_string(), try_read_entity(), and try_read_entity_and_string_c().

412 {
413  GMarkupParser xml_parser;
414  GError *error = NULL;
415  GMarkupParseContext *xml_context;
416  GString *string;
417  int socket;
418  time_t last_time;
419 
420  // Buffer for reading from the manager.
421  char *buffer = g_malloc0 (BUFFER_SIZE);
422 
423  /* Record the start time. */
424 
425  if (time (&last_time) == -1)
426  {
427  g_warning (" failed to get current time: %s\n",
428  strerror (errno));
429  g_free (buffer);
430  return -1;
431  }
432 
433  if (timeout > 0)
434  {
435  /* Turn off blocking. */
436 
437  socket = GPOINTER_TO_INT (gnutls_transport_get_ptr (*session));
438  if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
439  {
440  g_free (buffer);
441  return -1;
442  }
443  }
444  else
445  /* Quiet compiler. */
446  socket = 0;
447 
448  /* Setup return arg. */
449 
450  if (string_return == NULL)
451  string = NULL;
452  else if (*string_return == NULL)
453  string = g_string_new ("");
454  else
455  string = *string_return;
456 
457  /* Create the XML parser. */
458 
459  xml_parser.start_element = handle_start_element;
460  xml_parser.end_element = handle_end_element;
461  xml_parser.text = handle_text;
462  xml_parser.passthrough = NULL;
463  xml_parser.error = handle_error;
464 
465  context_data_t context_data;
466  context_data.done = FALSE;
467  context_data.first = NULL;
468  context_data.current = NULL;
469 
470  /* Setup the XML context. */
471 
472  xml_context =
473  g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
474 
475  /* Read and parse, until encountering end of file or error. */
476 
477  while (1)
478  {
479  ssize_t count;
480  while (1)
481  {
482  g_debug (" asking for %i\n", BUFFER_SIZE);
483  count =
484  gnutls_record_recv (*session, buffer, BUFFER_SIZE);
485  if (count < 0)
486  {
487  if (count == GNUTLS_E_INTERRUPTED)
488  /* Interrupted, try read again. */
489  continue;
490  if ((timeout > 0) && (count == GNUTLS_E_AGAIN))
491  {
492  /* Server still busy, either timeout or try read again. */
493  if ((timeout - (time (NULL) - last_time))
494  <= 0)
495  {
496  g_warning (" timeout\n");
497  fcntl (socket, F_SETFL, 0L);
498  g_markup_parse_context_free (xml_context);
499  g_free (buffer);
500  return -4;
501  }
502  continue;
503  }
504  if (count == GNUTLS_E_REHANDSHAKE)
505  /* Try again. TODO Rehandshake. */
506  continue;
507  if (context_data.first && context_data.first->data)
508  {
509  free_entity (context_data.first->data);
510  g_slist_free_1 (context_data.first);
511  }
512  if (string && *string_return == NULL)
513  g_string_free (string, TRUE);
514  if (timeout > 0)
515  fcntl (socket, F_SETFL, 0L);
516  g_markup_parse_context_free (xml_context);
517  g_free (buffer);
518  return -1;
519  }
520  if (count == 0)
521  {
522  /* End of file. */
523  g_markup_parse_context_end_parse (xml_context, &error);
524  if (error)
525  {
526  g_warning (" End error: %s\n", error->message);
527  g_error_free (error);
528  }
529  if (context_data.first && context_data.first->data)
530  {
531  free_entity (context_data.first->data);
532  g_slist_free_1 (context_data.first);
533  }
534  if (string && *string_return == NULL)
535  g_string_free (string, TRUE);
536  if (timeout > 0)
537  fcntl (socket, F_SETFL, 0L);
538  g_markup_parse_context_free (xml_context);
539  g_free (buffer);
540  return -3;
541  }
542  break;
543  }
544 
545  g_debug ("<= %.*s\n", (int) count, buffer);
546 
547  if (string)
548  g_string_append_len (string, buffer, count);
549 
550  g_markup_parse_context_parse (xml_context, buffer, count, &error);
551  if (error)
552  {
553  g_error_free (error);
554  if (context_data.first && context_data.first->data)
555  {
556  free_entity (context_data.first->data);
557  g_slist_free_1 (context_data.first);
558  }
559  if (string && *string_return == NULL)
560  g_string_free (string, TRUE);
561  if (timeout > 0)
562  fcntl (socket, F_SETFL, 0L);
563  g_markup_parse_context_free (xml_context);
564  g_free (buffer);
565  return -2;
566  }
567  if (context_data.done)
568  {
569  g_markup_parse_context_end_parse (xml_context, &error);
570  if (error)
571  {
572  g_warning (" End error: %s\n", error->message);
573  g_error_free (error);
574  if (context_data.first && context_data.first->data)
575  {
576  free_entity (context_data.first->data);
577  g_slist_free_1 (context_data.first);
578  }
579  if (timeout > 0)
580  fcntl (socket, F_SETFL, 0L);
581  g_markup_parse_context_free (xml_context);
582  g_free (buffer);
583  return -2;
584  }
585  *entity = (entity_t) context_data.first->data;
586  if (string)
587  *string_return = string;
588  if (timeout > 0)
589  fcntl (socket, F_SETFL, 0L);
590  g_slist_free (context_data.first);
591  g_markup_parse_context_free (xml_context);
592  g_free (buffer);
593  return 0;
594  }
595 
596  if ((timeout > 0) && (time (&last_time) == -1))
597  {
598  g_warning (" failed to get current time (1): %s\n",
599  strerror (errno));
600  fcntl (socket, F_SETFL, 0L);
601  g_markup_parse_context_free (xml_context);
602  g_free (buffer);
603  return -1;
604  }
605  }
606 }
GSList * first
The name of the very first entity.
Definition: xml.c:67
void handle_error(GMarkupParseContext *context, GError *error, gpointer user_data)
Handle an OMP XML parsing error.
Definition: xml.c:386
void handle_start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error)
Handle the start of an OMP XML element.
Definition: xml.c:285
void handle_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
Handle additional text of an XML element.
Definition: xml.c:359
GSList * current
The element currently being parsed.
Definition: xml.c:68
void handle_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
Handle the end of an XML element.
Definition: xml.c:321
XML context.
Definition: xml.c:65
struct entity_s * entity_t
Definition: xml.h:55
gboolean done
Flag which is true when the first element is closed.
Definition: xml.c:69
#define BUFFER_SIZE
Size of the buffer for reading from the manager.
Definition: xml.c:57
gchar * string
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xml.c:148
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read_entity_and_string_c()

int try_read_entity_and_string_c ( openvas_connection_t connection,
int  timeout,
entity_t entity,
GString **  string_return 
)

Try read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]entityPointer to an entity tree.
[out]stringAn optional return location for the text read from the session. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout.

Definition at line 625 of file xml.c.

References BUFFER_SIZE, context_data_t::current, context_data_t::done, context_data_t::first, free_entity(), handle_end_element(), handle_error(), handle_start_element(), handle_text(), openvas_connection_t::session, openvas_connection_t::socket, openvas_connection_t::tls, and try_read_entity_and_string().

Referenced by read_entity_and_string_c(), and try_read_entity_c().

627 {
628  GMarkupParser xml_parser;
629  GError *error = NULL;
630  GMarkupParseContext *xml_context;
631  GString *string;
632  time_t last_time;
633  /* Buffer for reading from the manager. */
634  char *buffer = g_malloc0 (BUFFER_SIZE);
635 
636  if (connection->tls)
637  return try_read_entity_and_string (&connection->session, timeout, entity,
638  string_return);
639 
640  /* Record the start time. */
641 
642  if (time (&last_time) == -1)
643  {
644  g_warning (" failed to get current time: %s\n",
645  strerror (errno));
646  return -1;
647  }
648 
649  if (timeout > 0)
650  {
651  /* Turn off blocking. */
652 
653  if (fcntl (connection->socket, F_SETFL, O_NONBLOCK) == -1)
654  return -1;
655  }
656 
657  /* Setup return arg. */
658 
659  if (string_return == NULL)
660  string = NULL;
661  else if (*string_return == NULL)
662  string = g_string_new ("");
663  else
664  string = *string_return;
665 
666  /* Create the XML parser. */
667 
668  xml_parser.start_element = handle_start_element;
669  xml_parser.end_element = handle_end_element;
670  xml_parser.text = handle_text;
671  xml_parser.passthrough = NULL;
672  xml_parser.error = handle_error;
673 
674  context_data_t context_data;
675  context_data.done = FALSE;
676  context_data.first = NULL;
677  context_data.current = NULL;
678 
679  /* Setup the XML context. */
680 
681  xml_context =
682  g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
683 
684  /* Read and parse, until encountering end of file or error. */
685 
686  while (1)
687  {
688  int count;
689  while (1)
690  {
691  g_debug (" asking for %i\n", BUFFER_SIZE);
692  count = read (connection->socket, buffer, BUFFER_SIZE);
693  if (count < 0)
694  {
695  if (errno == EINTR)
696  /* Interrupted, try read again. */
697  continue;
698  if (timeout > 0)
699  {
700  if ((errno == EAGAIN) || (errno == EAGAIN))
701  {
702  /* Server still busy, either timeout or try read again. */
703  if ((timeout - (time (NULL) - last_time))
704  <= 0)
705  {
706  g_warning (" timeout\n");
707  fcntl (connection->socket, F_SETFL, 0L);
708  g_markup_parse_context_free (xml_context);
709  g_free (buffer);
710  return -4;
711  }
712  }
713  continue;
714  }
715  if (context_data.first && context_data.first->data)
716  {
717  free_entity (context_data.first->data);
718  g_slist_free_1 (context_data.first);
719  }
720  if (string && *string_return == NULL)
721  g_string_free (string, TRUE);
722  if (timeout > 0)
723  fcntl (connection->socket, F_SETFL, 0L);
724  g_markup_parse_context_free (xml_context);
725  g_free (buffer);
726  return -1;
727  }
728  if (count == 0)
729  {
730  /* End of file. */
731  g_markup_parse_context_end_parse (xml_context, &error);
732  if (error)
733  {
734  g_warning (" End error: %s\n", error->message);
735  g_error_free (error);
736  }
737  if (context_data.first && context_data.first->data)
738  {
739  free_entity (context_data.first->data);
740  g_slist_free_1 (context_data.first);
741  }
742  if (string && *string_return == NULL)
743  g_string_free (string, TRUE);
744  if (timeout > 0)
745  fcntl (connection->socket, F_SETFL, 0L);
746  g_markup_parse_context_free (xml_context);
747  g_free (buffer);
748  return -3;
749  }
750  break;
751  }
752 
753  g_debug ("<= %.*s\n", (int) count, buffer);
754 
755  if (string)
756  g_string_append_len (string, buffer, count);
757 
758  g_markup_parse_context_parse (xml_context, buffer, count, &error);
759  if (error)
760  {
761  g_error_free (error);
762  // FIX there may be multiple entries in list
763  if (context_data.first && context_data.first->data)
764  {
765  free_entity (context_data.first->data);
766  g_slist_free_1 (context_data.first);
767  }
768  if (string && *string_return == NULL)
769  g_string_free (string, TRUE);
770  if (timeout > 0)
771  fcntl (connection->socket, F_SETFL, 0L);
772  g_markup_parse_context_free (xml_context);
773  g_free (buffer);
774  return -2;
775  }
776  if (context_data.done)
777  {
778  g_markup_parse_context_end_parse (xml_context, &error);
779  if (error)
780  {
781  g_warning (" End error: %s\n", error->message);
782  g_error_free (error);
783  if (context_data.first && context_data.first->data)
784  {
785  free_entity (context_data.first->data);
786  g_slist_free_1 (context_data.first);
787  }
788  if (timeout > 0)
789  fcntl (connection->socket, F_SETFL, 0L);
790  g_markup_parse_context_free (xml_context);
791  g_free (buffer);
792  return -2;
793  }
794  *entity = (entity_t) context_data.first->data;
795  if (string)
796  *string_return = string;
797  if (timeout > 0)
798  fcntl (connection->socket, F_SETFL, 0L);
799  g_markup_parse_context_free (xml_context);
800  g_free (buffer);
801  return 0;
802  }
803 
804  if ((timeout > 0) && (time (&last_time) == -1))
805  {
806  g_warning (" failed to get current time (1): %s\n",
807  strerror (errno));
808  fcntl (connection->socket, F_SETFL, 0L);
809  g_markup_parse_context_free (xml_context);
810  g_free (buffer);
811  return -1;
812  }
813  }
814 }
GSList * first
The name of the very first entity.
Definition: xml.c:67
gnutls_session_t session
Session.
void handle_error(GMarkupParseContext *context, GError *error, gpointer user_data)
Handle an OMP XML parsing error.
Definition: xml.c:386
void handle_start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error)
Handle the start of an OMP XML element.
Definition: xml.c:285
void handle_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
Handle additional text of an XML element.
Definition: xml.c:359
GSList * current
The element currently being parsed.
Definition: xml.c:68
int tls
Whether uses TCP-TLS (vs UNIX socket).
void handle_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
Handle the end of an XML element.
Definition: xml.c:321
XML context.
Definition: xml.c:65
struct entity_s * entity_t
Definition: xml.h:55
gboolean done
Flag which is true when the first element is closed.
Definition: xml.c:69
#define BUFFER_SIZE
Size of the buffer for reading from the manager.
Definition: xml.c:57
int try_read_entity_and_string(gnutls_session_t *session, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xml.c:410
gchar * string
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xml.c:148
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read_entity_c()

int try_read_entity_c ( openvas_connection_t connection,
int  timeout,
entity_t entity 
)

Try read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout.

Definition at line 989 of file xml.c.

References try_read_entity_and_string_c().

Referenced by omp_authenticate_info_ext_c(), omp_ping_c(), and read_entity_c().

991 {
992  return try_read_entity_and_string_c (connection, timeout, entity, NULL);
993 }
int try_read_entity_and_string_c(openvas_connection_t *connection, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xml.c:625
Here is the call graph for this function:
Here is the caller graph for this function:

◆ xml_count_entities()

int xml_count_entities ( entities_t  entities)

Count the number of entities.

Parameters
[in]entitiesEntities.
Returns
Number of entities.

Definition at line 1345 of file xml.c.

References first_entity(), and next_entities().

1346 {
1347  int count = 0;
1348  while (first_entity (entities))
1349  {
1350  entities = next_entities (entities);
1351  count++;
1352  }
1353  return count;
1354 }
entity_t first_entity(entities_t entities)
Return the first entity from an entities_t.
Definition: xml.c:115
entities_t next_entities(entities_t entities)
Return all the entities from an entities_t after the first.
Definition: xml.c:100
Here is the call graph for this function:

◆ xml_string_append()

void xml_string_append ( GString *  xml,
const char *  format,
  ... 
)

Append formatted escaped XML to a string.

Parameters
[in]xmlXML string.
[in]formatFormat string.
[in]...Arguments for format string.
Returns
Result of XSL transformation.

Definition at line 1366 of file xml.c.

Referenced by omp_get_system_reports_ext().

1367 {
1368  gchar *piece;
1369  va_list args;
1370 
1371  va_start (args, format);
1372  piece = g_markup_vprintf_escaped (format, args);
1373  va_end (args);
1374  g_string_append (xml, piece);
1375  g_free (piece);
1376 }
Here is the caller graph for this function: