From 59e02ea7f6bd8bc9a4f16368118df41b99b204bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Cort=C3=A9s=20Cano?= <38475681+TrapoSAMA@users.noreply.github.com> Date: Wed, 7 Oct 2026 15:39:04 -0300 Subject: [PATCH] Restore SMTP delivery without authentication (#1174) fix regression in smtp_mail() so only do SMTP AUTH if $CONF['admin_smtp_password'] is a non-empty string (used by password recovery, broadcast message, mailbox post-creation etc) Thanks @TrapoSAMA --- CHANGELOG.TXT | 1 + functions.inc.php | 16 +++--- tests/SmtpTest.php | 86 +++++++++++++++++++++++++++++ tests/fixtures/OptionalAuthSmtp.php | 75 +++++++++++++++++++++++++ 4 files changed, 171 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/OptionalAuthSmtp.php diff --git a/CHANGELOG.TXT b/CHANGELOG.TXT index c8155896..d7be49cc 100644 --- a/CHANGELOG.TXT +++ b/CHANGELOG.TXT @@ -9,6 +9,7 @@ Changes in 'master' (not in a release yet) (20XX/XX/XX) -------------------------- + - Restore SMTP delivery without authentication when admin_smtp_password is empty. - Show the configured TOTP status and Reset TOTP action without reserving space for the hidden form (see #1166, thanks @TrapoSAMA) - Distinguish unknown local alias destinations from external destinations without inspecting other administrators' domains (see discussion #886, thanks @TrapoSAMA) - Show available usage for unlimited quotas and retain neutral quota boxes. (see #1151, thanks @TrapoSAMA) diff --git a/functions.inc.php b/functions.inc.php index 9299ce38..055f6486 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -1395,13 +1395,15 @@ function smtp_mail(string $to, string $from, string $subject_or_data, ?string $b smtp_require_response($socket, 250); } - // AUTH - smtp_write($socket, "AUTH LOGIN\r\n"); - smtp_require_response($socket, 334); - smtp_write($socket, base64_encode($username) . "\r\n"); - smtp_require_response($socket, 334); - smtp_write($socket, base64_encode($password) . "\r\n"); - smtp_require_response($socket, 235); + // An empty password explicitly disables SMTP authentication. + if ($password !== '') { + smtp_write($socket, "AUTH LOGIN\r\n"); + smtp_require_response($socket, 334); + smtp_write($socket, base64_encode($username) . "\r\n"); + smtp_require_response($socket, 334); + smtp_write($socket, base64_encode($password) . "\r\n"); + smtp_require_response($socket, 235); + } // SMTP FLOW smtp_write($socket, "MAIL FROM:<$from>\r\n"); diff --git a/tests/SmtpTest.php b/tests/SmtpTest.php index d26230fe..9c6cf15f 100644 --- a/tests/SmtpTest.php +++ b/tests/SmtpTest.php @@ -33,4 +33,90 @@ class SmtpTest extends TestCase $this->expectException(RuntimeException::class); smtp_write($stream, "EHLO example.com\r\n"); } + + public static function smtpAuthenticationCases(): array + { + $cases = []; + foreach ([false, true] as $raw) { + foreach ([ + 'no authentication' => ['', 'none', true], + 'configured authentication' => ['fixture-secret', 'accept', true], + 'zero password' => ['0', 'accept', true], + 'rejected authentication' => ['fixture-secret', 'reject', false], + 'rejected recipient' => ['', 'recipient-reject', false], + ] as $name => [$password, $mode, $success]) { + $cases[$name . ($raw ? ' raw message' : ' subject and body')] = [$password, $mode, $success, $raw]; + } + } + return $cases; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('smtpAuthenticationCases')] + public function testSmtpAuthenticationIsOptional(string $password, string $mode, bool $success, bool $raw): void + { + $originalConf = $GLOBALS['CONF']; + $process = proc_open( + [PHP_BINARY, __DIR__ . '/fixtures/OptionalAuthSmtp.php', $mode], + [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], + $pipes + ); + $this->assertIsResource($process); + fclose($pipes[0]); + stream_set_timeout($pipes[1], 15); + + try { + $port = (int)fgets($pipes[1]); + $this->assertGreaterThan(0, $port); + $GLOBALS['CONF'] = array_replace($originalConf, [ + 'smtp_server' => '127.0.0.1', + 'smtp_port' => $port, + 'smtp_type' => 'plain', + 'smtp_client' => 'fixture.example.invalid', + 'smtp_username' => 'fixture-user', + 'admin_smtp_password' => $password, + ]); + $to = 'recipient@example.invalid'; + $from = 'sender@example.invalid'; + $subject = 'SMTP regression'; + $body = 'Welcome fixture body'; + if ($raw) { + $result = smtp_mail($to, $from, "Subject: $subject\r\n\r\n$body"); + } else { + $result = smtp_mail($to, $from, $subject, $body); + } + $this->assertSame($success, $result); + $capture = json_decode(stream_get_contents($pipes[1]), true, 512, JSON_THROW_ON_ERROR); + if ($password === '') { + $this->assertNotContains('AUTH LOGIN', $capture['commands']); + $this->assertSame('', $capture['auth_user']); + $this->assertSame('', $capture['auth_password']); + } else { + $this->assertContains('AUTH LOGIN', $capture['commands']); + if ($mode === 'accept') { + $this->assertSame(base64_encode('fixture-user'), $capture['auth_user']); + $this->assertSame(base64_encode($password), $capture['auth_password']); + } + } + if ($success) { + $this->assertContains("MAIL FROM:<$from>", $capture['commands']); + $this->assertContains("RCPT TO:<$to>", $capture['commands']); + $this->assertStringContainsString("Subject: $subject", $capture['data']); + $this->assertStringContainsString($body, $capture['data']); + $this->assertStringNotContainsString('fixture-secret', $capture['data']); + $this->assertStringNotContainsString(base64_encode('fixture-secret'), $capture['data']); + } else { + $this->assertNotContains('DATA', $capture['commands']); + $this->assertSame('', $capture['data']); + if ($mode === 'reject') { + $this->assertNotContains("MAIL FROM:<$from>", $capture['commands']); + } + } + } finally { + $GLOBALS['CONF'] = $originalConf; + fclose($pipes[1]); + fclose($pipes[2]); + proc_terminate($process); + proc_close($process); + } + } } diff --git a/tests/fixtures/OptionalAuthSmtp.php b/tests/fixtures/OptionalAuthSmtp.php new file mode 100644 index 00000000..4ed6601f --- /dev/null +++ b/tests/fixtures/OptionalAuthSmtp.php @@ -0,0 +1,75 @@ + [], 'auth_user' => '', 'auth_password' => '', 'data' => '']; +$state = 'command'; +$authenticated = false; +while (($line = fgets($client)) !== false) { + $command = rtrim($line, "\r\n"); + if ($state === 'auth_user') { + $capture['auth_user'] = $command; + $state = 'auth_password'; + fwrite($client, "334 UGFzc3dvcmQ6\r\n"); + continue; + } + if ($state === 'auth_password') { + $capture['auth_password'] = $command; + $state = 'command'; + $authenticated = true; + fwrite($client, "235 fixture authenticated\r\n"); + continue; + } + if ($state === 'data') { + if ($command === '.') { + $state = 'command'; + fwrite($client, "250 fixture accepted\r\n"); + } else { + $capture['data'] .= $line; + } + continue; + } + + $capture['commands'][] = $command; + if (str_starts_with($command, 'EHLO ')) { + fwrite($client, "250 fixture ready\r\n"); + } elseif ($command === 'AUTH LOGIN') { + if ($mode === 'accept') { + $state = 'auth_user'; + fwrite($client, "334 VXNlcm5hbWU6\r\n"); + } else { + fwrite($client, "503 authentication unavailable\r\n"); + } + } elseif (str_starts_with($command, 'MAIL FROM:')) { + $response = $mode === 'accept' && !$authenticated ? '530 authentication required' : '250 sender accepted'; + fwrite($client, $response . "\r\n"); + } elseif (str_starts_with($command, 'RCPT TO:')) { + $response = $mode === 'recipient-reject' ? '550 recipient rejected' : '250 recipient accepted'; + fwrite($client, $response . "\r\n"); + } elseif ($command === 'DATA') { + $state = 'data'; + fwrite($client, "354 fixture data\r\n"); + } elseif ($command === 'QUIT') { + fwrite($client, "221 fixture goodbye\r\n"); + break; + } else { + fwrite($client, "500 fixture unsupported command\r\n"); + } +} +fclose($client); +fclose($server); +echo json_encode($capture, JSON_THROW_ON_ERROR) . "\n";