diff --git a/rainloop/v/0.0.0/app/libraries/MailSo/Imap/ImapClient.php b/rainloop/v/0.0.0/app/libraries/MailSo/Imap/ImapClient.php index 5cc375967..1701edb05 100644 --- a/rainloop/v/0.0.0/app/libraries/MailSo/Imap/ImapClient.php +++ b/rainloop/v/0.0.0/app/libraries/MailSo/Imap/ImapClient.php @@ -126,7 +126,7 @@ class ImapClient extends \MailSo\Net\NetClient * @param string $sServerName * @param int $iPort = 143 * @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT - * @param bool $bCapturePeerCertIfSsl = false + * @param bool $bVerifySsl = false * * @return \MailSo\Imap\ImapClient * @@ -135,11 +135,11 @@ class ImapClient extends \MailSo\Net\NetClient * @throws \MailSo\Imap\Exceptions\Exception */ public function Connect($sServerName, $iPort = 143, - $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bCapturePeerCertIfSsl = false) + $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bVerifySsl = false) { $this->aTagTimeouts['*'] = \microtime(true); - parent::Connect($sServerName, $iPort, $iSecurityType, $bCapturePeerCertIfSsl); + parent::Connect($sServerName, $iPort, $iSecurityType, $bVerifySsl); $this->parseResponseWithValidation('*', true); diff --git a/rainloop/v/0.0.0/app/libraries/MailSo/Net/Exceptions/SocketCanNotConnectToHostException.php b/rainloop/v/0.0.0/app/libraries/MailSo/Net/Exceptions/SocketCanNotConnectToHostException.php index e33e2f623..09e4bfeb4 100644 --- a/rainloop/v/0.0.0/app/libraries/MailSo/Net/Exceptions/SocketCanNotConnectToHostException.php +++ b/rainloop/v/0.0.0/app/libraries/MailSo/Net/Exceptions/SocketCanNotConnectToHostException.php @@ -47,6 +47,6 @@ class SocketCanNotConnectToHostException extends \MailSo\Net\Exceptions\Connecti */ public function getSocketCode() { - return $this->sSocketMessage; + return $this->iSocketCode; } } diff --git a/rainloop/v/0.0.0/app/libraries/MailSo/Net/NetClient.php b/rainloop/v/0.0.0/app/libraries/MailSo/Net/NetClient.php index 4558637ec..2aa12769e 100644 --- a/rainloop/v/0.0.0/app/libraries/MailSo/Net/NetClient.php +++ b/rainloop/v/0.0.0/app/libraries/MailSo/Net/NetClient.php @@ -168,11 +168,24 @@ abstract class NetClient return $this->rConnect; } + /** + * @param int $iErrNo + * @param string $sErrStr + * @param string $sErrFile + * @param int $iErrLine + * + * @return bool + */ + public function capturePhpErrorWithException($iErrNo, $sErrStr, $sErrFile, $iErrLine) + { + throw new \MailSo\Base\Exceptions\Exception($sErrStr, $iErrNo); + } + /** * @param string $sServerName * @param int $iPort * @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT - * @param bool $bCapturePeerCertIfSsl = false + * @param bool $bVerifySsl = false * * @return void * @@ -181,7 +194,7 @@ abstract class NetClient * @throws \MailSo\Net\Exceptions\SocketCanNotConnectToHostException */ public function Connect($sServerName, $iPort, - $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bCapturePeerCertIfSsl = false) + $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bVerifySsl = false) { if (!\MailSo\Base\Validator::NotEmptyString($sServerName, true) || !\MailSo\Base\Validator::PortInt($iPort)) { @@ -202,17 +215,22 @@ abstract class NetClient $sErrorStr = ''; $iErrorNo = 0; - $this->iSecurityType = $iSecurityType; + $this->sConnectedHost = $sServerName; $this->iConnectedPort = $iPort; - $this->bSecure = \MailSo\Net\Enumerations\ConnectionSecurityType::UseSSL($iPort, $iSecurityType); - $this->sConnectedHost = $this->bSecure ? 'ssl://'.$sServerName : $sServerName; - - $bCapturePeerCertIfSsl = !!($bCapturePeerCertIfSsl && $this->bSecure); + $this->iSecurityType = $iSecurityType; + $this->bSecure = \MailSo\Net\Enumerations\ConnectionSecurityType::UseSSL( + $this->iConnectedPort, $this->iSecurityType); + + $this->sConnectedHost = \in_array(\strtolower(\substr($this->sConnectedHost, 0, 6)), array('ssl://', 'tcp://')) ? + \substr($this->sConnectedHost, 6) : $this->sConnectedHost; + + $this->sConnectedHost = ($this->bSecure ? 'ssl://' : 'tcp://').$this->sConnectedHost; +// $this->sConnectedHost = ($this->bSecure ? 'ssl://' : '').$this->sConnectedHost; if (!$this->bSecure && \MailSo\Net\Enumerations\ConnectionSecurityType::SSL === $this->iSecurityType) { $this->writeLogException( - new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('SSL isn\'t supported'), + new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('SSL isn\'t supported: ('.\implode(', ', \stream_get_transports()).')'), \MailSo\Log\Enumerations\Type::ERROR, true); } @@ -220,24 +238,33 @@ abstract class NetClient $this->writeLog('Start connection to "'.$this->sConnectedHost.':'.$this->iConnectedPort.'"', \MailSo\Log\Enumerations\Type::NOTE); - if ($bCapturePeerCertIfSsl && \MailSo\Base\Utils::FunctionExistsAndEnabled('stream_context_create') && - \MailSo\Base\Utils::FunctionExistsAndEnabled('stream_socket_client') && defined('STREAM_CLIENT_CONNECT')) - { - $rStreamContext = \stream_context_create(array('ssl' => array('capture_peer_cert' => true))); - $sRemoteSocket = (0 === \strpos($this->sConnectedHost, 'ssl://') - ? $this->sConnectedHost : 'tcp://'.$this->sConnectedHost).':'.$this->iConnectedPort; +// $this->rConnect = @\fsockopen($this->sConnectedHost, $this->iConnectedPort, +// $iErrorNo, $sErrorStr, $this->iConnectTimeOut); - if ($rStreamContext) - { - $this->rConnect = @\stream_socket_client($sRemoteSocket, $iErrorNo, $sErrorStr, - $this->iConnectTimeOut, STREAM_CLIENT_CONNECT, $rStreamContext); - } - } - else + $bVerifySsl = !!$bVerifySsl; + $rStreamContext = \stream_context_create(array( + 'ssl' => array( + 'verify_host' => $bVerifySsl, + 'verify_peer' => $bVerifySsl, + 'verify_peer_name' => $bVerifySsl, + 'allow_self_signed' => !$bVerifySsl + ) + )); + + \set_error_handler(array(&$this, 'capturePhpErrorWithException')); + + try { - $this->rConnect = @\fsockopen($this->sConnectedHost, $this->iConnectedPort, - $iErrorNo, $sErrorStr, $this->iConnectTimeOut); + $this->rConnect = \stream_socket_client($this->sConnectedHost.':'.$this->iConnectedPort, + $iErrorNo, $sErrorStr, $this->iConnectTimeOut, STREAM_CLIENT_CONNECT, $rStreamContext); } + catch (\Exception $oExc) + { + $sErrorStr = $oExc->getMessage(); + $iErrorNo = $oExc->getCode(); + } + + \restore_error_handler(); if (!\is_resource($this->rConnect)) { @@ -515,6 +542,11 @@ abstract class NetClient { if ($this->oLogger) { + if ($oException instanceof Exceptions\SocketCanNotConnectToHostException) + { + $this->oLogger->Write('Socket: ['.$oException->getSocketCode().'] '.$oException->getSocketMessage(), $iDescType, $this->getLogName()); + } + $this->oLogger->WriteException($oException, $iDescType, $this->getLogName()); } diff --git a/rainloop/v/0.0.0/app/libraries/MailSo/Smtp/SmtpClient.php b/rainloop/v/0.0.0/app/libraries/MailSo/Smtp/SmtpClient.php index ecd419394..9a33fc272 100644 --- a/rainloop/v/0.0.0/app/libraries/MailSo/Smtp/SmtpClient.php +++ b/rainloop/v/0.0.0/app/libraries/MailSo/Smtp/SmtpClient.php @@ -141,6 +141,7 @@ class SmtpClient extends \MailSo\Net\NetClient * @param int $iPort = 25 * @param string $sEhloHost = '[127.0.0.1]' * @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT + * @param bool $bVerifySsl = false * * @return \MailSo\Smtp\SmtpClient * @@ -149,11 +150,11 @@ class SmtpClient extends \MailSo\Net\NetClient * @throws \MailSo\Smtp\Exceptions\ResponseException */ public function Connect($sServerName, $iPort = 25, $sEhloHost = '[127.0.0.1]', - $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT) + $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bVerifySsl = false) { $this->iRequestTime = microtime(true); - parent::Connect($sServerName, $iPort, $iSecurityType); + parent::Connect($sServerName, $iPort, $iSecurityType, $bVerifySsl); $this->validateResponse(220); $this->preLoginStartTLSAndEhloProcess($sEhloHost); diff --git a/rainloop/v/0.0.0/app/libraries/RainLoop/Actions.php b/rainloop/v/0.0.0/app/libraries/RainLoop/Actions.php index 72f66aca0..2c34ca08f 100644 --- a/rainloop/v/0.0.0/app/libraries/RainLoop/Actions.php +++ b/rainloop/v/0.0.0/app/libraries/RainLoop/Actions.php @@ -2777,26 +2777,14 @@ class Actions $oDomain = $this->DomainProvider()->LoadOrCreateNewFromAction($this, 'domain-test-connection.de'); if ($oDomain) { -// $bOpenSSL = \MailSo\Base\Utils::FunctionExistsAndEnabled('openssl_x509_parse'); - $bOpenSSL = false; // TODO in dev - try { $oImapClient = \MailSo\Imap\ImapClient::NewInstance()->SetLogger($this->Logger()); $oImapClient->SetTimeOuts(5); $iTime = \microtime(true); - $oImapClient->Connect($oDomain->IncHost($oDomain->Name()), $oDomain->IncPort(), $oDomain->IncSecure(), $bOpenSSL); - - if ($bOpenSSL) - { - $aStreamContextParams = $oImapClient->StreamContextParams(); - if (isset($aStreamContextParams['options']['ssl']['peer_certificate'])) - { - $aParseData = @\openssl_x509_parse($aStreamContextParams['options']['ssl']['peer_certificate']); - $this->Logger()->WriteDump($aParseData); - } - } + $oImapClient->Connect($oDomain->IncHost($oDomain->Name()), $oDomain->IncPort(), + $oDomain->IncSecure(), $oDomain->IncVerifySsl()); $iImapTime = \microtime(true) - $iTime; $oImapClient->Disconnect(); @@ -2823,17 +2811,8 @@ class Actions $oSmtpClient->SetTimeOuts(5); $iTime = \microtime(true); - $oSmtpClient->Connect($oDomain->OutHost($oDomain->Name()), $oDomain->OutPort(), '127.0.0.1', $oDomain->OutSecure(), $bOpenSSL); - - if ($bOpenSSL) - { - $aStreamContextParams = $oSmtpClient->StreamContextParams(); - if (isset($aStreamContextParams['options']['ssl']['peer_certificate'])) - { - $aParseData = @\openssl_x509_parse($aStreamContextParams['options']['ssl']['peer_certificate']); - $this->Logger()->WriteDump($aParseData); - } - } + $oSmtpClient->Connect($oDomain->OutHost($oDomain->Name()), $oDomain->OutPort(), '127.0.0.1', + $oDomain->OutSecure(), $oDomain->OutVerifySsl()); $iSmtpTime = \microtime(true) - $iTime; $oSmtpClient->Disconnect(); diff --git a/rainloop/v/0.0.0/app/libraries/RainLoop/Domain.php b/rainloop/v/0.0.0/app/libraries/RainLoop/Domain.php index 0b44f9674..d3e26616f 100644 --- a/rainloop/v/0.0.0/app/libraries/RainLoop/Domain.php +++ b/rainloop/v/0.0.0/app/libraries/RainLoop/Domain.php @@ -28,6 +28,11 @@ class Domain */ private $iIncSecure; + /** + * @var bool + */ + private $bIncVerifySsl; + /** * @var bool */ @@ -48,6 +53,11 @@ class Domain */ private $iOutSecure; + /** + * @var bool + */ + private $bOutVerifySsl; + /** * @var bool */ @@ -83,10 +93,12 @@ class Domain $this->sIncHost = $sIncHost; $this->iIncPort = $iIncPort; $this->iIncSecure = $iIncSecure; + $this->bIncVerifySsl = false; $this->bIncShortLogin = $bIncShortLogin; $this->sOutHost = $sOutHost; $this->iOutPort = $iOutPort; $this->iOutSecure = $iOutSecure; + $this->bOutVerifySsl = false; $this->bOutShortLogin = $bOutShortLogin; $this->bOutAuth = $bOutAuth; $this->sWhiteList = \trim($sWhiteList); @@ -311,6 +323,14 @@ class Domain return $this->iIncSecure; } + /** + * @return bool + */ + public function IncVerifySsl() + { + return $this->bIncVerifySsl; + } + /** * @return bool */ @@ -344,6 +364,14 @@ class Domain return $this->iOutSecure; } + /** + * @return bool + */ + public function OutVerifySsl() + { + return $this->bOutVerifySsl; + } + /** * @return bool */