/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License.
 *
 *
 *                      ********************
 *
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **************************************************************************************/
#ifdef         __NESSUS_ENABLE

#include "Nessus.h"
#include "Debug.h"
#include "Interface/Nessus_Interface.h"
#include "Interface/Root_Interface.h"
#include "Modules.h"
#include "Objet.h"
#include "Reseau.h"
#include "Com/Commun.h"
#include "Com/Communication.h"
#include "Equipement_Nessus.h"
#include "Equipement_Historique.h"
#include "Macro.h"

void Nouvelle_Donnee_Nessus (gpointer * Donnee)
{
	struct struct_Retour_Nessus_Donnee *Message = (struct struct_Retour_Nessus_Donnee *) Donnee;
	struct Struct_Modele_Objet *Objet = NULL;
	struct Struct_Liste_Reseau *Reseau = NULL;
	struct Struct_Equipement *Equipement = NULL;

	ERROR_if (Donnee == NULL);
	Objet = Recherche_Objet_Equipement (Message->ID_Equipement, Message->ID_Reseau);
	if (Objet == NULL) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "(Equipment unknown)");
		return;
	}
	Equipement = (struct Struct_Equipement *) Objet->Pointeur_Attacher;

	Ajouter_Texte_Nessus (Equipement, Message->Info);
	Recuperation_Nb_Octet_Recu (sizeof (struct struct_Retour_Nessus_Donnee), Reseau);
}


void Nessus_Affiche_Message (gpointer * pointer)
{
	struct Struct_Modele_Objet *Objet = (struct Struct_Modele_Objet *) pointer;

	ERROR_if (pointer == NULL);
	Initialisation_Nessus_Message ();

	switch (Objet->ID_Modele_Objet) {
	    case INFO_NESSUS_ENFANT_VULNERABILITE:
		    Afficher_Texte_Nessus (Equipement_Selectionner, Objet->Texte_Defaut, HOLE_NIVEAU_NESSUS);
		    break;
	    case INFO_NESSUS_ENFANT_POTENTIEL:
		    Afficher_Texte_Nessus (Equipement_Selectionner, Objet->Texte_Defaut, NOTE_NIVEAU_NESSUS);
		    break;
	    case INFO_NESSUS_ENFANT_INFO:
		    Afficher_Texte_Nessus (Equipement_Selectionner, Objet->Texte_Defaut, INFO_NIVEAU_NESSUS);
		    break;
	    default:
		    debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Type Enfant Nessus Inconnu");
	}
}


void Fin_Nessus (gpointer * Donnee)
{
	struct struct_Retour_Nessus_Fin *Message = (struct struct_Retour_Nessus_Fin *) Donnee;
	struct Struct_Modele_Objet *Objet = NULL;
	struct Struct_Modele_Objet *Objet_Nessus = NULL;
	struct Struct_Liste_Reseau *Reseau = NULL;
	struct Struct_Equipement *Equipement = NULL;
	gchar    *Chaine_Historique;

	ERROR_if (Donnee == NULL);
	Objet = Recherche_Objet_Equipement (Message->ID_Equipement, Message->ID_Reseau);
	if (Objet == NULL) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "(Equipment unknown)");
		return;
	}
	Equipement = (struct Struct_Equipement *) Objet->Pointeur_Attacher;

	Reseau = Recupere_Pointeur_Structure_Reseau (Message->ID_Reseau);
	Equipement->Scan_Nessus_En_Cours = FALSE;
	if (Equipement == Equipement_Selectionner) {
		Objet_Nessus = Recherche_Si_Objet_Deja_Afficher (Reseau, INFO_NESSUS, "Nessus", NULL, LISTE_INFO);
		if (Objet_Nessus != NULL)
			Desactive_Rachaichissment_Objet (Objet_Nessus, Reseau, TRUE);
	}
	Chaine_Historique = g_strdup ("Nessus - Completed scan security");
	Ajouter_Historique (Equipement, Chaine_Historique, HISTORIQUE_INFO);
	MYFREE (Chaine_Historique);
	Recuperation_Nb_Octet_Recu (sizeof (struct struct_Retour_Nessus_Fin), Reseau);
}


void Nouvelle_Info_Nessus (gpointer * Donnee)
{
	struct struct_Retour_Nessus_Info *Message = (struct struct_Retour_Nessus_Info *) Donnee;
	struct Struct_Modele_Objet *Objet = NULL;
	struct Struct_Liste_Reseau *Reseau = NULL;
	struct Struct_Equipement *Equipement = NULL;
	gchar    *Texte = NULL;

	ERROR_if (Donnee == NULL);
	Objet = Recherche_Objet_Equipement (Message->ID_Equipement, Message->ID_Reseau);
	if (Objet == NULL) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "(Equipment unknown)");
		return;
	}
	Equipement = (struct Struct_Equipement *) Objet->Pointeur_Attacher;
	Reseau = Recupere_Pointeur_Structure_Reseau (Message->ID_Reseau);

	Texte = g_strdup (Message->Info);
	if (strncmp (Message->Type, "INFO", 4) == 0) {
		Ajouter_Info_Nessus (Equipement, Message->Info, INFO_NIVEAU_NESSUS);
		if (Equipement == Equipement_Selectionner)
			Ajouter_Nouvelle_Objet (INFO_NESSUS_ENFANT_INFO, Texte, NULL, NULL, INFO_NESSUS_INFO, NULL, INFO_NESSUS,PERE_RACINE, PERE_RACINE, PERE_RACINE, ID_Reseau_En_Cours, LISTE_INFO);
	}
	if (strncmp (Message->Type, "NOTE", 4) == 0) {
		Ajouter_Info_Nessus (Equipement, Message->Info, NOTE_NIVEAU_NESSUS);
		if (Equipement == Equipement_Selectionner)
			Ajouter_Nouvelle_Objet (INFO_NESSUS_ENFANT_POTENTIEL, Texte, NULL, NULL, INFO_NESSUS_POTENTIEL, NULL, INFO_NESSUS,PERE_RACINE, PERE_RACINE, PERE_RACINE, ID_Reseau_En_Cours, LISTE_INFO);
	}
	if (strncmp (Message->Type, "HOLE", 4) == 0) {
		Ajouter_Info_Nessus (Equipement, Message->Info, HOLE_NIVEAU_NESSUS);
		if (Equipement == Equipement_Selectionner)
			Ajouter_Nouvelle_Objet (INFO_NESSUS_ENFANT_VULNERABILITE, Texte, NULL, NULL, INFO_NESSUS_VULNERABILITE, NULL, INFO_NESSUS,PERE_RACINE, PERE_RACINE, PERE_RACINE, ID_Reseau_En_Cours, LISTE_INFO);
	}

	MYFREE (Texte);	
	Recuperation_Nb_Octet_Recu (sizeof (struct struct_Retour_Nessus_Info), Reseau);
}


void Demarrage_Nessus (gpointer * Donnee)
{
	struct struct_Demarrage_Nessus *Message = (struct struct_Demarrage_Nessus *) Donnee;
	struct Struct_Modele_Objet *Objet = NULL;
	struct Struct_Modele_Objet *Objet_Nessus = NULL;
	struct Struct_Liste_Reseau *Reseau = NULL;
	struct Struct_Equipement *Equipement = NULL;

	ERROR_if (Donnee == NULL);
	Objet = Recherche_Objet_Equipement (Message->ID_Equipement, Message->ID_Reseau);
	if (Objet == NULL) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "(Equipment unknown)");
		return;
	}
	Equipement = (struct Struct_Equipement *) Objet->Pointeur_Attacher;
	Reseau = Recupere_Pointeur_Structure_Reseau (Message->ID_Reseau);
	Equipement->Scan_Nessus_En_Cours = TRUE;
	if (Equipement == Equipement_Selectionner) {
		Objet_Nessus = Ajouter_Nouvelle_Objet (INFO_NESSUS, NULL, NULL, NULL, PERE_RACINE, NULL, PERE_RACINE,PERE_RACINE, PERE_RACINE, PERE_RACINE, ID_Reseau_En_Cours, LISTE_INFO);
		Objet_Nessus->Nb_Enfant = 0;
		/*Objet_Nessus = Ajouter_Nouvelle_Objet (INFO_NESSUS_VULNERABILITE, NULL, NULL, NULL, INFO_NESSUS, NULL, PERE_RACINE, ID_Reseau_En_Cours, LISTE_INFO);
		 * Objet_Nessus->Nb_Enfant = 0;
		 * Objet_Nessus = Ajouter_Nouvelle_Objet (INFO_NESSUS_POTENTIEL, NULL, NULL, NULL, INFO_NESSUS, NULL, PERE_RACINE, ID_Reseau_En_Cours, LISTE_INFO);
		 * Objet_Nessus->Nb_Enfant = 0;
		 * Objet_Nessus = Ajouter_Nouvelle_Objet (INFO_NESSUS_INFO, NULL, NULL, NULL, INFO_NESSUS, NULL, PERE_RACINE, ID_Reseau_En_Cours, LISTE_INFO);
		 * Objet_Nessus->Nb_Enfant = 0;
		 * Objet_Nessus = Recherche_Si_Objet_Deja_Afficher (Reseau, INFO_NESSUS, "Nessus", NULL, LISTE_INFO); */
		Active_Rachaichissment_Objet (Objet_Nessus, Reseau);
	}
	Recuperation_Nb_Octet_Recu (sizeof (struct struct_Demarrage_Nessus), Reseau);
}


void Connection_Nessus (void)
{
	struct struct_Demande_Nessus Message;
	struct Struct_Liste_Reseau *Reseau = NULL;


	if (Reseau_Selectionner->User_Nessus != NULL)
		MYFREE (Reseau_Selectionner->User_Nessus);
	if (Reseau_Selectionner->Password_Nessus != NULL)
		MYFREE (Reseau_Selectionner->Password_Nessus);
	if (Reseau_Selectionner->IP_Nessus != NULL)
		MYFREE (Reseau_Selectionner->IP_Nessus);

	Reseau_Selectionner->User_Nessus = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry_Nessus_Connect_User)));
	Reseau_Selectionner->Password_Nessus = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry_Nessus_Connect_Password)));
	Reseau_Selectionner->IP_Nessus = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry_Nessus_Connect_Domaine)));
	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox_liste_Modules), 0);

	bzero (&Message, sizeof (struct struct_Demande_Nessus));
	Reseau = Recupere_Pointeur_Structure_Reseau (ID_Reseau_En_Cours);
	Message.ID_Reseau = Reseau->Id_Reseau;
	memcpy (Message.ID_Equipement, Equipement_Selectionner->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
	strcpy (Message.Adresse_Client, Equipement_Selectionner->Adresse_Ip_Char);
	strcpy (Message.Utilisateur_Nessus, Reseau_Selectionner->User_Nessus);
	strcpy (Message.Password_Nessus, Reseau_Selectionner->Password_Nessus);
	strcpy (Message.Adresse_Serveur, Reseau_Selectionner->IP_Nessus);
	Communication_Envoie_Donnee (Reseau->Pid_Agent, DEMANDE_NESSUS, (gpointer *) & Message, sizeof (Message), Reseau);
}

#endif
