87 lines
2.7 KiB
PHP
87 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Domain;
|
|
use App\Models\DkimKey;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class InstallDkimKey implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public function __construct(
|
|
public int $domainId,
|
|
public int $dkimKeyId,
|
|
public string $privPath,
|
|
public string $dnsTxtContent,
|
|
public string $selector = 'mwl1',
|
|
) {}
|
|
|
|
public function handle(): void
|
|
{
|
|
$domain = Domain::findOrFail($this->domainId);
|
|
$dk = DkimKey::findOrFail($this->dkimKeyId);
|
|
|
|
// ABSOLUTER, kanonischer Pfad (falls irgendwo ../ o.ä. reinkommt)
|
|
$priv = realpath($this->privPath) ?: $this->privPath;
|
|
|
|
if (!is_readable($priv)) {
|
|
throw new \RuntimeException("DKIM private key missing or unreadable: {$priv}");
|
|
}
|
|
|
|
// TXT in Tempfile schreiben (damit der Helper optional nach /etc/mailwolt/dns kopieren kann)
|
|
$tmpTxt = tempnam(sys_get_temp_dir(), 'dkim_txt_');
|
|
if ($tmpTxt === false) {
|
|
throw new \RuntimeException('Failed to create temporary TXT file for DKIM.');
|
|
}
|
|
file_put_contents($tmpTxt, (string)$this->dnsTxtContent);
|
|
|
|
// sudo-Helper aufrufen
|
|
$cmd = [
|
|
'sudo', '/usr/local/sbin/mailwolt-install-dkim',
|
|
$domain->domain, // DOMAIN
|
|
$dk->selector, // SELECTOR
|
|
$priv, // Private PEM (absolut)
|
|
$tmpTxt, // TXT-Content-Datei
|
|
];
|
|
|
|
$proc = new Process($cmd, base_path());
|
|
$proc->setTimeout(60); // OpenDKIM/IO kann auf langsamen Platten mal dauern
|
|
$proc->run();
|
|
|
|
@unlink($tmpTxt);
|
|
|
|
if (!$proc->isSuccessful()) {
|
|
$exit = $proc->getExitCode();
|
|
$out = trim($proc->getOutput());
|
|
$err = trim($proc->getErrorOutput());
|
|
|
|
Log::error('DKIM install failed', [
|
|
'domain' => $domain->domain,
|
|
'selector' => $dk->selector,
|
|
'priv' => $priv,
|
|
'exit' => $exit,
|
|
'out' => $out,
|
|
'err' => $err,
|
|
]);
|
|
|
|
throw new \RuntimeException(
|
|
"mailwolt-install-dkim failed (rc={$exit})\nSTDOUT: {$out}\nSTDERR: {$err}"
|
|
);
|
|
}
|
|
|
|
Log::info('DKIM installed', [
|
|
'domain' => $domain->domain,
|
|
'selector' => $dk->selector,
|
|
'priv' => $priv,
|
|
]);
|
|
}
|
|
}
|