52 #define G_LOG_DOMAIN "lib xml" 57 #define BUFFER_SIZE 1048576 84 entity = g_malloc (
sizeof (*entity));
85 entity->
name = g_strdup (
name ? :
"");
86 entity->
text = g_strdup (text ? :
"");
138 *entities = g_slist_append (entities ? *entities : NULL, entity);
152 g_free (entity->
name);
153 g_free (entity->
text);
227 return match ? (
entity_t) match->data : NULL;
244 return (
const char *) g_hash_table_lookup (entity->
attributes,
name);
258 if (*names && *values)
262 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
263 while (*names && *values)
266 g_hash_table_insert (entity->
attributes, g_strdup (*names),
286 const gchar ** attribute_names,
287 const gchar ** attribute_values, gpointer user_data,
301 entity =
add_entity (NULL, element_name, NULL);
306 if (data->
first == NULL)
307 data->
current = data->
first = g_slist_prepend (NULL, entity);
322 gpointer user_data, GError ** error)
332 assert (strcmp (element_name,
345 g_slist_free_1 (front);
359 handle_text (GMarkupParseContext * context,
const gchar * text, gsize text_len,
360 gpointer user_data, GError ** error)
370 gchar *old = current->
text;
371 current->
text = g_strconcat (current->
text, text, NULL);
375 current->
text = g_strdup (text);
386 handle_error (GMarkupParseContext * context, GError * error, gpointer user_data)
390 g_message (
" Error: %s\n", error->message);
411 entity_t * entity, GString ** string_return)
413 GMarkupParser xml_parser;
414 GError *error = NULL;
415 GMarkupParseContext *xml_context;
425 if (time (&last_time) == -1)
427 g_warning (
" failed to get current time: %s\n",
437 socket = GPOINTER_TO_INT (gnutls_transport_get_ptr (*session));
438 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
450 if (string_return == NULL)
452 else if (*string_return == NULL)
453 string = g_string_new (
"");
455 string = *string_return;
462 xml_parser.passthrough = NULL;
466 context_data.
done = FALSE;
467 context_data.
first = NULL;
473 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
484 gnutls_record_recv (*session, buffer,
BUFFER_SIZE);
487 if (count == GNUTLS_E_INTERRUPTED)
490 if ((timeout > 0) && (count == GNUTLS_E_AGAIN))
493 if ((timeout - (time (NULL) - last_time))
496 g_warning (
" timeout\n");
497 fcntl (socket, F_SETFL, 0L);
498 g_markup_parse_context_free (xml_context);
504 if (count == GNUTLS_E_REHANDSHAKE)
507 if (context_data.
first && context_data.
first->data)
510 g_slist_free_1 (context_data.
first);
512 if (
string && *string_return == NULL)
513 g_string_free (
string, TRUE);
515 fcntl (socket, F_SETFL, 0L);
516 g_markup_parse_context_free (xml_context);
523 g_markup_parse_context_end_parse (xml_context, &error);
526 g_warning (
" End error: %s\n", error->message);
527 g_error_free (error);
529 if (context_data.
first && context_data.
first->data)
532 g_slist_free_1 (context_data.
first);
534 if (
string && *string_return == NULL)
535 g_string_free (
string, TRUE);
537 fcntl (socket, F_SETFL, 0L);
538 g_markup_parse_context_free (xml_context);
545 g_debug (
"<= %.*s\n", (
int) count, buffer);
548 g_string_append_len (
string, buffer, count);
550 g_markup_parse_context_parse (xml_context, buffer, count, &error);
553 g_error_free (error);
554 if (context_data.
first && context_data.
first->data)
557 g_slist_free_1 (context_data.
first);
559 if (
string && *string_return == NULL)
560 g_string_free (
string, TRUE);
562 fcntl (socket, F_SETFL, 0L);
563 g_markup_parse_context_free (xml_context);
567 if (context_data.
done)
569 g_markup_parse_context_end_parse (xml_context, &error);
572 g_warning (
" End error: %s\n", error->message);
573 g_error_free (error);
574 if (context_data.
first && context_data.
first->data)
577 g_slist_free_1 (context_data.
first);
580 fcntl (socket, F_SETFL, 0L);
581 g_markup_parse_context_free (xml_context);
589 fcntl (socket, F_SETFL, 0L);
590 g_slist_free (context_data.
first);
591 g_markup_parse_context_free (xml_context);
596 if ((timeout > 0) && (time (&last_time) == -1))
598 g_warning (
" failed to get current time (1): %s\n",
600 fcntl (socket, F_SETFL, 0L);
601 g_markup_parse_context_free (xml_context);
626 entity_t * entity, GString ** string_return)
628 GMarkupParser xml_parser;
629 GError *error = NULL;
630 GMarkupParseContext *xml_context;
642 if (time (&last_time) == -1)
644 g_warning (
" failed to get current time: %s\n",
653 if (fcntl (connection->
socket, F_SETFL, O_NONBLOCK) == -1)
659 if (string_return == NULL)
661 else if (*string_return == NULL)
662 string = g_string_new (
"");
664 string = *string_return;
671 xml_parser.passthrough = NULL;
675 context_data.
done = FALSE;
676 context_data.
first = NULL;
682 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
700 if ((errno == EAGAIN) || (errno == EAGAIN))
703 if ((timeout - (time (NULL) - last_time))
706 g_warning (
" timeout\n");
707 fcntl (connection->
socket, F_SETFL, 0L);
708 g_markup_parse_context_free (xml_context);
715 if (context_data.
first && context_data.
first->data)
718 g_slist_free_1 (context_data.
first);
720 if (
string && *string_return == NULL)
721 g_string_free (
string, TRUE);
723 fcntl (connection->
socket, F_SETFL, 0L);
724 g_markup_parse_context_free (xml_context);
731 g_markup_parse_context_end_parse (xml_context, &error);
734 g_warning (
" End error: %s\n", error->message);
735 g_error_free (error);
737 if (context_data.
first && context_data.
first->data)
740 g_slist_free_1 (context_data.
first);
742 if (
string && *string_return == NULL)
743 g_string_free (
string, TRUE);
745 fcntl (connection->
socket, F_SETFL, 0L);
746 g_markup_parse_context_free (xml_context);
753 g_debug (
"<= %.*s\n", (
int) count, buffer);
756 g_string_append_len (
string, buffer, count);
758 g_markup_parse_context_parse (xml_context, buffer, count, &error);
761 g_error_free (error);
763 if (context_data.
first && context_data.
first->data)
766 g_slist_free_1 (context_data.
first);
768 if (
string && *string_return == NULL)
769 g_string_free (
string, TRUE);
771 fcntl (connection->
socket, F_SETFL, 0L);
772 g_markup_parse_context_free (xml_context);
776 if (context_data.
done)
778 g_markup_parse_context_end_parse (xml_context, &error);
781 g_warning (
" End error: %s\n", error->message);
782 g_error_free (error);
783 if (context_data.
first && context_data.
first->data)
786 g_slist_free_1 (context_data.
first);
789 fcntl (connection->
socket, F_SETFL, 0L);
790 g_markup_parse_context_free (xml_context);
798 fcntl (connection->
socket, F_SETFL, 0L);
799 g_markup_parse_context_free (xml_context);
804 if ((timeout > 0) && (time (&last_time) == -1))
806 g_warning (
" failed to get current time (1): %s\n",
808 fcntl (connection->
socket, F_SETFL, 0L);
809 g_markup_parse_context_free (xml_context);
832 GString ** string_return)
853 GString ** string_return)
876 GString *
string = NULL;
881 g_string_free (
string, TRUE);
884 *text = g_string_free (
string, FALSE);
908 GString *
string = NULL;
913 g_string_free (
string, TRUE);
916 *text = g_string_free (
string, FALSE);
1034 GMarkupParser xml_parser;
1035 GError *error = NULL;
1036 GMarkupParseContext *xml_context;
1044 xml_parser.passthrough = NULL;
1047 context_data.
done = FALSE;
1048 context_data.
first = NULL;
1054 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
1058 g_markup_parse_context_parse (xml_context,
string, strlen (
string), &error);
1061 g_error_free (error);
1062 if (context_data.
first && context_data.
first->data)
1065 g_slist_free_1 (context_data.
first);
1069 if (context_data.
done)
1071 g_markup_parse_context_end_parse (xml_context, &error);
1074 g_warning (
" End error: %s\n", error->message);
1075 g_error_free (error);
1076 if (context_data.
first && context_data.
first->data)
1079 g_slist_free_1 (context_data.
first);
1084 g_slist_free_1 (context_data.
first);
1087 if (context_data.
first && context_data.
first->data)
1090 g_slist_free_1 (context_data.
first);
1102 foreach_print_entity_to_string (gpointer entity, gpointer
string)
1115 foreach_print_attribute_to_string (gpointer
name, gpointer value,
1118 g_string_append_printf ((GString *)
string,
" %s=\"%s\"", (
char *)
name,
1132 gchar *text_escaped = NULL;
1133 g_string_append_printf (
string,
"<%s", entity->
name);
1135 g_hash_table_foreach (entity->
attributes, foreach_print_attribute_to_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);
1152 foreach_print_entity (gpointer entity, gpointer stream)
1165 foreach_print_attribute (gpointer
name, gpointer value, gpointer stream)
1167 fprintf ((FILE *) stream,
" %s=\"%s\"", (
char *)
name, (
char *) value);
1179 gchar *text_escaped = NULL;
1180 fprintf (stream,
"<%s", entity->
name);
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);
1202 foreach_print_attribute_format (gpointer
name, gpointer value, gpointer none)
1205 printf (
" %s=\"%s\"", (
char *)
name, (
char *) value);
1223 int indentation = GPOINTER_TO_INT (indent);
1224 gchar *text_escaped = NULL;
1226 for (i = 0; i < indentation; i++)
1229 printf (
"<%s", entity->
name);
1231 g_hash_table_foreach (entity->
attributes, foreach_print_attribute_format,
1235 text_escaped = g_markup_escape_text (entity->
text, -1);
1236 printf (
"%s", text_escaped);
1237 g_free (text_escaped);
1243 GINT_TO_POINTER (indentation + 1));
1244 for (i = 0; i < indentation; i++)
1248 printf (
"</%s>\n", entity->
name);
1263 gchar *value2 = g_hash_table_lookup (attributes2, key);
1264 if (value2 && strcmp (value, value2) == 0)
1266 g_debug (
" compare failed attribute: %s\n", (
char *) value);
1281 if (entity1 == NULL)
1282 return entity2 == NULL ? 0 : 1;
1283 if (entity2 == NULL)
1286 if (strcmp (entity1->
name, entity2->
name))
1288 g_debug (
" compare failed name: %s vs %s\n", entity1->
name,
1292 if (strcmp (entity1->
text, entity2->
text))
1294 g_debug (
" compare failed text %s vs %s (%s)\n", entity1->
text,
1308 if (g_hash_table_find
1312 g_debug (
" compare failed attributes\n");
1320 while (list1 && list2)
1324 g_debug (
" compare failed subentity\n");
1327 list1 = g_slist_next (list1);
1328 list2 = g_slist_next (list2);
1333 g_debug (
" compare failed number of entities (%s)\n", entity1->
name);
1371 va_start (args, format);
1372 piece = g_markup_vprintf_escaped (format, args);
1374 g_string_append (xml, piece);
1391 xml_search_handle_start_element (GMarkupParseContext *ctx,
1392 const gchar *element_name,
1393 const gchar **attribute_names,
1394 const gchar **attribute_values,
1403 if (strcmp (element_name, search_data->
find_element) == 0
1404 && search_data->
found == 0)
1406 g_debug (
"%s: Found element <%s>", __FUNCTION__, element_name);
1412 GHashTable *found_attributes;
1413 found_attributes = g_hash_table_new_full (g_str_hash, g_str_equal,
1416 while (attribute_names[index])
1418 gchar *searched_value;
1421 attribute_names[index]);
1423 && strcmp (searched_value, attribute_values[index]) == 0)
1425 g_debug (
"%s: Found attribute %s=\"%s\"",
1427 attribute_names[index], searched_value);
1428 g_hash_table_add (found_attributes, searched_value);
1432 g_debug (
"%s: Found %d of %d attributes",
1434 g_hash_table_size (found_attributes),
1437 if (g_hash_table_size (found_attributes)
1440 search_data->
found = 1;
1443 g_hash_table_destroy (found_attributes);
1447 search_data->
found = 1;
1452 #define XML_FILE_BUFFER_SIZE 1048576 1464 GHashTable* find_attributes)
1469 GMarkupParser xml_parser;
1470 GMarkupParseContext *xml_context;
1472 GError *error = NULL;
1476 search_data.
found = 0;
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
1490 file = fopen (file_path,
"r");
1493 g_markup_parse_context_free (xml_context);
1494 g_warning (
"%s: Failed to open '%s':", __FUNCTION__, strerror (errno));
1499 && g_markup_parse_context_parse
1500 (xml_context, buffer, read_len, &error)
1504 g_markup_parse_context_end_parse (xml_context, &error);
1508 g_markup_parse_context_free (xml_context);
1509 return search_data.
found;
1511 #undef XML_FILE_BUFFER_SIZE int try_read_entity_c(openvas_connection_t *connection, int timeout, entity_t *entity)
Try read an XML entity tree from the manager.
Data for xml search functions.
GHashTable * attributes
Attributes.
entity_t add_entity(entities_t *entities, const char *name, const char *text)
Add an XML entity to a tree of entities.
int compare_entities(entity_t entity1, entity_t entity2)
Compare two XML entity.
int compare_entity_with_name(gconstpointer entity, gconstpointer name)
Compare a given name with the name of a given entity.
int read_entity_and_text(gnutls_session_t *session, entity_t *entity, char **text)
Read an XML entity tree from the manager.
entity_t first_entity(entities_t entities)
Return the first entity from an entities_t.
GSList * first
The name of the very first entity.
gboolean compare_find_attribute(gpointer key, gpointer value, gpointer attributes2)
Look for a key-value pair in a hash table.
gnutls_session_t session
Session.
int read_entity(gnutls_session_t *session, entity_t *entity)
Read an XML entity tree from the manager.
void handle_error(GMarkupParseContext *context, GError *error, gpointer user_data)
Handle an OMP XML parsing error.
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.
int read_entity_and_text_c(openvas_connection_t *connection, entity_t *entity, char **text)
Read an XML entity tree from the manager.
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.
char * entity_text(entity_t entity)
Get the text an entity.
int xml_count_entities(entities_t entities)
Count the number of entities.
void print_entity_format(entity_t entity, gpointer indent)
Print an XML entity to stdout, recusively printing its children.
#define XML_FILE_BUFFER_SIZE
void handle_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
Handle additional text of an XML element.
void xml_string_append(GString *xml, const char *format,...)
Append formatted escaped XML to a string.
GHashTable * find_attributes
void print_entity(FILE *stream, entity_t entity)
Print an XML entity.
entities_t entities
Children.
GSList * current
The element currently being parsed.
int read_string(gnutls_session_t *session, GString **string)
Read entity and text. Free the entity immediately.
int try_read_entity(gnutls_session_t *session, int timeout, entity_t *entity)
Try read an XML entity tree from the manager.
char * entity_name(entity_t entity)
Get the name an entity.
int tls
Whether uses TCP-TLS (vs UNIX socket).
const char * entity_attribute(entity_t entity, const char *name)
Get an attribute of an entity.
entities_t next_entities(entities_t entities)
Return all the entities from an entities_t after the first.
int parse_entity(const char *string, entity_t *entity)
Read an XML entity tree from a string.
int read_string_c(openvas_connection_t *connection, GString **string)
Read entity and text. Free the entity immediately.
void print_entity_to_string(entity_t entity, GString *string)
Print an XML entity tree to a GString, appending it if string is not.
void handle_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
Handle the end of an XML element.
struct entity_s * entity_t
gboolean done
Flag which is true when the first element is closed.
entity_t make_entity(const char *name, const char *text)
Create an entity.
#define BUFFER_SIZE
Size of the buffer for reading from the manager.
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.
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.
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.
entity_t entity_child(entity_t entity, const char *name)
Get a child of an entity.
int read_entity_c(openvas_connection_t *connection, entity_t *entity)
Read an XML entity tree from the manager.
void free_entity(entity_t entity)
Free an entity, recursively.
int read_entity_and_string(gnutls_session_t *session, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
void add_attributes(entity_t entity, const gchar **names, const gchar **values)
Add attributes from an XML callback to an entity.