/* ***********************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.
 **************************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "Log.h"
#include "Contribution.h"
#include "Debug.h"
#include "main.h"
#include "Declaration_Constante.h"
#include "Equipement_Port.h"
#include "Snmp.h"
#include "Com/Commun.h"
#include "Interface/Combo.h"
#include "Interface/Wizard_Interface_Add_FingerPrinting.h"
#include "Interface/Wizard_Callbacks_Add_FingerPrinting.h"
#include "Com/Initialisation_Com.h"
#include "Macro.h"

extern struct Struct_Liste_Port_UDP Liste_Port_UDP[];


gboolean Transfert_html (gchar * uri)
{
	GnomeVFSHandle *handle = NULL;
	GnomeVFSResult result;
	gchar     buffer[BUF_SIZE];
	GnomeVFSFileSize bytes_read;

	ERROR_if (uri == NULL);
	result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ);

	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "result : %d   Ok=%d", result, GNOME_VFS_OK);
	if (result != GNOME_VFS_OK) {
		debug_printf (INTERFACE, D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Unable to open URI, aborting\n");
		gnome_vfs_close (handle);
		return FALSE;
	}
	while (result == GNOME_VFS_OK) {
		result = gnome_vfs_read (handle, buffer, BUF_SIZE, &bytes_read);
		buffer[bytes_read] = 0;
	
	}
	gnome_vfs_close (handle);
	return TRUE;
}


void contribution (struct Struct_Equipement *Equipement_Contrib,gchar * Nom, gchar * Type)
{
#define PORT_RECUP 15

	struct Struct_Port *Port_Equipement= NULL;
	char     *DebugSignature = NULL;
	gchar    *dLog_Equipement = NULL,*Contrib = NULL, *dEnteteFTP = NULL, *dEnteteHTTP = NULL, *dEnteteSMTP = NULL,*dEnteteSSH = NULL,*dEnteteIMAP = NULL, *dEnteteTELNET = NULL, *dArch = NULL, *Adresse_MAC_Fournisseur = NULL,*Info_Description_Snmp = NULL,*Info_System_Snmp = NULL,*Texte_Snmp_System_Device = NULL, *Key=NULL;
	GList    *Glist_Snmp = NULL;
	struct struct_Liste_Snmp *Info_Snmp = NULL;
	gint      i,j, Nb_Objet_Liste, Port[PORT_RECUP],Nb_Info_Snmp;
	
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "contribution : %s",Nom);
	ERROR_if (Equipement_Contrib == NULL);
	ERROR_if (Nom == NULL);
	ERROR_if (Type == NULL);
	

	Key	= Get_Key_Signature_Equipement(Equipement_Contrib);
	

	dEnteteFTP = url_encode (Equipement_Contrib->Entete_FTP);
	dEnteteHTTP = url_encode (Equipement_Contrib->Entete_HTTP);
	dEnteteIMAP = url_encode (Equipement_Contrib->Entete_IMAP);
	dEnteteSMTP = url_encode (Equipement_Contrib->Entete_SMTP);
	dEnteteSSH = url_encode (Equipement_Contrib->Entete_SSH);
	dEnteteTELNET = url_encode (Equipement_Contrib->Entete_TELNET);
	DebugSignature = url_encode (Equipement_Contrib->Debug_Signature);
	dArch = url_encode (Info_Arch);


	if ((Equipement_Contrib->Snmp.System_Name!=NULL) && (strlen(Equipement_Contrib->Snmp.System_Name)!=0))
		Info_System_Snmp = url_encode (Equipement_Contrib->Snmp.System_Name);
	else
		Info_System_Snmp = url_encode (NULL);


	if ((Equipement_Contrib->Snmp.Description!=NULL) && (strlen(Equipement_Contrib->Snmp.Description)!=0))
		Info_Description_Snmp = url_encode (Equipement_Contrib->Snmp.Description);
	else
		Info_Description_Snmp = url_encode (NULL);


	if ((Equipement_Contrib->Snmp_Info_Device!=NULL) && (strlen(Equipement_Contrib->Snmp_Info_Device)!=0))
		Texte_Snmp_System_Device = url_encode (Equipement_Contrib->Snmp_Info_Device);
	else
		Texte_Snmp_System_Device = url_encode (NULL);

	Adresse_MAC_Fournisseur = g_strdup_printf ("%s[%s]", Equipement_Contrib->Adresse_MAC_Char, Equipement_Contrib->Fournisseur);

#ifdef __LOG_ENABLE
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Recuperation Log");

	Etat_Contribution = CONTRIBUTION_DONNEE_OK;

	while (Log_Equipement==NULL)
		sleep (1);
	dLog_Equipement = url_encode (Log_Equipement);
#else
	Etat_Contribution = CONTRIBUTION_DONNEE_OK;
	dLog_Equipement = g_strdup ("NoLog");
#endif

	Contrib = g_strdup_printf ("http://autoscan-network.com/Add_Contributing_V19.php?Version=" VERSION_AUTOSCAN 
	"&System_Name=%s&Key=%s&MD5_signature=%s&MD5_signature_W=%s&Type=%s&SCAN_Complet=%d&SCAN_Dns=%d&SCAN_Port=%d&SCAN_Signature=%d&SCAN_Samba=%d&Scan_Snmp=%d&NB_PORT_OUVERT=%d&PORT_LISTE_OUVERT=%s&NB_PORT_FERMER=%d&PORT_LISTE_FERMER=%s&NB_PORT_FIREWALLED=%d&PORT_LISTE_FIREWALLED=%s&PORT_LISTE_UDP=%s&ETAT_SNMP=%d&ADRESSE_MAC=%s&INFO_SYSTEM_SNMP=%s&INFO_DESCRIPTION_SNMP=%s&INFO_DESCRIPTION_SNMP_DEVICE=%s&PORT_FTP=%d&PORT_FTP_FLAG=%02X&PORT_HTTP=%d&PORT_HTTP_FLAG=%02X&PORT_HTTPS=%d&PORT_HTTPS_FLAG=%02X&PORT_IMAP=%d&PORT_IMAP_FLAG=%02X&PORT_SMTP=%d&PORT_SMTP_FLAG=%02X&PORT_SSH=%d&PORT_SSH_FLAG=%02X&PORT_TELNET=%d&PORT_TELNET_FLAG=%02X&PORT_SPOOLER=%d&PORT_SPOOLER_FLAG=%02X&PORT_SIP=%d&PORT_SIP_FLAG=%02X&GATEWAY=%d&ROUTAGE=%d&ENTETE_FTP=[%s]&ENTETE_HTTP=[%s]&ENTETE_IMAP=[%s]&ENTETE_SMTP=[%s]&ENTETE_SSH=[%s]&ENTETE_TELNET=[%s]&TEMP_FTP=%d&TEMP_HTTP=%d&TEMP_IMAP=%d&TEMP_SMTP=%d&TEMP_SSH=%d&TEMP_TELNET=%d&DEBUG_SIGNATURE=%s&ARCH=%s&LOG=%s", 
	Nom, 
	Key,
	Equipement_Contrib->Md5_Signature, 
	Equipement_Contrib->Md5_Signature_W, 
	Type, 
	Equipement_Contrib->Temps_Scan_Complet,
	Equipement_Contrib->Temps_Resolv_DNS,
	Equipement_Contrib->Temps_Scan_Port,
	Equipement_Contrib->Temps_Scan_Signature,
	Equipement_Contrib->Temps_Samba,
	Equipement_Contrib->Temps_Scan_Snmp,
	
	
	Equipement_Contrib->Nb_Port_Ouvert, 
	Equipement_Contrib->PortOuvertListe, 
	Equipement_Contrib->Nb_Port_Fermer, 
	Equipement_Contrib->PortFermerListe,
	Equipement_Contrib->Nb_Port_Firewaller, 
	Equipement_Contrib->PortFirewallerListe,
	Equipement_Contrib->PortUdpListe,
	Equipement_Contrib->Snmp_Ouvert,
	Adresse_MAC_Fournisseur, 
	Info_System_Snmp,
	Info_Description_Snmp,
	Texte_Snmp_System_Device, 
	Cherche_Si_Port_Ouvert (Equipement_Contrib, PORT_FTP),
	Cherche_Flags_Port (Equipement_Contrib, PORT_FTP),
	Cherche_Si_Port_Ouvert (Equipement_Contrib, PORT_HTTP),
	Cherche_Flags_Port(Equipement_Contrib, PORT_HTTP),
	Cherche_Si_Port_Ouvert (Equipement_Contrib, PORT_HTTPS), 
	Cherche_Flags_Port(Equipement_Contrib, PORT_HTTPS),
	Cherche_Si_Port_Ouvert (Equipement_Contrib, PORT_IMAP), 
	Cherche_Flags_Port(Equipement_Contrib, PORT_IMAP),
	Cherche_Si_Port_Ouvert (Equipement_Contrib, PORT_SMTP),
	Cherche_Flags_Port(Equipement_Contrib, PORT_SMTP),
	Cherche_Si_Port_Ouvert (Equipement_Contrib, PORT_SSH),
	Cherche_Flags_Port(Equipement_Contrib, PORT_SSH),
	Cherche_Si_Port_Ouvert (Equipement_Contrib, PORT_TELNET), 
	Cherche_Flags_Port(Equipement_Contrib, PORT_TELNET),
	Cherche_Si_Port_Ouvert (Equipement_Contrib, PORT_SPOOLER),
	Cherche_Flags_Port(Equipement_Contrib, PORT_SPOOLER),
	Cherche_Si_Port_Ouvert (Equipement_Contrib, PORT_SIP),
	Cherche_Flags_Port(Equipement_Contrib, PORT_SIP),
	Equipement_Contrib->GateWay, 
	Equipement_Contrib->Routage,
	dEnteteFTP, 
	dEnteteHTTP, 
	dEnteteIMAP, 
	dEnteteSMTP,
	dEnteteSSH,
	dEnteteTELNET, 
	Equipement_Contrib->Temps_Scan_Service_Ftp,
	Equipement_Contrib->Temps_Scan_Service_Http,
	Equipement_Contrib->Temps_Scan_Service_Imap,
	Equipement_Contrib->Temps_Scan_Service_Smtp,
	Equipement_Contrib->Temps_Scan_Service_Ssh,
	Equipement_Contrib->Temps_Scan_Service_Telnet,
	DebugSignature, 
	dArch,
	dLog_Equipement);
	printf("%s\n",Contrib);


	for (i = 0; i < strlen (Contrib); i++) {
		if ((Contrib[i] == ' ') || (Contrib[i] == ' &'))
			Contrib[i] = '_';
	}

	if (!Transfert_html (Contrib))
		debug_printf (INTERFACE, D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Error send\n");

	MYFREE (dEnteteFTP);
	MYFREE (dEnteteHTTP);
	MYFREE (dEnteteIMAP);
	MYFREE (dEnteteTELNET);
	MYFREE (dArch);
	MYFREE (Contrib);
	MYFREE (Adresse_MAC_Fournisseur);
	MYFREE (Info_System_Snmp);
	MYFREE (Info_Description_Snmp);
	MYFREE (dLog_Equipement);
	MYFREE (DebugSignature);
	MYFREE (Key);
}



gpointer Thread_Contribution_Fingerprinting (void)
{
	Create_Log(CATEG_THREAD_CONTRIBUTION,NULL,NULL,TRUE);
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Thread_Contribution_Fingerprinting");
	contribution (Equipement_Selectionner,New_System_Name,New_System_Type_Char);
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Fin Thread Contribution");
	Stop_Thread (0);
}




void contribution_Erreur (gchar * Message_Erreur)
{
	gchar    *Contrib = NULL, *Message_URL = NULL, *Message = NULL, *dArch = NULL;
	int       i;

	ERROR_if (Message_Erreur == NULL);
	if (Info_Message_Critique==NULL)
		Message = Message_Erreur;
	else
		Message = g_strdup_printf ("\n\n%s\n\n%s",Message_Erreur,Info_Message_Critique);
	
	Message_URL = url_encode (Message);
	dArch = url_encode (Info_Arch);
	Contrib = g_strdup_printf ("http://autoscan-network.com/ErreurV6.php?Version=" VERSION_AUTOSCAN "&Message_Erreur=%s&ARCH=%s", Message_URL, dArch);
	MYFREE (Message_URL);

	for (i = 0; i < strlen (Contrib); i++) {
		if (Contrib[i] == ' ')
			Contrib[i] = '_';
	}
	if (!Transfert_html (Contrib))
		debug_printf (INTERFACE, D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Error send\n");
	MYFREE (Contrib);
	MYFREE (dArch);
}



void Recuperation_Log_Equipement(gpointer * Donnee){
#ifdef __LOG_ENABLE
	struct struct_Retour_Log_Equipement *Message = (struct struct_Retour_Log_Equipement *) Donnee;
	
	ERROR_if (Donnee == NULL);
	ERROR_if (Message->Info == NULL);
	Log_Equipement = g_strdup(Message->Info);
#endif
}
