mirror of
https://github.com/postfixadmin/postfixadmin.git
synced 2026-11-20 23:43:26 +03:00
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
This commit is contained in:
1 parent
047bf192fb
commit
59e02ea7f6
4 files changed
+171
-7
No files matched your search
@@ -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)
|
||||
|
||||
+9
-7
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+75
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
$mode = $argv[1];
|
||||
$server = stream_socket_server('tcp://127.0.0.1:0', $errno, $error);
|
||||
if ($server === false) {
|
||||
fwrite(STDERR, "Unable to start fixture SMTP server\n");
|
||||
exit(1);
|
||||
}
|
||||
$address = stream_socket_get_name($server, false);
|
||||
echo substr($address, strrpos($address, ':') + 1) . "\n";
|
||||
flush();
|
||||
$client = stream_socket_accept($server, 15);
|
||||
if ($client === false) {
|
||||
fclose($server);
|
||||
exit(1);
|
||||
}
|
||||
stream_set_timeout($client, 15);
|
||||
fwrite($client, "220 fixture ready\r\n");
|
||||
$capture = ['commands' => [], '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";
|
||||
Reference in new issue
Block a user