/** * @param \RainLoop\Model\Account $oHmailAccount * @param string $sPrevPassword * @param string $sNewPassword * * @return bool */ public function ChangePassword(\RainLoop\Account $oHmailAccount, $sPrevPassword, $sNewPassword) { if ($this->oLogger) { $this->oLogger->Write('Try to change password for ' . $oHmailAccount->Email()); } $bResult = false; try { $oHmailApp = new COM("hMailServer.Application"); $oHmailApp->Connect(); if ($oHmailApp->Authenticate($this->sLogin, $this->sPassword)) { $sEmail = $oHmailAccount->Email(); $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); $oHmailDomain = $oHmailApp->Domains->ItemByName($sDomain); if ($oHmailDomain) { $oHmailAccount = $oHmailDomain->Accounts->ItemByAddress($sEmail); if ($oHmailAccount) { $oHmailAccount->Password = $sNewPassword; $oHmailAccount->Save(); $bResult = true; } else { $this->oLogger->Write('HMAILSERVER: Unknown account (' . $sEmail . ')', \MailSo\Log\Enumerations\Type::ERROR); } } else { $this->oLogger->Write('HMAILSERVER: Unknown domain (' . $sDomain . ')', \MailSo\Log\Enumerations\Type::ERROR); } } else { $this->oLogger->Write('HMAILSERVER: Auth error', \MailSo\Log\Enumerations\Type::ERROR); } } catch (\Exception $oException) { if ($this->oLogger) { $this->oLogger->WriteException($oException); } } return $bResult; }
/** * This function detects the SMTP Host, and if it is set to "auto", replaces it with the email domain. * * @param \RainLoop\Model\Account $oAccount * @param array $aSmtpCredentials */ public function FilterSmtpCredentials($oAccount, &$aSmtpCredentials) { if ($oAccount instanceof \RainLoop\Model\Account && \is_array($aSmtpCredentials)) { // Check for mail.$DOMAIN as entered value in RL settings if (!empty($aSmtpCredentials['Host']) && 'auto' === $aSmtpCredentials['Host']) { $aSmtpCredentials['Host'] = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email()); } } }
/** * @param \RainLoop\Account $oAccount * @param string $sPrevPassword * @param string $sNewPassword * * @return bool */ public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) { if ($this->oLogger) { $this->oLogger->Write('DirectAdmin: Try to change password for ' . $oAccount->Email()); } $bResult = false; if (!empty($this->sHost) && 0 < $this->iPort && $oAccount) { $sEmail = \trim(\strtolower($oAccount->Email())); $sHost = \trim($this->sHost); $sHost = \str_replace('{user:host-imap}', $oAccount->Domain()->IncHost(), $sHost); $sHost = \str_replace('{user:host-smtp}', $oAccount->Domain()->OutHost(), $sHost); $sHost = \str_replace('{user:domain}', \MailSo\Base\Utils::GetDomainFromEmail($sEmail), $sHost); $sHost = \rtrim($this->sHost, '/\\'); if (!\preg_match('/^http[s]?:\\/\\//i', $sHost)) { $sHost = 'http://' . $sHost; } $sUrl = $sHost . ':' . $this->iPort . '/CMD_CHANGE_EMAIL_PASSWORD'; $iCode = 0; $oHttp = \MailSo\Base\Http::SingletonInstance(); if ($this->oLogger) { $this->oLogger->Write('DirectAdmin[Api Request]:' . $sUrl); } $mResult = $oHttp->SendPostRequest($sUrl, array('email' => $sEmail, 'oldpassword' => $sPrevPassword, 'password1' => $sNewPassword, 'password2' => $sNewPassword, 'api' => '1'), 'MailSo Http User Agent (v1)', $iCode, $this->oLogger); if (false !== $mResult && 200 === $iCode) { $aRes = null; @\parse_str($mResult, $aRes); if (is_array($aRes) && (!isset($aRes['error']) || (int) $aRes['error'] !== 1)) { $bResult = true; } else { if ($this->oLogger) { $this->oLogger->Write('DirectAdmin[Error]: Response: ' . $mResult); } } } else { if ($this->oLogger) { $this->oLogger->Write('DirectAdmin[Error]: Empty Response: Code:' . $iCode); } } } return $bResult; }
/** * @param string $sStr * @param bool $bLowerIfAscii = false * * @return string */ public static function IdnToAscii($sStr, $bLowerIfAscii = false) { $sStr = $bLowerIfAscii ? \MailSo\Base\Utils::StrToLowerIfAscii($sStr) : $sStr; $sUser = ''; $sDomain = $sStr; if (false !== \strpos($sStr, '@')) { $sUser = \MailSo\Base\Utils::GetAccountNameFromEmail($sStr); $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sStr); } if (0 < \strlen($sDomain) && \preg_match('/[^\\x20-\\x7E]/', $sDomain)) { try { $sDomain = self::idn()->encode($sDomain); } catch (\Exception $oException) { } } return ('' === $sUser ? '' : $sUser . '@') . $sDomain; }
/** * @return array */ public function AjaxDomainGetDataByEmail() { $oAccount = $this->getAccountFromParam(); $sEmail = (string) $this->getParamValue('Email', ''); $sDomainName = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); if (empty($sEmail) || empty($sDomainName)) { throw new \ProjectCore\Exceptions\ClientException(\ProjectCore\Notifications::InvalidInputParameter); } $oApiDomains = \CApi::Manager('domains'); $oDomain = $oApiDomains->getDomainByName($sDomainName); return $this->DefaultResponse($oAccount, __FUNCTION__, $oDomain ? array('IsInternal' => $oDomain->IsInternal, 'IncomingMailServer' => $oDomain->IncomingMailServer, 'IncomingMailPort' => $oDomain->IncomingMailPort, 'OutgoingMailServer' => $oDomain->OutgoingMailServer, 'OutgoingMailPort' => $oDomain->OutgoingMailPort, 'OutgoingMailAuth' => $oDomain->OutgoingMailAuth, 'IncomingMailSsl' => $oDomain->IncomingMailUseSSL, 'OutgoingMailSsl' => $oDomain->OutgoingMailUseSSL) : false); }
/** * @param \RainLoop\Model\Account $oAccount * @param string $sPrevPassword * @param string $sNewPassword * * @return bool */ public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) { $bResult = false; try { $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email()); $sUserDn = \strtr($this->sUserDnFormat, array('{domain}' => $sDomain, '{domain:dc}' => 'dc=' . \strtr($sDomain, array('.' => ',dc=')), '{email}' => $oAccount->Email(), '{email:user}' => \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email()), '{email:domain}' => $sDomain, '{login}' => $oAccount->Login(), '{imap:login}' => $oAccount->Login(), '{imap:host}' => $oAccount->DomainIncHost(), '{imap:port}' => $oAccount->DomainIncPort())); $oCon = @\ldap_connect($this->sHostName); if ($oCon) { @\ldap_set_option($oCon, LDAP_OPT_PROTOCOL_VERSION, 3); if (!@\ldap_bind($oCon, $sUserDn, $sPrevPassword)) { if ($this->oLogger) { $sError = $oCon ? @\ldap_error($oCon) : ''; $iErrno = $oCon ? @\ldap_errno($oCon) : 0; $this->oLogger->Write('ldap_bind error: ' . $sError . ' (' . $iErrno . ')', \MailSo\Log\Enumerations\Type::WARNING, 'LDAP'); } return false; } } $sEncodedNewPassword = $sNewPassword; switch (\strtolower($this->sPasswordEncType)) { case 'sha': switch (true) { default: case \function_exists('sha1'): $sEncodedNewPassword = '******' . \base64_encode(\pack('H*', \sha1($sNewPassword))); break; case \function_exists('hash'): $sEncodedNewPassword = '******' . \base64_encode(\hash('sha1', $sNewPassword, true)); break; case \function_exists('mhash') && defined('MHASH_SHA1'): $sEncodedNewPassword = '******' . \base64_encode(\mhash(MHASH_SHA1, $sNewPassword)); break; } break; case 'md5': $sEncodedNewPassword = '******' . \base64_encode(\pack('H*', \md5($sNewPassword))); break; case 'crypt': $sEncodedNewPassword = '******' . \crypt($sNewPassword, $this->getSalt(2)); break; } $aEntry = array(); $aEntry[$this->sPasswordField] = (string) $sEncodedNewPassword; if (!!@\ldap_modify($oCon, $sUserDn, $aEntry)) { $bResult = true; } else { if ($this->oLogger) { $sError = $oCon ? @\ldap_error($oCon) : ''; $iErrno = $oCon ? @\ldap_errno($oCon) : 0; $this->oLogger->Write('ldap_modify error: ' . $sError . ' (' . $iErrno . ')', \MailSo\Log\Enumerations\Type::WARNING, 'LDAP'); } } } catch (\Exception $oException) { if ($this->oLogger) { $this->oLogger->WriteException($oException, \MailSo\Log\Enumerations\Type::WARNING, 'LDAP'); } $bResult = false; } return $bResult; }
/** * @param \RainLoop\Model\Account $oAccount * @param string $sPrevPassword * @param string $sNewPassword * * @return bool */ public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) { $this->WriteLog('Virtualmin: Try to change password for ' . $oAccount->Email()); $bResult = false; if (!empty($this->sHost) && !empty($this->sAdminUser) && !empty($this->sAdminPassword) && $oAccount) { $this->WriteLog('Virtualmin:[Check] Required Fields Present'); $sEmail = \trim(\strtolower($oAccount->Email())); $sEmailUser = \MailSo\Base\Utils::GetAccountNameFromEmail($sEmail); $sEmailDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); $sHost = \rtrim(\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); $this->WriteLog('Virtualmin: Send post request: ' . $sUrl); $mResult = \curl_exec($oCurl); $iCode = (int) \curl_getinfo($oCurl, CURLINFO_HTTP_CODE); $sContentType = (string) \curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE); $this->WriteLog('Virtualmin: Post request result: (Status: ' . $iCode . ', ContentType: ' . $sContentType . ')'); if (false === $mResult || 200 !== $iCode) { $this->WriteLog('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) { $aStatus = \explode(' ', $mResult); $sStatus = \trim(\array_pop($aStatus)); if ('0' === $sStatus) { $this->WriteLog('Virtualmin: Password Change Status: Success'); $bResult = true; } else { $this->WriteLog('Virtualmin[Error]: Response: ' . $mResult); } } } else { $this->WriteLog('Virtualmin[Error]: Response: ' . $mResult); } } else { $this->WriteLog('Virtualmin[Error]: Empty Response: Code: ' . $iCode); } } return $bResult; }
/** * @param string $sEmail * @param string $sLogin * @param string $sPassword * @param string $sSignMeToken = '' * @param bool $bThrowProvideException = false * * @return \RainLoop\Model\Account|null */ public function LoginProvide($sEmail, $sLogin, $sPassword, $sSignMeToken = '', $bThrowProvideException = false) { $oAccount = null; if (0 < \strlen($sEmail) && 0 < \strlen($sLogin) && 0 < \strlen($sPassword)) { $oDomain = $this->DomainProvider()->Load(\MailSo\Base\Utils::GetDomainFromEmail($sEmail), true); if ($oDomain instanceof \RainLoop\Model\Domain) { if ($oDomain->ValidateWhiteList($sEmail, $sLogin)) { $oAccount = \RainLoop\Model\Account::NewInstance($sEmail, $sLogin, $sPassword, $oDomain, $sSignMeToken); $this->Plugins()->RunHook('filter.acount', array(&$oAccount)); if ($bThrowProvideException && !$oAccount instanceof \RainLoop\Model\Account) { throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::AuthError); } } else { if ($bThrowProvideException) { throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::AccountNotAllowed); } } } else { if ($bThrowProvideException) { throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::DomainNotAllowed); } } } return $oAccount; }
/** * @param bool $bIdn = false * * @return string */ public function GetDomain($bIdn = false) { return \MailSo\Base\Utils::GetDomainFromEmail($this->GetEmail($bIdn)); }
/** * @return \RainLoop\Domain */ public function Hash() { return md5(APP_SALT . $this->Email() . APP_SALT . $this->oDomain->IncHost(\MailSo\Base\Utils::GetDomainFromEmail($this->Email())) . APP_SALT . $this->oDomain->IncPort() . APP_SALT . $this->Password() . APP_SALT . '0' . APP_SALT . $this->ParentEmail() . APP_SALT); }
/** * @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('Try to change password for ' . $oAccount->Email()); } if (!\class_exists('xmlapi')) { include_once __DIR__ . '/xmlapi.php'; } $bResult = false; if (!empty($this->sHost) && 0 < $this->iPost && 0 < \strlen($this->sUser) && 0 < \strlen($this->sPassword) && $oAccount && \class_exists('xmlapi')) { $sEmail = $oAccount->Email(); $sEmailUser = \MailSo\Base\Utils::GetAccountNameFromEmail($sEmail); $sEmailDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); $sHost = $this->sHost; $sHost = \str_replace('{user:domain}', $sEmailDomain, $sHost); $sUser = $this->sUser; $sUser = \str_replace('{user:email}', $sEmail, $sUser); $sUser = \str_replace('{user:login}', $sEmailUser, $sUser); $sPassword = $this->sPassword; $sPassword = \str_replace('{user:password}', $oAccount->Password(), $sPassword); try { $oXmlApi = new \xmlapi($sHost); $oXmlApi->set_port($this->iPost); $oXmlApi->set_protocol($this->bSsl ? 'https' : 'http'); $oXmlApi->set_debug(false); $oXmlApi->set_output('json'); // $oXmlApi->set_http_client('fopen'); $oXmlApi->set_http_client('curl'); $oXmlApi->password_auth($sUser, $sPassword); $aArgs = array('email' => $sEmailUser, 'domain' => $sEmailDomain, 'password' => $sNewPassword); $sResult = $oXmlApi->api2_query($sUser, 'Email', 'passwdpop', $aArgs); if ($sResult) { if ($this->oLogger) { $this->oLogger->Write('CPANEL: ' . $sResult, \MailSo\Log\Enumerations\Type::INFO); } $aResult = @\json_decode($sResult, true); $bResult = isset($aResult['cpanelresult']['data'][0]['result']) && !!$aResult['cpanelresult']['data'][0]['result']; } if (!$bResult && $this->oLogger) { $this->oLogger->Write('CPANEL: ' . $sResult, \MailSo\Log\Enumerations\Type::ERROR); } } catch (\Exception $oException) { if ($this->oLogger) { $this->oLogger->WriteException($oException); } } } else { if ($this->oLogger) { $this->oLogger->Write('CPANEL: Incorrent configuration data', \MailSo\Log\Enumerations\Type::ERROR); } } return $bResult; }
/** * @param \RainLoop\Account $oAccount * @param string $sPrevPassword * @param string $sNewPassword * * @return bool */ public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) { if ($this->oLogger) { $this->oLogger->Write('Try to change password for ' . $oAccount->Email()); } if (empty($this->mHost) || empty($this->mDatabase) || empty($this->mColumn) || empty($this->mTable)) { return false; } $bResult = false; $sDsn = 'mysql:host=' . $this->mHost . ';dbname=' . $this->mDatabase . ';charset=utf8'; $aOptions = array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); $sLoginPart = \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email()); $sDomainPart = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email()); try { $oConn = new PDO($sDsn, $this->mUser, $this->mPass, $aOptions); $oSelect = $oConn->prepare('SELECT ' . $this->mColumn . ' FROM ' . $this->mTable . ' WHERE pw_name=? AND pw_domain=? LIMIT 1'); $oSelect->execute(array($sLoginPart, $sDomainPart)); $aColCrypt = $oSelect->fetchAll(PDO::FETCH_ASSOC); $sCryptPass = isset($aColCrypt[0][$this->mColumn]) ? $aColCrypt[0][$this->mColumn] : ''; if (0 < \strlen($sCryptPass) && \crypt($sPrevPassword, $sCryptPass) === $sCryptPass) { $oUpdate = $oConn->prepare('UPDATE ' . $this->mTable . ' SET ' . $this->mColumn . '=ENCRYPT(?,concat("$1$",right(md5(rand()), 8 ),"$")), pw_clear_passwd=\'\' WHERE pw_name=? AND pw_domain=?'); $oUpdate->execute(array($sNewPassword, $sLoginPart, $sDomainPart)); $bResult = true; if ($this->oLogger) { $this->oLogger->Write('Success! Password changed.'); } } else { $bResult = false; if ($this->oLogger) { $this->oLogger->Write('Something went wrong. Either current password is incorrect, or new password does not match criteria.'); } } } catch (\Exception $oException) { $bResult = false; if ($this->oLogger) { $this->oLogger->WriteException($oException); } } return $bResult; }
/** * @param \RainLoop\Model\Account $oAccount * @param string $sQuery * * @return array */ private function ldapSearch($oAccount, $sQuery) { $sSearchEscaped = $this->escape($sQuery); $aResult = array(); $oCon = @\ldap_connect($this->sHostName, $this->iHostPort); if ($oCon) { $this->oLogger->Write('ldap_connect: connected', \MailSo\Log\Enumerations\Type::INFO, 'LDAP'); @\ldap_set_option($oCon, LDAP_OPT_PROTOCOL_VERSION, 3); if (!@\ldap_bind($oCon, $this->sAccessDn, $this->sAccessPassword)) { $this->logLdapError($oCon, 'ldap_bind'); return $aResult; } $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email()); $sSearchDn = \strtr($this->sUsersDn, array('{domain}' => $sDomain, '{domain:dc}' => 'dc=' . \strtr($sDomain, array('.' => ',dc=')), '{email}' => $oAccount->Email(), '{email:user}' => \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email()), '{email:domain}' => $sDomain, '{login}' => $oAccount->Login(), '{imap:login}' => $oAccount->Login(), '{imap:host}' => $oAccount->DomainIncHost(), '{imap:port}' => $oAccount->DomainIncPort())); $aEmails = empty($this->sEmailField) ? array() : \explode(',', $this->sEmailField); $aNames = empty($this->sNameField) ? array() : \explode(',', $this->sNameField); $aEmails = \array_map('trim', $aEmails); $aNames = \array_map('trim', $aNames); $aFields = \array_merge($aEmails, $aNames); $aItems = array(); $sSubFilter = ''; foreach ($aFields as $sItem) { if (!empty($sItem)) { $aItems[] = $sItem; $sSubFilter .= '(' . $sItem . '=*' . $sSearchEscaped . '*)'; } } $sFilter = '(&(objectclass=' . $this->sObjectClass . ')'; $sFilter .= (1 < count($aItems) ? '(|' : '') . $sSubFilter . (1 < count($aItems) ? ')' : ''); $sFilter .= ')'; $this->oLogger->Write('ldap_search: start: ' . $sSearchDn . ' / ' . $sFilter, \MailSo\Log\Enumerations\Type::INFO, 'LDAP'); $oS = @\ldap_search($oCon, $sSearchDn, $sFilter, $aItems, 0, 30, 30); if ($oS) { $aEntries = @\ldap_get_entries($oCon, $oS); if (is_array($aEntries)) { if (isset($aEntries['count'])) { unset($aEntries['count']); } foreach ($aEntries as $aItem) { if ($aItem) { $sName = $sEmail = ''; list($sEmail, $sName) = $this->findNameAndEmail($aItem, $aEmails, $aNames); if (!empty($sEmail)) { $aResult[] = array($sEmail, $sName); } } } } else { $this->logLdapError($oCon, 'ldap_get_entries'); } } else { $this->logLdapError($oCon, 'ldap_search'); } } else { return $aResult; } return $aResult; }
/** * @return string */ public function GetDomain() { return \MailSo\Base\Utils::GetDomainFromEmail($this->sEmail); }
/** * @return \RainLoop\Account|bool */ private function initMailClientConnection() { $oAccount = null; if (!$this->MailClient()->IsLoggined()) { $oAccount = $this->getAccountFromToken(); try { $this->MailClient()->Connect($oAccount->Domain()->IncHost(\MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email())), $oAccount->Domain()->IncPort(), $oAccount->Domain()->IncSecure())->Login($oAccount->IncLogin(), $oAccount->Password(), !!$this->Config()->Get('labs', 'use_imap_auth_plain')); } catch (\MailSo\Net\Exceptions\ConnectionException $oException) { throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::ConnectionError, $oException); } catch (\Exception $oException) { throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::AuthError, $oException); } $this->MailClient()->ImapClient()->__FORCE_SELECT_ON_EXAMINE__ = !!$this->Config()->Get('labs', 'use_imap_force_selection'); } return $oAccount; }