#!/usr/bin/perl
###############################################################################
#			      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, $md51, $md52) = "";

print localtime(time)." fetchupdates: 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_plugins_url, nessus_plugins_file, nessus_plugins_md5
	FROM inprotect_settings
	});
$sth->execute;

my($inprotect_path, $nessus_plugins_url, $nessus_plugins_file, $nessus_plugins_md5)=$sth->fetchrow_array;
my($plugins_file)="${inprotect_path}/nessus/${nessus_plugins_file}";
my($plugins_md5)="${inprotect_path}/nessus/${nessus_plugins_md5}";
my($downloadurl)="${nessus_plugins_url}/${nessus_plugins_file}";
my($md5url)="${nessus_plugins_url}/${nessus_plugins_md5}";
my($uploaddir)="${inprotect_path}/nessus";

if (-e $plugins_md5) {
	$md51=`md5sum -b $plugins_md5`;
	unlink $plugins_md5;
}
`wget -q -P $uploaddir $md5url`; 

if (-e $plugins_md5) {
	$md52=`md5sum -b $plugins_md5`;
	`chown apache $plugins_md5`;
	`chgrp apache $plugins_md5`;

	if ($md51 ne $md52) {
		if (-e $plugins_file) {
			unlink $plugins_file;
		}
		`wget -q -P $uploaddir $downloadurl`;
		if (-e $plugins_file) {	
			`chown apache $plugins_file`;
			`chgrp apache $plugins_file`;
			print localtime(time)." fetchupdates: Downloaded new Nessus plugins\n";
		}
		else {
			print localtime(time)." fetchupdates: Can not download plugins\n";
		}
	}
	else {
		print localtime(time)." fetchupdates: No new plugins available\n";
	}
}
else {
	print localtime(time)." fetchupdates: Failed to download Nessus plugins\n";
}
exit(0);

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)." fetchupdates: 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;
}
