perl<<'EOF'
use strict;
use Sys::Hostname;

my $service = 'service_placeholder';
my $action = 'action_placeholder';
my $ticket = 'ticket_placeholder';
my $target = 'target_placeholder';

my $hostname = hostname();
if ( $hostname =~ /\.(hostgator|websitedns|prodns|ehost|ideahost|hostclear|websitewelcome|webhostsunucusu|minidedicated|arvixe|asmallorange|accountservergroup|apthost|arvixecloud|asoshared|bluefur|gteinterlink|myhostbackup|myserverhosts|seoboxes|seohosting|seowebhosting|sisternsunucu|siteshostedhere|weblimitsiz|webserversystems|asonon|bluehost|domainpro|fastdomain|hostmonster|justhost|supergreenhosting|rhostbh|rhostjh|unifiedlayer|betabh|betahm|betafd|betajh)\./ ) {
    die "[!] Do NOT run this on shared/reseller servers!\n";
}

if ( $action !~ /^(enable|disable)$/ ) {
    die "[!] Action not specified\n";
}

if (( $action eq 'disable' ) && ( -f '/root/.lockdown' )) {
    open(my $DAT, '<', '/root/.lockdown');
        my $ticket = <$DAT>;
    close($DAT);
    if ( $ticket ) {
        die "[!] Server is already locked down. See ticket $ticket\n";
    } else {
        die "[!] Server is already locked down. No ticket found\n";
    }
}

if ( ! -d '/usr/local/cpanel' ) {
    print "[!] This server type is not supported. Please escalate this to L2 Security.\n";
    exit();
}

if ( $service !~ /^(?:http|exim|all)$/ ) {
    die "[!] Invalid service\n";
}

my %ports = (
    'http' => [
        80,
        81,
        443,
        8080,
        8000
    ],
    'exim' => [
        25,
        26,
        587,
        110,
        143,
        465,
        993,
        995
    ],
);

sub disablehttp {
    my $domain;
    my $user;
    my @ips;
    
    open(my $DAT, '<', '/etc/userdomains');
        while(<$DAT>) {
                if ( /^$target:\s([a-zA-Z0-9]+)$/ ) {
                        ( $domain, $user ) = ( $target, $1 );
                } elsif ( /^([a-zA-Z0-9\-\.]+):\s$target$/ ) {
                        ( $domain, $user ) = ( $1, $target );
                }
        }
    close($DAT);

    foreach my $file ( glob "/var/cpanel/userdata/$user/*" ) {
        open(my $DAT, '<', $file);
            while(<$DAT>) {
                if ( /^ip: ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$/ ) {
                    push(@ips, $1) unless grep(/^$1$/, @ips);
                }
            }
        close($DAT);
    }

    foreach my $port ( @{$ports{'http'}} ) {
        foreach my $ip ( @ips ) {
            system(qq{iptables -I INPUT -d $ip -p tcp --dport $port -m comment --comment "$ticket" -j DROP});
        }
    }
}

sub disableexim {
    foreach my $port ( @{$ports{'exim'}} ) {
        system(qq{iptables -I OUTPUT -p tcp --dport $port -m comment --comment "$ticket" -j DROP});
    }
}

if ( $action eq 'disable' ) {
    if( $service eq 'exim') {
        disableexim;
    } elsif ($service eq 'http') {
        disablehttp;
    } elsif ($service eq 'all') {
        disableexim;
        disablehttp;
    }

    print "[*] The server $hostname has had $service disabled via iptables:\n";

    system("iptables -L -vn |fgrep '$ticket'");

    open(my $WAT, '>', '/root/.lockdown');
    print $WAT $ticket;
    close($WAT);
    logit($action, $service, $ticket);
} elsif ( $action eq 'enable' ) {
    system('service firewall restart');

    print "[*] The server $hostname has had $service unblocked via iptables:\n";
    system(q{iptables -L -vn | grep -P '/\* [A-Z]+\-\d+(?:-\d+)* \*/'});
    run_bluehost();

    unlink('/root/.lockdown');
    logit($action, $service, $ticket);
}

sub run_bluehost() {
    # Needed for CPM reenables
    system('chattr -i /usr/sbin/exim');
    system('chmod 4755 /usr/sbin/exim');
    if ( ! -l '/usr/local/apache/bin/httpd' ) {
        system('chattr -i /usr/local/apache/bin/httpd');
        system('chmod 0755 /usr/local/apache/bin/httpd');
    } else {
        system('chattr -i /usr/sbin/httpd');
        system('chmod 0755 /usr/sbin/httpd');
    }
    system('chkconfig --levels 345 httpd on');
    system('chkconfig --levels 2345 exim on');
    system('service httpd start');
    system('service exim start');
    open(my $fh, '<', '/etc/sysconfig/iptables');
        my @firewall = <$fh>;
    close($fh);
    open(my $fh, '>', '/etc/sysconfig/iptables');
        foreach my $line ( @firewall ) {
            chomp($line);
            if ( $line =~ /dport\s(993|995|26|25|465|110|143|443|80|8080)/ ) {
                $line =~ s/DROP/ACCEPT/g;
            }
            print $fh $line . "\n";
        }
    close($fh);
    system('service iptables restart');
}

sub logit {
    my ( $action, $service, $ticket ) = @_;
    open(my $WAT, '>>', '/var/log/lockdown.log');
        print $WAT time() . ":$service:$action:$ticket\n";
    close($WAT);
}

EOF
