The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Finding account username from parked domain

Discussion in 'cPanel Developers' started by tdl, Sep 17, 2014.

  1. tdl

    tdl Member

    Joined:
    Jun 18, 2013
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hi all

    Context:

    I've got roundcube installed on our server separate to the default cPanel installation. It serves all sites on our server. This is an API question rather than a roundcube question.

    I've just employed their password plugin (it allows users to change their own passwords) which makes use of the cpanel api2. However it's only really designed to work with a single cPanel account. i.e. it doesn't cope with multiple accounts as it needs to know the username of the account which owns the email address whose password you need to change. As it stands, you hardcode the username and password of the cpanel account in their config.

    I've modified their code to achieve this successfully. I'm now connecting to port 2087 as the server admin account (not an individual account user) so I can make changes to all accounts. I'm also querying the listaccts search to find the username for the email domain in question which I need to make the call to passwdpop.

    Here's their code

    cpanel.php in github/plugins/password/drivers

    Here are my modifications

    PHP:

    class rcube_cpanel_password
    {
        public function 
    save($curpas$newpass)
        {
            require_once 
    'xmlapi.php';

            
    $rcmail rcmail::get_instance();

            
    // Setup the xmlapi connection
            
    $this->xmlapi = new xmlapi($rcmail->config->get('password_cpanel_host'));
            
    $this->xmlapi->set_port($rcmail->config->get('password_cpanel_port'));
            
    $this->xmlapi->password_auth($rcmail->config->get('password_cpanel_username'), $rcmail->config->get('password_cpanel_password'));
            
    $this->xmlapi->set_output('json');
            
    $this->xmlapi->set_debug(1);

            if (
    $this->setPassword($_SESSION['username'], $newpass)) {
                return 
    PASSWORD_SUCCESS;
            }
            else {
                return 
    PASSWORD_ERROR;
            }
        }

        
    /**
         * Change email account password
         *
         * Returns true on success or false on failure.
         * @param string $password email account password
         * @return bool
         */
        
    function setPassword($address$password)
        {
            if (
    strpos($address'@')) {
                list(
    $data['email'], $data['domain']) = explode('@'$address);
            }
            else {
                list(
    $data['email'], $data['domain']) = array($address'');
            }

            
    $data['password'] = $password;

            
    $current_account $this->xmlapi->listaccts('domain',$data['domain']);

            
    $current_account json_decode($current_accounttrue);

            
    $this->cuser $current_account['acct'][0]['user'];

            
    $query $this->xmlapi->api2_query($this->cuser'Email''passwdpop'$data);
            
    $query json_decode($querytrue);


            if (
    $query['cpanelresult']['data'][0]['result'] == 1) {
                return 
    true;
            }

            return 
    false;
        }
    }
    The main change is that once I've got the domain from the email address, I find the account username using listaccts by searching by domain then I retrieve the user from the response.

    PHP:
    $current_account $this->xmlapi->listaccts('domain',$data['domain']);
    This works perfectly where the email domain is the same as the main domain on the account. However it doesn't work where the domain is parked. This is the case for a number of our accounts.

    Does anyone know of a way in the API to find the main domain or user from a parked domain? listparkeddomains doesn't look like it will do the job.

    Or perhaps there's a way to call passwdpop which means I don't need the username of the account.

    Many thanks,

    Matt
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  3. tdl

    tdl Member

    Joined:
    Jun 18, 2013
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Thank you Michael

    That's exactly what I needed. Final code here for reference. I'll pass onto the original author / roundcube for consideration

    PHP:
    /**
     * cPanel Password Driver
     * 
     * 3.0
     * Driver that adds functionality to change the users cPanel password.
     * Originally written by Fulvio Venturelli <fulvio@venturelli.org>
     *
     * Completely rewritten using the cPanel API2 call Email::passwdpop
     * as opposed to the original coding against the UI, which is a fragile method that
     * makes the driver to always return a failure message for any language other than English
     * see http://trac.roundcube.net/ticket/1487015
     *
     * This driver has been tested with o2switch hosting and seems to work fine.
     *
     * 3.1 
     * I've made changes to the 3.1 code to work for multiple accounts. Previously the cPanel username & password were stored
     * For this version, the WHM username & password should be stored. Then any user from any domain accessing this webmail can modify their passwords.
     *
     *
     * @version 3.1
     * @author Christian Chech <christian@chech.fr>
     * @author Matt Moore <web@trusteddigital.co.uk>
     */

    class rcube_cpanel_password
    {
        public function 
    save($curpas$newpass)
        {
            require_once 
    'xmlapi.php';

            
    $rcmail rcmail::get_instance();

            
    // Setup the xmlapi connection
            
    $this->xmlapi = new xmlapi($rcmail->config->get('password_cpanel_host'));
            
    $this->xmlapi->set_port($rcmail->config->get('password_cpanel_port'));
            
    $this->xmlapi->password_auth($rcmail->config->get('password_cpanel_username'), $rcmail->config->get('password_cpanel_password'));
            
    $this->xmlapi->set_output('json');
            
    $this->xmlapi->set_debug(1);

            if (
    $this->setPassword($_SESSION['username'], $newpass)) {
                return 
    PASSWORD_SUCCESS;
            }
            else {
                return 
    PASSWORD_ERROR;
            }
        }

        
    /**
         * Change email account password
         *
         * Returns true on success or false on failure.
         * @param string $password email account password
         * @return bool
         */
        
    function setPassword($address$password)
        {
            if (
    strpos($address'@')) {
                list(
    $data['email'], $data['domain']) = explode('@'$address);
            }
            else {
                list(
    $data['email'], $data['domain']) = array($address'');
            }

            
    $data['password'] = $password;

            
    $current_account $this->xmlapi->domainuserdata($data['domain']);

            
    $current_account json_decode($current_accounttrue);

            
    $this->cuser $current_account['userdata']['user'];

            
    $query $this->xmlapi->api2_query($this->cuser'Email''passwdpop'$data);
            
    $query json_decode($querytrue);

            if (
    $query['cpanelresult']['data'][0]['result'] == 1) {
                return 
    true;
            }

            return 
    false;
        }
    }
     
Loading...

Share This Page