Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Password plugin, cPanel driver #5252

Merged
merged 6 commits into from
Oct 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion plugins/password/config.inc.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ $config['password_log'] = false;
// will be not available (no Password tab in Settings)
$config['password_login_exceptions'] = null;

// Array of hosts that support password changing. Default is NULL.
// Array of hosts that support password changing.
// Listed hosts will feature a Password option in Settings; others will not.
// Example: array('mail.example.com', 'mail2.example.org');
// Default is NULL (all hosts supported).
$config['password_hosts'] = null;

// Enables saving the new password even if it matches the old password. Useful
Expand Down Expand Up @@ -307,6 +308,12 @@ $config['password_cpanel_username'] = 'username';
// The cPanel admin password
$config['password_cpanel_password'] = 'password';

// The cPanel admin hash
// If you prefer to use a hash (Remote Access Key) instead of plain password, enter it below.
// Hash takes precedence over password auth.
// You can generate a Remote Access Key in WHM -> Clusters -> Remote Access Key
$config['password_cpanel_hash'] = '';

// The cPanel port to use
$config['password_cpanel_port'] = 2087;

Expand Down
23 changes: 21 additions & 2 deletions plugins/password/drivers/cpanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ public function save($curpas, $newpass)
// 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($this->cuser, $rcmail->config->get('password_cpanel_password'));
// Hash auth
if (!empty($cpanel_hash = $rcmail->config->get('password_cpanel_hash'))) {
$this->xmlapi->hash_auth( $this->cuser, $cpanel_hash);
}
// Pass auth
else if (!empty($cpanel_password = $rcmail->config->get('password_cpanel_password'))) {
$this->xmlapi->hash_auth( $this->cuser, $cpanel_password);
}
else {
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, would be nice to return error code and message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too familiar with the error handling. Would it be done using the following? $rcmail->output->command('display_message', 'message, 'error');

}

$this->xmlapi->set_output('json');
$this->xmlapi->set_debug(0);

Expand All @@ -70,8 +81,16 @@ function setPassword($address, $password)
}

$data['password'] = $password;

// Get the cPanel user
$query = $this->xmlapi->listaccts( 'domain', $data['domain'] );
$query = json_decode( $query, true );
if ( $query['status'] != 1 ) {
return false;
}
$cpanel_user = $query['acct'][0]['user'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have username from config, why we need to overwrite it here? I don't know cPanel API, but can you call listaccts() when using user/password auth. Do we need it at all when not using hash auth?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's required whether the password or the hash is used. The username retrieved from config is the API user (root user or reseller user). The cPanel user needs to be retrieved using listaccts() because otherwise, the following error will be returned:

You do not have an email account named “[email protected]”.

(Even if authenticated as root).

This can easily be tested on a cPanel server, using the following example URL:

https://hostname.example.com:2087/cpsess##########/json-api/cpanel?cpanel_jsonapi_user=user&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=Email&cpanel_jsonapi_func=passwdpop&domain="example.com"&email="user"&password="12345luggage"


$query = $this->xmlapi->api2_query($this->cuser, 'Email', 'passwdpop', $data);
$query = $this->xmlapi->api2_query($cpanel_user, 'Email', 'passwdpop', $data);
$query = json_decode($query, true);
$result = $query['cpanelresult']['data'][0];

Expand Down