perl <<'EOF'
$domain = "domain_placeholder";
$ruleid = "ruleid_placeholder";
$action = "action_placeholder";
my @blacklist = ("999997","999999","1235235","9000043","900079","900083","900084","900095","900096","900102","900108","900113","900115","900117","900119","900121","900122","900123","900124","900126","900135","900137","900139","900998","999022","999023");
print "\n";
sub showargs {
        print "[*] Usage: whitelist <domain.com> <(add/del)> <rule id>\n";
        print "           whitelist scriptninja.org add 31913\n";
        print "           whitelist scriptninja.org del 31913\n";
        exit;
}
if ( ($action !~ //) && ($action !~ /^.*\;.*$/) ) {
        showargs();
}
if ( ($ruleid !~ //) && ($ruleid !~ /^.*\;.*$/) ) {
        showargs();
}
$userinfo = `egrep ^$domain:.+\$ /etc/userdomains | cut -d: -f2`;
chomp($userinfo);
$userinfo =~ s/^\s+//;
if ( $userinfo eq "" ) {
        print "[*] Couldn't find this user. Aborting.\n";
        exit;
}
@suspendedusers = </var/cpanel/suspended/*>;
foreach (@suspendedusers) {
        if ( $_ !~ /^.*\.lock$/ ) {
                (undef,undef,undef,undef,$suspuser) = split('/', $_);
                if ( $userinfo eq $suspuser ) {
                        print "[*] This user is suspended. Aborting.\n";
                        exit;
                }
        }
}
@ruleids = split(/\s+/, $ruleid);
foreach my $ruleid ( @ruleids ) {
	$ruleid =~ s/\s+//g;
	if ( grep(/^$ruleid$/, @blacklist) ) {
		print "[!] Cannot whitelist $ruleid as it has been globally blacklisted!\n";
		next;
	}
	if ( ($ruleid =~ /[0-9]{1,100}/) && ($ruleid !~ /^.*\;.*$/) ) {
	        if ($action eq "add") {
	                open(DAT, "/opt/mod_security/whitelist.conf");
	                @wlfile = <DAT>;
	                close(DAT);
                        my @actionids;
			foreach my $line (@wlfile) {
			        if ( $line =~ /id:([0-9]+)$/ ) {
			                push (@actionids, $1);
			        }
			}
			@actionids = sort { $a <=> $b } @actionids;
			my $new = pop(@actionids);
			$new++;
	                foreach (@wlfile) {
	                        if (/SERVER_NAME "$domain.*$ruleid/) {
	                                print "[*] Already whitelisted!\n";
	                                exit;
	                        }
	                }
	                print "[*] Whitelisting rule $ruleid for $domain\n";
	                open(WRITE, ">>/opt/mod_security/whitelist.conf");
	                print WRITE "SecRule SERVER_NAME \"$domain\" phase:1,nolog,pass,ctl:ruleRemoveByID=$ruleid,id:$new\n";
	                close(WRITE);
	        } elsif ($action eq "del") {
	                open(DAT, "/opt/mod_security/whitelist.conf");
	                @wlfile = <DAT>;
	                close(DAT);
	                foreach (@wlfile) {
	                        if (/^.*$domain.*$ruleid.*$/) {
	                                $_ =~ s/^.*$domain.*$ruleid.*[\n\r]$//g;
	                                $removed=1;
	                        } 
	                }
	                open(WRITE, ">/opt/mod_security/whitelist.conf");
	                print WRITE @wlfile;
	                close(WRITE);
	                if ($removed) {
	                        print "[*] Removed rule $ruleid for $domain.\n";
	                } else {
	                        print "[*] No rule found for $ruleid and $domain.\n";
	                }
		}
	} else {
		print "Skipped [$ruleid]\n";
	        next;
	}
}
chomp($ea3check = `ps aux | grep easyapache | grep -v grep | wc -l`);
if ($ea3check ne "1" ) {
	print "[*] Testing apache configuration...\n";
	system('/usr/sbin/httpd -t 3>/dev/null 2>/dev/null 1>/dev/null');
	if ( $? != 0 ) {
		print "[!] Apache configuration not sane. Please notify your supervisor IMMEDIATELY\n";
		exit();
	}
	print "[*] Configuration good! Restarting Apache...";
	system("/etc/init.d/httpd graceful 1>/dev/null 2>/dev/null 3>/dev/null");
}
print "DONE!\n";
EOF
