#!/usr/bin/perl

#################################################################
################## Written by: Matthew Harris ###################
#################################################################
# vb_mail_off                                                   #
# Disable email related settings in vBulletin                   #
# https://gatorwiki.hostgator.com/Security/VB                   #
# http://git.toolbox.hostgator.com/vb_mail_off                  #
# Please submit all bug reports at bugs.hostgator.com           #
#                                                               #
# (C) 2011 - HostGator.com, LLC                                 #
#################################################################

use warnings;
use strict;
use File::Find::Rule;
use Getopt::Long;
use Cwd;
use DBI;

sub get_options {
	my($user, $database, $help);
	GetOptions(
		'user|u=s' => \$user,
		'db|d=s' => \$database,
		'help|h' => \$help,
	);
	if($help){
                show_usage();
        }
	if(! defined($user)){
		my $cwd = getcwd();
		my @path = split(/\//, $cwd);
		$user = $path[2];
	}
	check_user($user);
	if($user){
		process_user($user);	
	}
	if($database){
		process_db($database);
	}
}

sub process_user {
	my $user = shift;
	print "[+] Searching /home/$user/public_html for VBulletin\n";
	my $find = File::Find::Rule->new;
	$find->file;
	$find->name('config.php');
	my @files = $find->in("/home/$user/public_html");
	my(@found, @dbs, $count);
	foreach my $file (@files){
		my @content = read_file($file);
		foreach my $line (@content){
			if($line =~ /vBulletin/){
				push @found, $file;
				last;
			}
		}
	}
	foreach my $install (@found) {
		my @content = read_file($install);
		foreach my $line (@content) {
			if($line =~ /config\['Database'\]\['dbname'\] = '(\S+)';/){
				push @dbs, $1;
			}
			if($line =~ /config\['Database'\]\['tableprefix'\] = '';/){
				my $prefix = 'unknown';
				push @dbs, $prefix;
			}
			if($line =~ /config\['Database'\]\['tableprefix'\] = '(\S+)';/){
                                push @dbs, $1;
                        }
		}
		$count++;
	}
	my %dbconf = @dbs;
	my $num_of_installs = keys %dbconf;
	if($num_of_installs == 1){
		print "[+] Found $num_of_installs instance of vBulletin\n";
		print "[+] Processing database $dbs[0]\n";
		process_db($dbs[0], \%dbconf);
	}
	elsif($num_of_installs){
		print "[+] Found $num_of_installs instances of vBulletin\n";
		foreach my $db (keys %dbconf){
			process_db($db, \%dbconf);
		}
	}
	if(! defined($count)){
		print "[!] No vBulletin install found\n";
	}
}

sub process_db {
	my $database = shift;
	my $dbconf = shift;
	my $username = 'root';
	my $password = get_mysqlpw();
	my $dbh = DBI->connect("DBI:mysql:database=$database;host=localhost", $username, $password);
	my $tableprefix = $dbconf->{$database};
	if($tableprefix eq 'unknown'){
		my $update = $dbh->prepare("UPDATE setting SET value = '1' WHERE varname = 'disableerroremail'");
		$update->execute;
		$update = $dbh->prepare("UPDATE setting SET value = '0' WHERE varname = 'enableemail'");
		$update->execute;
		$update->finish;
	}
	else{
		my $table = $tableprefix . 'setting';
		my $update = $dbh->prepare("UPDATE $table SET value = '1' WHERE varname = 'disableerroremail'");
                $update->execute;
                $update = $dbh->prepare("UPDATE $table SET value = '0' WHERE varname = 'enableemail'");
                $update->execute;
		$update->finish;
	}
	print "[+] Disabled mail for $database\n";
}

sub read_file {
	my $file = shift;
	open my $fh, '<', $file or die "[!] Could not open: $file";
	my @content = <$fh>;
	close $fh;
	return @content;
}

sub check_user {
	my $user = shift;
	if(! -e "/var/cpanel/users/$user"){
		print "[!] User $user does not exist\n";
		exit;
	}
	return;
}

sub get_mysqlpw {
	open my $fh, '<', '/root/.my.cnf' or die "[!] Could not open: /root/.my.cnf\n";
	while (<$fh>) {
        	if ( $_ =~ /(?<=pass=")(\S+)(?=")/ ) {
            		my $rootpw = $1;
            		return $rootpw;
        	}
    	}
	close $fh;
}

sub show_usage {
	print <<'EOF';

 vb_mail_off [option] user|database
 
 Options:
   --user, -u      Autofind VB installation and disable mail
   --db, -d        Disable mail based on database

EOF
exit;
}

get_options


