From 78dbfd6880122d66470a12bd234da04ea349a04d Mon Sep 17 00:00:00 2001 From: icedman21 Date: Mon, 4 May 2015 13:24:17 +0800 Subject: [PATCH 1/6] Create index.php --- plugins/virtualmin-change-password/index.php | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 plugins/virtualmin-change-password/index.php diff --git a/plugins/virtualmin-change-password/index.php b/plugins/virtualmin-change-password/index.php new file mode 100644 index 000000000..9414e9991 --- /dev/null +++ b/plugins/virtualmin-change-password/index.php @@ -0,0 +1,52 @@ +addHook('main.fabrica', 'MainFabrica'); + } + + /** + * @param string $sName + * @param mixed $oProvider + */ + public function MainFabrica($sName, &$oProvider) + { + switch ($sName) + { + case 'change-password': + + include_once __DIR__.'/VirtualminChangePasswordDriver.php'; + $sHost = \trim($this->Config()->Get('plugin', 'host', '')); + $sAdminUser = (string) $this->Config()->Get('plugin', 'admin_user', ''); + $sAdminPassword = (string) $this->Config()->Get('plugin', 'admin_password', ''); + $oProvider = new VirtualminChangePasswordDriver(); + $oProvider->SetLogger($this->Manager()->Actions()->Logger()); + $oProvider->SetConfig($sHost,$sAdminUser, $sAdminPassword); + $oProvider->SetAllowedEmails(\strtolower(\trim($this->Config()->Get('plugin', 'allowed_emails', '')))); + + break; + } + } + + /** + * @return array + */ + public function configMapping() + { + return array( + \RainLoop\Plugins\Property::NewInstance('host')->SetLabel('Virtualmin Host') + ->SetDefaultValue('https://localhost:10000') + ->SetDescription('Virtualmin host URL. Example: https://example.com:10000'), + \RainLoop\Plugins\Property::NewInstance('admin_user')->SetLabel('Admin User') + ->SetDefaultValue(''), + \RainLoop\Plugins\Property::NewInstance('admin_password')->SetLabel('Admin Password') + ->SetDefaultValue(''), + \RainLoop\Plugins\Property::NewInstance('allowed_emails')->SetLabel('Allowed emails') + ->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT) + ->SetDescription('Allowed emails, space as delimiter, wildcard supported. Example: user1@domain1.net user2@domain1.net *@domain2.net') + ->SetDefaultValue('*') + ); + } +} From 664a338f04e66897bb011e1add95f6c5ed73e5f1 Mon Sep 17 00:00:00 2001 From: icedman21 Date: Mon, 4 May 2015 13:24:53 +0800 Subject: [PATCH 2/6] Create LICENSE --- plugins/virtualmin-change-password/LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 plugins/virtualmin-change-password/LICENSE diff --git a/plugins/virtualmin-change-password/LICENSE b/plugins/virtualmin-change-password/LICENSE new file mode 100644 index 000000000..c364cecf0 --- /dev/null +++ b/plugins/virtualmin-change-password/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 icedman21 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 078bf288eca219e6aa0b4c75e2f6bb1bec328582 Mon Sep 17 00:00:00 2001 From: icedman21 Date: Mon, 4 May 2015 13:25:28 +0800 Subject: [PATCH 3/6] Create VERSION --- plugins/virtualmin-change-password/VERSION | 1 + 1 file changed, 1 insertion(+) create mode 100644 plugins/virtualmin-change-password/VERSION diff --git a/plugins/virtualmin-change-password/VERSION b/plugins/virtualmin-change-password/VERSION new file mode 100644 index 000000000..d3827e75a --- /dev/null +++ b/plugins/virtualmin-change-password/VERSION @@ -0,0 +1 @@ +1.0 From 11da6f5e14de1e43209d44d1f1eb0165d610c335 Mon Sep 17 00:00:00 2001 From: icedman21 Date: Mon, 4 May 2015 13:26:27 +0800 Subject: [PATCH 4/6] Create VirtualminChangePasswordDriver.php --- .../VirtualminChangePasswordDriver.php | 157 ++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 plugins/virtualmin-change-password/VirtualminChangePasswordDriver.php diff --git a/plugins/virtualmin-change-password/VirtualminChangePasswordDriver.php b/plugins/virtualmin-change-password/VirtualminChangePasswordDriver.php new file mode 100644 index 000000000..a1d725d88 --- /dev/null +++ b/plugins/virtualmin-change-password/VirtualminChangePasswordDriver.php @@ -0,0 +1,157 @@ +sHost = $sHost; + $this->sAdminUser = $sAdminUser; + $this->sAdminPassword = $sAdminPassword; + + return $this; + } + /** + * @param string $sAllowedEmails + * + * @return \ChangePasswordExampleDriver + */ + public function SetAllowedEmails($sAllowedEmails) + { + $this->sAllowedEmails = $sAllowedEmails; + return $this; + } + /** + * @param \MailSo\Log\Logger $oLogger + * + * @return \HmailserverChangePasswordDriver + */ + public function SetLogger($oLogger) + { + if ($oLogger instanceof \MailSo\Log\Logger) + { + $this->oLogger = $oLogger; + } + + return $this; + } + /** + * @param \RainLoop\Model\Account $oAccount + * + * @return bool + */ + + public function PasswordChangePossibility($oAccount) + { + return $oAccount && $oAccount->Email() && + \RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails); + } + + /** + * @param \RainLoop\Model\Account $oAccount + * @param string $sPrevPassword + * @param string $sNewPassword + * + * @return bool + */ + public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) + { + if ($this->oLogger) + { + $this->oLogger->Write('Virtualmin: Try to change password for '.$oAccount->Email()); + } + + $bResult = false; + if (!empty($this->sHost) && !empty($this->sAdminUser) && !empty($this->sAdminPassword) && $oAccount) + { + if ($this->oLogger) + { + $this->oLogger->Write('Virtualmin: Required Fields Present'); + } + $sEmail = \trim(\strtolower($oAccount->Email())); + $sEmailUser = \MailSo\Base\Utils::GetAccountNameFromEmail($sEmail); + $sEmailDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); + $sHost = \trim($this->sHost); + $sUrl = $sHost.'/virtual-server/remote.cgi'; + + if ($this->oLogger) + { + $this->oLogger->Write('Virtualmin[Api Request]: '.$sUrl); + } + $sQuery = "wget -O - --quiet --http-user=$this->sAdminUser --http-passwd=$this->sAdminPassword --no-check-certificate '$sUrl?program=modify-user&domain=$sEmailDomain&pass=$sNewPassword&user=$sEmailUser'"; + //$this->oLogger->Write('Virtualmin[Api Request Call]: '.$sQuery); + $sResult = shell_exec($sQuery); + + $iPos = strpos($sResult, 'Exit status: '); + if ($iPos !== false) { + $sStatus = explode(' ', $sResult); + $sStatus=\trim(array_pop($sStatus)); + if ($this->oLogger) + { + $this->oLogger->Write('Virtualmin: Status: '.$sStatus); + } + if($sStatus=='0'){ + $bResult = true; + } + else + { + if ($this->oLogger) + { + $this->oLogger->Write('Virtualmin[Error]: Response: '.$sResult); + } + } + } + } + $this->oLogger->Write('Virtualmin: Operation Completed. Check logs for status.'); + return $bResult; + } +} + + + + + + + + + + + + + + + + + + + + + + From 1d55a87df0eaee58553308cd79e27b9643dae6de Mon Sep 17 00:00:00 2001 From: icedman21 Date: Mon, 4 May 2015 13:42:07 +0800 Subject: [PATCH 5/6] Create README --- plugins/virtualmin-change-password/README | 1 + 1 file changed, 1 insertion(+) create mode 100644 plugins/virtualmin-change-password/README diff --git a/plugins/virtualmin-change-password/README b/plugins/virtualmin-change-password/README new file mode 100644 index 000000000..7488b2ac9 --- /dev/null +++ b/plugins/virtualmin-change-password/README @@ -0,0 +1 @@ +This plugin utilizes Virtualmin's remote API to change user passwords. The plugin requires the Admin user name and password to succesfully execute the password change. The host and port where Virtualmin listens on is also needed. See https://www.virtualmin.com/documentation/developer/http for more information. From 7fb8357b9d3bfa89051b464bf4d3a36261f002b9 Mon Sep 17 00:00:00 2001 From: icedman21 Date: Tue, 5 May 2015 00:16:13 +0800 Subject: [PATCH 6/6] Update VirtualminChangePasswordDriver.php --- .../VirtualminChangePasswordDriver.php | 206 ++++++++++++++---- 1 file changed, 168 insertions(+), 38 deletions(-) diff --git a/plugins/virtualmin-change-password/VirtualminChangePasswordDriver.php b/plugins/virtualmin-change-password/VirtualminChangePasswordDriver.php index a1d725d88..21397afb9 100644 --- a/plugins/virtualmin-change-password/VirtualminChangePasswordDriver.php +++ b/plugins/virtualmin-change-password/VirtualminChangePasswordDriver.php @@ -74,6 +74,93 @@ class VirtualminChangePasswordDriver implements \RainLoop\Providers\ChangePasswo \RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails); } + /** + * @param string $sUrl + * @param array $aPost = array() + * @param string $sCustomUserAgent = 'MailSo Http User Agent (v1)' + * @param int $iCode = 0 + * @param \MailSo\Log\Logger $oLogger = null + * @param int $iTimeout = 20 + * @param string $sProxy = '' + * @param string $sProxyAuth = '' + * + * @return string|bool + * + * Had to costumize use this as the builtin SendPostRequest in \MailSo\Base\Http had no way of setting CURLOPT_USERPWD + */ + public function PostRequest($sUrl, $aPost = array(), $sAdminUser, $sAdminPassword, $sCustomUserAgent = 'Rainloop Http User Agent (v1)', &$iCode = 0, + $oLogger = null, $iTimeout = 20, $sProxy = '', $sProxyAuth = '') + { + + $oLogger->Write('Virtualmin: Inside function: '); + $aOptions = array( + CURLOPT_URL => $sUrl, + CURLOPT_HEADER => false, + CURLOPT_FAILONERROR => true, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => \http_build_query($aPost, '', '&'), + CURLOPT_TIMEOUT => (int) $iTimeout + ); + + if ($oLogger) + { + $oLogger->Write('Virtualmin: Inside function2: '); + } + + if (0 < \strlen($sAdminUser) && 0 < \strlen($sAdminPassword)) + { + $aOptions[CURLOPT_USERPWD] = $sAdminUser.':'.$sAdminPassword; + } + + if (0 < \strlen($sCustomUserAgent)) + { + $aOptions[CURLOPT_USERAGENT] = $sCustomUserAgent; + } + + if (0 < \strlen($sProxy)) + { + $aOptions[CURLOPT_PROXY] = $sProxy; + if (0 < \strlen($sProxyAuth)) + { + $aOptions[CURLOPT_PROXYUSERPWD] = $sProxyAuth; + } + } + if ($oLogger) + { + $oLogger->Write('Virtualmin: before init: '); + } + $oCurl = \curl_init(); + \curl_setopt_array($oCurl, $aOptions); + + if ($oLogger) + { + $oLogger->Write('cURL: Send post request: '.$sUrl); + } + + $mResult = \curl_exec($oCurl); + + $iCode = (int) \curl_getinfo($oCurl, CURLINFO_HTTP_CODE); + $sContentType = (string) \curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE); + + if ($oLogger) + { + $oLogger->Write('cURL: Post request result: (Status: '.$iCode.', ContentType: '.$sContentType.')'); + if (false === $mResult || 200 !== $iCode) + { + $oLogger->Write('cURL: Error: '.\curl_error($oCurl), \MailSo\Log\Enumerations\Type::WARNING); + } + } + + if (\is_resource($oCurl)) + { + \curl_close($oCurl); + } + + return $mResult; + } + /** * @param \RainLoop\Model\Account $oAccount * @param string $sPrevPassword @@ -93,65 +180,108 @@ class VirtualminChangePasswordDriver implements \RainLoop\Providers\ChangePasswo { if ($this->oLogger) { - $this->oLogger->Write('Virtualmin: Required Fields Present'); + $this->oLogger->Write('Virtualmin:[Check] Required Fields Present'); } $sEmail = \trim(\strtolower($oAccount->Email())); $sEmailUser = \MailSo\Base\Utils::GetAccountNameFromEmail($sEmail); $sEmailDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); $sHost = \trim($this->sHost); $sUrl = $sHost.'/virtual-server/remote.cgi'; + $sAdminUser = $this->sAdminUser; + $sAdminPassword=$this->sAdminPassword; + $iCode = 0; + + $aPost = array( + 'user' => $sEmailUser, + 'pass' => $sNewPassword, + 'domain' => $sEmailDomain, + 'program' => 'modify-user' + ); + + $aOptions = array( + CURLOPT_URL => $sUrl, + CURLOPT_HEADER => false, + CURLOPT_FAILONERROR => true, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => http_build_query($aPost, '', '&'), + CURLOPT_TIMEOUT => 20, + CURLOPT_SSL_VERIFYHOST => false, + CURLOPT_USERPWD => $sAdminUser.':'.$sAdminPassword + ); + + $oCurl = \curl_init(); + \curl_setopt_array($oCurl, $aOptions); + if ($this->oLogger) { - $this->oLogger->Write('Virtualmin[Api Request]: '.$sUrl); + $this->oLogger->Write('Virtualmin: Send post request: '.$sUrl); } - $sQuery = "wget -O - --quiet --http-user=$this->sAdminUser --http-passwd=$this->sAdminPassword --no-check-certificate '$sUrl?program=modify-user&domain=$sEmailDomain&pass=$sNewPassword&user=$sEmailUser'"; - //$this->oLogger->Write('Virtualmin[Api Request Call]: '.$sQuery); - $sResult = shell_exec($sQuery); - $iPos = strpos($sResult, 'Exit status: '); - if ($iPos !== false) { - $sStatus = explode(' ', $sResult); - $sStatus=\trim(array_pop($sStatus)); - if ($this->oLogger) + $mResult = \curl_exec($oCurl); + + $iCode = (int) \curl_getinfo($oCurl, CURLINFO_HTTP_CODE); + $sContentType = (string) \curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE); + + if ($this->oLogger) + { + $this->oLogger->Write('Virtualmin: Post request result: (Status: '.$iCode.', ContentType: '.$sContentType.')'); + if (false === $mResult || 200 !== $iCode) { - $this->oLogger->Write('Virtualmin: Status: '.$sStatus); + $this->oLogger->Write('Virtualmin: Error: '.\curl_error($oCurl), \MailSo\Log\Enumerations\Type::WARNING); + } + } + + if (\is_resource($oCurl)) + { + \curl_close($oCurl); + } + + if (false !== $mResult && 200 === $iCode) + { + $aRes = null; + @\parse_str($mResult, $aRes); + if (is_array($aRes) && (!isset($aRes['error']) || (int) $aRes['error'] !== 1)) + { + $iPos = strpos($mResult, 'Exit status: '); + if ($iPos !== false) { + $sStatus = explode(' ', $mResult); + $sStatus=\trim(array_pop($sStatus)); + + if($sStatus=='0'){ + if ($this->oLogger) + { + $this->oLogger->Write('Virtualmin: Password Change Status: Success'); + } + $bResult = true; + } + else + { + if ($this->oLogger) + { + $this->oLogger->Write('Virtualmin[Error]: Response: '.$mResult); + } + } + } } - if($sStatus=='0'){ - $bResult = true; - } else { if ($this->oLogger) { - $this->oLogger->Write('Virtualmin[Error]: Response: '.$sResult); + $this->oLogger->Write('Virtualmin[Error]: Response: '.$mResult); } } } + else + { + if ($this->oLogger) + { + $this->oLogger->Write('Virtualmin[Error]: Empty Response: Code:'.$iCode); + } + } } - $this->oLogger->Write('Virtualmin: Operation Completed. Check logs for status.'); return $bResult; } } - - - - - - - - - - - - - - - - - - - - - -