#!/usr/bin/perl
#restrict access to a given script by backing up htaccess and creating an ip limited htaccess
#base ips to grant access based on last cPanel, mail, and FTP accesses to the account

use Cwd;

if ($#ARGV != 0)
{
    print "Usage: restrict scriptname OR restrict --unblock\n";
    exit;
}

$cwd = cwd();

if ( $cwd !~ /\/home[1-4]?\/[a-zA-Z0-9]{1,8}\/(public_html|www)(\/.*?)?/ )
{
    print "Doesn't appear that $cwd is a home directory of a user.\n";
    exit;
}

if ( $cwd =~ /(\/home[1-4]?\/)([a-zA-Z0-9]{1,8})\/(public_html|www)(\/.*?)?/ )
{
    $homedir = $1;
    $user = $2;
}

$script = $ARGV[0];
$htaccess = $cwd . "/.htaccess";
$time = time;
$backuppath = $homedir . $user . "/.htaccessbackup";
$backupfile = $backuppath . "/.htaccess." . $time;

$filename = "/var/cpanel/users/" . $user;

if ( ! -e $filename )
{
    print "detected user $user does not have a valid $filename entry\n";
    exit;
}


if ( $ARGV[0] =~ /--unblock/ )
{
    print "Unblock mode\n";
    open(IN,$htaccess);
    $hgdeny = 0;
    foreach(<IN>)
    {
    if ( /^#\sBEGIN\sHGDENY/ )
        {
        $hgdeny = 1;
        } 
    if ( /^#\sEND\sHGDENY/ && $hgdeny == 1 )
    {
        $hgdeny = 0;
        next;
    }

   if ($hgdeny == 0)
   {
   push(@store2, $_);
   }

   }   
    close(IN);

    open(OUT,">$htaccess");
    foreach $line (@store2)
    {
    print OUT $line;
    }
    close(OUT);

    exit;
}

open(USER,$filename);
while(<USER>)
{
    if ( /^DNS=(\S+)$/ )
    {
    $domain = $1;
#    print "Primary domain appears to be : $1\n";
    }
}
close(USER);

open(ACCESS_LOG,"/usr/local/cpanel/logs/access_log");
while(<ACCESS_LOG>)
{
    if ( /^(\S+)\s-\s$user\s\[/ )
    {
    push(@ips, $1);
#    print "IP match in access log : $1\n";
    }
}
close(ACCESS_LOG);

open(MESSAGES,"/var/log/messages");
while(<MESSAGES>)
{
#    if ( /^.*?pure-ftpd\:\s\($user@(\S+)\)\s/ )
    if ( /^[a-zA-Z]{3}\s[\d]{1,2}\s[\d]{2}:[\d]{2}:[\d]{2}\s\S+\spure-ftpd:\s\(\S+@(\S+)\)\s\[INFO\]\s$user/ )
    {
#    print "FTP Match IP : $1\n";    
    push(@ips, $1);
    }
}
close(MESSAGES);

# Nov 10 11:34:52 gator1200 imapd: LOGIN, user=kyle@mjnewsalerts.com, ip=[::ffff:127.0.0.1], port=[50405], protocol=IMAP
# Nov 10 11:40:04 gator1200 pop3d: LOGIN, user=info@langue-onze-toulouse.fr, ip=[::ffff:90.5.46.171], port=[52157]
open(MAILLOG,"/var/log/maillog");
while(<MAILLOG>)
{
    if ( /^[a-zA-Z]{3}\s[\d]{1,2}\s[\d]{2}:[\d]{2}:[\d]{2}\s\S+\s(imapd|pop3d)\:\sLOGIN,\suser=\S+\@($domain),\sip=\[\:\:ffff\:(\S+)\],/ )
    {
#        print $3;
        if ( $3 !~ /127\.0\.0\.1/ )
        {
#        print $3;
        push(@ips, $3);
        }
    }
}

close(MAILLOG);

my %seen;
my @uniqips = grep !$seen{$_}++, @ips;

print "Operating CWD is : $cwd\n";
print "cPanel user is : $user\n";
print "Domain of user : $domain\n";
print "Target script: $script\n";
print "htaccess is $htaccess\n";

print "IP's harvested for : $user\n";
foreach $ip ( @uniqips )
{
    print "$ip\n";
}


if ( ! -d $backuppath )
{
    print "No directory called $backuppath exists. Creating it now.\n";
    mkdir($backuppath);
    system("chown $user.$user $backuppath");
}

if ( -e $htaccess )
{
    open(READ,$htaccess);
    @store = <READ>;
    close(READ);

    print "Backing up $htaccess to destination $backupfile\n";
    open(OUTPUT,">$backupfile");
    foreach $line (@store)
    {
    print OUTPUT $line;
    }
    close(OUTPUT);
}

if (! @uniqips)
{
    print "There are no unique IP's for $user , we will have to restrict access to $cwd/" . $script . " entirely\n";
    open(OUTPUT, ">$htaccess");
    print OUTPUT "# BEGIN HGDENY\n";
    print OUTPUT "<Files " . $script . ">\n";
    print OUTPUT "order deny,allow\n";
    print OUTPUT "allow from 216.110.94.228\n";
    print OUTPUT "allow from 74.202.255.243\n";
    print OUTPUT "deny from all\n";
    print OUTPUT "</Files>\n";
    print OUTPUT "# END HGDENY\n\n";
    foreach $line (@store)
    {
    print OUTPUT $line;
    }
    close(OUTPUT);
    exit;
}

if (@uniqips)
{
    open(OUTPUT, ">$htaccess");
    print OUTPUT "# BEGIN HGDENY\n";
    print OUTPUT "<Files " . $script . ">\n";
    print OUTPUT "order deny,allow\n";
    foreach (@uniqips)
    {
        print OUTPUT "allow from $_\n";
    }
    print OUTPUT "allow from 216.110.94.228\n";
    print OUTPUT "allow from 74.202.255.243\n";
    print OUTPUT "deny from all\n";
    print OUTPUT "</Files>\n";
    print OUTPUT "# END HGDENY\n";
    foreach $line (@store)
    {
        print OUTPUT $line;
    }
    close(OUTPUT);
}


