#!/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;

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

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

#Use Perl modules
use DBI;

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

#Declare variables to use
my $servreg;
my($stopq, $stopid, $stopcmd, $schedid, $username, $email, $schedule_type, $day_of_week, $day_of_month, $time);
my ($stime, $scantime, $sid, $sth_sel, $scanq, $pm);
my ($infield1, $infield2, $infield3, $infield4, $infield5, $infield6, $infield7, $insertid);
my ($id, $host, $str, $dbh, $sth, $sth_ins, $sth_upd, $timeout );

#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{
	UPDATE nessus_servers
	SET checkin_time=now(), current_scans=0
	WHERE id=$serverid
	});
$sth->execute;

close LOG;
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)." inprotect_reset: - 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;
}
