117 lines
4.0 KiB
PHP
117 lines
4.0 KiB
PHP
<?php
|
||
|
||
namespace Database\Seeders;
|
||
|
||
use App\Models\DkimKey;
|
||
use App\Models\Domain;
|
||
use App\Models\MailUser;
|
||
use App\Services\DnsRecordService;
|
||
use Illuminate\Database\Seeder;
|
||
|
||
class SystemDomainSeeder extends Seeder
|
||
{
|
||
public function run(): void
|
||
{
|
||
$base = config('mailpool.platform_zone', 'example.com');
|
||
if (!$base || $base === 'example.com') {
|
||
$this->command->warn("BASE_DOMAIN ist 'example.com' – Seeder überspringt produktive Einträge.");
|
||
return;
|
||
}
|
||
|
||
$systemSub = config('mailpool.platform_system_zone');
|
||
$base = "{$systemSub}.{$base}";
|
||
|
||
// Domain anlegen/holen
|
||
$domain = Domain::firstOrCreate(
|
||
['domain' => $base],
|
||
['is_active' => true, 'is_system' => true]
|
||
);
|
||
|
||
// System Absender (no-reply) – ohne Passwort (kein Login)
|
||
MailUser::firstOrCreate(
|
||
['email' => "no-reply@{$base}"],
|
||
[
|
||
'domain_id' => $domain->id,
|
||
'localpart' => 'no-reply',
|
||
'password_hash' => null,
|
||
'is_active' => true,
|
||
'is_system' => true,
|
||
'must_change_pw' => false,
|
||
'quota_mb' => 0,
|
||
]
|
||
);
|
||
|
||
// DKIM – Key erzeugen, falls keiner aktiv existiert
|
||
if (! $domain->dkimKeys()->where('is_active', true)->exists()) {
|
||
[$privPem, $pubTxt] = $this->generateDkimKeyPair();
|
||
$selector = 'mwl1'; // frei wählbar, z. B. rotierend später
|
||
|
||
DkimKey::create([
|
||
'domain_id' => $domain->id,
|
||
'selector' => $selector,
|
||
'private_key_pem'=> $privPem,
|
||
'public_key_txt' => $pubTxt,
|
||
'is_active' => true,
|
||
]);
|
||
|
||
$this->command->info("DKIM angelegt: Host = {$selector}._domainkey.{$base}");
|
||
}
|
||
|
||
$dk = $domain->dkimKeys()->where('is_active', true)->latest()->first();
|
||
$dkimTxt = $dk ? "v=DKIM1; k=rsa; p={$dk->public_key_txt}" : null;
|
||
|
||
app(DnsRecordService::class)->provision(
|
||
$domain,
|
||
dkimSelector: $dk?->selector,
|
||
dkimTxt: $dkimTxt,
|
||
opts: [
|
||
'dmarc_policy' => 'none',
|
||
'spf_tail' => '-all',
|
||
// optional: 'ipv4' => $serverIp, 'ipv6' => ...
|
||
]
|
||
);
|
||
|
||
$this->command->info("System-Domain '{$base}' fertig. SPF/DMARC/DKIM & DNS-Empfehlungen eingetragen.");
|
||
$this->printDnsHints($domain);
|
||
}
|
||
|
||
/** @return array{0:string privatePem,1:string publicTxt} */
|
||
private function generateDkimKeyPair(): array
|
||
{
|
||
$res = openssl_pkey_new([
|
||
'private_key_bits' => 2048,
|
||
'private_key_type' => OPENSSL_KEYTYPE_RSA,
|
||
]);
|
||
openssl_pkey_export($res, $privateKeyPem);
|
||
$details = openssl_pkey_get_details($res);
|
||
// $details['key'] ist PEM, wir brauchen Base64 ohne Header/Footers
|
||
$pubDer = $details['key'];
|
||
// Public PEM zu "p=" Wert (reines Base64) normalisieren
|
||
$pubTxt = trim(preg_replace('/-----(BEGIN|END) PUBLIC KEY-----|\s+/', '', $pubDer));
|
||
|
||
return [$privateKeyPem, $pubTxt];
|
||
}
|
||
|
||
private function printDnsHints(Domain $domain): void
|
||
{
|
||
$base = $domain->domain;
|
||
$dkim = $domain->dkimKeys()->where('is_active', true)->latest()->first();
|
||
if ($dkim) {
|
||
$this->command->line(" • DKIM TXT @ {$dkim->selector}._domainkey.{$base}");
|
||
$this->command->line(" v=DKIM1; k=rsa; p={$dkim->public_key_txt}");
|
||
}
|
||
|
||
$spf = $domain->spf()->where('is_active', true)->latest()->first();
|
||
if ($spf) {
|
||
$this->command->line(" • SPF TXT @ {$base}");
|
||
$this->command->line(" {$spf->record_txt}");
|
||
}
|
||
|
||
$dmarc = $domain->dmarc()->where('is_active', true)->latest()->first();
|
||
if ($dmarc) {
|
||
$this->command->line(" • DMARC TXT @ _dmarc.{$base}");
|
||
$this->command->line(" " . ($dmarc->record_txt ?? $dmarc->renderTxt()));
|
||
}
|
||
}
|
||
}
|