summaryrefslogtreecommitdiff
path: root/spamassassin/tweak.d/networks.sh
blob: 24f09d422c749bcc4197793187cdd90fa78a9b4b (plain)
  1. #!/bin/sh
  2. #
  3. # Tweak SpamAssassin settings internal_networks trusted_networks
  4. set -e
  5. exit1() {
  6. response="${1:+Error: }${1:-Internal error!}"
  7. echo "$response" >&2
  8. exit 1
  9. }
  10. firstitem() {
  11. file="$1"
  12. cat "$file" | grep -v '^#' | sed 's/#.*//' | head -n 1
  13. }
  14. items() {
  15. file="$1"
  16. cat "$file" | grep -v '^#' | sed 's/#.*//'
  17. }
  18. items_singleline() {
  19. perl -0777 -ne 'print join " ",m/^\s*([^#\s]+)/gm' "$1"
  20. }
  21. addoroverwritewithindents() {
  22. set -e
  23. file="$1"
  24. export token="$2"
  25. # provide replacement as STDIN
  26. perl -0777 -pi \
  27. -E 'BEGIN{ $s=<STDIN>; chomp $s }'\
  28. -E 's/$ENV{"token"}.*(?:\n\h.*)+\n/$s/m and $seen=1;'\
  29. -E 'END{ unless ($seen) { print $s } }'\
  30. "$file"
  31. }
  32. REDPILL=$(firstitem "/etc/local-ORG/redpill")
  33. redpilldir="/etc/local-$REDPILL/"
  34. REALMS="$(items "$redpilldir/realms")"
  35. REALM="$(cat /etc/local-ORG/realm || dnsdomainname | tr '[a-z]' '[A-Z]')"
  36. for realm in $REALMS; do
  37. realmdir="$redpilldir/$realm"
  38. [ -d "$realmdir" ] || continue
  39. mailnets=$(items_singleline "$realmdir/mailnetworks") #'
  40. if [ "$REALM" = "$realm" ]; then
  41. ournets="${ournets:+$ournets }$mailnets"
  42. [ ! -f "$realmdir/localnetworks" ] || ournets="${ournets:+$ournets }$(items "$realmdir/localnetworks")"
  43. else
  44. redpillnets="${redpillnets:+$redpillnets }$mailnets"
  45. redpillhosts="${redpillhosts:+$redpillhosts }$(items "$realmdir/mailhost")"
  46. fi
  47. done
  48. echo "FIXME: Add these to /etc/spamassassin/local.cf:"
  49. echo "internal_networks $ournets"
  50. echo "trusted_networks $ournets $redpillnets"
  51. echo "whitelist_bounce_relays $redpillhosts"