#!/usr/bin/perl

# please do not modify anything
# # mail me: Leonardo Bolsoni <leonardo.bolsoni@hostgator.com.br>

# Sanatize @INC
BEGIN {
  unshift @INC, '/usr/local/cpanel';
  @INC = grep( !/(^\.|\.\.|\/\.+)/, @INC );
}

use Storable ();

# Parse the arguments passed to the testadmin scripts via stdin
my $theargs = <STDIN>;
unless ( $theargs =~ /^(\d+) ([A-Z]+) (\S+)/ ) {
  print STDERR "$0 : Syntax Mismatch\n";
  exit 1;
}
chomp($theargs);

my ( $uid, $action, @args ) = split(/ /, $theargs );

# This command hash contains all the logic for the actions to be run as root
# to add a new escalated action, add a new key with a value of a coderef to this hash
my %commands = (
  'PKGACCT' => sub {
    my ($acct) = @_;
    system("/scripts/pkgacct", $acct, "--skip-limit");
    print "$acct - Backup complete\n";
  },
  'UI' => sub {
    my ($acct) = @_;
    system("/root/bin/ui", $acct);
  },
  'UIJ' => sub {
    my ($acct) = @_;
    system("/opt/hgctrl/ui", "--json", $acct);
  },
  'LSATTR' => sub {
    my ($dir) = @_;
    system("/opt/hgctrl/ls.sh", "attr", $dir);
  },
  'LSPERMS' => sub {
    my ($dir) = @_;
    system("/opt/hgctrl/ls.sh", "perms", $dir);
  },
  'FIXPERMS' => sub {
    my ($account) = @_;
    system("/root/bin/perms", $account);
  },
  'MODSEC' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/modsec.sh", $domain);
  },
  'FIXMYFILES' => sub {
    my ($homedir) = @_;
    system("/opt/hgctrl/fix_immutable.pl", $homedir);
  },
  'GETHOME' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/info.pl", $domain, "home");
  },
  'GETDOCROOT' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/info.pl", $domain, "docroot");
  },
  'GETUSER' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/info.pl", $domain, "user");
  },
  'GETEMAIL' => sub {
    my ($acct) = @_;
    system("/root/bin/ui $acct | grep Email | awk \'BEGIN { FS=\":\" } { print \$2 }\' | awk \'{ print \$1 }\' | sed -e \'s/ //g\'");
  },
  'SMTPWHITELIST' => sub {
    my ($acct) = @_;
    system("/opt/hgctrl/smtp_whitelist.sh", "get", $acct);
  },
  'FIXSMTP' => sub {
    my ($acct) = @_;
    system("/opt/hgctrl/smtp_whitelist.sh", "set", $acct);
  },
  'VULNSCAN' => sub {
    my ($acct) = @_;
    system("/root/bin/vdetect --json --plain --no-progress --user=$acct");
  },
  'BUPTIME' => sub {
    my ($acct) = @_;
    system("/root/bin/buptime", "$acct");
  },
  'PROC' => sub {
    my ($acct) = @_;
    my $srv = `hostname -s`;
    print "Server: $srv\n";
    system("/root/bin/user", "$acct");
  },
  'DNSINFO' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/dns.pl", $domain);
  },
  'GETALLDOMAINS' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/domains.pl", $domain);
  },
  'GETEMAILS' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/emails.pl", $domain);
  },
  'GETSHELL' => sub {
    my ($acct) = @_;
    system("/opt/hgctrl/jailed.sh", "get", $acct);
  },
  'ENABLESHELL' => sub {
    my ($acct) = @_;
    system("/opt/hgctrl/jailed.sh", "set", $acct);
  },
  'DISABLESHELL' => sub {
    my ($acct) = @_;
    system("/opt/hgctrl/jailed.sh", "disable", $acct);
  },
  'ENABLEGOMOBI' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/gomobi.sh", "set", $domain);
  },
  'DISABLEGOMOBI' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/gomobi.sh", "disable", $domain);
  },
  'ENABLEGAPPS' => sub {
    my ($domain, $txt) = @_;
    system("/opt/hgctrl/gapps.sh", $domain, $txt);
  },

  'ENABLEWP' => sub {
    my ($domain, $email, $title) = @_;
    system("/opt/hgctrl/wp.sh", $domain, $email, $title);
  },

  'DISABLEWP' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/wp_remove.sh", $domain);
  },

  'MANUALGOMOBI' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/gomobi.sh", "manual", $domain);
  },
  'LISTDBS' => sub {
    my ($domain) = @_;
    system("/opt/hgctrl/mysql/list.pl", $domain);
  },
  'MYSQL' => sub {
    my ($domain, $action, $info) = @_;
    if ( $action eq 'create' ) {
      system("/opt/hgctrl/mysql.pl", $domain, "create_db", $info);
    } elsif ( $action eq 'createuser' ) {
      my ($param1, $param2) = (split(/-/, $info));
      system("/opt/hgctrl/mysql.pl", $domain, "create_user", $param1, $param2);
    } elsif ( $action eq 'grant' ) {
      my ($param1, $param2) = (split(/-/, $info));
      system("/opt/hgctrl/mysql.pl", $domain, "grant_access", $param1, $param2);
    } elsif ( $action eq 'import' ) {
      system("/opt/hgctrl/mysql.pl", $domain, "import", $info);
    }
  },
  'ROOTBIN' => sub {
    my ($bin, $acct) = @_;

    if ( $bin eq 'oi') { 
      system("/root/bin/oi", "-v", $acct);
    } elsif ( $bin eq 'search_cpanel' ) {
      system("/root/bin/search_cpanel", "-u", $acct);
    } else {
      system("/root/bin/$bin", $acct);
    }
  },
  'PWTEMP' => sub {
    my (%OPTS) = @_;
    
    my $domain = $OPTS{'domain'};
    my $acct   = $OPTS{'user'};
    my $query  = $OPTS{'query'};
    my $info   = $OPTS{'info'};

    if ( $query eq 'setuser' ) {
      system("/opt/hgctrl/pwtemp", "setuser", $acct);
    } else {
      system("/opt/hgctrl/pwtemp $query $info");
    }
  },
  'HTACCESS' => sub {
    my ($acct, $action) = @_;
    system("/opt/hgctrl/files/htaccess.sh", $action, $acct);
  },
  'UNSUSPENDALL' => sub {
    my ($acct) = @_;
    system("/opt/hgctrl/resellers/unsuspend.sh", $acct);
  },
  'CATCHALL' => sub {
    my ($acct) = @_;
    system("/opt/hgctrl/catchall.sh", $acct);
  },
  'DBCHECKER' => sub {
    my ($acct) = @_;
    system("/opt/hgctrl/dbchecker.sh", $acct);
  },
  'SCAN' => sub {
    my ($acct) = @_;
    system("/opt/hgctrl/scan.sh", $acct);
  },
  'FIREWALL' => sub {
    my (%OPTS) = @_;
    my $acct   = $OPTS{'user'};
    my $ticket = $OPTS{'ticket_id'};
    my $port   = $OPTS{'port'};

    system("/opt/hgctrl/firewall.sh", $ticket, $acct, $port);
  },
  'GETSESSIONURL' => sub {
    my ($acct) = @_ ;
    system("/opt/hgctrl/session_url.sh", $acct);
  },
  'REVERTPWTEMP' => sub {
    my ($acct) = @_ ;
    system("/opt/hgctrl/revert_password.sh", $acct);
  },
  'REVERTTHEME' => sub {
    my ($acct) = @_ ;
    system("/opt/hgctrl/revert_theme.sh", $acct);
  },
  'SKIPBACKUP' => sub {
    my ($acct) = @_ ;
    system("/opt/hgctrl/skip_backup.sh", $acct);
  },
);

# Code used to ensure that the correct user is being run.
my ( $user, $gid, $home ) = ( getpwuid( int($uid) ) )[ 0, 3, 7 ];

if ( $user ne 'hgdummy' || $user eq 'root' || $user eq '' ) {
  print "Admin Binary called with invalid user\n";
  exit;
}

if ( !-e "/var/cpanel/users/$user" && $user ne 'cpanel' ) {
  print "Admin binary called with invalid user\n";
  exit;
}

# Verify that the command exists
if (!exists $commands{$action}) {
  print "not a valid action\n";
  exit 1;
}

# This is needed by the Cpanel::AdminBin module
#print "\n";

# Execute the action in question
$commands{$action}->(@args);
