mirror of
https://github.com/postfixadmin/postfixadmin.git
synced 2026-11-20 23:43:26 +03:00
Show DNS status for domains to administrators. Adds new config setting domain_dns_status_check ( 0 - disabled, 1 authoritative zone, 2 usable MX ) Document installation, an optional CLI/cron command, permissions, networking, timeouts, and saved-result semantics. thanks @TrapoSAMA
179 lines
6.4 KiB
PHP
179 lines
6.4 KiB
PHP
<?php
|
|
|
|
class DomainDnsStatusTest extends \PHPUnit\Framework\TestCase
|
|
{
|
|
public function testDnsStatusMigrationIsRetrySafe(): void
|
|
{
|
|
upgrade_1857();
|
|
upgrade_1857();
|
|
|
|
self::assertTrue(_db_field_exists(table_by_key('domain'), 'dns_active'));
|
|
self::assertTrue(_db_field_exists(table_by_key('domain'), 'dns_checked'));
|
|
self::assertNull((new DomainHandler())->getStruct()['dns_checked']['default']);
|
|
self::assertSame(0, DomainDnsStatus::CHECK_DISABLED);
|
|
self::assertSame(1, DomainDnsStatus::CHECK_ZONE);
|
|
self::assertSame(2, DomainDnsStatus::CHECK_MX);
|
|
}
|
|
|
|
public function testDomainIsActiveWhenAnAuthoritativeNameserverResponds(): void
|
|
{
|
|
$checker = new FakeDomainDnsStatus(
|
|
['example.com' => ['ns1.example.net', 'ns2.example.net']],
|
|
['ns1.example.net' => ['192.0.2.1'], 'ns2.example.net' => ['192.0.2.2']],
|
|
['192.0.2.2']
|
|
);
|
|
self::assertTrue($checker->isActive('example.com'));
|
|
}
|
|
|
|
public function testDomainIsInactiveWithoutDelegation(): void
|
|
{
|
|
self::assertFalse((new FakeDomainDnsStatus([], [], []))->isActive('example.com'));
|
|
}
|
|
|
|
public function testDomainIsInactiveWhenNoAuthoritativeNameserverResponds(): void
|
|
{
|
|
$checker = new FakeDomainDnsStatus(
|
|
['example.com' => ['ns1.example.net']],
|
|
['ns1.example.net' => ['192.0.2.1']],
|
|
[]
|
|
);
|
|
self::assertFalse($checker->isActive('example.com'));
|
|
}
|
|
|
|
public function testDomainIsActiveWhenMxTargetHasAnAddress(): void
|
|
{
|
|
$checker = new FakeDomainDnsStatus(
|
|
[],
|
|
['mx.example.net' => ['192.0.2.25']],
|
|
[],
|
|
2,
|
|
['example.com' => ['mx.example.net']]
|
|
);
|
|
self::assertTrue($checker->isActive('example.com'));
|
|
}
|
|
|
|
public function testDomainIsInactiveWithoutUsableMx(): void
|
|
{
|
|
$checker = new FakeDomainDnsStatus([], [], [], 2, ['example.com' => ['mx.example.net']]);
|
|
self::assertFalse($checker->isActive('example.com'));
|
|
}
|
|
|
|
public function testDisabledModeDoesNotCheckOrPersistDomains(): void
|
|
{
|
|
$checker = new FakeDomainDnsStatus([], [], [], 0);
|
|
self::assertSame(['active' => 0, 'inactive' => 0], $checker->refresh(['example.com']));
|
|
self::assertFalse($checker->isActive('example.com'));
|
|
}
|
|
|
|
public function testRefreshPersistsStatusAndInactiveCount(): void
|
|
{
|
|
$active = 'dns-active-' . uniqid() . '.example';
|
|
$inactive = 'dns-inactive-' . uniqid() . '.example';
|
|
db_insert('domain', ['domain' => $active, 'description' => 'test', 'transport' => '']);
|
|
db_insert('domain', ['domain' => $inactive, 'description' => 'test', 'transport' => '']);
|
|
|
|
try {
|
|
$checker = new FakeDomainDnsStatus(
|
|
[$active => ['ns.example.net'], $inactive => []],
|
|
['ns.example.net' => ['192.0.2.1']],
|
|
['192.0.2.1']
|
|
);
|
|
self::assertSame(['active' => 1, 'inactive' => 1], $checker->refresh([$active, $inactive]));
|
|
$rows = db_query_all(
|
|
'SELECT dns_checked FROM domain WHERE domain IN (:active, :inactive)',
|
|
['active' => $active, 'inactive' => $inactive]
|
|
);
|
|
self::assertCount(2, $rows);
|
|
self::assertNotEmpty($rows[0]['dns_checked']);
|
|
self::assertNotEmpty($rows[1]['dns_checked']);
|
|
|
|
$_SESSION = ['sessid' => ['roles' => ['global-admin']]];
|
|
$handler = new DomainHandler();
|
|
self::assertSame(1, $handler->countInactiveDns());
|
|
self::assertTrue($handler->getList(['dns_active' => 0]));
|
|
self::assertArrayHasKey($inactive, $handler->result());
|
|
self::assertArrayNotHasKey($active, $handler->result());
|
|
} finally {
|
|
db_delete('domain', 'domain', $active);
|
|
db_delete('domain', 'domain', $inactive);
|
|
}
|
|
}
|
|
|
|
public function testHandlerReturnsStatusAndOldestCompleteCheck(): void
|
|
{
|
|
$domain = 'dns-group-' . uniqid() . '.example';
|
|
db_insert('domain', ['domain' => $domain, 'description' => 'test', 'transport' => '']);
|
|
try {
|
|
$handler = new DomainHandler();
|
|
self::assertSame('', $handler->oldestDnsCheck());
|
|
$checker = new FakeDomainDnsStatus([], [], []);
|
|
$checker->refresh([$domain]);
|
|
self::assertNotEmpty($handler->oldestDnsCheck());
|
|
$saved = $handler->dnsStatus($domain);
|
|
self::assertSame(0, $saved['dns_active']);
|
|
self::assertNotEmpty($saved['dns_checked']);
|
|
} finally {
|
|
db_delete('domain', 'domain', $domain);
|
|
}
|
|
}
|
|
|
|
public function testFailedRefreshDoesNotPersistStatus(): void
|
|
{
|
|
$checker = new class (0.01, 1) extends DomainDnsStatus {
|
|
public function isActive(string $domain): bool
|
|
{
|
|
throw new RuntimeException('Simulated interrupted check');
|
|
}
|
|
};
|
|
$domain = 'interrupted-' . uniqid() . '.example';
|
|
try {
|
|
$checker->refresh([$domain]);
|
|
self::fail('The check must fail');
|
|
} catch (RuntimeException $exception) {
|
|
self::assertSame('Simulated interrupted check', $exception->getMessage());
|
|
$handler = new DomainHandler();
|
|
self::assertSame('', $handler->oldestDnsCheck());
|
|
}
|
|
}
|
|
|
|
public function testStatusReadRejectsUnauthorizedDomain(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$handler = new DomainHandler(0, 'nobody@example.test');
|
|
$handler->dnsStatus('outside.example');
|
|
}
|
|
}
|
|
|
|
class FakeDomainDnsStatus extends DomainDnsStatus
|
|
{
|
|
public function __construct(
|
|
private array $ns,
|
|
private array $addresses,
|
|
private array $respondingAddresses,
|
|
int $mode = 1,
|
|
private array $mx = []
|
|
) {
|
|
parent::__construct(0.01, $mode);
|
|
}
|
|
|
|
protected function nameservers(string $domain): array
|
|
{
|
|
return $this->ns[$domain] ?? [];
|
|
}
|
|
|
|
protected function nameserverAddresses(string $nameserver): array
|
|
{
|
|
return $this->addresses[$nameserver] ?? [];
|
|
}
|
|
|
|
protected function mxTargets(string $domain): array
|
|
{
|
|
return $this->mx[$domain] ?? [];
|
|
}
|
|
|
|
protected function authoritativeServerResponds(string $domain, string $address): bool
|
|
{
|
|
return in_array($address, $this->respondingAddresses, true);
|
|
}
|
|
}
|