#!/usr/local/bin/perl

#-------------------------------------------------------------------#
#                       (c) indexit v1.1 
#-------------------------------------------------------------------#
#
# Created Aug 1999-2000 by avika.  ("The CGIPATH")
#
# http://www.avika.com/thecgipath     scripts@avika.com
#
#-------------------------------------------------------------------#
# You may freely distribute this script, and modify it for your
# needs.  Although we do ask that you keep the above notice in tact.
# This script does not have any hyperlinks, so in return 
# please leave the notice as is.  Distribution of this script must 
# be FREE.  You may choose to profit only from installation of this 
# script.  
# If you modify this script, you may place your modification details 
# below this notice so you can recieve credit for it.  Enjoy the
# script.
#-------------------------------------------------------------------#
#            Modification Details (Please do not remove)
#-------------------------------------------------------------------#
#
# Modified by :
# Date Modified :
# Version :
# URL :
# Email :
# What's New :
#
#-------------------------------------------------------------------#
#
# Modified by :
# Date Modified :
# Version :
# URL :
# Email :
# What's New :
#
#-------------------------------------------------------------------#
#
# Please leave all modified user information as is.  It's only fair 
# that everyone who modifies this script gets recognized for it.
#
#-------------------------------------------------------------------#
# 
#####################################################################
#
# The scripts main purpose is to be able to let visitors and yourself
# know that someone has tried to enter a directory that you don't want 
# them to have access to.  This is not a password protected directory! 
# We made this script because some cgi scripts conflict with password
# protected directories.  In other words, they don't work if access
# is password protected.  Of course other reasons for making this 
# script is to cover our directories that don't have an index.html
# file.  Which most servers require so that your directory files are
# not exposed.  Indexit v1.1 will email you the details of an 
# unauthorized visitor trying to take a peek at your directories. The
# script also writes the attempts to the indexit.txt file, just incase
# your email is not working.  It's a good idea to empty this file 
# every month.  (that is if it's full - must mean you have a lot of
# special files people want :)  Okay so lets get started!
#
# First thing you need to do is make sure you are pointing to the
# right path for perl.  At the very top, this should be >>
# #!/usr/local/bin/perl  or #!/usr/bin/perl5  ask your server's 
# administrator if you don't know. 
#
#-------------------------------------------------------------------#
# Things you need to personalize...
#-------------------------------------------------------------------#
#
# Your email address: The script emails you the details of someone
# trying to look at your directories.
#
$admin = 'junk@kalpol.com';
#-------------------------------------------------------------------#
# Your name or company name for a personalized email :)
#
$name = 'kalpol.com';
#-------------------------------------------------------------------#
# The Subject of your email:
#
$subject = 'violation!';
#
#-------------------------------------------------------------------#
# This is your servers mail program:
# Hypermart members: /var/qmail/bin/qmail-inject 
#
$mailprog = '/usr/lib/sendmail';
#-------------------------------------------------------------------#
# This is your *absolute path* to where you have placed your indexit.txt file
#
$indexit_path = "../cgi-bin/indexit.txt";
#-------------------------------------------------------------------#
# This is the URL to where you have placed your indexit.txt file
#
$indexit_url = "http://www.kalpol.com/cgi-bin/indexit.txt";
#
#-------------------------------------------------------------------#
# You shouldn't have to change this, but if your server conflicts with your 
# local time just minus or add the time difference  ie: Your server time is 
# 10:00 AM and you are 1:00 PM just type in +3 or your server is 10:00 AM
# and you are 7:00 AM type in -3;
#
$timeoffset = 0;
#
#-------------------------------------------------------------------#
# That it!  All you do know is upload both files to your selected 
# directory.  Upload in ASCII mode only!  Next chmod index.cgi 755
# and indexit.txt 777  You can now upload index.cgi in any directory
# you choose  (so long as your server supports cgi extentions placed 
# freely among your site)  Leave indexit.txt where it is.  You don't 
# have to have the two files together (as long as indexit.txt is in the
# directory that you specified above ^ )
# Email The CGIPATH if you are encountering any problems.  Don't forget
# to place your link on our site if you're using our script.  We'd love
# to see it in action.
#-------------------------------------------------------------------#
#
#####################################################################
#
# Okay that's it.  There's no need to change anything below...
#
#####################################################################

$referer = $ENV{'HTTP_REFERER'};
$ip = $ENV{'REMOTE_ADDR'}; 
$browser = $ENV{'HTTP_USER_AGENT'}; 
@digits = split (/\./, $ip); 
$address = pack ("C4", @digits); 
$host = gethostbyaddr ($address, 2); 

print ("Content-type: text/html\n\n"); 

print "<!-- BEGIN indexit v1.1 (c) 1999-2000 avika get yours at: http://www.avika.com -->\n";


($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time + (3600*$timeoffset));
@months = ("1","2","3","4","5","6","7","8","9","10","11","12"); 
@days = ("Sun","Mon","Tue","Wed","Thur","Fri","Sat");

   if ($sec < 10) {$sec = "0$sec";}
   if ($min < 10) {$min = "0$min";}
   if ($hour < 10) {$hour = "0$hour";}
   if ($hour > 11) {$variable = "PM";}
   if ($hour < 12) {$variable = "AM";}
   if ($mday < 10) {$mday = "0$mday";}

$year = 1900 + $year;
   
$date = "@days[$wday] $mday/@months[$mon]/$year"; 
$time = "$hour:$min:$sec $variable";
$time_it = "$hour:$min:<font size=2>$sec</font> $variable";
$body = "<head><title>Unauthorized Access!</title></head><center>
<br><b><u><font face=Verdana><font color=FF0000><font size=+2>You
have entered a restricted area!</font></font></font></u></b><b><font face=Verdana><font color=FF0000><font size=+2></font></font></font></b>
<p><b><font face=Verdana><font color=FF0000><font size=+1>Your ip
number has been recorded and sent</font></font></font></b>
<br><b><font face=Verdana><font color=FF0000><font size=+1>to the
webmaster for further investigation.</font></font></font></b>
<br><b><font face=Verdana><font color=FF0000><font size=+1>If this
was an honest mistake, please leave now.</font></font></font></b><b><font face=Verdana><font color=FF0000><font size=+1></font></font></font></b>
<p><b><u><font face=Verdana><font color=000000>Unauthorized attempt
made on:</font></font></u></b></center><br><br>

<ul>
<ul>
<li>
<b><font face=Verdana><font color=3333FF>$date </font><font color=000066>
</font><i><font color=000000> at </font></i><font color=3333FF>$time_it</font><font color=000000><i> by: </i> </font><font color=3333FF>$ip</font></font></b></li>

<li>
<b><font face=Verdana><font color=3333FF>$host</font></font></b></li>

<li>
<b><font face=Verdana><i><font color=000000> os: </font></i><font color=3333FF>
$browser</font></font></b></li>

<li>
<b><font face=Verdana><font color=3333FF>$referer</font></font></b></li>
</ul>
</ul>
<br><center>
<b><font face=Verdana><font color=3333FF><font size=-1><a href=mailto:$admin>email the webmaster</a></font></font></font></b></center><br><br><center>
<p><font face=Verdana><font color=CCCCCC><font size=-2>&copy; powered by Indexit v1.1 by The CGIPATH @ avika</font></font></font></center>";

print "$body\n"; 

print "<!-- END indexit v1.1 (c) 1999-2000 avika get yours at: http://www.avika.com -->\n";
		
open (INDEXIT, ">>$indexit_path") || die "I can't open indexit_path\n";
print INDEXIT "$date, $time, ( $ip ), $host, $browser, $referer\n";
print INDEXIT "------------------------------------------------------------------------\n";
close (INDEXIT);

#####################################################################
#
# Send the webmaster an e-mail about the unauhorized attempt to access.
#
#####################################################################
    open (MAIL, "| $mailprog -t") || die "I can't open mailprog\n";
    print MAIL "To: $admin ($name)\n";
    print MAIL "From: $admin ($name)\n";
    print MAIL "Subject: $subject\n";
    print MAIL "\n";
    print MAIL "$name, someone has tried to look at your directories!\n\n";
    print MAIL "These are the details.\n";
    print MAIL "-----------------------------------------------------\n\n";
    print MAIL "Time:        $date - $time\n";
    print MAIL "IP:          $ip\n";
    print MAIL "Host:        $host\n";
    print MAIL "Browser:     $browser\n";
    print MAIL "Referer:     $referer\n";
    print MAIL "-----------------------------------------------------\n\n";
    print MAIL "You can access your log file here:\n";
    print MAIL "$indexit_url\n\n";
    print MAIL "Powered by (c) indexit v1.1 by avika (The CGIPATH)\n";
    print MAIL "http://www.avika.com/thecgipath\n";
    print MAIL "Thank-you for using one of our scripts!\n\n";
    close (MAIL);
#############################################################################
#                                                                           #
#  (c) indexit.pl  created by avika ("The CGIPATH") scripts@avika.com       #
#                                                                           #
#############################################################################