Skip to content

Commit

Permalink
Prepare for custom SMTP credentials for #859 #458 #431 #233
Browse files Browse the repository at this point in the history
  • Loading branch information
the-djmaze committed Jan 17, 2023
1 parent 038e093 commit 61899ce
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 49 deletions.
4 changes: 1 addition & 3 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,6 @@ public function AppData(bool $bAdmin): array
$aResult = \array_merge($aResult, [
'Auth' => true,
'Email' => \MailSo\Base\Utils::IdnToUtf8($oAccount->Email()),
'IncLogin' => $oAccount->IncLogin(),
'OutLogin' => $oAccount->OutLogin(),
'AccountHash' => $oAccount->Hash(),
'AccountSignMe' => isset($_COOKIE[self::AUTH_SIGN_ME_TOKEN_KEY]),
'MainEmail' => \MailSo\Base\Utils::IdnToUtf8($this->getMainAccountFromToken()->Email()),
Expand Down Expand Up @@ -1103,7 +1101,7 @@ protected function initMailClientConnection(): ?Model\Account

if (!$this->MailClient()->IsLoggined()) {
try {
$oAccount->ImapConnectAndLoginHelper($this->oPlugins, $this->MailClient()->ImapClient(), $this->oConfig);
$oAccount->ImapConnectAndLogin($this->oPlugins, $this->MailClient()->ImapClient(), $this->oConfig);
} catch (\MailSo\Net\Exceptions\ConnectionException $oException) {
throw new Exceptions\ClientException(Notifications::ConnectionError, $oException);
} catch (\Throwable $oException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,6 @@ public function DoAccountSwitch(): array
if ($this->switchAccount(\trim($this->GetActionParam('Email', '')))) {
$oAccount = $this->getAccountFromToken();
$aResult['Email'] = $oAccount->Email();
$aResult['IncLogin'] = $oAccount->IncLogin();
$aResult['OutLogin'] = $oAccount->OutLogin();
$aResult['AccountHash'] = $oAccount->Hash();
$aResult['MainEmail'] = ($oAccount instanceof AdditionalAccount)
? $oAccount->ParentEmail() : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ private function smtpSendMessage(Account $oAccount, \MailSo\Mime\Message $oMessa
$oSmtpClient->SetLogger($this->Logger());

$bUsePhpMail = false;
$oAccount->SmtpConnectAndLoginHelper($this->Plugins(), $oSmtpClient, $this->Config(), $bUsePhpMail);
$oAccount->SmtpConnectAndLogin($this->Plugins(), $oSmtpClient, $this->Config(), $bUsePhpMail);

if ($bUsePhpMail) {
if (\MailSo\Base\Utils::FunctionCallable('mail')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ protected function imapConnect(Account $oAccount, bool $bAuthLog = false, \MailS
if (!$oImapClient) {
$oImapClient = $this->MailClient()->ImapClient();
}
$oAccount->ImapConnectAndLoginHelper($this->Plugins(), $oImapClient, $this->Config());
$oAccount->ImapConnectAndLogin($this->Plugins(), $oImapClient, $this->Config());
} catch (ClientException $oException) {
throw $oException;
} catch (\MailSo\Net\Exceptions\ConnectionException $oException) {
Expand Down
74 changes: 38 additions & 36 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Model/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ abstract class Account implements \JsonSerializable

private string $sPassword = '';

private string $sSmtpLogin = '';

private string $sSmtpPassword = '';

private string $sProxyAuthUser = '';

private string $sProxyAuthPassword = '';
Expand All @@ -32,16 +36,6 @@ public function Name() : string
return $this->sName;
}

public function ProxyAuthUser() : string
{
return $this->sProxyAuthUser;
}

public function ProxyAuthPassword() : string
{
return $this->sProxyAuthPassword;
}

public function IncLogin() : string
{
return $this->oDomain->IncShortLogin()
Expand All @@ -56,23 +50,8 @@ public function IncPassword() : string

public function OutLogin() : string
{
return $this->oDomain->OutShortLogin()
? \MailSo\Base\Utils::GetAccountNameFromEmail($this->sLogin)
: $this->sLogin;
}

// Deprecated
public function Login() : string
{
\trigger_error('Use \RainLoop\Model\Account->IncLogin()', \E_USER_DEPRECATED);
return $this->IncLogin();
}

// Deprecated
public function Password() : string
{
\trigger_error('Use \RainLoop\Model\Account->IncPassword()', \E_USER_DEPRECATED);
return $this->IncPassword();
$sSmtpLogin = $this->sSmtpLogin ?: $this->sLogin;
return $this->oDomain->OutShortLogin() ? \MailSo\Base\Utils::GetAccountNameFromEmail($sSmtpLogin) : $sSmtpLogin;
}

public function Domain() : Domain
Expand All @@ -95,6 +74,11 @@ public function SetPassword(string $sPassword) : void
$this->sPassword = $sPassword;
}

public function SetSmtpPassword(string $sPassword) : void
{
$this->sSmtpLogin = $sPassword;
}

public function SetProxyAuthUser(string $sProxyAuthUser) : void
{
$this->sProxyAuthUser = $sProxyAuthUser;
Expand All @@ -116,6 +100,12 @@ public function jsonSerialize()
// '', // 4 sClientCert
'name' => $this->sName
];
if ($this->sSmtpLogin && $this->sSmtpPassword) {
$result['smtp'] = [
'user' => $this->sSmtpLogin,
'pass' => $this->sSmtpPassword
];
}
if ($this->sProxyAuthUser && $this->sProxyAuthPassword) {
$result['proxy'] = [
'user' => $this->sProxyAuthUser, // 5
Expand Down Expand Up @@ -200,19 +190,25 @@ public static function NewInstanceFromTokenArray(
if (isset($aAccountHash['name'])) {
$oAccount->sName = $aAccountHash['name'];
}
$oActions->Logger()->AddSecret($oAccount->sPassword);
// init smtp user/password
if (isset($aAccountHash['smtp'])) {
$oAccount->sSmtpLogin = $aAccountHash['smtp']['user'];
$oAccount->sSmtpPassword = $aAccountHash['smtp']['pass'];
$oActions->Logger()->AddSecret($oAccount->sSmtpPassword);
}
// init proxy user/password
if (isset($aAccountHash['proxy'])) {
$oAccount->sProxyAuthUser = $aAccountHash['proxy']['user'];
$oAccount->sProxyAuthPassword = $aAccountHash['proxy']['pass'];
$oActions->Logger()->AddSecret($oAccount->sProxyAuthPassword);
}
$oActions->Logger()->AddSecret($oAccount->IncPassword());
$oActions->Logger()->AddSecret($oAccount->ProxyAuthPassword());
}
}
return $oAccount;
}

public function ImapConnectAndLoginHelper(\RainLoop\Plugins\Manager $oPlugins, \MailSo\Imap\ImapClient $oImapClient, \RainLoop\Config\Application $oConfig) : bool
public function ImapConnectAndLogin(\RainLoop\Plugins\Manager $oPlugins, \MailSo\Imap\ImapClient $oImapClient, \RainLoop\Config\Application $oConfig) : bool
{
$oSettings = $this->Domain()->ImapSettings();
$oSettings->timeout = \max($oSettings->timeout, (int) $oConfig->Get('imap', 'timeout', $oSettings->timeout));
Expand All @@ -231,10 +227,11 @@ public function ImapConnectAndLoginHelper(\RainLoop\Plugins\Manager $oPlugins, \
$oImapClient->Connect($oSettings);
$oPlugins->RunHook('imap.after-connect', array($this, $oImapClient, $oSettings));

$oSettings->Password = $this->IncPassword();
return $this->netClientLogin($oImapClient, $oPlugins, $oSettings);
}

public function SmtpConnectAndLoginHelper(\RainLoop\Plugins\Manager $oPlugins, \MailSo\Smtp\SmtpClient $oSmtpClient, \RainLoop\Config\Application $oConfig, bool &$bUsePhpMail = false) : bool
public function SmtpConnectAndLogin(\RainLoop\Plugins\Manager $oPlugins, \MailSo\Smtp\SmtpClient $oSmtpClient, \RainLoop\Config\Application $oConfig, bool &$bUsePhpMail = false) : bool
{
$oSettings = $this->Domain()->SmtpSettings();
$oSettings->Login = $this->OutLogin();
Expand All @@ -249,11 +246,16 @@ public function SmtpConnectAndLoginHelper(\RainLoop\Plugins\Manager $oPlugins, \
$oSmtpClient->Connect($oSettings, $oSettings->Ehlo);
}
$oPlugins->RunHook('smtp.after-connect', array($this, $oSmtpClient, $oSettings));

/*
if ($this->oDomain->OutAskCredentials() && !($this->sSmtpPassword && $this->sSmtpLogin)) {
throw new RequireCredentialsException
}
*/
$oSettings->Password = $this->sSmtpPassword ?: $this->sPassword;
return $this->netClientLogin($oSmtpClient, $oPlugins, $oSettings);
}

public function SieveConnectAndLoginHelper(\RainLoop\Plugins\Manager $oPlugins, \MailSo\Sieve\SieveClient $oSieveClient, \RainLoop\Config\Application $oConfig)
public function SieveConnectAndLogin(\RainLoop\Plugins\Manager $oPlugins, \MailSo\Sieve\SieveClient $oSieveClient, \RainLoop\Config\Application $oConfig)
{
$oSettings = $this->Domain()->SieveSettings();
$oSettings->Login = $this->IncLogin();
Expand All @@ -262,6 +264,7 @@ public function SieveConnectAndLoginHelper(\RainLoop\Plugins\Manager $oPlugins,
$oSieveClient->Connect($oSettings);
$oPlugins->RunHook('sieve.after-connect', array($this, $oSieveClient, $oSettings));

$oSettings->Password = $this->IncPassword();
return $this->netClientLogin($oSieveClient, $oPlugins, $oSettings);
}

Expand All @@ -276,9 +279,8 @@ private function netClientLogin(\MailSo\Net\NetClient $oClient, \RainLoop\Plugin
[cipher_version] => TLSv1.3
)
*/
$oSettings->Password = $this->IncPassword();
$oSettings->ProxyAuthUser = $this->ProxyAuthUser();
$oSettings->ProxyAuthPassword = $this->ProxyAuthPassword();
$oSettings->ProxyAuthUser = $this->sProxyAuthUser;
$oSettings->ProxyAuthPassword = $this->sProxyAuthPassword;

$client_name = \strtolower($oClient->getLogName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public function asTokenArray(MainAccount $oMainAccount) : array
$sHash = $oMainAccount->CryptKey();
$aData = $this->jsonSerialize();
$aData['pass'] = \SnappyMail\Crypt::EncryptUrlSafe($aData['pass'], $sHash); // sPassword
if (isset($aAccountHash['proxy'])) {
if (!empty($aData['smtp']['pass'])) {
$aData['smtp']['pass'] = \SnappyMail\Crypt::EncryptUrlSafe($aData['smtp']['pass'], $sHash);
}
if (!empty($aData['proxy']['pass'])) {
$aData['proxy']['pass'] = \SnappyMail\Crypt::EncryptUrlSafe($aData['proxy']['pass'], $sHash); // sProxyAuthPassword
}
$aData['hmac'] = \hash_hmac('sha1', $aData['pass'], $sHash);
Expand All @@ -49,10 +52,23 @@ public static function NewInstanceFromTokenArray(
$sHash = $oActions->getMainAccountFromToken()->CryptKey();
// hmac only set when asTokenArray() was used
$sPasswordHMAC = $aAccountHash['hmac'] ?? null;
if ($sPasswordHMAC && $sPasswordHMAC === \hash_hmac('sha1', $aAccountHash['pass'], $sHash)) {
$aAccountHash['pass'] = \SnappyMail\Crypt::DecryptUrlSafe($aAccountHash['pass'], $sHash);
if (isset($aAccountHash['proxy'])) {
$aAccountHash['proxy']['pass'] = \SnappyMail\Crypt::DecryptUrlSafe($aAccountHash['proxy']['pass'], $sHash);
if ($sPasswordHMAC) {
if ($sPasswordHMAC === \hash_hmac('sha1', $aAccountHash['pass'], $sHash)) {
$aAccountHash['pass'] = \SnappyMail\Crypt::DecryptUrlSafe($aAccountHash['pass'], $sHash);
if (!empty($aData['smtp']['pass'])) {
$aAccountHash['smtp']['pass'] = \SnappyMail\Crypt::DecryptUrlSafe($aAccountHash['smtp']['pass'], $sHash);
}
if (!empty($aData['proxy']['pass'])) {
$aAccountHash['proxy']['pass'] = \SnappyMail\Crypt::DecryptUrlSafe($aAccountHash['proxy']['pass'], $sHash);
}
} else {
$aAccountHash['pass'] = '';
if (!empty($aData['smtp']['pass'])) {
$aAccountHash['smtp']['pass'] = '';
}
if (!empty($aData['proxy']['pass'])) {
$aAccountHash['proxy']['pass'] = '';
}
}
}
return parent::NewInstanceFromTokenArray($oActions, $aAccountHash, $bThrowExceptionOnFalse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function getConnection(\RainLoop\Model\Account $oAccount) : ?\MailSo\S
{
$oSieveClient = new \MailSo\Sieve\SieveClient();
$oSieveClient->SetLogger($this->oLogger);
return $oAccount->SieveConnectAndLoginHelper($this->oPlugins, $oSieveClient, $this->oConfig)
return $oAccount->SieveConnectAndLogin($this->oPlugins, $oSieveClient, $this->oConfig)
? $oSieveClient
: null;
}
Expand Down

0 comments on commit 61899ce

Please sign in to comment.