Hi,
This could be a way to get real enforcement, but please treat it as an idea / starting point, not a finished solution. I tested the script logic only against a mocked "plesk" command, never on a real Plesk server. The plesk CLI/DB parts are unverified assumptions. Dry run is the default, so check the output first and try it on a non-production box.
Why not Postfix: smtpd restrictions only see the original recipient, not the target after alias expansion. Removing external targets with the Plesk CLI avoids that.
What it does:
- reads all domains and domain aliases hosted on this server
- reads all forwarding targets from the Plesk DB
- reports (dry run) or removes (--apply) targets whose domain is not hosted here
- skips targets listed in an optional whitelist
- never touches targets with unusual characters (e.g. unicode domains), it only reports them
- aborts if a DB query fails or returns no domains
- refuses --apply if more than MAX_REMOVE (default 25) targets would be removed
- writes a list of everything it removed to /var/backups/check-forwards/
1) Verify the two assumptions first (should print rows, not errors, and the data should look right):
Code:
plesk db -Ne "SELECT name FROM domain_aliases LIMIT 3"
plesk db -Ne "SELECT CONCAT(m.mail_name,'@',d.name), mr.address FROM mail m JOIN domains d ON d.id=m.dom_id JOIN mail_redir mr ON mr.mn_id=m.id WHERE m.mail_group='true' LIMIT 5"
Better: create a test mailbox, forward it to one internal and one external address in the GUI, and check that exactly these two rows show up.
2) Save as /usr/local/sbin/check-forwards.sh, chmod 700:
Code:
#!/bin/bash
# check-forwards.sh - remove forwarding targets that are not hosted on this server
# Default is a dry run. Nothing is changed without --apply.
export PATH="$PATH:/usr/local/sbin:/usr/sbin:/usr/local/bin:/usr/bin:/sbin:/bin"
ALLOW=${ALLOW:-/etc/check-forwards.allow} # optional whitelist: one domain or full address per line
BACKUP_DIR=${BACKUP_DIR:-/var/backups/check-forwards}
LOCK=${LOCK:-/var/lock/check-forwards.lock}
MAX_REMOVE=${MAX_REMOVE:-25} # --apply refuses to remove more targets than this
APPLY=0
[ "$1" = "--apply" ] && APPLY=1
log() { echo "$(date -Is) $*"; }
exec 9>"$LOCK"
flock -n 9 || { log "already running"; exit 0; }
# Read everything first. Any error or empty domain list aborts, so a failed
# query can never make all targets look "external".
DOMS=$(plesk db -Ne "SELECT name FROM domains UNION SELECT name FROM domain_aliases") \
|| { log "ERROR: domain query failed, aborting"; exit 1; }
[ -n "$DOMS" ] || { log "ERROR: no local domains read, aborting"; exit 1; }
FWD=$(plesk db -Ne "SELECT CONCAT(m.mail_name,'@',d.name), mr.address FROM mail m JOIN domains d ON d.id=m.dom_id JOIN mail_redir mr ON mr.mn_id=m.id WHERE m.mail_group='true'") \
|| { log "ERROR: forwarding query failed, aborting"; exit 1; }
[ -n "$FWD" ] || { log "no forwardings found"; exit 0; }
declare -A LOCAL ALLOWED TOTAL EXT
while read -r d; do [ -n "$d" ] && LOCAL["${d,,}"]=1; done <<< "$DOMS"
if [ -r "$ALLOW" ]; then
while read -r a; do
a="${a%%#*}"; a="${a//[[:space:]]/}"
[ -n "$a" ] && ALLOWED["${a,,}"]=1
done < "$ALLOW"
fi
REMOVE=0
while IFS=$'\t' read -r box target; do
[ -z "$target" ] && continue
target="${target,,}"; tdom="${target##*@}"
TOTAL["$box"]=$(( ${TOTAL["$box"]:-0} + 1 ))
# unusual characters (e.g. unicode IDN): never touch automatically
if [[ ! "$tdom" =~ ^[a-z0-9.-]+$ ]]; then
log "skip (check manually): $box -> $target"; continue
fi
[ -n "${LOCAL[$tdom]}" ] && continue
if [ -n "${ALLOWED[$target]}" ] || [ -n "${ALLOWED[$tdom]}" ]; then continue; fi
EXT["$box"]+="$target,"
REMOVE=$((REMOVE + 1))
log "external: $box -> $target"
done <<< "$FWD"
log "$REMOVE external target(s) found"
[ "$APPLY" -eq 1 ] || exit 0
[ "$REMOVE" -gt 0 ] || exit 0
if [ "$REMOVE" -gt "$MAX_REMOVE" ]; then
log "ERROR: $REMOVE > MAX_REMOVE ($MAX_REMOVE), not applying. Check the dry run, then run: MAX_REMOVE=$REMOVE $0 --apply"
exit 1
fi
# keep a record so removed targets can be restored
mkdir -p "$BACKUP_DIR"; chmod 700 "$BACKUP_DIR"
BACKUP="$BACKUP_DIR/$(date +%Y%m%d-%H%M%S).txt"
for box in "${!EXT[@]}"; do
printf '%s\t%s\n' "$box" "${EXT[$box]%,}" >> "$BACKUP"
done
for box in "${!EXT[@]}"; do
list="${EXT[$box]%,}"
n=$(tr ',' '\n' <<< "$list" | wc -l)
if [ "$n" -ge "${TOTAL[$box]}" ]; then
plesk bin mail -u "$box" -forwarding false || log "ERROR: could not disable forwarding for $box"
fi
plesk bin mail -u "$box" -forwarding-addresses del:"$list" \
|| { log "ERROR: removing targets failed for $box"; continue; }
log "removed: $box -> $list"
done
3) Optional whitelist /etc/check-forwards.allow (whole domain or single address per line):
4) Dry run (changes nothing):
Code:
/usr/local/sbin/check-forwards.sh
5) Remove external targets. If the dry run found more than 25, the script refuses and prints the command with the right MAX_REMOVE:
Code:
/usr/local/sbin/check-forwards.sh --apply
6) Optional, hourly via /etc/cron.d/check-forwards (only after you ran --apply by hand successfully):
Code:
0 * * * * root /usr/local/sbin/check-forwards.sh --apply >> /var/log/check-forwards.log 2>&1
Restore (syntax untested): the backup files contain "mailbox<TAB>targets", so a removed target can be re-added with
Open questions / limits:
- Not verified: that mail_group='true' really means "forwarding active", and that plesk accepts "del:" on a mailbox where forwarding was just switched off (the script switches off first, then removes the targets). Please test with one mailbox first.
- "Internal" means any domain or alias in the Plesk DB, even if its mail service is disabled or points to an external mail server.
- Not real-time (runs hourly), catch-all addresses and Sieve rules in Roundcube are not covered.
- Removing targets is silent for the customer. You may want to inform them.
- Not verified: that "del:" matches case-insensitively (the script lowercases targets). Test with a forwarding target containing uppercase letters.
Combined with moving spam to the Spam folder instead of only marking it (see #10/#12), this should cover most of the reputation problem. If someone tests it, please report back.
Regards
Tessa