#!/usr/bin/perl -w

#$Header: /mnt/u1/cvs/logtrend/logtrend-doc/doc/LogTrend-SimpleAgent-0.82.2/bin/AgentDescriptionToDB,v 1.1 2002/04/10 01:18:46 jdive Exp $
#
#**************************************************
#                                                 *
# LogTrend 1.0.0.0 - Atrid Systemes               *
#                                                 *
# Author : Laurent Simonneau l.simonneau@atrid.fr *
# Author : David Mascle d.mascle@atrid.fr         *
#                                                 *
#**************************************************
#
#$Log: AgentDescriptionToDB,v $
#Revision 1.1  2002/04/10 01:18:46  jdive
#fixed description, uncompressed simpleagent
#
#Revision 1.13.2.1  2002/02/14 15:43:30  lsimonneau
#Minor features enhancement
#Add a Sender attribut in the Mail tag of agents config file.
#Use SMTP Server when available or /usr/lib/sendmail otherwise.
#Add the -s <smtp_server> option to MailBridge.pl.
#
#Revision 1.13  2002/02/06 13:06:39  lsimonneau
#Minor bugfixes.
#
#Revision 1.12  2001/12/10 16:58:34  lsimonneau
#Use GnuPG::Interface instead of Crypt::RSA for perl 5.005 compatibility.
#
#Revision 1.11  2001/11/16 10:32:28  lsimonneau
#Major feature enhancement : Implementation of authentification with RSA signatu
#re.
#
#IMPORTANT : Agents are incompatible with older version.
#
#Revision 1.10  2001/10/12 16:30:30  lsimonneau
#Major feature improvement in MailBridge.
#Now, queued message when execution fail, automaticaly reexecute these mail
#And send alarm mail to Agent or Server admin.
#
#Revision 1.9  2001/08/16 14:02:17  lsimonneau
#Modification du texte d'aide (--help).
#
#Revision 1.8  2001/07/27 14:41:23  lsimonneau
#Include UpdateAgentDescription in AgentDescriptionToDB.
#
#Revision 1.7  2001/06/07 14:08:54  slhullier
#
#Passage de  unshift @INC,'..';  aux packages Logtrend::....
#
#Revision 1.6  2001/03/23 17:14:35  slhullier
#
#MailBridge: todo en XML, donnees gzip
#Pas fini : gunzip description
#
#Revision 1.5  2001/03/23 16:29:01  slhullier
#
#Nom
#
#Revision 1.4  2001/03/07 15:15:30  slhullier
#
#LogDie mis en place sur le serveur et l'agent Linux
#
#Revision 1.3  2001/02/20 17:01:27  slhullier
#
#Mise en commentaire (+tab etc)
#
#Revision 1.2  2001/02/09 13:25:30  slhullier
#
#
#MailBridge pour AgentDescriptionToDB
#Ok pour la connection directe.
#Ok pour le mail cote emission.
#
#Revision 1.1.1.1  2001/02/06 09:48:28  fdubuy
#
#First CVS version : all work done by D.Mascle
#
#

use strict;
use LogTrend::Agent::AgentDescriptionToDB::PostgreSQLDBLayer;
use LogTrend::Agent::AgentDescriptionToDB::XMLToDB;
use LogTrend::Agent::AgentDescriptionToDB::DirectDescriptionSender;
use LogTrend::Agent::AgentDescriptionToDB::MailDescriptionSender;
use LogTrend::Crypto::Signature qw(sign_message);
use MIME::Entity;

##******************************************************************************
sub display_version
{
    print "\nAgentDescriptionToDB version 1.0.0.0\n\n";
}

##******************************************************************************
sub display_help
{
    print "\n";
    print "Usage : AgentDescriptionToDB -f <file> \\\n";
    print "                             -g <LogTrend GnuPG home> \\\n";
    print "                              [-S <agent to upgrade source>] \\\n";
    print "                              [-N <agent to upgrade number>] \\\n";
    print "                               -d <database> \\\n";
    print "                              [-H <host>] [-P <port>]    \\\n";
    print "                               -u <user>   -p <password> \\\n";
    print "          [-M <mailbridge-adress> -a <admin-mail> [-m <sender-mail>] [-s <smtp-serveur>]]\n";
    print "        AgentDescriptionToDB --version\n";
    print "\n";
    print "Options :\n";
    print "   -f : agent's description filename.\n";
    print "   -g : LogTrend GnuPG home.\n";
    print "   -S : the source of the agent to upgrade (optional).\n";
    print "   -N : the number of the agent to upgrade (optional).\n";
    print "   -d : database name.\n";
    print "   -H : host name of the database server (optional).\n";
    print "   -P : port number of the database server (optional).\n";
    print "   -u : database user.\n";
    print "   -p : database user's password.\n";
    print "   -M : email adress for MailBridge (optional).\n";
    print "   -a : mail of the admin (where errors messages are sent) (optional).\n";
    print "   -m : mail of the sender (the address in the 'from' field of the mail.\n".
	  "        By default, the address specified in -a is used)  (optional).\n";
    print "   -s : SMTP serveur to use to send mail (optional).\n";
    print "\n";
}

##******************************************************************************
sub main
{
    my $agentfilename = "";
    my $gpghome = "";
    my $databasename = "";
    my $ancestor_source;
    my $ancestor_number;
    my $host = "";
    my $port = "";
    my $username = "";
    my $password = "";
    my $mailforbridge = "";
    my $admin_mail = "";
    my $sender_mail = "";
    my $smtp_serveur = "";

    if( @ARGV == 0 )
    {
	display_help();
    }
    elsif( @ARGV == 1 )
    {
	if( $ARGV[0] eq "--version")
	{ display_version(); }
	elsif( $ARGV[0] eq "--help" )
	{ display_help(); }
	else
	{ display_help(); } 
    }
    else
    {
	for( my $i = 0; $i<(@ARGV-1); $i++ )
	{
	    if( $ARGV[$i] eq "-f" )
	    { $agentfilename = $ARGV[$i + 1]; }
	    if( $ARGV[$i] eq "-g" )
	    { $gpghome = $ARGV[$i + 1]; }
	    elsif ($ARGV[$i] eq "-S") {
		$ancestor_source =  $ARGV[$i + 1];
	    }
	    elsif ($ARGV[$i] eq "-N") {
		$ancestor_number =  $ARGV[$i + 1];
	    }
	    elsif ($ARGV[$i] eq "-d")
	    { $databasename = $ARGV[$i + 1]; }
	    elsif ($ARGV[$i] eq "-H")
	    { $host = $ARGV[$i + 1]; }
	    elsif ($ARGV[$i] eq "-P")
	    { $port = $ARGV[$i + 1]; }
	    elsif ($ARGV[$i] eq "-u")
	    { $username = $ARGV[$i + 1]; }
	    elsif ($ARGV[$i] eq "-p")
	    { $password = $ARGV[$i + 1]; }
	    elsif ($ARGV[$i] eq "-M")
	    { $mailforbridge = $ARGV[$i + 1]; }
	    elsif ($ARGV[$i] eq "-a")
	    { $admin_mail = $ARGV[$i + 1]; }
	    elsif ($ARGV[$i] eq "-m")
	    { $sender_mail = $ARGV[$i + 1]; }
	    elsif ($ARGV[$i] eq "-s")
	    { $smtp_serveur = $ARGV[$i + 1]; }
	}

	$gpghome = "/etc/LogTrend/.gnupg" if ((! defined $gpghome) or $gpghome eq "");
	if( ($agentfilename eq "") || ($databasename eq "") ||
	    ($username      eq "") || ($password     eq "")) {
	    display_help();
	    exit(0);
	}
	
	$sender_mail = $admin_mail if $sender_mail eq "";

	my $descriptionSender;
	if( $mailforbridge eq "" )
	{	     
	    $descriptionSender = 
	      LogTrend::Agent::AgentDescriptionToDB::DirectDescriptionSender->new($databasename, $host, $port, $username, $password,
										  $ancestor_source, $ancestor_number);
	}
	else
	{
	    $descriptionSender = 
	      LogTrend::Agent::AgentDescriptionToDB::MailDescriptionSender->new($databasename, $host, $port, $username, $password,
										$mailforbridge, $smtp_serveur, $admin_mail, $sender_mail,
										$ancestor_source, $ancestor_number);
	}
	
	my $response;
	if(open(F_DESCFILE, $agentfilename)) {	  

	    my $xmldata;
	    while(<F_DESCFILE>) {$xmldata .= $_;}
	    
	    my $signature = sign_message($gpghome, $xmldata);
	    
	    $response = $descriptionSender->SendDescription($xmldata, $signature);
	}
	else {
	    $response = "ERROR IO Can't open $agentfilename : $!";
	}
	
	if($response eq "OK") {
	    print "OK\n";
	}
	else {
	    print $response."\n";
	}
    }
}

#**************************************************************************
main();
