mailwolt/app/Livewire/Ui/Domain/DkimStatus.php

94 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Livewire\Ui\Domain;
use App\Models\Domain;
use App\Services\DkimService;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Process;
use Livewire\Component;
class DkimStatus extends Component
{
public Domain $domain;
public ?string $selector = null;
public function mount(Domain $domain, ?string $selector = null): void
{
$this->domain = $domain;
$this->selector = $selector
?: optional($domain->dkimKeys()->where('is_active', true)->latest()->first())->selector
?: (string) config('mailpool.defaults.dkim_selector', 'mwl1');
}
/** interne Normalisierung nur fürs Dateisystem */
protected function safeKey(string|Domain $value, int $len = 64): string
{
$val = $value instanceof Domain ? $value->domain : (string) $value;
return preg_replace('/[^a-zA-Z0-9_.-]+/', '_', substr($val, 0, $len));
}
/** Minimalcheck: existiert .private + stehts in KeyTable & SigningTable? */
protected function isDkimReady(string $domain, string $selector): bool
{
$hasFile = is_readable("/etc/opendkim/keys/{$domain}/{$selector}.private");
$keyTab = is_readable('/etc/opendkim/KeyTable')
? (@file_get_contents('/etc/opendkim/KeyTable') ?: '')
: '';
$signTab = is_readable('/etc/opendkim/SigningTable')
? (@file_get_contents('/etc/opendkim/SigningTable') ?: '')
: '';
$inKey = (bool) preg_match(
"/^{$selector}\._domainkey\.{$domain}\s+{$domain}:{$selector}:/m",
$keyTab
);
$inSign = (bool) preg_match(
"/^\*\@{$domain}\s+{$selector}\._domainkey\.{$domain}\s*$/m",
$signTab
);
return $hasFile && $inKey && $inSign;
}
/** Button: (re)generieren + Helper einhängen + OpenDKIM reload */
public function regenerate(?string $selector = null): void
{
$selector = $selector ?: ($this->selector ?: (string) config('mailpool.defaults.dkim_selector', 'mwl1'));
try {
/** @var DkimService $svc */
$svc = app(DkimService::class);
// 1) Key erzeugen/auffrischen deine Funktion macht bereits alles Nötige:
// - schreibt .pem/.pub/.private/.txt in storage
// - pflegt DB (DkimKey)
// - ruft /usr/local/sbin/mailwolt-install-dkim auf
// - reloadet opendkim
$res = $svc->generateForDomain($this->domain, 2048, $selector);
// 2) Sicherstellen, dass der Helper wirklich geladen hat (idempotent):
Process::run(['systemctl', 'reload', 'opendkim']);
// 3) Status checken & Feedback
$ok = $this->isDkimReady($this->domain->domain, $selector);
$this->dispatch('toast',
type: $ok ? 'success' : 'warning',
message: $ok ? 'DKIM ist aktiv.' : 'DKIM generiert OpenDKIM/Tabellen prüfen.');
} catch (\Throwable $e) {
$this->dispatch('toast', type: 'error', message: 'DKIM Fehler: '.$e->getMessage());
}
// ggf. Selector für UI festhalten
$this->selector = $selector;
}
public function render(): View
{
$dkimOk = $this->isDkimReady($this->domain->domain, $this->selector ?: (string) config('mailpool.defaults.dkim_selector', 'mwl1'));
return view('livewire.ui.domain.dkim-status', compact('dkimOk'));
}
}