1010 * General management of agent installers.
1111 */
1212
13+ #include "gmp_base.h"
1314#include "manage_agent_installers.h"
1415#include "manage_sql_agent_installers.h"
1516#include <gvm/util/jsonpull.h>
@@ -487,17 +488,21 @@ get_agent_installer_data_from_json (cJSON *json,
487488 *
488489 * @param[in] entry The JSON object to process.
489490 * @param[in] rebuild Whether to also update installers with old timestamps.
491+ * @param[in,out] installers_list_sql String buffer for a list of UUIDs
492+ * of installers in the feed in SQL-quoted
493+ * form.
490494 *
491495 * @return 0 success, -1 error.
492496 */
493497static int
494- agent_installers_json_handle_entry (cJSON * entry , gboolean rebuild )
498+ agent_installers_json_handle_entry (cJSON * entry , gboolean rebuild ,
499+ GString * installers_list_sql )
495500{
496501 agent_installer_data_t data ;
497502 agent_installer_t agent_installer ;
498- int trash = 0 ;
499503 time_t db_modification_time = 0 ;
500504 int ret = 0 ;
505+ gchar * quoted_uuid ;
501506
502507 memset (& data , 0 , sizeof (data ));
503508 if (get_agent_installer_data_from_json (entry , & data ))
@@ -508,30 +513,27 @@ agent_installers_json_handle_entry (cJSON *entry, gboolean rebuild)
508513 return -1 ;
509514 }
510515
511- agent_installer = agent_installer_by_uuid (data .uuid , FALSE );
516+ agent_installer = agent_installer_by_uuid (data .uuid );
512517 if (agent_installer )
513518 {
514519 db_modification_time
515- = agent_installer_modification_time (agent_installer , FALSE);
516- }
517- else
518- {
519- agent_installer = agent_installer_by_uuid (data .uuid , TRUE);
520- if (agent_installer )
521- {
522- trash = 1 ;
523- db_modification_time
524- = agent_installer_modification_time (agent_installer , TRUE);
525- }
520+ = agent_installer_modification_time (agent_installer );
526521 }
527522
528523 if (agent_installer == 0 )
529524 ret = create_agent_installer_from_data (& data );
530525 else if (rebuild || db_modification_time < data .modification_time )
531- ret = update_agent_installer_from_data (agent_installer , trash , & data );
526+ ret = update_agent_installer_from_data (agent_installer , & data );
532527 else
533528 g_debug ("%s: skipping agent installer %s" , __func__ , data .uuid );
534529
530+ quoted_uuid = sql_quote (data .uuid );
531+ g_string_append_printf (installers_list_sql ,
532+ "%s'%s'" ,
533+ installers_list_sql -> len ? ", " : "" ,
534+ quoted_uuid );
535+ g_free (quoted_uuid );
536+
535537 g_ptr_array_free (data .cpes , TRUE);
536538 return ret ? -1 : 0 ;
537539}
@@ -544,13 +546,17 @@ agent_installers_json_handle_entry (cJSON *entry, gboolean rebuild)
544546 * @param[in] parser The JSON parser.
545547 * @param[in] parser The JSON parser event data structure.
546548 * @param[in] rebuild Whether to also update installers with old timestamps.
549+ * @param[in,out] installers_list_sql String buffer for a list of UUIDs
550+ * of installers in the feed in SQL-quoted
551+ * form.
547552 *
548553 * @return 0 success, -1 error.
549554 */
550555static int
551556agent_installers_json_handle_list_items (gvm_json_pull_parser_t * parser ,
552557 gvm_json_pull_event_t * event ,
553- gboolean rebuild )
558+ gboolean rebuild ,
559+ GString * installers_list_sql )
554560{
555561 gvm_json_pull_parser_next (parser , event );
556562 while (event -> type != GVM_JSON_PULL_EVENT_ARRAY_END )
@@ -569,7 +575,9 @@ agent_installers_json_handle_list_items (gvm_json_pull_parser_t *parser,
569575 cJSON_Delete (entry );
570576 return -1 ;
571577 }
572- else if (agent_installers_json_handle_entry (entry , rebuild )) {
578+ else if (agent_installers_json_handle_entry (entry ,
579+ rebuild ,
580+ installers_list_sql )) {
573581 cJSON_Delete (entry );
574582 return -1 ;
575583 }
@@ -606,6 +614,7 @@ sync_agent_installers_with_feed (gboolean rebuild)
606614 gchar * feed_owner_uuid , * feed_owner_name ;
607615 gvm_json_pull_parser_t parser ;
608616 gvm_json_pull_event_t event ;
617+ GString * installers_list_sql ;
609618
610619 g_info ("Updating agent installers%s" , rebuild ? " (rebuild)" : "" );
611620 update_meta_agent_installers_last_update ();
@@ -656,7 +665,10 @@ sync_agent_installers_with_feed (gboolean rebuild)
656665 return -1 ;
657666 }
658667
659- if (agent_installers_json_handle_list_items (& parser , & event , rebuild ))
668+ installers_list_sql = g_string_new ("" );
669+
670+ if (agent_installers_json_handle_list_items (& parser , & event , rebuild ,
671+ installers_list_sql ))
660672 {
661673 gvm_json_pull_parser_cleanup (& parser );
662674 gvm_json_pull_event_cleanup (& event );
@@ -667,6 +679,34 @@ sync_agent_installers_with_feed (gboolean rebuild)
667679 gvm_json_pull_parser_cleanup (& parser );
668680 gvm_json_pull_event_cleanup (& event );
669681 fclose (stream );
682+
683+ if (installers_list_sql -> len )
684+ {
685+ iterator_t deleted_installers ;
686+
687+ sql_begin_immediate ();
688+
689+ sql ("DELETE FROM agent_installer_cpes WHERE agent_installer NOT IN"
690+ " (SELECT id FROM agent_installers WHERE uuid IN (%s));" ,
691+ installers_list_sql -> str );
692+
693+ init_iterator (& deleted_installers ,
694+ "DELETE FROM agent_installers WHERE uuid NOT IN (%s)"
695+ " RETURNING uuid;" ,
696+ installers_list_sql -> str );
697+ while (next (& deleted_installers ))
698+ {
699+ log_event ("agent_installer" , "Agent Installer" ,
700+ iterator_string (& deleted_installers , 0 ),
701+ "deleted" );
702+ }
703+ cleanup_iterator (& deleted_installers );
704+
705+ sql_commit ();
706+ }
707+ else
708+ g_warning ("%s: No agent installers found in metadata file" , __func__ );
709+
670710 g_info ("Finished updating agent installers" );
671711 return 0 ;
672712}
0 commit comments