mirror of
https://github.com/myvesta/vesta.git
synced 2024-12-21 11:31:21 -08:00
120 lines
3.6 KiB
Bash
120 lines
3.6 KiB
Bash
#!/bin/bash
|
|
# info: Add a domain to SpamAssassin whitelist
|
|
# usage: v-whitelist-email-domain DOMAIN SUBDOMAIN(YES/NO)
|
|
|
|
#----------------------------------------------------------#
|
|
# Variable&Function #
|
|
#----------------------------------------------------------#
|
|
|
|
whoami=$(whoami)
|
|
if [ "$whoami" != "root" ]; then
|
|
echo "You must be root to execute this script"
|
|
exit 1
|
|
fi
|
|
|
|
# Importing system environment
|
|
source /etc/profile
|
|
|
|
# Determine Debian version and set SpamAssassin service name
|
|
release=$(cat /etc/debian_version | tr "." "\n" | head -n1)
|
|
if [ "$release" -lt 12 ]; then
|
|
SPAMD_SERVICE="spamassassin.service"
|
|
else
|
|
SPAMD_SERVICE="spamd.service"
|
|
fi
|
|
|
|
SPAMASSASSIN_FILE="/etc/spamassassin/local.cf"
|
|
|
|
# Flags to track changes
|
|
SPAMASSASSIN_CHANGED=false
|
|
|
|
# Function to check if a SpamAssassin whitelist entry already exists
|
|
check_whitelist_exists() {
|
|
local entry=$1
|
|
local file=$2
|
|
grep -qF "whitelist_from $entry" "$file"
|
|
}
|
|
|
|
# Function to check if a domain/email is already blacklisted
|
|
check_blacklist_exists() {
|
|
local domain=$1
|
|
local file=$2
|
|
grep -qE "blacklist_from.*${domain}$" "$file"
|
|
}
|
|
|
|
# Function to add whitelist entry to file
|
|
add_whitelist_to_file() {
|
|
local entry=$1
|
|
local file=$2
|
|
echo "whitelist_from $entry" >> "$file"
|
|
}
|
|
|
|
# Display usage if no arguments are provided
|
|
if [ $# -lt 2 ]; then
|
|
echo "Usage: v-whitelist-email-domain DOMAIN SUBDOMAIN(YES/NO)"
|
|
exit 1
|
|
fi
|
|
|
|
#----------------------------------------------------------#
|
|
# Action #
|
|
#----------------------------------------------------------#
|
|
|
|
DOMAIN=$1
|
|
SUBDOMAIN=${2^^} # Convert to uppercase for consistency (YES/NO)
|
|
|
|
# Validate SUBDOMAIN parameter
|
|
if [[ "$SUBDOMAIN" != "YES" && "$SUBDOMAIN" != "NO" ]]; then
|
|
echo "Invalid parameter for SUBDOMAIN. Use YES or NO."
|
|
exit 1
|
|
fi
|
|
|
|
# Prepare entries for SpamAssassin
|
|
WHITELIST_ENTRY_MAIN="*@${DOMAIN}"
|
|
WHITELIST_ENTRY_SUB="*.$DOMAIN"
|
|
BLACKLIST_ENTRY_MAIN="*@${DOMAIN}"
|
|
BLACKLIST_ENTRY_SUB="*.$DOMAIN"
|
|
|
|
#----------------------------------------------------------#
|
|
# SpamAssassin Whitelist #
|
|
#----------------------------------------------------------#
|
|
|
|
echo "Updating $SPAMASSASSIN_FILE..."
|
|
|
|
# Check if the domain is already blacklisted
|
|
if check_blacklist_exists "$DOMAIN" "$SPAMASSASSIN_FILE"; then
|
|
echo "Cannot whitelist $DOMAIN. It is already blacklisted."
|
|
exit 1
|
|
fi
|
|
|
|
# Add the main entry
|
|
if ! check_whitelist_exists "$WHITELIST_ENTRY_MAIN" "$SPAMASSASSIN_FILE"; then
|
|
add_whitelist_to_file "$WHITELIST_ENTRY_MAIN" "$SPAMASSASSIN_FILE"
|
|
echo "Added whitelist_from $WHITELIST_ENTRY_MAIN to $SPAMASSASSIN_FILE."
|
|
SPAMASSASSIN_CHANGED=true
|
|
else
|
|
echo "whitelist_from $WHITELIST_ENTRY_MAIN already exists in $SPAMASSASSIN_FILE."
|
|
fi
|
|
|
|
# Add the subdomain entry if needed
|
|
if [ "$SUBDOMAIN" == "YES" ]; then
|
|
if ! check_whitelist_exists "$WHITELIST_ENTRY_SUB" "$SPAMASSASSIN_FILE"; then
|
|
add_whitelist_to_file "$WHITELIST_ENTRY_SUB" "$SPAMASSASSIN_FILE"
|
|
echo "Added whitelist_from $WHITELIST_ENTRY_SUB to $SPAMASSASSIN_FILE."
|
|
SPAMASSASSIN_CHANGED=true
|
|
else
|
|
echo "whitelist_from $WHITELIST_ENTRY_SUB already exists in $SPAMASSASSIN_FILE."
|
|
fi
|
|
fi
|
|
|
|
# Restart SpamAssassin only if changes were made
|
|
if [ "$SPAMASSASSIN_CHANGED" == "true" ]; then
|
|
systemctl restart "$SPAMD_SERVICE"
|
|
echo "SpamAssassin service ($SPAMD_SERVICE) restarted."
|
|
fi
|
|
|
|
#----------------------------------------------------------#
|
|
# Done #
|
|
#----------------------------------------------------------#
|
|
|
|
exit 0
|