From 57d7bc27c0588af8fdb159c88361c30d2b47a7fe Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 02 Sep 2014 05:55:40 -0400
Subject: [PATCH] Merge pull request #187 from adfinis-sygroup/master

---
 plugins/password/drivers/plesk.php   |  234 ++++++++++++++++++++++++++++++++++++++++++++++
 plugins/password/README              |   22 ++++
 plugins/password/config.inc.php.dist |   21 ++++
 3 files changed, 277 insertions(+), 0 deletions(-)

diff --git a/plugins/password/README b/plugins/password/README
index c50eb0b..936aa53 100644
--- a/plugins/password/README
+++ b/plugins/password/README
@@ -43,6 +43,7 @@
  2.17. Expect (expect)
  2.18. Samba (smb)
  2.19. Vpopmail daemon (vpopmaild)
+ 2.20. Plesk (Plesk RPC-API)
  3. Driver API
 
 
@@ -314,6 +315,27 @@
  connection to vpopmaild (You may want to set it higher on busy servers).
 
 
+ 2.20. Plesk (Plesk RPC-API)
+ ---------------------------
+ 
+ Driver for changing Passwords via Plesk RPC-API. This Driver also works with
+ Parallels Plesk Automation (PPA).
+ 
+ You need to allow the IP of the Roundcube-Server for RPC-Calls in the Panel.
+ 
+ 
+ Set $config['password_plesk_host'] to the Hostname / IP where Plesk runs
+ 
+ Set your Admin or RPC User: $config['password_plesk_user']
+ 
+ Set the Password of the User: $config['password_plesk_pass']
+ 
+ Set $config['password_plesk_rpc_port']  for the RPC-Port. Usually its 8443
+ 
+ Set the RPC-Path in $config['password_plesk_rpc_path']. Normally this is: enterprise/control/agent.php;
+
+ 
+
  3. Driver API
  -------------
 
diff --git a/plugins/password/config.inc.php.dist b/plugins/password/config.inc.php.dist
index d979a19..6610b4d 100644
--- a/plugins/password/config.inc.php.dist
+++ b/plugins/password/config.inc.php.dist
@@ -372,3 +372,24 @@
 // ---------------------
 // Gearman host (default: localhost)
 $config['password_gearman_host'] = 'localhost';
+
+
+
+// Plesk/PPA Driver options
+// --------------------
+// You need to allow RCP for IP of roundcube-server in Plesk/PPA Panel 
+
+// Plesk RCP Host
+$config['password_plesk_host'] = '10.0.0.5';
+
+// Plesk RPC Username
+$config['password_plesk_user'] = 'admin';
+
+// Plesk RPC Password
+$config['password_plesk_pass'] = 'password';
+
+// Plesk RPC Port
+$config['password_plesk_rpc_port'] = '8443';
+
+// Plesk RPC Path
+$config['password_plesk_rpc_path'] = 'enterprise/control/agent.php';
diff --git a/plugins/password/drivers/plesk.php b/plugins/password/drivers/plesk.php
new file mode 100644
index 0000000..6f646d2
--- /dev/null
+++ b/plugins/password/drivers/plesk.php
@@ -0,0 +1,234 @@
+<?php
+/**
+ * Roundcube Password Driver for Plesk-RPC.
+ *
+ * This driver changes a E-Mail-Password via Plesk-RPC
+ * Deps: PHP-Curl, SimpleXML
+ *
+ * @author        Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
+ * @copyright     Adfinis SyGroup AG, 2014
+ * @license       GNU GPL v3
+ *
+ * Config needed:
+ * $config['password_plesk_host'] 	= '10.0.0.5';
+ * $config['password_plesk_user'] 	= 'admin';
+ * $config['password_plesk_pass'] 	= 'pass';
+ * $config['password_plesk_rpc_port'] 	= 8443;
+ * $config['password_plesk_rpc_path'] 	= enterprise/control/agent.php;
+ *
+ */
+
+/**
+ * Roundcube Password Driver Class
+ *
+ * See {ROUNDCUBE_ROOT}/plugins/password/README for API description
+ *
+ * @author        Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
+ */
+class rcube_plesk_password {
+
+	/**
+	 * this method is called from roundcube to change the password
+	 *
+	 * roundcube allready validated the old password so we just need to change it at this point
+	 *
+	 * @author        Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
+	 * @param         string $curpass current password
+	 * @param         string $newpass new password
+	 * @returns       PASSWORD_SUCCESS|PASSWORD_ERROR
+	 */
+	function save($currpass, $newpass) {
+
+		// get config
+		$rcmail	= rcmail::get_instance();
+		$host	= $rcmail->config->get('password_plesk_host');
+		$user	= $rcmail->config->get('password_plesk_user');
+		$pass	= $rcmail->config->get('password_plesk_pass');
+		$port	= $rcmail->config->get('password_plesk_rpc_port');
+		$path	= $rcmail->config->get('password_plesk_rpc_path');
+
+		// create plesk-object
+		$plesk	= new plesk_rpc;
+		$plesk->init($host, $port, $path, $user, $pass);
+
+		// try to change password and return the status
+		$result = $plesk->change_mailbox_password($_SESSION['username'], $newpass);
+		//$plesk->destroy();
+
+		if ($result) {
+			return PASSWORD_SUCCESS;
+		}
+
+		return PASSWORD_ERROR;
+	}
+
+}
+
+
+/**
+ * Plesk RPC-Class
+ *
+ * Striped down version of Plesk-RPC-Class
+ * Just functions for changing mail-passwords included
+ *
+ * Documentation of Plesk RPC-API: http://download1.parallels.com/Plesk/PP11/11.0/Doc/en-US/online/plesk-api-rpc/
+ *
+ * @author        Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
+ */
+class plesk_rpc {
+
+	/**
+	 * init plesk-rpc via curl
+	 *
+	 * @author        Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
+	 * @param         string $host plesk host
+	 * @param         string $port plesk rpc port
+	 * @param         string $path plesk rpc path
+	 * @param         string $user plesk user
+	 * @param         string $user plesk password
+	 * @returns       void
+	 */
+	function init($host, $port, $path, $user, $pass) {
+		$headers    = array(
+			sprintf("HTTP_AUTH_LOGIN: %s", $user),
+			sprintf("HTTP_AUTH_PASSWD: %s", $pass),
+			"Content-Type: text/xml"
+		);
+		$url        = sprintf("https://%s:%s/%s", $host, $port, $path);
+		$this->curl = curl_init();
+		curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT , 5);
+		curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST , 0);
+		curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER , false);
+		curl_setopt($this->curl, CURLOPT_HTTPHEADER     , $headers);
+		curl_setopt($this->curl, CURLOPT_URL            , $url);
+	}
+
+
+	/**
+	 * send a request to the plesk
+	 *
+	 * @author        Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
+	 * @param         string $packet XML-Packet to send to Plesk
+	 * @returns       bool request was successfull or not
+	 */
+	function send_request($packet) {
+		curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
+		curl_setopt($this->curl, CURLOPT_POSTFIELDS, $packet);
+		$retval = curl_exec($this->curl);
+
+		return $retval;
+	}
+
+
+	/**
+	 * close curl
+	 *
+	 * @author        Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
+	 * @returns       void
+	 */
+	function destroy(){
+		curl_close($this->curl);
+	}
+
+
+	/**
+	 * Creates an Initial SimpleXML-Object for Plesk-RPC
+	 *
+	 * @author        Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
+	 * @returns       object SimpleXML object
+	 */
+	function get_request_obj(){
+		$request = new SimpleXMLElement("<packet></packet>");
+		$request->addAttribute("version", "1.6.3.0");
+
+		return $request;
+	}
+
+	/**
+	 * Get all hosting-informations of a domain
+	 *
+	 * @author        Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
+	 * @param         string $domain domain-name
+	 * @returns       object SimpleXML object
+	 */
+	function domain_info($domain){
+		// build xml
+		$request = $this->get_request_obj();
+		$site    = $request->addChild("site");
+		$get     = $site->addChild("get");
+		$filter  = $get->addChild("filter");
+
+		$filter->addChild("name", utf8_encode($domain));
+		$dataset = $get->addChild("dataset");
+
+		$dataset->addChild("hosting");
+		$packet = $request->asXML();
+
+		// send the request
+		$res = $this->send_request($packet);
+
+		// make it to simple-xml-object
+		$xml = new SimpleXMLElement($res);
+
+		return $xml;
+	}
+
+	/**
+	 * Get psa-id of a domain
+	 *
+	 * @author        Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
+	 * @param         string $domain domain-name
+	 * @returns       bool|int false if failed and integer if successed
+	 */
+	function get_domain_id($domain){
+		$xml = $this->domain_info($domain);
+		$id  = intval($xml->site->get->result->id);
+		$id  = (is_int($id)) ? $id : false;
+		return $id;
+	}
+
+
+	/**
+	 * Change Password of a mailbox
+	 *
+	 * @author        Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
+	 * @param         string $mailbox full email-adress (user@domain.tld)
+	 * @param         string $newpass new password of mailbox
+	 * @returns       bool
+	 */
+	function change_mailbox_password($mailbox, $newpass){
+
+		list($user, $domain) = explode("@", $mailbox);
+		$domain_id = $this->get_domain_id($domain);
+
+		// if domain cannot be resolved to an id, do not continue
+		if (!$domain_id) {
+			return false;
+		}
+
+		// build xml-packet
+		$request = $this    -> get_request_obj();
+		$mail    = $request -> addChild("mail");
+		$update  = $mail    -> addChild("update");
+		$add     = $update  -> addChild("set");
+		$filter  = $add     -> addChild("filter");
+		$filter->addChild("site-id", $domain_id);
+
+		$mailname = $filter->addChild("mailname");
+		$mailname->addChild("name", $user);
+
+		$password = $mailname->addChild("password");
+		$password->addChild("value", $newpass);
+		$password->addChild("type", "plain");
+
+		$packet = $request->asXML();
+
+		// send the request to plesk
+		$res = $this->send_request($packet);
+		$xml = new SimpleXMLElement($res);
+		$res = strval($xml->mail->update->set->result->status);
+
+		return $res == "ok";
+	}
+}
+

--
Gitblit v1.9.1