#!/usr/bin/perl
#
# $Id: fetchnes.pl,v 1.2 2004/11/23 22:43:03 greg_k Exp $
#

###############################################################################
#			      Last update: 09.10.2004			      #
#-----------------------------------------------------------------------------#
#				     Inprotect				      #
#-----------------------------------------------------------------------------#
# Copyright (C) 2002 - 2004 Inprotect.com				      #
#									      #
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of version 2 of the GNU General Public License as published #
# by the Free Software Foundation.					      #
#									      #
# 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 General Public License for    #
# more details.								      #
#									      #
# 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			      #
#									      #
# Contact Information:							      #
# Alex Zimin (alex@inprotect.com)					      #
# http://www.inprotect.com						      #
###############################################################################
# See the README.txt and/or help files for more information on how to use &   #
# configuration.							      #
# See the LICENSE.txt file for more information on the License this software  #
# is distributed under.							      #
#									      #
# This program is intended for use in an authorized manner only, and the      #
# author can not be held liable for anything done with this program, code, or #
# items discovered with this program's use.				      #
###############################################################################

use strict;
use warnings;
use DBI;

#Declare constants
use constant TRUE => 1;
use constant FALSE => 0;

#Read settings from the inprotect.cfg file
use vars qw/%CONFIG/;
load_configs();

#Redirect output to the log file
my $nessuslog = "$CONFIG{'UPDATEPLUGINSLOG'}";
open(LOG,">>$nessuslog") or die "Failed to create $nessuslog: $!\n";
*STDERR=*LOG;
*STDOUT=*LOG;

my($dbh, $sth);
my($md51, $md52) = "";

print localtime(time)." fetchnes: Downloading Nessus updates\n";

#Connecting to the database
$dbh = DBI->connect("$CONFIG{'DATABASEDSN'}:$CONFIG{'DATABASENAME'}:$CONFIG{'DATABASEHOST'}","$CONFIG{'DATABASEUSER'}","$CONFIG{'DATABASEPASSWORD'}", {
                        PrintError => 0,
                        RaiseError => 1,
                        AutoCommit => 1 } ) or die("Failed to connect : $DBI::errstr\n");

$sth=$dbh->prepare(qq{
                       SELECT inprotect_path, nessus_install_url, nessus_install_file
                       FROM inprotect_settings
                   });
$sth->execute;

my($inprotect_path, $nessus_install_url, $nessus_install_file)=$sth->fetchrow_array;
my($nessus_file)="${inprotect_path}/nessus/${nessus_install_file}";
#my($nessus_md5)="${inprotect_path}/nessus/${nessus_install_md5}";
my($nessus_md5)="${inprotect_path}/nessus/${nessus_install_file}.md5";
my($downloadurl)="${nessus_install_url}/${nessus_install_file}";
my($md5url)="${nessus_install_url}/${nessus_install_file}.md5";
my($uploaddir)="${inprotect_path}/nessus";
my($newfile)="$nessus_file.new";

if (-e $nessus_md5) {
    $md51=`cat $nessus_md5`;
}

`wget -q -P $uploaddir $downloadurl -O $nessus_file.new`;
if (-e $newfile) {
    unlink $nessus_md5;
    `md5sum -b $nessus_file | cut -d " " -f 1 > $nessus_md5`;
    `chown apache $nessus_md5`;
    `chgrp apache $nessus_md5`;

    if (-e $nessus_md5) {
        $md52=`cat $nessus_md5`;
        if ($md51 ne $md52) {
            print localtime(time)." fetchnes: Downloaded new nessus version\n";
            unlink $nessus_file;
            `mv $newfile $nessus_file`;
            `chown $nessus_file apache`;
            `chggrp $nessus_file apache`;
        }
        else {
            print localtime(time)." fetchnes: No new nessus version available\n";
            unlink $newfile;
        }

    }
}
else {
    print localtime(time)." fetchnes: Failed to download Nessus\n";
}

sub load_configs
{
    my $configfile="/usr/local/etc/inprotect.cfg";
    my $noconfig=0;
    open(CONF,"<$configfile") || $noconfig++;
    my @CONFILE=<CONF>;
    close(CONF);

    if ($noconfig) { print localtime(time)." fetchnes: No inprotect.cfg file found.\n"; }

    foreach my $line (@CONFILE)
    {
        $line =~ s/\#.*$//;
               chomp($line);
        $line =~ s/\s+$//;
               $line =~ s/^\s+//;
                      if ($line eq "") { next; }
                      my @temp=split(/=/,$line,2);
        if ($temp[0] ne "") { $CONFIG{$temp[0]}=$temp[1]; }
    }
    return;
}
