#!/usr/bin/perl -w 
#-----------------------------------------------------------------------------#
#  Copyright (C) 2001 - 2002 Multi-Tech Systems Inc., USA
#  All Rights Reserved
#  Multi-Tech Systems, Inc., 2205, Woodale Dr, Mounds View MN USA
#-----------------------------------------------------------------------------#
#  ModName: Web GUI - Interface
#  Version: 3.25
#  Date: 2005-10-05
#  Modification History - Begin
#20040413 Javeed Had to add checkintrusion update for all iptablerules file update
#20040419 Prabha - Support for Squid 2.5STABLE5 changes.
#  Modification History - End


#Description	:This script is responsible for interacting with the web and
#		 updating the junkbstr.ini file and the squid.conf file
#                according to the changes made by the user. Also by SDK

$DIR = "/etc/multiconf";
$SQUIDDIR = "/var/chroot-squid/usr/local/squid/etc";
$JUNKBSTRDIR ="/usr/local/src/ijb20-js";
$SCRIPTS ="/etc/multiconf/scripts";

#For the above signals, make the handler as 'inthdlr'
$SIG{QUIT} = 'inthdlr';
$SIG{INT} = 'inthdlr';
$SIG{TERM} = 'inthdlr';

#In inthdlr, we delete the file tempfile and then assign the default handlers
#for the signals and come out.

sub inthdlr {
#	`rm -f $DIR/$file`;
#	$SIG{QUIT} = 'DEFAULT';
#	$SIG{INT} = 'DEFAULT';
#	$SIG{TERM} = 'DEFAULT';
#	die;
}

#This subroutine is added to avoid race condition while adding Iptablerules
sub saverules
{
        open(IPTABLERULES,"/etc/multiconf/iptablerules");
        flock(IPTABLERULES,2);
#        system("iptables-save > /etc/multiconf/iptablerules");
        `/etc/multiconf/scripts/iptableop save`;
        close(IPTABLERULES);
}

##############################################################################
# Function Name	: openfile
# Description   : To open a file in read, write or append mode
# Parameters	: filehandle,filename with mode( > or >>)	
##############################################################################


sub openfile {
        open($_[0],$_[1]) ||
	die "Open failed $_[0]\r\n";
}
	



##############################################################################
# Function Name	: savetemp
# Description   : This function moves the temporary file into the configuration 
#		  file(squid.conf) after the changes are updated.
# Parameters	: temporary file, configuration file
# Return Value  : None
##############################################################################

sub savetemp {
	`cp $DIR/$_[0] $_[1]`;
#	`mv $DIR/$_[0] $_[1]`;
}

##############################################################################
# Function Name	: gettempfile
# Description   :The function will call the function rand, get the value, 
# find out if there is a file already existing with that name. If so, it will
# repeat the process. Until it gets a name which is not been used by anyone.
# Parameters	: none
# Return Value  : filename	
##############################################################################
sub gettempfile
{
	while (1)
	{
	$randomno = rand;
	$randomno *= 100000000;
	$filename = sprintf "tmp%ld", $randomno;

	if (!-e $filename)
	{
		return $filename;
	}
	}
}

#Senthil 20050912 - Added for Flash ISO

sub qmailconc
{
	# Get Qmail Status and change the concurrencyincoming if smtp proxy is enabled
	open (PROCS, "/etc/multiconf/processstatus");
	while(<PROCS>)
	{
		if(/QMAIL/)
		{
			my @splitvals = split(/=/, $_);
			chomp(@splitvals);
			$qmailstat = $splitvals[1];
			last;
		}
	}
	close(PROCS);
	if($qmailstat eq "ON")
	{
		my $process = 1;
		my $hardwaretype = "G";
		$OURCPU = `cat /proc/cpuinfo | grep "cpu family" | cut -d: -f2`;
		chomp ($OURCPU);
		# Now any Intel 686 processor we take it as P3
	        if ($OURCPU == 6)
        	{
                	$hardwaretype = "P3";
	        }
	
        	#Check if it is a P4 processor (RF760 VPN)
	        elsif ($OURCPU == 15)
        	{
	                $hardwaretype = "P4";
        	}

		if($hardwaretype ne "G")
		{
			$process = 2;
		}
		$concurrencyincoming = $process * 3;
		open (CONC, ">/var/qmail/control/concurrencyincoming");
		print CONC "$concurrencyincoming\n";
		close (CONC);

		system ("killall -v qmail-smtpd");
		system ("/etc/init.d/qmail resmtpd");
	}
}

#Senthil 20050811 - Added for Flash ISO
#Get Disk Usage

sub getdiskusage {
	my @disk = @_;
	chomp(@disk);
	my $logpart = $disk[0];
	my $block_size=`df | grep $logpart | tr -s " " | cut -d " " -f2`;
	chomp ($block_size);
	$block_size = $block_size/1024;
	my $maxlogsize = $block_size * 0.15;
	my $logsize = $maxlogsize * 0.2;

	my $du = `du -sb /var/log`;
	chomp ($du);
        my @values = split (' ', $du);
       	chomp ($values[0]);
        $du = $values[0];
        my $logusage = $du / (1024 * 1024);
	return ($logsize, $logusage);
}
#Check for disk space of /var/log partation to take the backup of category backup
sub checkdiskspace
{
	#Find out where the hard disk is...
	open (LILOCONF, "/etc/lilo.conf");
	while (<LILOCONF>)
	{
        	if (/^boot=/)
	        {
        	        @splitvals = split (/=/, $_);
                	$hdisk = $splitvals[1];
	                chomp ($hdisk);
        	        last;
	        }
	}
	close (LILOCONF);

	my $count = 0;
	my $logusage;
	my $logsize;
	my @logsizeusage;
	$logpart = $hdisk . "3";
	#First check the root partition
	$logdisk=`df | grep $logpart | tr -s " " | cut -d " " -f5`;
	chop ($logdisk);
	chop ($logdisk);
	if ($logdisk >= 70){
		
		system ("rm -f /var/log/qmail/\@*");
	        system ("rm -f /var/log/qmail/smtpd/\@*");
		@logsizeusage = getdiskusage($logpart);
		chomp(@logsizeusage);
		($logsize,$logusage) = @logsizeusage;

	        while ($logusage > $logsize)
        	{
                	chdir ("/var/log/backup");
	                $oldestfile = `ls -clt | grep -v ^d | tr -s " " | cut -d " " -f9 | tail -1`;
        	        chomp ($oldestfile);
	                system ("rm -f $oldestfile");
			@logsizeusage = getdiskusage($logpart);
			chomp(@logsizeusage);
			($logsize,$logusage) = @logsizeusage;
			if($logusage > $logsize){
	                	chdir ("/var/log/backup/iptables");
		                $oldfile = `ls -clt | grep -v ^d | tr -s " " | cut -d " " -f9 | tail -1`;
        		        chomp ($oldfile);
	                	system ("rm -f $oldfile");
			}
			if($count == 0){	        
		        	chdir ("/var/log/qmail/qmaillog");
		        	$filecount = `ls | wc -l`;
			        chomp ($filecount);
        			$filecount = sprintf ("%d", ($filecount * 75) / 100);
			        for ($i = $filecount; $i > 1; $i--)
			        {
	        		        $oldefile = `ls -clt | grep -v ^d | tr -s " " | cut -d " " -f9 | tail -1`;
	                		chomp ($oldefile);
		                	if ($oldefile eq "")
			                {
        			                last;
                			}
		        	        system ("rm -f $oldefile");
	        		}
				$count++;
			}
			if(($oldfile eq "") && ($oldestfile eq ""))
			{
				last;
			}
			@logsizeusage = getdiskusage($logpart);
			chomp(@logsizeusage);
			($logsize,$logusage) = @logsizeusage;
		}

	}

}

sub backupb4update
{
	# Take the backup of database files before the update takes place

	#Senthil 20050811 - Added for FlashISO
	#To Take the backup do forced log rotate
	system("/usr/sbin/logrotate -f /etc/logrotate.conf");

	#Check the disk space to store the category backup	
	checkdiskspace();	

	# Check for directory existence
	   if (-e "/var/log/URL")
	   {
       		#print "Directory Exists\n";
	   }
	   else
           {
        	`mkdir /var/log/URL`; # Create new directory named URL
           }

	# Change to the backup directory
 	   chdir("/var/log/URL");
                                                                                
	# Take the backup of previous successfully updated database
	
	# Tar the files listed in the category_backup file
   	   `tar -zcvf $_[0] --files-from=/etc/multiconf/category_backup`;

}

sub restore_category_database
{

#       If updation is not successful
#              (1) Could not connect to the server at all
#              (2) The half-file is updated.
#                  Here case (2) is considered.

=flashiso
	if ($_[0] eq "UserCategory.tar.gz") 
	{
		# Change to root directory since all files listed from the root directory
                    chdir ("/");
                                                                                                                           
                # Use Previously Updated Database
                    `tar -zxvf /var/log/URL/$_[0]`;
		# Return Success
		     return $?;
	}
=cut

	system("sync");
	system("sleep 10");
	qmailconc();
    
	# Added for mailing appropriate error messages to admin after the update
  	   $warning_msg = "URL category updation failed.\nNow it uses the previously updated database.\n";

	for($w=0; $w<3; $w++){

		system("sleep 30");
		system("sync");
		# Call the binary to get the categories
		# And redirect output to the warning.log file
 		system ("/var/chroot-squid/usr/local/squid/surfcontrol/getcategory > /var/chroot-squid/usr/local/squid/surfcontrol/warning.log");
                                                                                 
		# Initialize $warning_found variable to 0
		$warning_found = 0;
    
                                                                         
	        # Open the warning.log file for checking against Warnings
        	open (WARNING, "/var/chroot-squid/usr/local/squid/surfcontrol/warning.log");

	        while (<WARNING>)
        	{
                	# Check for 'Warning' or 'Error' string in the warning.log file
	                   if ( (/Warning/i) || (/Error/i) ) # if found
        	           {
                	       	#print $warning_msg;
                                                                             
                       		# Set to 1
	                           $warning_found = 1;
                                                                                
 			 	# Change to root directory since all files listed from the root directory
         #       	           chdir ("/");
                                                                                
                        	# Use Previously Updated Database
	#                           `tar -zxvf /var/log/URL/$_[0]`;
				#Remove the Category Backup
	#			   `rm -f /var/log/URL/$_[0]`;
                                                                                
                	        # Skip While Loop
                        	   last;
                                                                                
	                   } # if condition

        	} # while loop
                                                                                
	        # Close warning.log file
        	   close (WARNING);
		if($warning_found == 0)
		{
			last;
		}
	}
                                                                                
        # Remove warning.log file
           `rm -rf /var/chroot-squid/usr/local/squid/surfcontrol/warning.log`;
        
	if ($warning_found == 1)
        {
	 	# Change to root directory since all files listed from the root directory
                chdir ("/");
                                                                                
               	# Use Previously Updated Database
	        `tar -zxvf /var/log/URL/$_[0]`;
		#Remove the Category Backup
		`rm -f /var/log/URL/$_[0]`;

                # Open http-debug file
 		   open (DEBUG, ">>/etc/multiconf/http-debug");
                # Write debug message to the http-debug file
                   print DEBUG $warning_msg."\n";
                # Close http-debug file
                   close (DEBUG);
                # Return -1 for Failure	   
		   return -1;

        } # if condition
	else
	{
		#Remove the Category Backup
		   `rm -f /var/log/URL/$_[0]`;
		# Return 0 for Success
		   return 0;
	}
                                                                                
# Continue
}

sub snapshotb4update()
{
	#Take the snapshot of the files before the update takes place.
	`ls -al /var/chroot-squid/usr/local/SurfControl/CatEngine/CSP/SDK\\ Core\\ and\\ Productivity\\ Aura\\ List | grep -v total | tr -s \" \" | cut -d \" \" -f9`;
	@file_names = `ls -al /var/chroot-squid/usr/local/SurfControl/CatEngine/CSP/SDK\\ Core\\ and\\ Productivity\\ Aura\\ List | grep -v total | tr -s \" \" | cut -d \" \" -f9`;

	open(UPDATETRACK,">/etc/multiconf/update_track");
	for($i=0; $i<@file_names; $i++)
	{
		chomp($file_names[$i]);
		#Check to see if there are any extra files present in the directory
		if (($file_names[$i] ne "List2001V135.csf") &&
		($file_names[$i] ne "List204V508.cdb") &&
		($file_names[$i] ne "List205V508.cdb") &&
		($file_names[$i] ne "List206V508.cdb") &&
		($file_names[$i] ne "\.") &&
		($file_names[$i] ne "\.\."))
		{
			#Remove the unwanted files.
			system("rm -f /var/chroot-squid/usr/local/SurfControl/CatEngine/CSP/SDK\\ Core\\ and\\ Productivity\\ Aura\\ List/$file_names[$i]");
		}
		else
		{
			if ($file_names[$i] eq "\.\.")
			{
				next;
			}
			#If the file is correct, then get the latest modification time of the file and then store it in a file along with the file name.
			chdir("/var/chroot-squid/usr/local/SurfControl/CatEngine/CSP/SDK\ Core\ and\ Productivity\ Aura\ List");
			$mod_time = (stat $file_names[$i])[9];
			print UPDATETRACK "$file_names[$i] $mod_time\n";
		}
	}
	close(UPDATETRACK);
}

sub modify_license()
{
	open(SURFLICENSE,"/etc/multiconf/surflicense");
	while(<SURFLICENSE>)
	{
		chomp($_);
		$final_license = $_;
	}
	close(SURFLICENSE);
	@final_vals = split(/:/,$final_license);
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	open(SURFLICENSE,"/etc/multiconf/surflicense");
	while(<SURFLICENSE>)
	{
		chomp($_);
		if($_ eq $final_license)
		{
			$final_vals[1] = `cat /var/chroot-squid/etc/.drtn`;
			chomp($final_vals[1]);
			$new_time = `date +%s`;
			$dur_time = $final_vals[1] * 60;
			$tot_time = $new_time + $dur_time;
			print TEMPFILE "$final_vals[0]:$final_vals[1]:$tot_time\n";
		}
		else
		{
			print TEMPFILE "$_\n";
		}	
	}
	close(SURFLICENSE);
	close(TEMPFILE);
	system("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/surflicense");
	system("sync");
}

#20050212 Senthil - Getting the Status of the JunkBuster
sub synflood_junkbuster() {
	openfile(CONF,"$DIR/httpconf");

	while (<CONF>)	
	{
		if(/JAVA/)
		{
			my @junk_splitvals = split (/:/,$_);
			 $junk_java = $junk_splitvals[1];
			chomp ($junk_java);
		}
		elsif(/HTTP COOKIE/)
		{
			my @junk_splitvals = split (/:/,$_);
			 $junk_cookie = $junk_splitvals[1];
			chomp ($junk_cookie);
		}
		elsif(/HTTP BANNER/)
		{
			my @junk_splitvals = split (/:/,$_);
			 $junk_banner = $junk_splitvals[1];
			chomp ($junk_banner);
		}
	}
	return ($junk_java,$junk_cookie,$junk_banner);
}

##############################################################################
# Script Begins Here


if($ARGV[0] eq 'squid')
{ 
	if ($ARGV[1] eq "restart")
	{
		system ("/etc/multiconf/scripts/restartproc squid");
	}
	openfile(OUT,"$DIR/httpconf");
	$file =gettempfile();
	openfile(NEW,">$DIR/$file");
	$tempfile = gettempfile();
	open (TEMPFILE, ">$DIR/$tempfile");
 	if ($ARGV[1] eq 'enable')
	{
		$AlreadyOn = 0;
		open (PROCESSSTAT, "$DIR/processstatus");
		while (<PROCESSSTAT>)
		{
			if (/SQUID/)
			{
				@values = split (/=/,$_);
				$status = $values[1];
				chomp ($status);
				if ($status eq "ON")
				{
					$AlreadyOn = 1;
					last;
				}
				print TEMPFILE "SQUID=ON\n";
			}
			elsif (/SURF/)
			{
				$surfstatus = `/etc/multiconf/scripts/http geturlstatus`;
				chomp ($surfstatus);
				if ($surfstatus eq "ON")
				{
					print TEMPFILE "SURF=ON\n";
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			else
			{
				print TEMPFILE $_;
			}
		}
		close (PROCESSSTAT);
		close (TEMPFILE);
		while (<OUT>)
		{
			if(/HTTP STATUS/)
			{
				print NEW "HTTP STATUS:ON\n";
		 	}
			elsif(/HTTP TRANS/)
			{
		 		print NEW $_;
				@splvals = split (/:/,$_);
				$transstatus = $splvals[1];
				chomp ($transstatus);
		 	}
		 	else
		 	{
		 		print NEW $_;
		 	}
		} 

		savetemp("$file","$DIR/httpconf");
		system("rm -f $DIR/$file");

		if ($AlreadyOn == 0)
		{
			savetemp("$tempfile","$DIR/processstatus");
			system("rm -f $DIR/$tempfile");
			$eth0address = `$SCRIPTS/getethaddress eth0`;
			@eth0add = split(/:/,$eth0address);
			chomp ($eth0add[1]);

			if ($transstatus eq "ON")
			{
				#The file transnet contains the networks allowed
				#and denied for transparent proxy
				#Add iptables rules for the networks
				open (TRANSFILE, "/etc/multiconf/transnet");
				$allowno = 1;
				$denyno = 1;
				while (<TRANSFILE>)
				{
					@splitval = split (/ /,$_);
					@AddMask = `/etc/multiconf/scripts/netname $splitval[0]`;
					chomp ($AddMask[0]);
					chomp ($AddMask[1]);
					$Address = $AddMask[0];
					$Mask = $AddMask[1];
					chomp ($splitval[1]);
					if ($splitval[1] eq "Allowed")
					{
						system("$SCRIPTS/iptables addtransfilter allow $eth0add[0] $eth0add[1] $Address $Mask tcp 80 $allowno");
						$allowno++;
					}
					elsif ($splitval[1] eq "Denied")
					{
						system("$SCRIPTS/iptables addtransfilter drop $eth0add[0] $eth0add[1] $Address $Mask tcp 80 $denyno");
						$denyno++;
					}
				}
				close (TRANSFILE);
#20050212 Senthil - Syn Flood limit...
				system ("/etc/multiconf/scripts/synfloodchain addrule 80");
			}
			else
			{
				#The file nontransnet contains the networks
				#allowed and denied for nontransparent proxy
				#Add iptables rules for the networks
				open (NONTRANSFILE, "/etc/multiconf/nontransnet");
				$allowno = 1;
				$denyno = 1;
				while (<NONTRANSFILE>)
				{
					@splitval = split (/ /,$_);
					@AddMask = `/etc/multiconf/scripts/netname $splitval[0]`;
					chomp ($AddMask[0]);
					chomp ($AddMask[1]);
					$Address = $AddMask[0];
					$Mask = $AddMask[1];
					chomp ($splitval[1]);
					if ($splitval[1] eq "Allowed")
					{
						system("$SCRIPTS/iptables addsquidfilter allow $eth0add[0] $eth0add[1] $Address $Mask tcp 3128");
						$allowno++;
					}
					elsif ($splitval[1] eq "Denied")
					{
						system("$SCRIPTS/iptables addsquidfilter drop $eth0add[0] $eth0add[1] $Address $Mask tcp 3128");
						$denyno++;
					}
				}
				close (NONTRANSFILE);
			}

			#Add port 80 on the OUTPUT chain
			system ("iptables -A HTTP_PORT -s 0/0 -d 0/0 -p tcp --dport 80 -j LOGACCEPT_FIREWALL_OUTBOUND");
			system ("iptables -A HTTP_PORT -s 0/0 -d 0/0 -p tcp --dport 443 -j LOGACCEPT_FIREWALL_OUTBOUND");
			system ("iptables -A HTTP_PORT -s 0/0 -d 0/0 -p tcp --dport 21 -j LOGACCEPT_FIREWALL_OUTBOUND");
#20050212 Senthil - Syn Flood limit...
			my @junkbuster_status = synflood_junkbuster();
			chomp(@junkbuster_status);
			if(($junkbuster_status[0] eq "ON") || ($junkbuster_status[1] eq "ON") || ($junkbuster_status[2] eq "ON")) {
				system ("/etc/multiconf/scripts/synfloodchain addrule 8000");

			}
			system ("/etc/multiconf/scripts/synfloodchain addrule 3128");

			#system ("iptables-save >/etc/multiconf/iptablerules");
			saverules();
#20040413 Javeed 
			system("/etc/multiconf/scripts/CheckIntrusion update");
			system ("/etc/init.d/squid start");
			if ($surfstatus eq "ON")
			{
				system ("/etc/init.d/surf start");
			}
		}
		else
		{
			system("rm -f $DIR/$tempfile");
		}
		system("/etc/multiconf/scripts/cvscheckin update /etc/multiconf/httpconf");
	}
	elsif ($ARGV[1] eq 'disable')
	{
		$AlreadyOff = 0;
		open (PROCESSSTAT, "/etc/multiconf/processstatus");
		while (<PROCESSSTAT>)
		{
			if (/SQUID/)
			{
				@values = split (/=/,$_);
				$status = $values[1];
				chomp ($status);
				if ($status eq "OFF")
				{
					$AlreadyOff = 1;
					last;
				}
				print TEMPFILE "SQUID=OFF\n";
			}
			elsif (/SURF/)
			{
				@values = split (/=/,$_);
				$surfstatus = $values[1];
				chomp ($surfstatus);
				print TEMPFILE "SURF=OFF\n";
			}
			else
			{
				print TEMPFILE $_;
			}
		}
		close (PROCESSSTAT);
		if ($AlreadyOff == 0)
		{
			savetemp("$tempfile","$DIR/processstatus");
			system("rm -f $DIR/$tempfile");
			$eth0address = `$SCRIPTS/getethaddress eth0`;
			@eth0add = split(/:/,$eth0address);
			chomp ($eth0add[1]);

			#Remove the iptables rules for the nontrans networks
			open (NONTRANSFILE, "/etc/multiconf/nontransnet");
			while (<NONTRANSFILE>)
			{
				@splitval = split (/ /,$_);
				@AddMask = `/etc/multiconf/scripts/netname $splitval[0]`;
				chomp ($AddMask[0]);
				chomp ($AddMask[1]);
				$Address = $AddMask[0];
				$Mask = $AddMask[1];
				chomp ($splitval[1]);
				if ($splitval[1] eq "Allowed")
				{
					system("$SCRIPTS/iptables delsquidfilter allow $eth0add[0] $eth0add[1] $Address $Mask tcp 3128");
				}
				elsif ($splitval[1] eq "Denied")
				{
					system("$SCRIPTS/iptables delsquidfilter drop $eth0add[0] $eth0add[1] $Address $Mask tcp 3128");
				}
			}
			close (NONTRANSFILE);

			#Remove the iptables rules for the transparent networks
			open (TRANSFILE, "/etc/multiconf/transnet");
			while (<TRANSFILE>)
			{
				@splitval = split (/ /,$_);
				@AddMask = `/etc/multiconf/scripts/netname $splitval[0]`;
				chomp ($AddMask[0]);
				chomp ($AddMask[1]);
				$Address = $AddMask[0];
				$Mask = $AddMask[1];
				chomp ($splitval[1]);
				if ($splitval[1] eq "Allowed")
				{
					system("$SCRIPTS/iptables deltransfilter allow $eth0add[0] $eth0add[1] $Address $Mask tcp 80");
				}
				elsif ($splitval[1] eq "Denied")
				{
					system("$SCRIPTS/iptables deltransfilter drop $eth0add[0] $eth0add[1] $Address $Mask tcp 80");
				}
			}
			close (TRANSFILE);

			#Delete port 80 from the OUTPUT chain
			system ("iptables -F SQUID_PORT");
			system ("iptables -F SQUID_DROP");
			system ("iptables -D HTTP_PORT -s 0/0 -d 0/0 -p tcp --dport 80 -j LOGACCEPT_FIREWALL_OUTBOUND");
			system ("iptables -D HTTP_PORT -s 0/0 -d 0/0 -p tcp --dport 443 -j LOGACCEPT_FIREWALL_OUTBOUND");
			system ("iptables -D HTTP_PORT -s 0/0 -d 0/0 -p tcp --dport 21 -j LOGACCEPT_FIREWALL_OUTBOUND");
#20050212 Senthil - Syn Flood limit...
			system ("/etc/multiconf/scripts/synfloodchain delrule 8000");
			system ("/etc/multiconf/scripts/synfloodchain delrule 3128");
			system ("/etc/multiconf/scripts/synfloodchain delrule 80");
			#system ("iptables-save >/etc/multiconf/iptablerules");
			saverules();	
#20040413 Javeed 
			system("/etc/multiconf/scripts/CheckIntrusion update");
		}
		else
		{
			system ("rm -f $DIR/$tempfile");
		}
		while (<OUT>)
		{
			if(/HTTP STATUS/)
			{	
				print NEW "HTTP STATUS:OFF\n";
			}
			else
			{
		 		print NEW $_;
		 	}
		}
		savetemp("$file","$DIR/httpconf");
		system("rm -f $DIR/$file");
		system ("/etc/init.d/squid stop");
		system("/etc/multiconf/scripts/cvscheckin update /etc/multiconf/httpconf");
		if ($surfstatus eq "ON")
		{
			system ("/etc/init.d/surf stop");
		}
	}
	close(OUT);
	close(NEW);
}
elsif($ARGV[0] eq 'transparent')
{
	openfile(CONF,"$DIR/httpconf");
	while (<CONF>)
	{
		if(/HTTP TRANS/)
		{
			@splitval = split (/:/,$_);
			$transstatus = $splitval[1];
			chomp ($transstatus);
		}
	}
	close(CONF);
	openfile(CONF,"$DIR/httpconf");
        openfile(SQUIDCONF,"$SQUIDDIR/squid.conf");
	$file =gettempfile();
	$file1 = gettempfile();
	openfile(NEW,">$DIR/$file"); 
	openfile(TEMPCONF,">$DIR/$file1"); 

 	if ($ARGV[1] eq 'enable')
	{
		# Calling Iptables to enable forwarding of packet from port 80 to port 3128
		$eth0address = `$SCRIPTS/getethaddress eth0`;
		@address = split(/:/,$eth0address);
		chomp ($address[1]);

		while (<CONF>)
		{
			if(/HTTP TRANS/)
			{
			 	print NEW "HTTP TRANS:ON\n";
			}
			else
			{
				print NEW $_;
			}
		}
		close(CONF);
 		close(NEW);
		savetemp("$file","$DIR/httpconf");
		system("rm -f $DIR/$file");

		while (<SQUIDCONF>)
		{
			chomp();		
			if(($_ eq '# httpd_accel_port 80')||($_ eq '#httpd_accel_port 80')) 	
		  	{
				print TEMPCONF "httpd_accel_host virtual\n";
				print TEMPCONF "httpd_accel_port 80\n";
			}
			elsif(($_ eq '# httpd_accel_with_proxy off')||($_ eq '#httpd_accel_with_proxy off')||($_ eq 'httpd_accel_with_proxy off')) 	
			{
				print TEMPCONF "httpd_accel_with_proxy on\n";
			}
			elsif(($_ eq '# httpd_accel_uses_host_header off')||($_ eq '#httpd_accel_uses_host_header off') || ($_ eq 'httpd_accel_uses_host_header off')) 
			{
				print TEMPCONF "httpd_accel_uses_host_header on\n";
			}
			else
			{
                		print TEMPCONF "$_\n";  
			}
		}
		close(SQUIDCONF);
		close(TEMPCONF);
		savetemp("$file1","$SQUIDDIR/squid.conf");
		system("rm -f $DIR/$file1");
		system("/etc/multiconf/scripts/cvscheckin update $SQUIDDIR/squid.conf");
		if ($transstatus eq "OFF")
		{
			$eth0address = `$SCRIPTS/getethaddress eth0`;
			@eth0add = split(/:/,$eth0address);
			chomp ($eth0add[1]);
			open (NONTRANSFILE, "/etc/multiconf/nontransnet");
			while (<NONTRANSFILE>)
			{
				@splitval = split (/ /,$_);
				@AddMask = `/etc/multiconf/scripts/netname $splitval[0]`;
				chomp ($AddMask[0]);
				chomp ($AddMask[1]);
				$Address = $AddMask[0];
				$Mask = $AddMask[1];
				chomp ($splitval[1]);
				if ($splitval[1] eq "Allowed")
				{
					system("$SCRIPTS/iptables delsquidfilter allow $eth0add[0] $eth0add[1] $Address $Mask tcp 3128");
				}
				elsif ($splitval[1] eq "Denied")
				{
					system("$SCRIPTS/iptables delsquidfilter drop $eth0add[0] $eth0add[1] $Address $Mask tcp 3128");
				}
			}
			close (NONTRANSFILE);

			open (TRANSFILE, "/etc/multiconf/transnet");
			$allowno = 1;
			$denyno = 1;
			while (<TRANSFILE>)
			{
				@splitval = split (/ /,$_);
				@AddMask = `/etc/multiconf/scripts/netname $splitval[0]`;
				chomp ($AddMask[0]);
				chomp ($AddMask[1]);
				$Address = $AddMask[0];
				$Mask = $AddMask[1];
				chomp ($splitval[1]);
				if ($splitval[1] eq "Allowed")
				{
					system("$SCRIPTS/iptables addtransfilter allow $eth0add[0] $eth0add[1] $Address $Mask tcp 80 $allowno");
					$allowno = $allowno + 2;
				}
				elsif ($splitval[1] eq "Denied")
				{
					system("$SCRIPTS/iptables addtransfilter drop $eth0add[0] $eth0add[1] $Address $Mask tcp 80 $denyno");
					$denyno = $denyno + 2;
				}
			}
			close (TRANSFILE);
		}
#20050212 Senthil - Syn Flood limit...
		system ("/etc/multiconf/scripts/synfloodchain addrule 80");

		system("/etc/init.d/squid reread");
	}
	elsif ($ARGV[1] eq 'disable')
	{
		# Disable forwarding of packets from port 80 to port 3128
		$eth0address = `$SCRIPTS/getethaddress eth0`;
		@address = split(/:/,$eth0address);
		chomp ($address[1]);

		while (<CONF>)
		{
	        	if(/HTTP TRANS/)
			{	
				print NEW "HTTP TRANS:OFF\n";
		 	}
		 	else
		 	{
		 		print NEW $_;
		 	}
		}

	 	close(CONF);
	 	close(NEW);

		savetemp("$file","$DIR/httpconf");
		system("rm -f $DIR/$file");
#		system ("killall -v squid");
		while (<SQUIDCONF>)
		{
			chomp();
			if($_ eq 'httpd_accel_host virtual')
 		  	{
				#Do nothing	
		  	}
			elsif($_ eq 'httpd_accel_port 80') 	
		  	{
		   		print TEMPCONF "# httpd_accel_port 80\n";
		  	}
		  	elsif($_ eq 'httpd_accel_with_proxy on') 	
		  	{
		   		print TEMPCONF "httpd_accel_with_proxy off\n";
		  	}
			elsif($_ eq 'httpd_accel_uses_host_header on') 	
		  	{
		   		print TEMPCONF "httpd_accel_uses_host_header off\n";
		  	}
			else
		  	{
                  		print TEMPCONF "$_\n";  
		  	}
		}
	
		close(SQUIDCONF);
	 	close(TEMPCONF);
	 	savetemp("$file1","$SQUIDDIR/squid.conf");
		system("rm -f $DIR/$file1");
		system("/etc/multiconf/scripts/cvscheckin update $SQUIDDIR/squid.conf");
		if ($transstatus eq "ON")
		{
			$eth0address = `$SCRIPTS/getethaddress eth0`;
			@eth0add = split(/:/,$eth0address);
			chomp ($eth0add[1]);
			open (TRANSFILE, "/etc/multiconf/transnet");
			while (<TRANSFILE>)
			{
				@splitval = split (/ /,$_);
				@AddMask = `/etc/multiconf/scripts/netname $splitval[0]`;
				chomp ($AddMask[0]);
				chomp ($AddMask[1]);
				$Address = $AddMask[0];
				$Mask = $AddMask[1];
				chomp ($splitval[1]);
				if ($splitval[1] eq "Allowed")
				{
					system("$SCRIPTS/iptables deltransfilter allow $eth0add[0] $eth0add[1] $Address $Mask tcp 80");
				}
				elsif ($splitval[1] eq "Denied")
				{
					system("$SCRIPTS/iptables deltransfilter drop $eth0add[0] $eth0add[1] $Address $Mask tcp 80");
				}
			}
			close (TRANSFILE);

			open (NONTRANSFILE, "/etc/multiconf/nontransnet");
			$allowno = 1;
			$denyno = 1;
			while (<NONTRANSFILE>)
			{
				@splitval = split (/ /,$_);
				@AddMask = `/etc/multiconf/scripts/netname $splitval[0]`;
				chomp ($AddMask[0]);
				chomp ($AddMask[1]);
				$Address = $AddMask[0];
				$Mask = $AddMask[1];
				chomp ($splitval[1]);
				if ($splitval[1] eq "Allowed")
				{
					system("$SCRIPTS/iptables addsquidfilter allow $eth0add[0] $eth0add[1] $Address $Mask tcp 3128 $allowno");
					$allowno++;
				}
				elsif ($splitval[1] eq "Denied")
				{
					system("$SCRIPTS/iptables addsquidfilter drop $eth0add[0] $eth0add[1] $Address $Mask tcp 3128 $denyno");
					$denyno++;
				}
			}
			close (NONTRANSFILE);
		}
#20050212 Senthil - Syn Flood limit...
		system ("/etc/multiconf/scripts/synfloodchain delrule 80");
		system("/etc/init.d/squid reread");
	}
}
elsif($ARGV[0] eq 'auth')
{
	openfile(CONF,"$DIR/httpconf");
        openfile(SQUIDCONF,"$SQUIDDIR/squid.conf");
	$file =gettempfile();
	$file1 = gettempfile();
	openfile(NEW,">$DIR/$file"); 
	openfile(TEMPCONF,">$DIR/$file1");
 
	if ($ARGV[1] eq 'enable')
	{
		while (<CONF>)
		{
			if(/HTTP AUTH/)
		 	{
				print NEW "HTTP AUTH:ON\n";
		 	}
		 	else
		 	{
				print NEW $_;
			}
		}

		savetemp("$file","$DIR/httpconf");
		system("rm -f $DIR/$file");
		system("/etc/multiconf/scripts/cvscheckin update /etc/multiconf/httpconf");

		while (<SQUIDCONF>)
		{
			chomp();	
			#20040419 Prabha
			if ($_ =~ /auth_param basic program \/usr\/local\/squid\/bin\/pam_auth -o/)
	  	{
				print TEMPCONF "auth_param basic program /usr/local/squid/bin/pam_auth -o\n";
	  	}
			elsif ($_ =~ /auth_param basic children 5/)
	  	{
	  		print TEMPCONF "auth_param basic children 5\n";
	  	}
			elsif ($_ =~ /auth_param basic credentialsttl 1 hour/)
	  	{
			#Typo corrected 20041118 abhishek
	   		print TEMPCONF "auth_param basic credentialsttl 1 hour\n";
	  	}
			elsif ($_ =~ /acl pamauth proxy_auth REQUIRED/)
	  	{
	   		print TEMPCONF "acl pamauth proxy_auth REQUIRED\n";
	  	}
	  	elsif ($_ =~ /http_access deny \!pamauth/)
	  	{
	   		print TEMPCONF "http_access deny !pamauth\n";	
	  	}
			#20040419 Prabha
	  	else
	  	{
     		print TEMPCONF "$_\n";  
	  	}
		}
		close(SQUIDCONF);
	 	close(TEMPCONF);
	 	savetemp("$file1","$SQUIDDIR/squid.conf");
		system("rm -f $DIR/$file1");
		system("/etc/multiconf/scripts/cvscheckin update $SQUIDDIR/squid.conf");
		system("/etc/init.d/squid reread");
	}
	else
	{
		if ($ARGV[1] eq 'disable')
		{
			while (<CONF>)
			{
		        	if(/HTTP AUTH/)
			 	{	
			 		print NEW "HTTP AUTH:OFF\n";
			 	}
			 	else
		 	 	{
		 	 		print NEW $_;
		 	 	}
			}
			close(CONF);
	 		close(NEW);
			savetemp("$file","$DIR/httpconf");
			system("rm -f $DIR/$file");
			system("/etc/multiconf/scripts/cvscheckin update $DIR/httpconf");
			while (<SQUIDCONF>)
			{
				chomp();	
				#20040419 Prabha
				if ($_ =~ /auth_param basic program \/usr\/local\/squid\/bin\/pam_auth -o/)
		  	{
					print TEMPCONF "#auth_param basic program /usr/local/squid/bin/pam_auth -o\n";
		  	}
				elsif ($_ =~ /auth_param basic children 5/)
		  	{
		  		print TEMPCONF "#auth_param basic children 5\n";
		  	}
				elsif ($_ =~ /auth_param basic credentialsttl 1 hour/)
		  	{
				#Typo corrected 20041118 abhishek
		   		print TEMPCONF "#auth_param basic credentialsttl 1 hour\n";
		  	}
				elsif ($_ =~ /acl pamauth proxy_auth REQUIRED/)
		  	{
		   		print TEMPCONF "#acl pamauth proxy_auth REQUIRED\n";
		  	}
	  		elsif ($_ =~ /http_access deny \!pamauth/)
		  	{
		   		print TEMPCONF "#http_access deny !pamauth\n";	
		  	}
				#20040419 Prabha
	  		else
	  		{
     			print TEMPCONF "$_\n";  
	  		}
			}
			close(SQUIDCONF);
	 		close(TEMPCONF);
	 		savetemp("$file1","$SQUIDDIR/squid.conf");
			system("rm -f $DIR/$file1");
			system("/etc/multiconf/scripts/cvscheckin update $SQUIDDIR/squid.conf");
			system("/etc/init.d/squid reread");
		}
	}
}
elsif ($ARGV[0] eq 'allowednetworks')
{ 
	if ($ARGV[1] eq 'transparent')
	{
		open (TRANSFILE, "/etc/multiconf/transnet");
		while (<TRANSFILE>)
		{
			if ((/$ARGV[2]/) && (/$ARGV[3]/))
			{
				close (TRANSFILE);
				system ("sync");
				exit;
			}
		}
		close (TRANSFILE);
		$eth0address = `$SCRIPTS/getethaddress eth0`;
		@address = split(/:/,$eth0address);
		chomp ($address[1]);
		$addresult = system("$SCRIPTS/iptables addtransfilter allow $address[0] $address[1] $ARGV[2] $ARGV[3] tcp 80");
		if ($addresult == 0)
		{
			open (TRANSFILE, ">>/etc/multiconf/transnet");
			$strtowrite = $ARGV[2] . " " . $ARGV[3] . "\n";
			print TRANSFILE $strtowrite;
			close (TRANSFILE);
		}
	}
	else
	{
		open (NONTRANSFILE, "/etc/multiconf/nontransnet");
		while (<NONTRANSFILE>)
		{
			if ((/$ARGV[1]/) && (/$ARGV[2]/))
			{
				close (NONTRANSFILE);
				system ("sync");
				exit;
			}
		}
		close (NONTRANSFILE);

		$eth0address = `$SCRIPTS/getethaddress eth0`;
		@address = split(/:/,$eth0address);
		chomp ($address[1]);
		$addresult = system("$SCRIPTS/iptables addsquidfilter allow $address[0] $address[1] $ARGV[1] $ARGV[2] tcp 3128");
		if ($addresult == 0)
		{
			open (NONTRANSFILE, ">>/etc/multiconf/nontransnet");
			$strtowrite = $ARGV[1] . " " . $ARGV[2] . "\n";
			print NONTRANSFILE $strtowrite;
			close (NONTRANSFILE);
		}
	}
}
elsif ($ARGV[0] eq 'delallowednetworks')
{ 
	if ($ARGV[1] eq 'transparent')
	{
		$eth0address = `$SCRIPTS/getethaddress eth0`;
		@address = split(/:/,$eth0address);
		chomp ($address[1]);

		if ($ARGV[4] eq "Allowed")
		{
			system("$SCRIPTS/iptables deltransfilter allow $address[0] $address[1] $ARGV[2] $ARGV[3] tcp 80");
		}
		elsif ($ARGV[4] eq "Denied")
		{
			system("$SCRIPTS/iptables deltransfilter drop $address[0] $address[1] $ARGV[2] $ARGV[3] tcp 80");
		}
	}
	else
	{
		$eth0address = `$SCRIPTS/getethaddress eth0`;
		@address = split(/:/,$eth0address);
		chomp ($address[1]);

		if ($ARGV[3] eq "Allowed")
		{
			system("$SCRIPTS/iptables delsquidfilter allow $address[0] $address[1] $ARGV[1] $ARGV[2] tcp 3128");
		}
		elsif ($ARGV[3] eq "Denied")
		{
			system("$SCRIPTS/iptables delsquidfilter drop $address[0] $address[1] $ARGV[1] $ARGV[2] tcp 3128");
		}
	}
}
elsif ($ARGV[0] eq 'javascript')
{
	openfile(CONF,"$DIR/httpconf");

	$file1 =gettempfile();
	openfile(STATUS,">$DIR/$file1");

	if($ARGV[1] eq 'disable')
	{
		while (<CONF>)	
		{
			if(/JAVA/)
			{
				@splitvals = split (/:/,$_);
				$javastatus = $splitvals[1];
				chomp ($javastatus);
				if ($javastatus eq "OFF")
				{
					close (CONF);
					close (STATUS);
					system("rm -f $DIR/$file1");
					exit;
				}

				$string = "HTTP JAVA:OFF\n";
				print STATUS $string;
			}
			elsif(/HTTP COOKIE/)
			{
				@splitvals = split (/:/,$_);
				$cookiestatus = $splitvals[1];
				chomp ($cookiestatus);
				print STATUS $_;
			}
			elsif(/HTTP BANNER/)
			{
				@splitvals = split (/:/,$_);
				$bannerstatus = $splitvals[1];
				chomp ($bannerstatus);
				print STATUS $_;
			}
			else
			{
				print STATUS $_;
			}
		
		}

		savetemp("$file1","$DIR/httpconf");
		system("rm -f $DIR/$file1");
		system("/etc/multiconf/scripts/cvscheckin update $DIR/httpconf");

#Deleted lines which write into the junkbstr.ini
		system ("echo \"*\">/usr/local/src/ijb20-js/jsfile.ini");
		if (($cookiestatus eq "OFF") && ($bannerstatus eq "OFF"))
		{
			open (SQUIDCONF, "/var/chroot-squid/usr/local/squid/etc/squid.conf");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<SQUIDCONF>)
			{
				if ((/cache_peer/ && /parent/ && /8000/) || (/acl HTTP proto HTTP/) || (/acl FTP proto FTP/) || (/never_direct allow HTTP/) || (/always_direct allow FTP/))
				{
					$strtowrite = "\#" . $_;
					print TEMPFILE $strtowrite;
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (SQUIDCONF);
			close (TEMPFILE);
			system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/etc/squid.conf");

			open (PROCESSSTATUS, "/etc/multiconf/processstatus");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<PROCESSSTATUS>)
			{
				if (/JUNKBUSTER/)
				{
					print TEMPFILE "JUNKBUSTER=OFF\n";
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (PROCESSSTATUS);
			system ("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/processstatus");
			system ("/etc/init.d/junkbuster stop");

                        system ("iptables -D ALLOW_PORTS_INBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_TO_FIREWALL");
                        system ("iptables -D ALLOW_PORTS_OUTBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_FIREWALL_OUTBOUND");
#20050212 Senthil - Syn Flood limit...
			system ("/etc/multiconf/scripts/synfloodchain delrule 8000");
			#system ("iptables-save >/etc/multiconf/iptablerules");
			saverules();
#20040413 Javeed 
			system("/etc/multiconf/scripts/CheckIntrusion update");
		}
		system ("/etc/init.d/squid stop");
		system ("sleep 3");
		system ("echo \"\" >/var/chroot-squid/usr/local/squid/cache/swap.state");
		system ("/etc/init.d/squid start");
	}

	elsif($ARGV[1] eq 'enable')
	{ 
		while (<CONF>)	
		{
			if(/JAVA/)
			{
				@splitvals = split (/:/,$_);
				$javastatus = $splitvals[1];
				chomp ($javastatus);
				if ($javastatus eq "ON")
				{
					close (CONF);
					close (STATUS);
					system("rm -f $DIR/$file1");
					exit;
				}
				$string = "HTTP JAVA:ON\n";
				print STATUS $string;
			}
			elsif(/HTTP COOKIE/)
			{
				@splitvals = split (/:/,$_);
				$cookiestatus = $splitvals[1];
				chomp ($cookiestatus);
				print STATUS $_;
			}
			elsif(/HTTP BANNER/)
			{
				@splitvals = split (/:/,$_);
				$bannerstatus = $splitvals[1];
				chomp ($bannerstatus);
				print STATUS $_;
			}
			else
			{
				print STATUS $_;
			}
		}
		savetemp("$file1","$DIR/httpconf");
		system("rm -f $DIR/$file1");
		system("/etc/multiconf/scripts/cvscheckin update $DIR/httpconf");

		system ("echo >/usr/local/src/ijb20-js/jsfile.ini");
		if (($cookiestatus eq "OFF") && ($bannerstatus eq "OFF"))
		{
			open (SQUIDCONF, "/var/chroot-squid/usr/local/squid/etc/squid.conf");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<SQUIDCONF>)
			{
				if ((/cache_peer/ && /parent/ && /8000/) || (/acl HTTP proto HTTP/) || (/acl FTP proto FTP/) || (/never_direct allow HTTP/) || (/always_direct allow FTP/))
				{
					@splitval = split (/\#/,$_);
					print TEMPFILE $splitval[1];
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (SQUIDCONF);
			close (TEMPFILE);
			system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/etc/squid.conf");

			open (PROCESSSTATUS, "/etc/multiconf/processstatus");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<PROCESSSTATUS>)
			{
				if (/JUNKBUSTER/)
				{
					print TEMPFILE "JUNKBUSTER=ON\n";
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (PROCESSSTATUS);
			system ("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/processstatus");

			system ("/etc/multiconf/scripts/restartproc junkbuster 1>/dev/null 2>/dev/null &");


                        system ("iptables -A ALLOW_PORTS_INBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_TO_FIREWALL");
                        system ("iptables -A ALLOW_PORTS_OUTBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_FIREWALL_OUTBOUND");
#20050212 Senthil - Syn Flood limit...
			system ("/etc/multiconf/scripts/synfloodchain addrule 8000");
			#system ("iptables-save >/etc/multiconf/iptablerules");
			saverules();
#20040413 Javeed 
			system("/etc/multiconf/scripts/CheckIntrusion update");
		}
		system ("/etc/init.d/squid stop");
		system ("sleep 3");
		system ("echo \"\" >/var/chroot-squid/usr/local/squid/cache/swap.state");
		system ("/etc/init.d/squid start");
	}
}
elsif ($ARGV[0] eq 'cookie')
{ 
	openfile(CONF,"$DIR/httpconf");
	$file =gettempfile();
	openfile(NEW,">$DIR/$file");

	$file1 =gettempfile();
	openfile(STATUS,">$DIR/$file1");
	if($ARGV[1] eq 'disable')
	{
	  
		while (<CONF>)	
		{
			if(/HTTP COOKIE/)
			{
				@splitvals = split (/:/,$_);
				$cookiestatus = $splitvals[1];
				chomp ($cookiestatus);
				if ($cookiestatus eq "OFF")
				{
					close (CONF);
					close (STATUS);
					system("rm -f $DIR/$file1");
					exit;
				}
				$string = "HTTP COOKIE:OFF\n";
				print STATUS $string;
			}
			elsif(/HTTP JAVA/)
			{
				@splitvals = split (/:/,$_);
				$javastatus = $splitvals[1];
				chomp ($javastatus);
				print STATUS $_;
			}
			elsif(/HTTP BANNER/)
			{
				@splitvals = split (/:/,$_);
				$bannerstatus = $splitvals[1];
				chomp ($bannerstatus);
				print STATUS $_;
			}
			else
			{
				print STATUS $_;
			}
		
		}

		savetemp("$file1","$DIR/httpconf");
		system("rm -f $DIR/$file1");
		system("/etc/multiconf/scripts/cvscheckin update $DIR/httpconf");
		system("echo \"*\" >/usr/local/src/ijb20-js/scookie.ini");
		if (($javastatus eq "OFF") && ($bannerstatus eq "OFF"))
		{
			open (SQUIDCONF, "/var/chroot-squid/usr/local/squid/etc/squid.conf");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<SQUIDCONF>)
			{
				if ((/cache_peer/ && /parent/ && /8000/) || (/acl HTTP proto HTTP/) || (/acl FTP proto FTP/) || (/never_direct allow HTTP/) || (/always_direct allow FTP/))
				{
					$strtowrite = "\#" . $_;
					print TEMPFILE $strtowrite;
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (SQUIDCONF);
			close (TEMPFILE);
			system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/etc/squid.conf");

			open (PROCESSSTATUS, "/etc/multiconf/processstatus");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<PROCESSSTATUS>)
			{
				if (/JUNKBUSTER/)
				{
					print TEMPFILE "JUNKBUSTER=OFF\n";
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (PROCESSSTATUS);
			system ("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/processstatus");
			system ("/etc/init.d/junkbuster stop");

                        system ("iptables -D ALLOW_PORTS_INBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_TO_FIREWALL");
                        system ("iptables -D ALLOW_PORTS_OUTBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_FIREWALL_OUTBOUND");
#20050212 Senthil - Syn Flood limit...
			system ("/etc/multiconf/scripts/synfloodchain delrule 8000");
			#system ("iptables-save >/etc/multiconf/iptablerules");
			saverules();
#20040413 Javeed 
			system("/etc/multiconf/scripts/CheckIntrusion update");
		}
		system ("/etc/init.d/squid stop");
		system ("sleep 3");
		system ("echo \"\" >/var/chroot-squid/usr/local/squid/cache/swap.state");
		system ("/etc/init.d/squid start");
	}
	elsif($ARGV[1] eq 'enable')
	{
		while (<CONF>)	
		{
			if(/HTTP COOKIE/)
			{
				@splitvals = split (/:/,$_);
				$cookiestatus = $splitvals[1];
				chomp ($cookiestatus);
				if ($cookiestatus eq "ON")
				{
					close (CONF);
					close (STATUS);
					system("rm -f $DIR/$file1");
					exit;
				}
				$string = "HTTP COOKIE:ON\n";
				print STATUS $string;
			}
			elsif(/HTTP JAVA/)
			{
				@splitvals = split (/:/,$_);
				$javastatus = $splitvals[1];
				chomp ($javastatus);
				print STATUS $_;
			}
			elsif(/HTTP BANNER/)
			{
				@splitvals = split (/:/,$_);
				$bannerstatus = $splitvals[1];
				chomp ($bannerstatus);
				print STATUS $_;
			}
			else
			{
				print STATUS $_;
			}
		}
		savetemp("$file1","$DIR/httpconf");
		system("rm -f $DIR/$file1");
		system("/etc/multiconf/scripts/cvscheckin update $DIR/httpconf");
		system("echo >/usr/local/src/ijb20-js/scookie.ini");

		if (($javastatus eq "OFF") && ($bannerstatus eq "OFF"))
		{
			open (SQUIDCONF, "/var/chroot-squid/usr/local/squid/etc/squid.conf");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<SQUIDCONF>)
			{
				if ((/cache_peer/ && /parent/ && /8000/) || (/acl HTTP proto HTTP/) || (/acl FTP proto FTP/) || (/never_direct allow HTTP/) || (/always_direct allow FTP/))
				{
					@splitval = split (/\#/,$_);
					print TEMPFILE $splitval[1];
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (SQUIDCONF);
			close (TEMPFILE);
			system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/etc/squid.conf");

			open (PROCESSSTATUS, "/etc/multiconf/processstatus");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<PROCESSSTATUS>)
			{
				if (/JUNKBUSTER/)
				{
					print TEMPFILE "JUNKBUSTER=ON\n";
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (PROCESSSTATUS);
			system ("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/processstatus");
			system ("/etc/multiconf/scripts/restartproc junkbuster 1>/dev/null 2>/dev/null &");

                        system ("iptables -A ALLOW_PORTS_INBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_TO_FIREWALL");
                        system ("iptables -A ALLOW_PORTS_OUTBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_FIREWALL_OUTBOUND");
#20050212 Senthil - Syn Flood limit...
			system ("/etc/multiconf/scripts/synfloodchain addrule 8000");
			#system ("iptables-save >/etc/multiconf/iptablerules");
			saverules();
#20040413 Javeed 
			system("/etc/multiconf/scripts/CheckIntrusion update");
		}
		system ("/etc/init.d/squid stop");
		system ("sleep 3");
		system ("echo \"\" >/var/chroot-squid/usr/local/squid/cache/swap.state");
		system ("/etc/init.d/squid start");
	}
}
elsif ($ARGV[0] eq 'banner')
{ 
	openfile(CONF,"$DIR/httpconf");
	$file1=gettempfile();
	openfile(STATUS,">$DIR/$file1");

	if($ARGV[1] eq 'disable')
	{
		while (<CONF>)	
		{
			if(/HTTP BANNER/)
			{
				@splitvals = split (/:/,$_);
				$bannerstatus = $splitvals[1];
				chomp ($bannerstatus);
				if ($bannerstatus eq "OFF")
				{
					close (CONF);
					close (STATUS);
					system("rm -f $DIR/$file1");
					exit;
				}
				$string = "HTTP BANNER:OFF\n";
				print STATUS $string;
			}
			elsif(/HTTP JAVA/)
			{
				@splitvals = split (/:/,$_);
				$javastatus = $splitvals[1];
				chomp ($javastatus);
				print STATUS $_;
			}
			elsif(/HTTP COOKIE/)
			{
				@splitvals = split (/:/,$_);
				$cookiestatus = $splitvals[1];
				chomp ($cookiestatus);
				print STATUS $_;
			}
			else
			{
				print STATUS $_;
			}
		
		}

		savetemp("$file1","$DIR/httpconf");
		system("rm -f $DIR/$file1");
		system("/etc/multiconf/scripts/cvscheckin update $DIR/httpconf");
		openfile(OUT,"$JUNKBSTRDIR/junkbstr.ini");
		$file =gettempfile();
		openfile(NEW,">$DIR/$file");
		while(<OUT>) 
		{
			if(/sblock.ini/)
		  	{
#		  		$string = "#blockfile /usr/local/src/ijb20-js/sblock.ini\n";	
				print NEW "\#blockfile /usr/local/src/ijb20-js/sblock.ini\n";	
		  	}
		  	else
		  	{	
	  			print NEW $_;			
		  	}
		}
		close (OUT);
		close (NEW);
	
		savetemp("$file","$JUNKBSTRDIR/junkbstr.ini");
		system("rm -f $DIR/$file");
		system("/etc/multiconf/scripts/cvscheckin update $JUNKBSTRDIR/junkbstr.ini");
		if (($cookiestatus eq "OFF") && ($javastatus eq "OFF"))
		{
			open (SQUIDCONF, "/var/chroot-squid/usr/local/squid/etc/squid.conf");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<SQUIDCONF>)
			{
				if ((/cache_peer/ && /parent/ && /8000/) || (/acl HTTP proto HTTP/) || (/acl FTP proto FTP/) || (/never_direct allow HTTP/) || (/always_direct allow FTP/))
				{
					$strtowrite = "\#" . $_;
					print TEMPFILE $strtowrite;
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (SQUIDCONF);
			close (TEMPFILE);
			system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/etc/squid.conf");

			open (PROCESSSTATUS, "/etc/multiconf/processstatus");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<PROCESSSTATUS>)
			{
				if (/JUNKBUSTER/)
				{
					print TEMPFILE "JUNKBUSTER=OFF\n";
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (PROCESSSTATUS);
			system ("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/processstatus");
			system ("/etc/init.d/junkbuster stop");

                        system ("iptables -D ALLOW_PORTS_INBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_TO_FIREWALL");
                        system ("iptables -D ALLOW_PORTS_OUTBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_FIREWALL_OUTBOUND");
#20050212 Senthil - Syn Flood limit...
			system ("/etc/multiconf/scripts/synfloodchain delrule 8000");
			#system ("iptables-save >/etc/multiconf/iptablerules");
			saverules();
#20040413 Javeed 
			system("/etc/multiconf/scripts/CheckIntrusion update");
		}
		system ("/etc/init.d/squid stop");
		system ("sleep 3");
		system ("echo \"\" >/var/chroot-squid/usr/local/squid/cache/swap.state");
		system ("/etc/init.d/squid start");
	}
	if($ARGV[1] eq 'enable')
	{
		while (<CONF>)	
		{
			if(/HTTP BANNER/)
			{
				@splitvals = split (/:/,$_);
				$bannerstatus = $splitvals[1];
				chomp ($bannerstatus);
				if ($bannerstatus eq "ON")
				{
					close (CONF);
					close (STATUS);
					system("rm -f $DIR/$file1");
					exit;
				}
				$string = "HTTP BANNER:ON\n";
				print STATUS $string;
			}
			elsif(/HTTP JAVA/)
			{
				@splitvals = split (/:/,$_);
				$javastatus = $splitvals[1];
				chomp ($javastatus);
				print STATUS $_;
			}
			elsif(/HTTP COOKIE/)
			{
				@splitvals = split (/:/,$_);
				$cookiestatus = $splitvals[1];
				chomp ($cookiestatus);
				print STATUS $_;
			}
			else
			{
				print STATUS $_;
			}
		
		}
		savetemp("$file1","$DIR/httpconf");
		system("rm -f $DIR/$file1");
		system("/etc/multiconf/scripts/cvscheckin update $DIR/httpconf");
		openfile(OUT,"$JUNKBSTRDIR/junkbstr.ini");
		$file =gettempfile();
		openfile(NEW,">$DIR/$file");
		while(<OUT>) 
		{
			if(/sblock.ini/)
                  	{
				$string = "blockfile /usr/local/src/ijb20-js/sblock.ini\n";	
		  		print NEW $string;		
		  	}
		  	else
	          	{
				print NEW $_;			
		  	}
		}
		close (OUT);
		close (NEW);
		savetemp("$file","$JUNKBSTRDIR/junkbstr.ini");
		system("rm -f $DIR/$file");
		system("/etc/multiconf/scripts/cvscheckin update $JUNKBSTRDIR/junkbstr.ini");

		if (($javastatus eq "OFF") && ($cookiestatus eq "OFF"))
		{
			open (SQUIDCONF, "/var/chroot-squid/usr/local/squid/etc/squid.conf");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<SQUIDCONF>)
			{
				if ((/cache_peer/ && /parent/ && /8000/) || (/acl HTTP proto HTTP/) || (/acl FTP proto FTP/) || (/never_direct allow HTTP/) || (/always_direct allow FTP/))
				{
					@splitval = split (/\#/,$_);
					print TEMPFILE $splitval[1];
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (SQUIDCONF);
			close (TEMPFILE);
			system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/etc/squid.conf");

			open (PROCESSSTATUS, "/etc/multiconf/processstatus");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<PROCESSSTATUS>)
			{
				if (/JUNKBUSTER/)
				{
					print TEMPFILE "JUNKBUSTER=ON\n";
				}
				else
				{
					print TEMPFILE $_;
				}
			}
			close (PROCESSSTATUS);
			system ("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/processstatus");
			system ("/etc/multiconf/scripts/restartproc junkbuster 1>/dev/null 2>/dev/null &");

                        system ("iptables -A ALLOW_PORTS_INBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_TO_FIREWALL");
                        system ("iptables -A ALLOW_PORTS_OUTBOUND -s 0/0 -d 0/0 -p tcp --dport 8000 -j LOGACCEPT_FIREWALL_OUTBOUND");
#20050212 Senthil - Syn Flood limit...
			system ("/etc/multiconf/scripts/synfloodchain addrule 8000");
			#system ("iptables-save >/etc/multiconf/iptablerules");
			saverules();
#20040413 Javeed 
			system("/etc/multiconf/scripts/CheckIntrusion update");
		}
		system ("/etc/init.d/squid stop");
		system ("sleep 3");
		system ("echo \"\" >/var/chroot-squid/usr/local/squid/cache/swap.state");
		system ("/etc/init.d/squid start");
	}
	close(OUT);
	close(NEW);
}
elsif ($ARGV[0] eq "url")
{ 
	openfile(CONF,"$DIR/httpconf");
	$file1=gettempfile();
	openfile(STATUS,">$DIR/$file1");

	if($ARGV[1] eq 'disable')
	{
		while (<CONF>)	
		{
			if(/HTTP URL/)
			{
				$string = "HTTP URL:OFF\n";
				print STATUS $string;
			}
			else
			{
				print STATUS $_;
			}
		}

		savetemp("$file1","$DIR/httpconf");
		system("rm -f $DIR/$file1");
		system("/etc/multiconf/scripts/cvscheckin update $DIR/httpconf");
		open (PROCESSSTATUS, "$DIR/processstatus");
		$tempfile = `/etc/multiconf/scripts/gettempfile`;
		open (TEMPFILE, ">$DIR/$tempfile");
		while (<PROCESSSTATUS>)
		{
			if (/SURF/)
			{
				print TEMPFILE "SURF=OFF\n";
			}
			else
			{
				print TEMPFILE $_;
			}
		}
		close (TEMPFILE);
		close (PROCESSSTATUS);
		system ("mv -f $DIR/$tempfile $DIR/processstatus");

		open (SQUIDCONF, "$SQUIDDIR/squid.conf");
		$tempfile = `/etc/multiconf/scripts/gettempfile`;
		open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
		while (<SQUIDCONF>)
		{
			if (/redirect_program/)
			{
				print TEMPFILE "\#redirect_program /usr/local/squid/surfcontrol/surfcontrol\n";
			}
			else
			{
				print TEMPFILE $_;
			}
		}
		close (SQUIDCONF);
		close (TEMPFILE);
		system ("mv -f /etc/multiconf/scripts/$tempfile $SQUIDDIR/squid.conf");
		system("/etc/multiconf/scripts/flashqmailconfig");
		system ("/etc/init.d/surf stop");
	}
	elsif($ARGV[1] eq 'enable')
	{
		while (<CONF>)	
		{
			if(/HTTP URL/)
			{
				$string = "HTTP URL:ON\n";
				print STATUS $string;
			}
			else
			{
				print STATUS $_;
			}
		
		}
		savetemp("$file1","$DIR/httpconf");
		system("rm -f $DIR/$file1");
		system("/etc/multiconf/scripts/cvscheckin update $DIR/httpconf");

		open (PROCESSSTATUS, "$DIR/processstatus");
		$tempfile = `/etc/multiconf/scripts/gettempfile`;
		open (TEMPFILE, ">$DIR/$tempfile");
		while (<PROCESSSTATUS>)
		{
			if (/SURF/)
			{
				@splitvals = split (/=/,$_);
				$status = $splitvals[1];
				chomp ($status);
				if ($status eq "ON")
				{
					$AlreadyOn = 1;
					close (TEMPFILE);
					close (PROCESSSTATUS);
					system ("rm -f $DIR/$tempfile");
					exit;
				}
				print TEMPFILE "SURF=ON\n";
			}
			else
			{
				print TEMPFILE $_;
			}
		}
		close (TEMPFILE);
		close (PROCESSSTATUS);
		system ("mv -f $DIR/$tempfile $DIR/processstatus");
		system ("/etc/init.d/surf start");

		open (SQUIDCONF, "$SQUIDDIR/squid.conf");
		$tempfile = `/etc/multiconf/scripts/gettempfile`;
		open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
		while (<SQUIDCONF>)
		{
			if (/redirect_program/)
			{
				print TEMPFILE "redirect_program /usr/local/squid/surfcontrol/surfcontrol\n";
			}
			else
			{
				print TEMPFILE $_;
			}
		}
		close (SQUIDCONF);
		close (TEMPFILE);
		system ("mv -f /etc/multiconf/scripts/$tempfile $SQUIDDIR/squid.conf");
	}
	system ("/etc/init.d/squid stop");
	system ("sleep 3");
	system ("echo \"\" >/var/chroot-squid/usr/local/squid/cache/swap.state");
	system("/etc/multiconf/scripts/flashqmailconfig");
	system ("/etc/init.d/squid start");
}
elsif ($ARGV[0] eq 'edittransnet')
{
	$status = "OFF";
	$transstatus = "OFF";
	open (CONF,"$DIR/httpconf");
	while (<CONF>)
	{
		if (/STATUS/)
		{
			@splitval = split (/:/,$_);
			$status = $splitval[1];
			chomp ($status);
		}
		elsif (/TRANS/)
		{
			@splitval = split (/:/,$_);
			$transstatus = $splitval[1];
			chomp ($transstatus);
		}
	}
	close (CONF);
	if (($status eq "ON") && ($transstatus eq "ON"))
	{
		$eth0address = `$SCRIPTS/getethaddress eth0`;
		@address = split(/:/,$eth0address);
		chomp ($address[1]);

		open (TRANSFILE, "/etc/multiconf/transnet");
		$allowno = 1;
		$denyno = 1;
		while (<TRANSFILE>)
		{
			@splitval = split (/ /,$_);
			if ($splitval[0] eq $ARGV[5])
			{
				chomp ($splitval[1]);
				if ($splitval[1] eq "Allowed")
				{
					system("$SCRIPTS/iptables edittransfilter $address[0] $address[1] $ARGV[1] $ARGV[2] tcp 80 $address[0] $address[1] $ARGV[3] $ARGV[4] tcp 80 $splitval[1] $allowno");
				}
				elsif ($splitval[1] eq "Denied")
				{
					system("$SCRIPTS/iptables edittransfilter $address[0] $address[1] $ARGV[1] $ARGV[2] tcp 80 $address[0] $address[1] $ARGV[3] $ARGV[4] tcp 80 $splitval[1] $denyno");
				}
		
				last;
			}
			chomp ($splitval[1]);
			if ($splitval[1] eq "Allowed")
			{
				#Two rules will get added for transparent proxy.
				$allowno = $allowno + 2;
			}
			elsif ($splitval[1] eq "Denied")
			{
				#Two rules will get added for transparent proxy.
				$denyno = $denyno + 2;
			}
		}
		close (TRANSFILE);
	}
}
elsif ($ARGV[0] eq 'editnontransnet')
{
	$status = "OFF";
	$transstatus = "OFF";
	open (CONF,"$DIR/httpconf");
	while (<CONF>)
	{
		if (/STATUS/)
		{
			@splitval = split (/:/,$_);
			$status = $splitval[1];
			chomp ($status);
		}
		elsif (/TRANS/)
		{
			@splitval = split (/:/,$_);
			$transstatus = $splitval[1];
			chomp ($transstatus);
		}
	}
	close (CONF);
	system ("sync");
	if (($status eq "ON") && ($transstatus eq "OFF"))
	{
		$eth0address = `$SCRIPTS/getethaddress eth0`;
		@address = split(/:/,$eth0address);
		chomp ($address[1]);

		open (NONTRANSFILE, "/etc/multiconf/nontransnet");
		$allowno = 1;
		$denyno = 1;
		while (<NONTRANSFILE>)
		{
			@splitval = split (/ /,$_);
			if ($splitval[0] eq $ARGV[5])
			{
				chomp ($splitval[1]);
				if ($splitval[1] eq "Allowed")
				{
					system("$SCRIPTS/iptables editsquidfilter $address[0] $address[1] $ARGV[1] $ARGV[2] tcp 3128 $address[0] $address[1] $ARGV[3] $ARGV[4] tcp 3128 $splitval[1] $allowno");
				}
				elsif ($splitval[1] eq "Denied")
				{
					system("$SCRIPTS/iptables editsquidfilter $address[0] $address[1] $ARGV[1] $ARGV[2] tcp 3128 $address[0] $address[1] $ARGV[3] $ARGV[4] tcp 3128 $splitval[1] $denyno");
				}
				last;
			}
			chomp ($splitval[1]);
			if ($splitval[1] eq "Allowed")
			{
				$allowno++;
			}
			elsif ($splitval[1] eq "Denied")
			{
				$denyno++;
			}
		}
		close (NONTRANSFILE);
	}
}
elsif ($ARGV[0] eq 'httpdisplay')
{
	openfile(OUT,"$DIR/httpconf");
	while(<OUT>)
	{
	($temp,$status)=split(/:/);
	print $status;
	}
	close(OUT);
}
elsif ($ARGV[0] eq "geturlstatus")
{
	openfile(OUT,"$DIR/httpconf");
	while(<OUT>)
	{
		if (/URL/)
		{
			($temp,$status)=split(/:/);
			print $status;
		}
	}
	close(OUT);
}
elsif ($ARGV[0] eq 'mailid')
{
	$Id = $ARGV[1];
	open (SQUIDCONF, "$SQUIDDIR/squid.conf");
	$tempfile = `$SCRIPTS/gettempfile`;
	open (TEMPFILE, ">$SCRIPTS/$tempfile");
	while (<SQUIDCONF>)
	{
		if (/TAG: cache_mgr/)
		{
			print TEMPFILE $_;
			while (<SQUIDCONF>)
			{
				if (/cache_mgr/)
				{
					print TEMPFILE "cache_mgr $Id\n";
					last;
				}
				else
				{
					print TEMPFILE $_;
				}
			}
		}
		else
		{
			print TEMPFILE $_;
		}
	}
	close (SQUIDCONF);
	close (TEMPFILE);
	system ("mv $SCRIPTS/$tempfile $SQUIDDIR/squid.conf");
#	system ("/etc/multiconf/scripts/restartproc squid");
	system("/etc/init.d/squid reread");

	#open (SURFFILE, "/usr/local/apache/htdocs/access.html");
	open (SURFFILE, "/var/chroot-squid/usr/local/squid/surfcontrol/access.html");
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	while (<SURFFILE>)
	{
		if (/mailto/)
		{
			print TEMPFILE "<P>Your administrator is <A HREF=\"mailto:$Id\">$Id<\/A>.\n";
		}
		else
		{
			print TEMPFILE $_;
		}
	}
	close (TEMPFILE);
	close (SURFFILE);
	system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/surfcontrol/access.html");
}
elsif ($ARGV[0] eq "urladd")
{
	open (URLFILE, "$SQUIDDIR/customurl");
	while (<URLFILE>)
	{
#		chomp ($_);
#		if ($_ eq $ARGV[1])
		if (/$ARGV[1]/)
		{
			close (URLFILE);
			system ("sync");
			exit;
		}
	}
	close (URLFILE);
	open (URLFILE, "$SQUIDDIR/customurl");
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	print TEMPFILE "http:\/\/" . "$ARGV[1]\n";
	while (<URLFILE>)
	{
		print TEMPFILE $_; 
	}
	close (URLFILE);
	close (TEMPFILE);
	system ("mv -f /etc/multiconf/scripts/$tempfile $SQUIDDIR/customurl");
	system ("/var/chroot-squid/usr/local/squid/createfilter/bin/Linux_i686_standard_release/custom < $SQUIDDIR/customurl");
	system ("mv -f /var/chroot-squid/usr/local/squid/createfilter/filters/Custom.bin /var/chroot-squid/usr/local/squid/filter/filters/");
}
elsif ($ARGV[0] eq "urldel")
{
	open (URLFILE, "$SQUIDDIR/customurl");
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	while (<URLFILE>)
	{
		if (/$ARGV[1]/)
		{
		}
		else
		{
			print TEMPFILE $_;
		}
	}
	close (URLFILE);
	close (TEMPFILE);
	system ("mv -f /etc/multiconf/scripts/$tempfile $SQUIDDIR/customurl");
	system ("/var/chroot-squid/usr/local/squid/createfilter/bin/Linux_i686_standard_release/custom < $SQUIDDIR/customurl");
	system ("mv -f /var/chroot-squid/usr/local/squid/createfilter/filters/Custom.bin /var/chroot-squid/usr/local/squid/filter/filters/");
}
elsif ($ARGV[0] eq "urldisp")
{
	open (URLFILE, "$SQUIDDIR/customurl");
	while (<URLFILE>)
	{
		if ($_ eq "q\n")
		{
			close (URLFILE);
			system ("sync");
			exit;
		}
		print $_;
	}
	close (URLFILE);
}
elsif ($ARGV[0] eq 'editeth0trans')
{
	open (CONF,"$DIR/httpconf");
	while (<CONF>)
	{
		if (/TRANS/)
		{
			@splitval = split (/:/,$_);
			$status = $splitval[1];
			chomp ($status);
			close (CONF);
			system ("sync");
			last;
		}
	}
	if ($status eq "ON")
	{
		$eth0OldAddress = $ARGV[1];
		$eth0OldMask = $ARGV[2];
		$eth0NewAddress = $ARGV[3];
		$eth0NewMask = $ARGV[4];
		@splitvals = `/etc/multiconf/scripts/netname $ARGV[5]`;
		chomp ($splitvals[0]);
		chomp ($splitvals[1]);
		$NetAddress = $splitvals[0];
		$NetMask = $splitvals[1];
		open (TRANS, "/etc/multiconf/transnet");
		$allowno = 1;
		$denyno = 1;
		while (<TRANS>)
		{
			@splitvals = split (/ /,$_);
			chomp ($splitvals[1]);
			if ($splitvals[0] eq $ARGV[5])
			{
				if ($splitvals[1] eq "Allowed")
				{
					system("$SCRIPTS/iptables edittransfilter $eth0OldAddress $eth0OldMask $NetAddress $NetMask tcp 80 $eth0NewAddress $eth0NewMask $NetAddress $NetMask tcp 80 $splitvals[1] $allowno");
				}
				elsif ($splitvals[1] eq "Denied")
				{
					system("$SCRIPTS/iptables edittransfilter $eth0OldAddress $eth0OldMask $NetAddress $NetMask tcp 80 $eth0NewAddress $eth0NewMask $NetAddress $NetMask tcp 80 $splitvals[1] $denyno");
				}
				last;
			}
			if ($splitvals[1] eq "Allowed")
			{
				$allowno = $allowno + 2;
			}
			elsif ($splitvals[1] eq "Denied")
			{
				$denyno = $denyno + 2;
			}
		}
		close (TRANS);
	}
}
elsif ($ARGV[0] eq 'editeth0nontrans')
{
	open (CONF,"$DIR/httpconf");
	while (<CONF>)
	{
		if (/STATUS/)
		{
			@splitval = split (/:/,$_);
			$status = $splitval[1];
			chomp ($status);
			close (CONF);
			system ("sync");
			last;
		}
	}
	if ($status eq "ON")
	{
		$eth0OldAddress = $ARGV[1];
		$eth0OldMask = $ARGV[2];
		$eth0NewAddress = $ARGV[3];
		$eth0NewMask = $ARGV[4];
		@splitvals = `/etc/multiconf/scripts/netname $ARGV[5]`;
		chomp ($splitvals[0]);
		chomp ($splitvals[1]);
		$NetAddress = $splitvals[0];
		$NetMask = $splitvals[1];
		open (NONTRANS, "/etc/multiconf/nontransnet");
		$allowno = 1;
		$denyno = 1;
		while (<NONTRANS>)
		{
			@splitvals = split (/ /,$_);
			chomp ($splitvals[1]);
			if ($splitvals[0] eq $ARGV[5])
			{
				if ($splitvals[1] eq "Allowed")
				{
					system("$SCRIPTS/iptables editsquidfilter $eth0OldAddress $eth0OldMask $NetAddress $NetMask tcp 3128 $eth0NewAddress $eth0NewMask $NetAddress $NetMask tcp 3128 $splitvals[1] $allowno");
				}
				elsif ($splitvals[1] eq "Denied")
				{
					system("$SCRIPTS/iptables editsquidfilter $eth0OldAddress $eth0OldMask $NetAddress $NetMask tcp 3128 $eth0NewAddress $eth0NewMask $NetAddress $NetMask tcp 3128 $splitvals[1] $denyno");
				}
			}
			if ($splitvals[1] eq "Allowed")
			{
				$allowno++;
			}
			elsif ($splitvals[1] eq "Denied")
			{
				$denyno++;
			}
		}
		close (NONTRANS);
	}
}
#Called for updation of categories
elsif ($ARGV[0] eq "catupdate")
{
	$expired = `cat /etc/multiconf/surf_status`;
	chomp($expired);

	if($expired eq "EXPIRED")
	{
		exit;
	}
#Senthil 20050812 - Added For FlashISO
#Category database should not be greater than 17.3% of Root partition
  $disksize = `du -lh /var/chroot-squid/usr/local/SurfControl/CatEngine/CSP/SDK\\ Core\\ and\\ Productivity\\ Aura\\ List`;

  @splitvals = split(' ', $disksize);
  chomp(@splitvals);
  chop($splitvals[0]);

	#Find out where the hard disk is...
	open (LILOCONF, "/etc/lilo.conf");
	while (<LILOCONF>)
	{
        	if (/^boot=/)
	        {
        	        @splitvals = split (/=/, $_);
                	$hdisk = $splitvals[1];
	                chomp ($hdisk);
        	        last;
	        }
	}
	close (LILOCONF);
	my $rootpart = $hdisk . "3";
	my $block_size=`df | grep $rootpart | tr -s " " | cut -d " " -f2`;
	chomp ($block_size);
	$block_size = $block_size/1024;
	my $x = $block_size * 0.173;
	
  if($splitvals[0] >= $x)
  {
	open(DEBUG, ">>/etc/multiconf/http-debug");
	print DEBUG "Category Database exceeds $x - Exiting Category Update...\n";
	close(DEBUG);
	exit;
  }

	$time = 0;
	`pidof updater`;
	if ($? == 0)
	{
		$time = `ps -eo "%t,%p,%a" | grep updater | cut -d"," -f1 | tr -s " " | cut -d"-" -f1`;
		if($time < 2)
		{
			print "running\n";
			exit;
		}
		else
		{
			$pid = `pidof updater | cut -d" " -f1`;
			`kill -9 $pid`;
		}
	}

 	$call_date = `date +%d-%b-%Y`;
 	$call_time = `date +%X`;
	if(-e "/etc/multiconf/cat_date")
	{	
		open(DATEFILE,"/etc/multiconf/cat_date");
		$_ = <DATEFILE>;
		$_ = <DATEFILE>;
		$_ = <DATEFILE>;
		if ($_ ne "")
		{
			$update_date = $_;
			$update_time = <DATEFILE>;
		}
	}
	open (DATEFILE, ">/etc/multiconf/cat_date");
	print DATEFILE "$call_date";
	print DATEFILE "$call_time";
	if($update_date ne "")
	{
		print DATEFILE "$update_date";
		print DATEFILE "$update_time";
	}
	close (DATEFILE);

	qmailconc();
	snapshotb4update();
	
	# Backup the database files before update take place
	&backupb4update("Category.tar.gz");

	chdir("/");

	system ("/var/chroot-squid/usr/local/SurfControl/update/updater -c /var/chroot-squid/usr/local/SurfControl/update/swsys.cfg 1>/dev/null 2>/dev/null &");
	system("/etc/multiconf/scripts/cvscheckin update /var/chroot-squid/usr/local/squid/surfcontrol/update/swsys.cfg");

	#Senthil 20050810 - Added for FlashISO
	#Removing the database file
	system("/etc/multiconf/scripts/catremove 1>/dev/null 2>/dev/null &");
}
#To enter the license numbers
elsif ($ARGV[0] eq "catlicense")
{
	$lic_exists = 0;
	open(LICFILE,"/etc/multiconf/surflicense");
	while(<LICFILE>)
	{
		chomp($_);
		$final_license = $_;
		@vals = split(/:/,$_);
		if($vals[0] eq $ARGV[1])
		{
			if($vals[1] <= 0)
			{
				print "invalid\n";
				exit;
			}
			else
			{
				$lic_exists = 1;
			}
		}
	}
	close(LICFILE);
	@final_values = split(/:/,$final_license);
	if($lic_exists == 0)
	{
		$ret_val = `/etc/multiconf/scripts/validate validate $ARGV[1]`;
		if($ret_val == 1)
		{
			print "invalid\n";
			exit;
		}
		$drtn = `cat /var/chroot-squid/etc/.drtn`;
		chomp($drtn);
		open(SURFLICENSE,">>/etc/multiconf/surflicense");
		print SURFLICENSE "$ARGV[1]:$drtn:\n";
		close(SURFLICENSE);
		modify_license();
	}
	else
	{
		$tempfile = `/etc/multiconf/scripts/gettempfile`;
		open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
		open (SURFLICENSE,"/etc/multiconf/surflicense");
		while(<SURFLICENSE>)
		{
			chomp($_);
			@licenses = split(/:/,$_);
			if($licenses[0] eq $final_values[0])
			{
				$timerpid = `pidof timer`;
				system("kill -2 $timerpid");
				sleep(2);
				$duration = `cat /var/chroot-squid/etc/.drtn`;
				chomp($duration);
				print TEMPFILE "$licenses[0]:$duration:$final_values[2]\n";
			}
			elsif($licenses[0] ne $ARGV[1])
			{
				print TEMPFILE "$_\n";
			}
			else
			{
				$lic_duration = $licenses[1];
			}	
		}
		system("rm -f /var/chroot-squid/etc/.drtn");
		system("echo $lic_duration > /var/chroot-squid/etc/.drtn");
		system("/etc/multiconf/scripts/timer 1>>/dev/null 2>>/dev/null &");
		$new_time = `date +%s`;
		$dur_time = $lic_duration * 60;
		$tot_time = $new_time + $dur_time;
		print TEMPFILE "$ARGV[1]:$lic_duration:$tot_time\n";
		close(TEMPFILE);
		close(SURFLICENSE);
		system("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/surflicense");
		system("sync");
	}
}
#To get the license numbers
elsif ($ARGV[0] eq "getcatlicense")
{
	open(SURFLICENSE,"/etc/multiconf/surflicense");
	while(<SURFLICENSE>)
	{
		chomp($_);
		$lickey = $_;
	}
	@lic_values = split(/:/,$lickey);
	print "$lic_values[0]";
}
#To activate the surf control engine
elsif ($ARGV[0] eq "activatecat")
{
	if (!(-e "/var/chroot-squid/etc/.drtn"))
	{
		exit;
	}

	$oldupdate = `pidof updater`;
	chomp ($oldupdate);
	system ("kill -9 $oldupdate");

	open (SURF_STATUS, ">/etc/multiconf/surf_status");
	print SURF_STATUS "ACTIVATED\n";
	close (SURF_STATUS);

	open (PROCESSSTATUS, "/etc/multiconf/processstatus");
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	while (<PROCESSSTATUS>)
	{
		if (/TIMER/)
		{
			print TEMPFILE "TIMER=ON\n";
		}
		else
		{
			print TEMPFILE $_;
		}
	}
	close (PROCESSSTATUS);
	close (TEMPFILE);
	system ("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/processstatus");
	
	$timer = `pidof timer`;
	chomp($timer);
	if($timer eq "")
	{
		system("/etc/multiconf/scripts/timer 1>/dev/null 2>/dev/null &");
		modify_license();
	}
#following line is commented for lack of need for updating the database :Abhishek 20050321
#	snapshotb4update();
#	system ("/var/chroot-squid/usr/local/SurfControl/update/updater -c /var/chroot-squid/usr/local/SurfControl/update/swsys.cfg 1>/dev/null 2>/dev/null &");	
	system("/etc/multiconf/scripts/cvscheckin update /var/chroot-squid/usr/local/squid/surfcontrol/update/swsys.cfg");
	system("/etc/multiconf/scripts/cvscheckin update /etc/multiconf/update_track");
	system("/etc/multiconf/scripts/http catgetexpiry");	

#Senthil 2005-10-17 Added for restoring the autoupdate in cron 
	open (CONFFILE, "/etc/multiconf/httpconf");
        	while (<CONFFILE>)
	        {
        	        if (/HTTP UPDATE/)
                	{
                        	@updatearr = split (/:/,$_);
	                }
			elsif (/HTTP TIME/)
	                {
        	                @timearr = split (/:/,$_);
                	}

        	}
	close (CONFFILE);
	chomp(@updatearr);
	chomp(@timearr);
	if($updatearr[1] eq "enable")
	{
		system("/etc/multiconf/scripts/http updatetime enable $timearr[1]");
	}

	system("/etc/multiconf/scripts/CheckIntrusion update");
	system("sync");
}
#To get the expiry date
elsif ($ARGV[0] eq "catgetexpiry")
{
	#Validate the License first. Expire it if it current time is past expiry given. else do nothing
	`/etc/multiconf/scripts/validatesurf`;
	#get the expiry status
	$expired = `cat /etc/multiconf/surf_status`;
	chomp($expired);
	$processpid = 0;
	$processpid = `pidof timer`;
	chomp($processpid);

	if($expired eq "EXPIRED")
	{
		print "expired\n";
	}
	else
	{
		#Added Check for INITIAL:Abhishek 20050321
		if(($processpid == 0) || ($expired eq "INITIAL"))
		{
			print "empty\n";
		}
		else
		{
			$expiry = `/var/chroot-squid/usr/local/squid/surfcontrol/getcatexpiry`;
			@splitvals = split (/ /,$expiry);
			#The first entry will have the expiry date in yyyy:mm:dd format
			print "$splitvals[0]\n";
			print "$splitvals[1]";
		}
	}
}
#To get the SurfControl Status
elsif($ARGV[0] eq 'SCStatus')
{
	open (PROCESSSTATUS, "/etc/multiconf/processstatus");
	$status = `cat /etc/multiconf/surf_status`;
	chomp ($status);
	while (<PROCESSSTATUS>)
	{
		if (/TIMER/)
		{
			chomp ($_);
			@splitvals = split (/=/, $_);
			#Surf control status is on  only when it is turned on in processstatus and License status is activated: Abhishek 20050321
			if (($splitvals[1] eq "ON")&&($status eq "ACTIVATED"))
			{
				print "enable"
			}
			else
			{
				print "disable"
			}
			last;
		}
	}
	close (PROCESSSTATUS);
}
#Actions to be taken when SurfControl license expired
elsif ($ARGV[0] eq "catexpired")
{
	system("/etc/multiconf/scripts/http url disable");
	system("rm -f /var/chroot-squid/etc/.drtn");

	system ("rm -f /etc/cron.daily-3/catupdate");
	system ("rm -f /etc/cron.weekly/catupdate");
	system ("rm -f /etc/cron.fortnightly/catupdate");
	system ("rm -f /etc/cron.monthly/catupdate");

	open(SURFSTAT, ">/etc/multiconf/surf_status");
	print SURFSTAT "EXPIRED";
	close(SURFSTAT);

	open (PROCESSSTATUS, "/etc/multiconf/processstatus");
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	while (<PROCESSSTATUS>)
	{
		if (/TIMER/)
		{
			print TEMPFILE "TIMER=OFF\n";
		}
		else
		{
			print TEMPFILE $_;
		}
	}
	close (PROCESSSTATUS);
	close (TEMPFILE);
	system ("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/processstatus");
	
	#Get the list of admin mailids from email.conf and
	#send mails to all the ids
	
	my $NotifyUser = `/etc/multiconf/scripts/settings emailnotify check 'URL Filtering Key Expired'`;	
	if (-e "/etc/multiconf/mailids/email.conf" && ($NotifyUser eq 'yes'))
        {
	open (MAILIDS, "/etc/multiconf/mailids/email.conf") || die "Cannot open mail id list file\n";
	$maillist = "";
	$AdminIdPresent = 0;
	while (<MAILIDS>)
	{
		$AdminIdPresent = 1;
		$mailid = $_;
		chomp ($mailid);
		if ($maillist eq "")
		{
			$maillist = $mailid;
		}
		else
		{
			$maillist = $maillist . " " . $mailid;
		}
	} 
	close (MAILIDS);
	if ($AdminIdPresent == 1)
	{
		$hostname = `hostname`;
		#If the second parameter is for expiry, send the administrator
		#a mail on expiry.
		$subject = $hostname . " - URL Categorization Key Expired";
		#Write the message to be sent into a file
		$datetoprint = `date`;
		chomp ($datetoprint);
		`echo "Your Multi-Tech URL Content Filtering Subscription has expired.\nTo continue your content filtering subscription, renew your license by contacting your Multi-Tech dealer or Multi-Tech directly at 1-888-288-5470 or 763-785-3500.\n" | mutt -s "$subject" $maillist`;
	}
	}
	system("/etc/multiconf/scripts/CheckIntrusion update");
}
#To send a mail when the license key expires
elsif ($ARGV[0] eq "sendmail")
{
	my $NotifyUser = `/etc/multiconf/scripts/settings emailnotify check 'URL Filtering Key Will Expire'`;

        if (-e "/etc/multiconf/mailids/email.conf" && ($NotifyUser eq 'yes'))
        {
	open (MAILIDS, "/etc/multiconf/mailids/email.conf") || die "Cannot open mail id list file\n";
	$maillist = "";
	$AdminIdPresent = 0;
	while (<MAILIDS>)
	{
		$AdminIdPresent = 1;
		$mailid = $_;
		chomp ($mailid);
		if ($maillist eq "")
		{
			$maillist = $mailid;
		}
		else
		{
			$maillist = $maillist . " " . $mailid;
		}
	} 
	close (MAILIDS);
	if ($AdminIdPresent == 1)
	{
		$hostname = `hostname`;
		#If the second parameter is for expiry, send the administrator
		#a mail on expiry.
		$subject = $hostname . " - URL Categorization Key Expiry";
		#Write the message to be sent into a file
		$datetoprint = `date`;
		chomp ($datetoprint);
		`echo "Your Multi-Tech URL Content Filtering Subscription will expire in $ARGV[1] day(s).\nTo continue your content filtering subscription, renew your license by contacting your Multi-Tech dealer or Multi-Tech directly at 1-888-288-5470 or 763-785-3500.\n" | mutt -s "$subject" $maillist`;
	}
	}
}
elsif ($ARGV[0] eq "updatetime")
{
	system ("rm -f /etc/cron.daily-3/catupdate");
	system ("rm -f /etc/cron.weekly/catupdate");
	system ("rm -f /etc/cron.fortnightly/catupdate");
	system ("rm -f /etc/cron.monthly/catupdate");

	open (CONFFILE, "/etc/multiconf/httpconf");
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	while (<CONFFILE>)
	{
		if (/HTTP UPDATE/)
		{
			print TEMPFILE "HTTP UPDATE:$ARGV[1]\n";
		}
		elsif (/HTTP TIME/)
		{
			print TEMPFILE "HTTP TIME:$ARGV[2]\n";
		}
		else
		{
			print TEMPFILE $_;
		}
	}
	close (CONFFILE);
	close (TEMPFILE);
	system ("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/httpconf");
	if ($ARGV[1] eq "enable")
	{
		$time = $ARGV[2];
		if ($time eq "Daily")
		{
			$crontime = "cron\.daily-3";
		}
		elsif ($time eq "Weekly")
		{
			$crontime = "cron\.weekly";
		}
		elsif ($time eq "Fortnightly")
		{
			$crontime = "cron\.fortnightly";
		}
		elsif ($time eq "Monthly")
		{
			$crontime = "cron\.monthly";
		}
		open (CRONFILE, ">/etc/$crontime/catupdate");
		print CRONFILE "/etc/multiconf/scripts/http catupdate\n";
		close (CRONFILE);
		system ("chmod 755 /etc/$crontime/catupdate");
	}
}
elsif ($ARGV[0] eq "getupdatetime")
{
	open (CONFFILE, "/etc/multiconf/httpconf");
	while (<CONFFILE>)
	{
		if (/HTTP UPDATE/)
		{
			@updatearr = split (/:/,$_);
		}
		elsif (/HTTP TIME/)
		{
			@timearr = split (/:/,$_);
		}
	}
	close (CONFFILE);
	print $updatearr[1];
	print $timearr[1];
}
#Things to be done when the updation of filters is completed.
#Senthil 20050825 - Added for FLASH ISO
#Sync is not done properly when updater is running. So after killing the updater
#the updatefiles will be called.

elsif ($ARGV[0] eq "updatefiles")
{
	open(UPDATEFILE, ">/etc/cron.fivemins/urlupdatefiles");
	print UPDATEFILE "#!/usr/bin/perl\n\n";
	print UPDATEFILE "system(\"/etc/multiconf/scripts/http updatefiles_up\")\;\n";
	print UPDATEFILE "system(\"rm -f /etc/cron.fivemins/urlupdatefiles\")\;\n";
	close(UPDATEFILE);
	system("chmod 700 /etc/cron.fivemins/urlupdatefiles");
}
elsif ($ARGV[0] eq "updatefiles_up")
{
	#Updation has been completed. There are cases to be considered:
	#1. If updation is successful
	#	1. If the new files are the same as the files present so far
	#	2. If there has been a proper updation (ie) new files are there
	#2. If updation is not successful
	#	1. Could not connect to the server at all
	#	2. The half-file updated will still be there in this case.

	#Added for mailing appropriate Error Messages to admin after the Update
	$err_msg = "URL Categorization Database Updation Failed.\nRetry the process.";
	$file_changed = 0;
	$extra_files = 0;

	#Take the latest snapshot for comparison
	@new_val = `ls -l /var/chroot-squid/usr/local/SurfControl/CatEngine/CSP/SDK\\ Core\\ and\\ Productivity\\ Aura\\ List | grep -v total | tr -s " " | cut -d " " -f9`;

	chdir("/var/chroot-squid/usr/local/SurfControl/CatEngine/CSP/SDK\ Core\ and\ Productivity\ Aura\ List");
	for ($i = 0; $i < @new_val; $i++)
	{
		$file_found = 0;
		chomp ($new_val[$i]);
		#Take the last modification time for comparison
		$new_mod_val = (stat $new_val[$i])[9];

		open (UPDATETRACK,"/etc/multiconf/update_track");
		while (<UPDATETRACK>)
		{
			chomp ($_);
			@old_vals = split (/ /,$_);
			if ($old_vals[0] eq $new_val[$i])
			{
				#The file exists.
				open (DEBUG,">>/etc/multiconf/http-debug");
				print DEBUG "File Name - $new_val[$i] & $old_vals[0] !!!\n";
				close (DEBUG);
				$file_found = 1;
				if ($old_vals[1] != $new_mod_val)
				{
					#The file has got changed. So, update has taken place successfully.
					$file_changed = 1;
					#last;
				}
				#last;
			}
		}
		close(UPDATETRACK);
		if ($file_found != 1)
		{
			#The file is not found. Hence, it must be an error file. Delete it.
		
			# Bypass block.log and error.log
			if (($new_val[$i] eq "block.log") || ($new_val[$i] eq "error.log"))
			{
				system ("rm -f /var/chroot-squid/usr/local/SurfControl/CatEngine/CSP/SDK\\ Core\\ and\\ Productivity\\ Aura\\ List/$new_val[$i]");
			}
			else
			{  
				if (!(($new_val[$i] =~ /\.csf/) || ($new_val[$i] =~ /\.cdb/)))
        {
	
					open (DEBUG,">>/etc/multiconf/http-debug");
					print DEBUG "Error file $new_val[$i].\n";
					system ("rm -f /var/chroot-squid/usr/local/SurfControl/CatEngine/CSP/SDK\\ Core\\ and\\ Productivity\\ Aura\\ List/$new_val[$i]");
					$extra_files++; 
					print DEBUG "Extra - $extra_files.\n";
					close (DEBUG);
				}
			}
		}
		if ($file_changed == 1)
		{	
			#Sending the Correct message to the user.
			$err_msg = "URL Categorization Database Updation Completed Successfully";
			last;
		}
	}
=sen
	if (($extra_files == 0) && ($file_changed == 0))
	{
		#All the correct files were present and also there are no 
		#new modifications done
		#This may mean
		#	1. There was no update available or
		#	2. The server could not be contacted at all (not sure what to do for this) or
		# 3. The update started, but it failed in between.
		#	The half-updated file would have got deleted
		#check for case 3.
		$dirname = "/var/chroot-squid/usr/local/SurfControl/CatEngine/CSP/SDK\ Core\ and\ Productivity\ Aura\ List";
		$dirmod_time = (stat $dirname)[9];
		#Get the last modified time of the folder.
		open (UPDATETRACK,"/etc/multiconf/update_track");
		while (<UPDATETRACK>)
		{
			chomp ($_);
			@old_vals = split (/ /,$_);
			if ($old_vals[0] eq "\.")
			{
				if ($old_vals[1] == $dirmod_time)
				{
					$err_msg = "The server could not be contacted or there were no Files to update\n";
					open (DEBUG,">>/etc/multiconf/http-debug");
					print DEBUG "No files to update\n";
					close (DEBUG);
				}
				else
				{
					$err_msg = "URL Categorization Database Updation Failed.\nRetry the process.";
					open (DEBUG,">>/etc/multiconf/http-debug");
					print DEBUG "Failed - dir modtime different!!!\n";
					print DEBUG "$old_vals[1] and $dirmod_time\n";
					close (DEBUG);
				}
			}
		}
	}
	elsif ($extra_files > 0)
=cut
	if ($extra_files > 0)
	{
		#There is/are some extra files in the system which are not required. It means that the update was not completed.
		$err_msg = "URL Categorization Database Updation Failed.\nRetry the process.";
		open (DEBUG,">>/etc/multiconf/http-debug");
		print DEBUG "Failed !!!\n";
		close (DEBUG);
	}

=sen
	if ($file_changed != 1)
	{
		system("rm -f /var/log/URL/Category.tar.gz");
		system("/etc/multiconf/scripts/CheckIntrusion update");
		exit;
	}
=cut
	if (($file_changed == 1) || ($extra_files > 0))
  {

		my $NotifyUser = `/etc/multiconf/scripts/settings emailnotify check 'URL Filtering Categories Updated'`;	

		if (-e "/etc/multiconf/mailids/email.conf" && ($NotifyUser eq 'yes'))
		{
			open (MAILIDS, "/etc/multiconf/mailids/email.conf") || die "Cannot open mail id list file\n";
			$maillist = "";
			$AdminIdPresent = 0;
			while (<MAILIDS>)
			{
				$AdminIdPresent = 1;
				$mailid = $_;
				chomp ($mailid);
				if ($maillist eq "")
				{
					$maillist = $mailid;
				}
				else
				{
					$maillist = $maillist . " " . $mailid;
				}
			} 
			close (MAILIDS);
			if ($AdminIdPresent == 1)
			{
				$datetoprint = `date`;
				chomp ($datetoprint);
				$hostname = `hostname`;
				chomp ($hostname);
				$subject = $hostname . " - URL Categories Updation";

				`echo "$datetoprint $err_msg\n" | mutt -s "$subject" $maillist`;
			}
		}
	}

	system ("/etc/init.d/surf stop");


	open(SURF_TIME, "/etc/multiconf/cat_date");
	$exec_date = <SURF_TIME>;
	$exec_time = <SURF_TIME>;
	close(SURF_TIME);
	
  	$update_date = `date +%d-%b-%Y`;
	$update_time = `date +%X`;

	open(SURF_TIME, ">/etc/multiconf/cat_date");
	print SURF_TIME "$exec_date";
	print SURF_TIME "$exec_time";
	print SURF_TIME "$update_date";
	print SURF_TIME "$update_time";
	close(SURF_TIME);
	
	system("/etc/multiconf/scripts/cvscheckin update /etc/multiconf/surf_status");

	#The list of categories has to be updated
	#The binary called below will put all the new categories after update
	#in the file /var/chroot-squid/usr/local/squid/surfcontrol/Categories
	#So, before calling it, copy the file to a temporary file which can
	#be used for comparison
	system ("cp -f /var/chroot-squid/usr/local/squid/surfcontrol/Categories /var/chroot-squid/usr/local/squid/surfcontrol/oldcat");

	#Call the binary to get the categories
	chdir ("/var/chroot-squid/usr/local/squid/surfcontrol");
	#Lock Aide
        system("/etc/multiconf/scripts/aide_dis create dont_run_aide");
	system ("rm -f /var/chroot-squid/usr/local/squid/surfcontrol/Categories");

	# Following line is commented to check against errors/warnings while 
	# update takes place

	# system ("/var/chroot-squid/usr/local/squid/surfcontrol/getcategory");
	
	# Restore previous successfully updated database files if
	# any error occurs while update takes place

	if (&restore_category_database("Category.tar.gz") == -1) # Check for Success / Failure of updation
	{
		system ("rm -f /var/chroot-squid/usr/local/squid/surfcontrol/oldcat"); # Remove the oldcat file
                system("/etc/multiconf/scripts/CheckIntrusion update");	# Update CheckIntrusion
		#Unlock Aide
	        `/etc/multiconf/scripts/aide_dis delete dont_run_aide`;
                exit; # Exit at this point on Failure
	}	

	$EntryFound = 0;

	open (NEWCAT, "/var/chroot-squid/usr/local/squid/surfcontrol/Categories");
	while (<NEWCAT>)
	{
		chomp ($_);
		if ($_ ne "")
		{
			$EntryFound = 1;
			last;
		}
	}
	close (NEWCAT);
	if ($EntryFound == 0)
	{
		system ("mv -f /var/chroot-squid/usr/local/squid/surfcontrol/oldcat /var/chroot-squid/usr/local/squid/surfcontrol/Categories");
		system("/etc/multiconf/scripts/CheckIntrusion update");
		#Unlock Aide
	        system("/etc/multiconf/scripts/aide_dis delete dont_run_aide");
		exit;
	}

	#Now do a comparison of the old and new categories
	$diffres = system ("diff /var/chroot-squid/usr/local/squid/surfcontrol/oldcat /var/chroot-squid/usr/local/squid/surfcontrol/Categories");
	#If there is no difference, return back
	if ($diffres == 0)
	{
		system ("rm -f /var/chroot-squid/usr/local/squid/surfcontrol/oldcat");
		system("/etc/multiconf/scripts/CheckIntrusion update");
		#Unlock Aide
	        system("/etc/multiconf/scripts/aide_dis delete dont_run_aide");
		exit;
	}

	#If the old category list had something extra, remove the category
	#from the Allowed or Denied Category list
	open (OLDCAT, "/var/chroot-squid/usr/local/squid/surfcontrol/oldcat");
	while (<OLDCAT>)
	{
		$category = $_;
		chomp ($category);
		$CatFound = 0;
		open (NEWCAT, "/var/chroot-squid/usr/local/squid/surfcontrol/Categories");
		while (<NEWCAT>)
		{
			if (/^$category$/)
			{
				$CatFound = 1;
				last;
			}
		}
		close (NEWCAT);
		#If the category is not found in the new file, we have to remove
		#it from the Allowed / Denied Categories.
		if ($CatFound == 0)
		{
			open (ALLOWCAT, "/var/chroot-squid/usr/local/squid/surfcontrol/AllowedCategories");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<ALLOWCAT>)
			{
				if (!(/^$category$/))
				{
					print TEMPFILE $_;
				}
			}
			close (TEMPFILE);
			close (ALLOWCAT);
			system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/surfcontrol/AllowedCategories");

			open (DENYCAT, "/var/chroot-squid/usr/local/squid/surfcontrol/DeniedCategories");
			$tempfile = `/etc/multiconf/scripts/gettempfile`;
			open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
			while (<DENYCAT>)
			{
				if (!(/^$category$/))
				{
					print TEMPFILE $_;
				}
			}
			close (TEMPFILE);
			close (DENYCAT);
			system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/surfcontrol/DeniedCategories");
		}
	}
	close(OLDCAT);

	#If the new category list has got something extra, add it in the
	#allowed category list
	open (NEWCAT, "/var/chroot-squid/usr/local/squid/surfcontrol/Categories");
	while (<NEWCAT>)
	{
		$category = $_;
		chomp ($category);
		$CatFound = 0;
		open (OLDCAT, "/var/chroot-squid/usr/local/squid/surfcontrol/oldcat");
		while (<OLDCAT>)
		{
			if (/^$category$/)
			{
				$CatFound = 1;
				last;
			}
		}
		close (OLDCAT);
		#If the category is not found in the old file and it is not in the current list, add it in the
		#Allowed category file
		if ($CatFound == 0)
		{
			 @presence = `grep \"\^$category\$\" \/var\/chroot-squid\/usr\/local\/squid\/surfcontrol\/AllowedCategories`;
       chomp (@presence);
       if (0 == @presence)
       {
				open (ALLOWCAT, ">>/var/chroot-squid/usr/local/squid/surfcontrol/AllowedCategories");
				print ALLOWCAT "$category\n";
				close (ALLOWCAT);
			}
		}
	}
	close(NEWCAT);
	system ("rm -f /var/chroot-squid/usr/local/squid/surfcontrol/oldcat");
	system ("chown multiweb:multiweb /var/chroot-squid/usr/local/squid/surfcontrol/AllowedCategories");
	system ("chown multiweb:multiweb /var/chroot-squid/usr/local/squid/surfcontrol/DeniedCategories");
	#Unlock Aide
        system("/etc/multiconf/scripts/aide_dis delete dont_run_aide");

	# Start - Niranjan
	# Added to overcome URL categorization problem whenever updates happened, but using the 
	# old profile.cfg file

	# Move profile.cfg to profile.cfg.bak file
	system ("mv -f /var/chroot-squid/usr/local/SurfControl/profile/profile.cfg /var/chroot-squid/usr/local/SurfControl/profile/profile.cfg.bak");

	# Remove profile.cfg file to recreate by calling profile binary
	#system ("rm -f /var/chroot-squid/usr/local/SurfControl/profile/profile.cfg");
	# End - Niranjan

	system("/etc/multiconf/scripts/CheckIntrusion update");
	system("/etc/multiconf/scripts/cvscheckin update /var/chroot-squid/usr/local/squid/surfcontrol/Categories");
	system("/etc/multiconf/scripts/cvscheckin update /var/chroot-squid/usr/local/squid/surfcontrol/AllowedCategories");
	system("/etc/multiconf/scripts/cvscheckin update /var/chroot-squid/usr/local/squid/surfcontrol/DeniedCategories");
	system("/etc/multiconf/scripts/cvscheckin update /var/chroot-squid/usr/local/squid/surfcontrol/swsys.cfg");
	system("/etc/multiconf/scripts/cvscheckin update /etc/multiconf/update_track");
	system("/etc/multiconf/scripts/cvscheckin update /etc/multiconf/surf_status");
	system("/etc/init.d/surf start");
	system("/etc/init.d/squid reread");
	
	# Start - Niranjan
	# Check whether new profile.cfg recreated properly or not
        if (-f "/var/chroot-squid/usr/local/SurfControl/profile/profile.cfg")
        {
                # profile.cfg is newly created by profile, so remove the profile.cfg.bak file
                system ("rm -f /var/chroot-squid/usr/local/SurfControl/profile/profile.cfg.bak");
        }
        else
        {
                # Get profile.cfg from the profile.cfg.bak file
                system ("mv -f /var/chroot-squid/usr/local/SurfControl/profile/profile.cfg.bak /var/chroot-squid/usr/local/SurfControl/profile/profile.cfg");
        }
        # End - Niranjan
}
elsif ($ARGV[0] eq "updateprogress")
{
	$pidofsurf = `pidof updater`;
	chomp($pidofsurf);
	
}
elsif ($ARGV[0] eq "backup")					# Backup on Demand
{
	$backupstatus = `pidof gzip`;				# Get process id
        chomp($backupstatus);					# Strip new line character and extra blanks
	if ($backupstatus eq '')				# Check process id
	{
		&backupb4update("UserCategory.tar.gz");		# Backup Function Call
	}
	else
	{
		print "Backup/Restore is in progress.\n";		# Process is already running
	}
}
elsif ($ARGV[0] eq "restore")							# Restore on Demand
{
	if (!(-e "/var/log/URL/UserCategory.tar.gz"))			# Check for file existence
	{
		print "Backup does not exists.\nFirst take backup.\n";		# Backup does not exists
	}
	else
	{

		$restorestatus = `pidof gzip`;			# Get process id
        	chomp($restorestatus);				# Strip new line character and extra blanks
        	if ($restorestatus eq '')			# Check process id
        	{
			system("/etc/init.d/surf stop");			# Stop Surf 
#			&restore_category_database("UserCategory.tar.gz");	# Restore Function Call
			system("/etc/init.d/surf start");			# Start Surf

		}
		else
		{
			print "Restore/Backup is in progress.\n";	# Process is already running
		}
	}
}
elsif ($ARGV[0] eq "dispbackup")				# Display Backup / Restore Button(s)
{
	if (-e "/var/log/URL/UserCategory.tar.gz")		# Check for file existence
	{
		print "enable\n";				# print enable on console
		$last_backup = `ls /var/log/URL/UserCategory.tar.gz -l --full-time | tr -s " " | cut -d " " -f6-10`;
		print $last_backup."\n";
	}
	else
	{
		print "disable\n";				# print disable on console
	}
}					
elsif ($ARGV[0] eq "lastupdatetime")
{
	$count = 0;
	if (!(-e "/etc/multiconf/cat_date"))
	{
		print "empty\n";
		print "empty";
	}
	else
	{
		open (CATDATE, "/etc/multiconf/cat_date");
		while (<CATDATE>)
		{
			if ($_ eq "")
			{
				print "empty\n";
				print "empty";
				close (CATDATE);
				exit;
			}
			else
			{
				$count++;
				print $_;
			}
		}
		close (CATDATE);
	}
	if($count <= 2)
	{
		print "empty\n";
		print "empty\n";
		$urlupdater = `pidof updater`;
		chomp ($urlupdater);
		$updatefiles = `ps ax --cols 250 | grep -v grep | grep "/usr/bin/perl -w /etc/multiconf/scripts/http updatefiles_up" | tr -s " " | cut -d" " -f1`;
		chomp ($updatefiles);
		$catupdate = `ps ax --cols 250 | grep -v grep | grep "/usr/bin/perl -w /etc/multiconf/scripts/http catupdate" | tr -s " " | cut -d" " -f1`;
		chomp($catupdate);
		if (($urlupdater ne "") || ($updatefiles ne "") || ($catupdate ne ""))
		{
			print "URL Updation in Progress\n";
		}
		else
		{
			print "No Updation";
		}
	}
	else
	{
		$urlupdater = `pidof updater`;
		chomp ($urlupdater);
		$updatefiles = `ps ax --cols 250 | grep -v grep | grep "/usr/bin/perl -w /etc/multiconf/scripts/http updatefiles_up" | tr -s " " | cut -d" " -f1`;
		chomp ($updatefiles);
		$catupdate = `ps ax --cols 250 | grep -v grep | grep "/usr/bin/perl -w /etc/multiconf/scripts/http catupdate" | tr -s " " | cut -d" " -f1`;
		chomp($catupdate);
		if (($urlupdater ne "") || ($updatefiles ne "") || ($catupdate ne ""))
		{
			print "URL Updation in Progress\n";
		}
		#else
		#{
		#	print "URL Updation Completed\n";
		#}
	}
}
#To check if the SurfControl is activated
elsif ($ARGV[0] eq "checkactivate")
{
	$temp_val = `cat /var/chroot-squid/etc/.drtn`;
	chomp($temp_val);
	if( $temp_val > 0)
	{
		print "yes\n";
	}
	else
	{
		print "no\n";
	}
}
elsif ($ARGV[0] eq "notifyerror")
{
	open(SURFERROR,"/etc/multiconf/surferror");
	$message = <SURFERROR>;
	close(SURFERROR);

	my $NotifyUser = `/etc/multiconf/scripts/settings emailnotify check 'URL Categories Update Failed'`;

	if (-e "/etc/multiconf/mailids/email.conf" && ($NotifyUser eq 'yes'))
	{
		open (MAILIDS, "/etc/multiconf/mailids/email.conf") || die "Cannot open mail id list file\n";
		$maillist = "";
		$AdminIdPresent = 0;
		while (<MAILIDS>)
		{
			$AdminIdPresent = 1;
			$mailid = $_;
			chomp ($mailid);
			if ($maillist eq "")
			{
				$maillist = $mailid;
			}
			else
			{
				$maillist = $maillist . " " . $mailid;
			}
		} 
		close (MAILIDS);
		if ($AdminIdPresent == 1)
		{
			$datetoprint = `date`;
			chomp ($datetoprint);
			$hostname = `hostname`;
			chomp ($hostname);
			$subject_info = " - URL Categories Updation";
			$subject = $hostname . "-" . " " . "URL" . "Categories" . "Updation";
			$subject = $hostname . $subject_info;

			`echo "$datetoprint URL Categories Updation failed.\n Error Message -$message" | mutt -s "$subject" $maillist`;
		}
	}
	
	system("/etc/multiconf/scripts/CheckIntrusion update");
	system("/etc/multiconf/scripts/cvscheckin update /etc/multiconf/update_track");
}
#M13 LVPNVer 2.0...
elsif ($ARGV[0] eq "CacheSize")
{
=niranjan
	#Get the squid partition size
	$squidpartsize = `df | grep squid | tr -s " " | cut -d " " -f2`;
	chomp ($squidpartsize);
	#Calculate 60 % of the size in MB and round it off
	$cachesize = ($squidpartsize * 0.6) / 1024;
	@decval = split (/\./,$cachesize);
	$cachesize = $decval[0];
	if ($cachesize > 2000)
	{
		$cachesize = 2000;
	}

	#Update the squid configuration file with the new squid cache size	
	open (SQUIDCONF, "/var/chroot-squid/usr/local/squid/etc/squid.conf");
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	while (<SQUIDCONF>)
	{
		if (/cache_dir ufs \/usr\/local\/squid\/cache/)
		{
			print TEMPFILE "cache_dir ufs \/usr\/local\/squid\/cache 1 1 1\n";
		}
		else
		{
			print TEMPFILE $_;
		}
	}
	close (SQUIDCONF);
	close (TEMPFILE);
	system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/etc/squid.conf");
	system("/etc/multiconf/scripts/CheckIntrusion update");
=cut
}
elsif ($ARGV[0] eq "logrotate")
{
	#SARG uses the access log of squid. So, if access log is rotated,
	#SARG will not have any data for creating the reports.
	#So, before rotation, we will copy the latest access log to the
	#file /etc/multiconf/sargfile (this is the file used by sarg for
	#report generation. Here, we have to avoid duplicacy of data while
	#copying. So we maintain a file sargtime to keep track of the time
	#field of the last entry in the access log when it was copied.

	$logfile = $ARGV[1];
	if ($logfile eq "access")
	{
		$datestr = `date +%Y-%m-%d`;
		chomp ($datestr);
		#Form the name for the zip file
		$sargzipfile = "sargfile_$datestr" . "\." . "zip";

		chdir ("/etc/multiconf");
		#Delete any old zip file present
		@oldfiles = `ls sargfile_*.zip | grep -v $sargzipfile`;
		$oldfilecount = @oldfiles;
		for ($i = 0; $i < $oldfilecount; $i++)
		{
			system ("rm -f $oldfiles[$i]");
		}

		#The sargfile will be a zip file. Unzip it and use.
		if (-e "/etc/multiconf/$sargzipfile")
		{
			system ("unzip -o $sargzipfile");
			open (SARG, "/etc/multiconf/sargtime");
			$lasttime = <SARG>;
			chomp ($lasttime);
			close (SARG);
			if ($lasttime eq "")
			{
				#SARG has not run since the last log rotate
				#Append everything from access.log to sargfile
				open (ACCESSLOG, "/var/chroot-squid/usr/local/squid/logs/access.log");
				open (SARG, ">>/etc/multiconf/sargfile");
				while (<ACCESSLOG>)
				{	
					print SARG $_;
				}
				close (ACCESSLOG);
				close (SARG);
			}
			else
			{
				#We have to copy only the new lines to sargfile
				open (ACCESSLOG, "/var/chroot-squid/usr/local/squid/logs/access.log");
				open (SARG, ">>/etc/multiconf/sargfile");
				while (<ACCESSLOG>)
				{	
					if (/$lasttime/)
					{
						while (<ACCESSLOG>)
						{	
							print SARG $_;
						}
						last;
					}
				}
				close (ACCESSLOG);
				close (SARG);
			}
		}
		else
		{
			#First time in the day, the zip file is to be created.
			my $sizeofaccess = `tail -n 10 /var/chroot-squid/usr/local/squid/logs/access.log | wc -l`;
		        chomp($sizeofaccess);
		        if($sizeofaccess > 0) #Check for non empty
		        {
				system ("cp -f /var/chroot-squid/usr/local/squid/logs/access.log /etc/multiconf/sargfile");
			}
		}
		if(-e "/etc/multiconf/sargfile")
		{
			system ("echo \"\" >/etc/multiconf/sargtime");
			chdir ("/etc/multiconf");
			system ("zip $sargzipfile sargfile");
			system ("rm -f sargfile");
		}
	}
	elsif ($logfile eq "deny")
	{
		$datestr = `date +%Y-%m-%d`;
		chomp ($datestr);
		#Form the name for the zip file
		$sargzipfile = "sargdeny_$datestr" . "\." . "zip";

		chdir ("/etc/multiconf");
		#Delete any old zip file present
		@oldfiles = `ls sargdeny_*.zip | grep -v $sargzipfile`;
		$oldfilecount = @oldfiles;
		for ($i = 0; $i < $oldfilecount; $i++)
		{
			system ("rm -f $oldfiles[$i]");
		}

		#The sargfile will be a zip file. Unzip it and use.
		if (-e "/etc/multiconf/$sargzipfile")
		{
			system ("unzip -o $sargzipfile");
			open (SARG, "/etc/multiconf/sargdenytime");
			$lasttime = <SARG>;
			chomp ($lasttime);
			close (SARG);
			if ($lasttime eq "")
			{
				#SARG has not run since the last log rotate
				#Append everything from surfing.log to sargfile
				open (ACCESSLOG, "/var/chroot-squid/usr/local/squid/surfcontrol/surfing.log");
				open (SARG, ">>/etc/multiconf/sargdenyfile");
				while (<ACCESSLOG>)
				{
					chomp ($_);	
					print SARG "$_\n";
				}
				print SARG "\n";
				close (ACCESSLOG);
				close (SARG);
			}
			else
			{
				#We have to copy only the new lines to sargfile
				open (ACCESSLOG, "/var/chroot-squid/usr/local/squid/surfcontrol/surfing.log");
				open (SARG, ">>/etc/multiconf/sargdenyfile");
				while (<ACCESSLOG>)
				{	
					if (/$lasttime/)
					{
						while (<ACCESSLOG>)
						{	
							chomp ($_);
							print SARG "$_\n";
						}
						last;
					}
				}
				print SARG "\n";
				close (ACCESSLOG);
				close (SARG);
			}
		}
		else
		{
			#First time in the day, the zip file is to be created.
			my $sizeofdeny = `tail -n 10 /var/chroot-squid/usr/local/squid/surfcontrol/surfing.log | wc -l`;
		        chomp($sizeofdeny);
		        if($sizeofdeny > 0) #Check for non empty
		        {
				system ("cp -f /var/chroot-squid/usr/local/squid/surfcontrol/surfing.log /etc/multiconf/sargdenyfile");
			}
		}
		if(-e "/etc/multiconf/sargdenyfile")
		{
			system ("echo \"\" >/etc/multiconf/sargdenytime");
			chdir ("/etc/multiconf");
			system ("zip $sargzipfile sargdenyfile");
			system ("rm -f sargdenyfile");
		}
	}
}
elsif ($ARGV[0] eq "changenetaction")
{
	$netw = $ARGV[2];
	@pars = `/etc/multiconf/scripts/netname $netw`;
	$address = $pars[0];
	$mask = $pars[1];
	chomp($address);
	chomp($mask);

	$old_action = $ARGV[3];
	$new_action = $ARGV[4];

	$eth0address = `$SCRIPTS/getethaddress eth0`;
	@eth0add = split(/:/,$eth0address);
	chomp ($eth0add[1]);

	open (HTTPCONF,"$DIR/httpconf");
	while (<HTTPCONF>)
	{
		if(/HTTP TRANS/)
		{
			@splvals = split (/:/,$_);
			$transstatus = $splvals[1];
			chomp ($transstatus);
			last;
	 	}
	} 
	close (HTTPCONF);
	if ($ARGV[1] eq 'transparent')
	{
		if ($transstatus eq "OFF")
		{
			exit;
		}

		open (NONTRANSFILE, "/etc/multiconf/transnet");
		$allowno = 1;
		$denyno = 1;
		while (<NONTRANSFILE>)
		{
			@splitvals = split (/ /,$_);
			if ($splitvals[0] eq $netw)
			{
				last;
			}
			chomp ($splitvals[1]);
			if ($splitvals[1] eq "Allowed")
			{
				$allowno = $allowno + 2;
			}
			elsif ($splitvals[1] eq "Denied")
			{
				$denyno = $denyno + 2;
			}
		}
		close (NONTRANSFILE);

		if ($old_action eq "Allowed")
		{
			system("$SCRIPTS/iptables deltransfilter allow $eth0add[0] $eth0add[1] $address $mask tcp 80");
		}
		elsif ($old_action eq "Denied")
		{
			system("$SCRIPTS/iptables deltransfilter drop $eth0add[0] $eth0add[1] $address $mask tcp 80");
		}
		if ($new_action eq "Allowed")
		{
			system("$SCRIPTS/iptables addtransfilter allow $eth0add[0] $eth0add[1] $address $mask tcp 80 $allowno");
		}
		elsif ($new_action eq "Denied")
		{
			system("$SCRIPTS/iptables addtransfilter drop $eth0add[0] $eth0add[1] $address $mask tcp 80 $denyno");
		}
	}
	elsif($ARGV[1] eq 'nontransparent')
	{
		if ($transstatus eq "ON")
		{
			exit;
		}

		open (NONTRANSFILE, "/etc/multiconf/nontransnet");
		$allowno = 1;
		$denyno = 1;
		while (<NONTRANSFILE>)
		{
			@splitvals = split (/ /,$_);
			if ($splitvals[0] eq $netw)
			{
				last;
			}
			chomp ($splitvals[1]);
			if ($splitvals[1] eq "Allowed")
			{
				$allowno++;
			}
			elsif ($splitvals[1] eq "Denied")
			{
				$denyno++;
			}
		}
		close (NONTRANSFILE);

		if ($old_action eq "Allowed")
		{
			system("$SCRIPTS/iptables delsquidfilter allow $eth0add[0] $eth0add[1] $address $mask tcp 3128");
		}
		elsif ($old_action eq "Denied")
		{
			system("$SCRIPTS/iptables delsquidfilter drop $eth0add[0] $eth0add[1] $address $mask tcp 3128");
		}
		if ($new_action eq "Allowed")
		{
			system("$SCRIPTS/iptables addsquidfilter allow $eth0add[0] $eth0add[1] $address $mask tcp 3128 $allowno");
		}
		elsif ($new_action eq "Denied")
		{
			system("$SCRIPTS/iptables addsquidfilter drop $eth0add[0] $eth0add[1] $address $mask tcp 3128 $denyno");
		}
	}
}
#To add / delete IP addresses which bypass URL category
elsif ($ARGV[0] eq "bypass_url")
{
	$action = $ARGV[1];
	$address = $ARGV[2];
	$mask = $ARGV[3];

	if ($action eq "add")
	{
		open (EXCLUDEIP, ">>/var/chroot-squid/usr/local/squid/etc/excludeip");
		print EXCLUDEIP "$address $mask\n";
		close (EXCLUDEIP);
	}
	elsif ($action eq "delete")
	{
		open (EXCLUDEIP, "/var/chroot-squid/usr/local/squid/etc/excludeip");
		$tempfile = `/etc/multiconf/scripts/gettempfile`;
		open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
		$strtosearch = "$address $mask";
		while (<EXCLUDEIP>)
		{
			chomp ($_);
			if ($_ ne $strtosearch)
			{
				print TEMPFILE "$_\n";
			}
		}
		close (TEMPFILE);
		close (EXCLUDEIP);
		system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/etc/excludeip");
	}
	system ("/etc/init.d/squid reread");
}
elsif ($ARGV[0] eq "edit_bypass_url")
{
	$oldip = $ARGV[1];
	$oldmask = $ARGV[2];
	$newip = $ARGV[3];
	$newmask = $ARGV[4];

	open (EXCLUDEIP, "/var/chroot-squid/usr/local/squid/etc/excludeip");
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	$strtosearch = "$oldip $oldmask";
	while (<EXCLUDEIP>)
	{
		chomp ($_);
		if ($_ eq $strtosearch)
		{
			print TEMPFILE "$newip $newmask\n";
		}
		else
		{
			print TEMPFILE "$_\n";
		}
	}
	close (TEMPFILE);
	close (EXCLUDEIP);
	system ("mv -f /etc/multiconf/scripts/$tempfile /var/chroot-squid/usr/local/squid/etc/excludeip");
	system ("/etc/init.d/squid reread");
}
elsif ($ARGV[0] eq "disp_bypass_url")
{
	open (EXCLUDEIP, "/var/chroot-squid/usr/local/squid/etc/excludeip");
	while (<EXCLUDEIP>)
	{
		print $_;
	}
	close (EXCLUDEIP);
}
#...M13 LVPNVer 2.0
elsif ($ARGV[0] eq "clean_cache")
{
	system ("/etc/init.d/squid stop");
	system ("echo \"\" >/var/chroot-squid/usr/local/squid/cache/swap.state");
	system ("rm -rf /var/chroot-squid/usr/local/squid/cache/*");
	system ("/etc/init.d/squid createcache");
}
elsif ($ARGV[0] eq "modifylicense")
{
	modify_license();
}
elsif ($ARGV[0] eq "set_hostname")
{
	openfile (SQUIDCONF, "$SQUIDDIR/squid.conf");
	$tempfile = gettempfile ();
	openfile (TEMP, ">$DIR/$tempfile");
	while (<SQUIDCONF>)
	{
		if (/^visible_hostname/)
		{
			print TEMP "visible_hostname $ARGV[1]\n";
		}
		else
		{
			print TEMP $_;
		}
	}
	close (SQUIDCONF);
	close (TEMP);
	system ("mv -f $DIR/$tempfile $SQUIDDIR/squid.conf");
	system ("/etc/init.d/squid reread");
}
elsif ($ARGV[0] eq "get_urlcategory")
{
	$url = $ARGV[1];
	$category = `chroot /var/chroot-squid/ /usr/local/squid/surfcontrol/url-category -c /usr/local/squid/surfcontrol/swsys.cfg http://$url`;
	print "$category";
}
#Senthil 20050830 - Added for Flash

elsif($ARGV[0] eq "sarggen")
{
	my $datestr = `date +%Y-%m-%d`;
	chomp ($datestr);
	system("/etc/cron.hourly/00sarg");
	if($ARGV[1] eq "access")
	{
		#Form the name for the zip file
		my $sargzipfile = "sargfile_$datestr" . "\." . "zip";
		chdir ("/etc/multiconf");

		if (-e "/etc/multiconf/$sargzipfile")
		{
		        system ("unzip -o $sargzipfile");
			`/usr/bin/sarg -f /usr/local/sarg/sarg-first-access.conf`;
		}
		else
		{
		   #     system ("cp -f /var/chroot-squid/usr/local/squid/logs/access.log /etc/multiconf/sargfile");
	#		`/usr/bin/sarg -f /usr/local/sarg/sarg-first-access.conf`;
	#		system("rm -f /etc/multiconf/sargfile");
			#Access Log is Empty
			print "1\n";
		}
	}
	elsif($ARGV[1] eq "reject")
	{
		#Form the name for the zip file
		my $sargzipfile = "sargdeny_$datestr" . "\." . "zip";
		chdir ("/etc/multiconf");

		if (-e "/etc/multiconf/$sargzipfile")
		{
		        system ("unzip -o $sargzipfile");
			`/usr/bin/sarg -f /usr/local/sarg/sarg-first-deny.conf`;
		}
		else
		{
#			system ("cp -f /var/chroot-squid/usr/local/squid/surfcontrol/surfing.log /etc/multiconf/sargdenyfile");

#			`/usr/bin/sarg -f /usr/local/sarg/sarg-first-deny.conf`;
#			system("rm -f /etc/multiconf/sargdenyfile");
			#Deny Log is Empty
			print "1\n";
		}
	}
	else
	{
		print "1\n";
	}
}
elsif($ARGV[0] eq "upload")
{
	system("sync");
        if($ARGV[2] ne ""){
                my $filename = $ARGV[2];
		chomp ($filename);
                my @splitvals = split(/\./, $filename);
                if($splitvals[1] eq "zip")
                {
                        chdir ("/tmp");
                        system("unzip -o $filename 1>/dev/null 2>/dev/null");
			system("rm -f $filename 1>/dev/null 2>/dev/null");
                        if($ARGV[1] eq "access"){
				if(-e "/tmp/sargfile")
				{
	                                `/usr/bin/sarg -f /usr/local/sarg/sarg-access.conf`;
        	                        system("rm -f sargfile");
					print "0\n";
				}
				else
				{
					print "1\n";
				}
				
                        }
                        elsif($ARGV[1] eq "reject"){
				if(-e "/tmp/sargdenyfile")
				{
	                                `/usr/bin/sarg -f /usr/local/sarg/sarg-deny.conf`;
        	                        system("rm -f sargdenyfile");
					print "0\n";
				}
				else
				{
					print "1\n";
				}
                        }

                }
                else
                {
                        print "1\n";
                }

        }
        else
        {
                print "1\n";
        }
}

elsif ($ARGV[0] eq "sendlog")
{

	$datestr = `date +%Y-%m-%d`;
	chomp ($datestr);

	if($ARGV[1] eq "accesslog")
	{
		#Form the name for the zip file
		$sargzipfile = "sargfile_$datestr" . "\." . "zip";
		chdir ("/etc/multiconf");

		#This option is to send the Access reports as a mail to the administrator
		#First, check out the size of sargfile_XXXXX.zip file.
		if(-e "/etc/multiconf/$sargzipfile")
		{
			$filesize = (stat $sargzipfile)[7];
			chomp ($filesize);
		}
		else
		{
			exit;
		}
	}
	elsif($ARGV[1] eq "denylog")
	{
		#Form the name for the zip file
		$sargzipfile = "sargdeny_$datestr" . "\." . "zip";
		chdir ("/etc/multiconf");

		#This option is to send the Deny reports as a mail to the administrator

		#First, check out the size of sargdenyfile_XXXXX.zip file.
		if(-e "/etc/multiconf/$sargzipfile")
		{
			$filesize = (stat $sargzipfile)[7];
			chomp ($filesize);
		}
		else
		{
			exit;
		}
	}
	my $NotifyUser = `/etc/multiconf/scripts/settings emailnotify check 'HTTP Access Deny Reports'`;

	if (-e "/etc/multiconf/mailids/email.conf" && ($NotifyUser eq 'yes'))
	{
		open (MAILIDS, "/etc/multiconf/mailids/email.conf") || die "Cannot open mail id list file\n";
		$maillist = "";
		$AdminIdPresent = 0;
		while (<MAILIDS>)
		{
			$AdminIdPresent = 1;
			$mailid = $_;
			chomp ($mailid);
			if ($maillist eq "")
			{
				$maillist = $mailid;
			}
			else
			{
				$maillist = $maillist . " " . $mailid;
			}
		}
		close (MAILIDS);
		if ($AdminIdPresent == 1)
		{

			if (($filesize > 5000000) || ($ARGV[2] eq "force"))
			{
				$datetoprint = `date`;
				chomp ($datetoprint);
				$hostname = `hostname`;
				chomp ($hostname);
				$datetime = `date +%y-%m-%d_%H-%M`;
				chomp($datetime);
				if($ARGV[1] eq "accesslog")
				{
					$subject = $hostname . " - HTTP Access Report";
					$sargtimefile = "sargfile_$datetime" . "\." . "zip";
					if( -e "/etc/multiconf/$sargzipfile"){
						system("mv -f $sargzipfile $sargtimefile");
						`echo "$datetoprint\nThis message is generated by the HTTP.\nThe attached file contains the HTTP Access reports for the  $hostname." | mutt -s "$subject" -a /etc/multiconf/$sargtimefile $maillist`;
						system ("echo \"\" >/etc/multiconf/sargtime");
					}
				}
				elsif($ARGV[1] eq "denylog")
				{
					$subject = $hostname . " - HTTP Deny Report";
					$sargtimefile = "sargdeny_$datetime" . "\." . "zip";
					if( -e "/etc/multiconf/$sargzipfile"){
						system("mv -f $sargzipfile $sargtimefile");
						`echo "$datetoprint\nThis message is generated by the HTTP.\nThe attached file contains the HTTP Deny reports for the  $hostname." | mutt -s "$subject" -a /etc/multiconf/$sargtimefile $maillist`;
						system ("echo \"\" >/etc/multiconf/sargdenytime");
					}
				}
				system("rm -f $sargtimefile");

			}
		}

	}
	elsif ($filesize > 5000000)
	{
		system("rm -f $sargzipfile");
		if($ARGV[1] eq "accesslog")
		{
			system ("echo \"\" >/etc/multiconf/sargtime");
		}
		elsif($ARGV[1] eq "denylog")
		{
			system ("echo \"\" >/etc/multiconf/sargdenytime");
		}
	}
}


system ("sync");

$SIG{QUIT} = 'DEFAULT';
$SIG{INT} = 'DEFAULT';
$SIG{TERM} = 'DEFAULT';
