Script to reset emaill account password in cpanel

 To reset the  emailaccount password on a cPanel server without login whm or cpanel,  You can use the given script to reset the password.

#!/usr/bin/perl -wl
# cpanel – chemailpass Copyright(c) 2011 prajith.in.
# All rights Reserved.
# http://prajith.in

BEGIN { unshift @INC, ‘/usr/local/cpanel’; }

use strict;
use Cpanel::SafetyBits ();
use Cpanel::AcctUtils ();
use Cpanel::PublicAPI ();
my ( $email, $pass ) = @ARGV;
use Crypt::PasswdMD5 qw(unix_md5_crypt);
my ( $user, $domain ) = split( /\@/, $email );
if ( !$email ) {
die “Usage: chemailpass <emailaddress> <password>”;
}
if ( !$pass ) {
die “Usage: chemailpass <emailaddress> <password>”;
}
my $owner = Cpanel::AcctUtils::getdomainowner( $domain, { ‘default’ => ” } );
if ( !$owner ) {
die “Cannot find the owner of $domain, try rebuilding /etc/userdomains first with /usr/local/cpanel/scripts/updateuserdomains”;
}
my $pubapi = Cpanel::PublicAPI->new();
my $res = $pubapi->whm_api(‘domainuserdata’, “domain=$domain”);
my $homedir = $res->{‘userdata’}->{‘homedir’};
if ( !$homedir ) {
die “Cannot find the owner of $domain”;
}

open(FILE, “$homedir/etc/$domain/shadow”);
my @list=<FILE>;close FILE;
my $string = $user;
my @f=grep /$string/,@list;

if ( !@f ) {
die “email account not created yet”;
}
system(“/bin/cat $homedir/etc/$domain/shadow|grep -v \”$user\” > $homedir/etc/$domain/shadow”);

my @salt = ( ‘.’, ‘/’, 0 .. 9, ‘A’ .. ‘Z’, ‘a’ .. ‘z’ );

my $password = $pass;
my %encrypted;
my $encrypted = unix_md5_crypt( $password, gensalt(8) );
print $encrypted;
open FILE, “>>$homedir/etc/$domain/shadow” or die $!;
print FILE “$user:$encrypted”;
close FILE;

sub gensalt {
my $count = shift;

my $salt;
for (1..$count) {
$salt .= (@salt)[rand @salt];
}

return $salt;
}

 

Don’t copy paste the above script directly. Just use given command to download.

wget -O chemilpass.pl prajith.in/cpanel/emailpass

Script to reset emaill account password in cpanel
Tagged on:

2 thoughts on “Script to reset emaill account password in cpanel

  • June 12, 2014 at 5:07 pm
    Permalink

    Interesting script. Can you elaborate on exactly what it does, though?

    Is it just changing mail accounts, or cPanel accounts as well? For all domains on a server? When might I opt to use this script? Wondering why/when I should use it.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fork me on GitHub