From fce4f43833aadc17a5103e7ae489afa02578e55b Mon Sep 17 00:00:00 2001 From: boban Date: Mon, 27 Apr 2026 20:15:21 +0200 Subject: [PATCH] Fix: mailwolt-apply-hostname Script + myhostname/aliases in apply-domains integriert --- app/Livewire/Ui/System/SettingsForm.php | 27 ++--------------- scripts/mailwolt-apply-domains | 27 ++++++++++++++++- scripts/mailwolt-apply-hostname | 39 +++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 scripts/mailwolt-apply-hostname diff --git a/app/Livewire/Ui/System/SettingsForm.php b/app/Livewire/Ui/System/SettingsForm.php index c1c7959..84f47e2 100644 --- a/app/Livewire/Ui/System/SettingsForm.php +++ b/app/Livewire/Ui/System/SettingsForm.php @@ -507,31 +507,8 @@ class SettingsForm extends Component $safeHost = preg_replace('/[^a-zA-Z0-9.\-]/', '', $mailHost); if (!$safeHost) return; - $destination = "{$safeHost}, localhost.localdomain, localhost"; - - @shell_exec('sudo -n /usr/sbin/postconf -e ' . escapeshellarg("myhostname={$safeHost}") . ' 2>/dev/null'); - @shell_exec('sudo -n /usr/sbin/postconf -e ' . escapeshellarg("myorigin={$safeHost}") . ' 2>/dev/null'); - @shell_exec('sudo -n /usr/sbin/postconf -e ' . escapeshellarg("mydestination={$destination}") . ' 2>/dev/null'); - @shell_exec('sudo -n /usr/sbin/postfix reload 2>/dev/null'); - - // root-Alias → /dev/null verhindert Bounce-Schleife bei System-Mails von www-data - $this->sudoTee('/etc/aliases', "# Managed by Mailwolt\npostmaster: root\nroot: /dev/null\nclamav: /dev/null\n"); - @shell_exec('sudo -n /usr/bin/newaliases 2>/dev/null'); - } - - private function sudoTee(string $target, string $content): void - { - $proc = proc_open( - sprintf('sudo -n /usr/bin/tee %s >/dev/null', escapeshellarg($target)), - [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], - $pipes - ); - if (!is_resource($proc)) return; - fwrite($pipes[0], $content); - fclose($pipes[0]); - stream_get_contents($pipes[1]); - stream_get_contents($pipes[2]); - proc_close($proc); + $helper = '/usr/local/sbin/mailwolt-apply-hostname'; + @shell_exec(sprintf('sudo -n %s %s 2>/dev/null', escapeshellarg($helper), escapeshellarg($safeHost))); } public function render() diff --git a/scripts/mailwolt-apply-domains b/scripts/mailwolt-apply-domains index 58bf2fe..43cda5a 100755 --- a/scripts/mailwolt-apply-domains +++ b/scripts/mailwolt-apply-domains @@ -270,11 +270,36 @@ if [ -d "${STATE_DIR}" ]; then fi # --- Phase 4: Postfix + Dovecot mit Mail-Domain-Zertifikat konfigurieren --- +if [ -n "${MAIL_HOST}" ]; then + # Hostname immer setzen, unabhängig vom Zertifikat + if command -v postconf >/dev/null 2>&1; then + postconf -e "myhostname = ${MAIL_HOST}" + postconf -e "myorigin = ${MAIL_HOST}" + postconf -e "mydestination = ${MAIL_HOST}, localhost.localdomain, localhost" + fi + + # /etc/aliases bereinigen: root → /dev/null verhindert Bounce-Schleife + if [ -f /etc/aliases ]; then + # Vorhandene root/clamav-Zeilen ersetzen oder am Ende ergänzen + if grep -q '^root:' /etc/aliases; then + sed -i 's|^root:.*|root: /dev/null|' /etc/aliases + else + echo 'root: /dev/null' >> /etc/aliases + fi + if grep -q '^clamav:' /etc/aliases; then + sed -i 's|^clamav:.*|clamav: /dev/null|' /etc/aliases + else + echo 'clamav: /dev/null' >> /etc/aliases + fi + command -v newaliases >/dev/null 2>&1 && newaliases + fi +fi + if [ -n "${MAIL_HOST}" ] && [ "${MAIL_HAS_CERT}" = "1" ]; then MAIL_CERT="/etc/letsencrypt/live/${MAIL_HOST}/fullchain.pem" MAIL_KEY="/etc/letsencrypt/live/${MAIL_HOST}/privkey.pem" - # Postfix + # Postfix TLS if command -v postconf >/dev/null 2>&1; then postconf -e "smtpd_tls_cert_file = ${MAIL_CERT}" postconf -e "smtpd_tls_key_file = ${MAIL_KEY}" diff --git a/scripts/mailwolt-apply-hostname b/scripts/mailwolt-apply-hostname new file mode 100644 index 0000000..14852ba --- /dev/null +++ b/scripts/mailwolt-apply-hostname @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# Setzt Postfix myhostname/myorigin und bereinigt /etc/aliases. +# Wird von Mailwolt aufgerufen wenn die Mail-Domain gespeichert wird. +set -euo pipefail + +MAIL_HOST="${1:-}" +if [ -z "${MAIL_HOST}" ]; then + echo "Usage: mailwolt-apply-hostname " >&2 + exit 1 +fi + +# Nur sichere Hostnamen zulassen +if ! echo "${MAIL_HOST}" | grep -qE '^[a-zA-Z0-9][a-zA-Z0-9.\-]+$'; then + echo "Ungültiger Hostname: ${MAIL_HOST}" >&2 + exit 1 +fi + +if command -v postconf >/dev/null 2>&1; then + postconf -e "myhostname = ${MAIL_HOST}" + postconf -e "myorigin = ${MAIL_HOST}" + postconf -e "mydestination = ${MAIL_HOST}, localhost.localdomain, localhost" + systemctl reload postfix 2>/dev/null || true +fi + +if [ -f /etc/aliases ]; then + if grep -q '^root:' /etc/aliases; then + sed -i 's|^root:.*|root: /dev/null|' /etc/aliases + else + echo 'root: /dev/null' >> /etc/aliases + fi + if grep -q '^clamav:' /etc/aliases; then + sed -i 's|^clamav:.*|clamav: /dev/null|' /etc/aliases + else + echo 'clamav: /dev/null' >> /etc/aliases + fi + command -v newaliases >/dev/null 2>&1 && newaliases +fi + +echo "mailwolt-apply-hostname fertig"