#!/usr/bin/perl
#################################################################
# ecpp_2.pl - Michael Karr
# Cpanel password changer.
#
# Git: http://git.toolbox.hostgator.com/chpass
# Wiki: https://gatorwiki.hostgator.com/Admin/ChPass
#
# Please submit all bug reports at http://bugs.hostgator.com
#
# (c) 2012 - HostGator.com, LLC.
#################################################################

use strict;
use warnings;

# globals

my $passlength = 12; # length for random passwords
my @passchars = ("a".."z","A".."Z","0".."9"); # characters to include in random passwords

# begin main program code

my ($user, $password) = @ARGV;

if ($user) {
   if ($user eq 'root') { # are we root
       print "[-] ERROR! I will not change the root password.\n";
   } else {
       unless ($password) { # if they did not give us a password, make one up
           print "[+] You did not give me a password. I'm gonna make one up.\n";
           $password = randtext($passlength, @passchars);
       }

   chomp(my $msg = `ALLOW_PASSWORD_CHANGE=1 /usr/local/cpanel/scripts/realchpass $user $password 2>/dev/null`);

       if ( $? ) {
       print "[-] ERROR!: $msg\n";
       } else {
       print "[+] Success: $msg\n";
       print "[+] Processing Password Change\n";
       print "[+] Username: $user\n";
       print "[+] Password: $password\n\n";
       }
   }
} else {
   print "[-] ERROR! No user provided.\n";
   usage();
}

exit;

# end main program code

sub usage {
   print "[-] Usage: $0 USERNAME [PASSWORD]\n";
   print "[-] Password will be randomly generated if omitted.\n";
}

sub randtext {
   my $length = shift;
   return join("",@_[map{rand @_} (1..$length)]);
}
