#!/bin/sh # Version 0.2 - 2003/08/27 # Author: Jochen "Joe" Savelberg # Contact: joe@mailfilter.be # # Please make sure to backup your mailblocking settings before using this script. It will always override any previous settings! echo -n "Reading configuration..." postofficehost="www.domain.com:9090" # This is your Post.Office URL that you use to connect to P.O using a web browser smtpaddress="postmaster@domain.com" # This is your postmaster e-mail address password="mypassword" # This is your postmaster password blockipfile="/Spammer_IP.txt" # This file contains the IP addresses or networks to block. Will be overwritten when sievertdownload="yes" blockaddrfile="/Spammer_Addr.txt" # This file contains the e-mail addresses of spammers. Will be overwritten when easynetdownload="yes" blockdomainsfile="/Spammer_Domains.txt" # This file contains the domain names of the spammers. Willy be overwritten when easynetdownload="yes" blockuserfile="/Spammer_Usernames.txt" # This file contains the user names (before the @-sign), this is seldom used listlimit="10000" # This limits the number of lines in blockipfile, blockdomainsfile, blockaddrfile - 10000 seems to be a hard limit, at least for blockdomainsfile removetmp="yes" # Set this variable to "yes" if you'd like to remove any temporary files created by this script, except those listed above. sievertdownload="yes" # Set this option to yes to download the latest IP block list from http://www.customer1st.com/AntiSpam/Spammer_IP.txt sieverttemp="/sieverttmp.txt" # This is the temporary file name used to store John Sievert's IP block list easynetdownload="yes" # Set this option to yes if you'd like to download the hourly updated spammer list from http://abuse.easynet.nl/spamlist.txt easynettemp="/easynettmp.txt" # This is the temporary file name used to store the easynet spam list # Because the easynet spamlist is larger than 10000 lines, I need to limit it somehow. # The first step to limit the list is to set the listlimit. Alternatively you can filter # special domain names or words in order to reduce the list even further. # easynetdomfilter is an egrep regular expression that looks at spam domains. If you don't want to filter anything then set it to "" # easynetaddrfilter is an egrep regular expression that looks at spammer e-mails. You could just add spammer e-mails from # hotmail, yahoo and aol because many spammers use accounts on those services and you don't want to block these domains completely easynetdomfilter="\.br|\.ar|\.cn|\.tw|\.kr|\.th|\.uy|\.mx|\.biz|\.us|\.info|sex|porn|adult|opt|order|mail|offer|xxx|print|casino|net|org|ad|promo|pres|med|www|web|int|online" easynetaddrfilter="hotmail\.com|aol\.com|yahoo\.com" # Normally you should keep the mail blocking enabled or this script wouldn't make much sense. mailblocking="enabled" # enable mail blocking: enabled|disabled # Here you can enable and disable various blocking options blockconnections="yes" # Block from these IP Addresses: yes|no blockaddresses="no" # Block from these E-Mail Addresses: yes|no blockdomains="yes" # Block from these Domain Names: yes|no blockuser="no" # Block from these User Names (before the @-sign): yes|no echo " done." echo -n "Creating block files if they don't exist... " tmp="${blockipfile} ${blockaddrfile} ${blockdomainsfile} ${blockuserfile}" for i in $tmp do if [ -f $i ] then touch $i fi done echo " done." if [ ${sievertdownload} = "yes" ] then echo -n "Download John Sievert's list..." spamlisturl="http://www.customer1st.com/AntiSpam/Spammer_IP.txt" curl -s ${spamlisturl} -o ${sieverttemp} # downloading the file from John Sievert's server cat ${sieverttemp} | sort -n | uniq | head -n $listlimit > $blockipfile # I'm sorting the list and stripping duplicates before I limit it to listlimit echo " done." fi if [ ${easynetdownload} = "yes" ] then echo -n "Download easynet.nl spammer list..." # downloading the file from easynet.nl curl -s http://abuse.easynet.nl/spamlist.txt -o ${easynettemp} # extracting the domains and e-mails from the easynet list. Domains are identified by JUNK whereas spam e-mail addresses are identified by SPAMMER # The lists are compared against our easynet filters and limited according to the number of lines set in listlimit grep "JUNK" ${easynettemp} | egrep "$easynetdomfilter" | sed 's/JUNK//g' | tr -d "\t" | grep -v "###" | head -n $listlimit > $blockdomainsfile grep "SPAMMER" ${easynettemp} | egrep "$easynetaddrfilter" | sed 's/SPAMMER//g' | tr -d "\t" | grep -v "###" | head -n $listlimit > $blockaddrfile echo " done." fi # Now I'm logging into the Post.Office server and extracting the session id which we need to POST our mailblocking data through the script. echo -n "Getting Post.Office session ID..." sessionid=`curl -s -d "SMTP-Address=${smtpaddress}&Password=${password}&submit=Authenticate" http://${postofficehost}/Authentication | tr ">" "\n" | grep "a href" | cut -d "/" -f 2 | head -1` echo " done." echo "" echo "My post.office session id: $sessionid" echo "" formelements="-d SMTP-Accept/Config/MailBlocking=${mailblocking}" # If you enabled the IP list blocking, then the following part will be executed. The data from the blockipfile will be # written to a temporary file as I need to url-encode it. I'm just converting the newline characters to \r\n (carriage return & newlines) # It seems that web browsers send form data from textareas in this way. if [ ${blockconnections} = "yes" ] then #"SMTP-Accept/Blocks/Connections" echo -n "Constructing list of IP addresses to block..." blockipfiletmp=${blockipfile}".tmp" echo "SMTP-Accept/Blocks/Connections="`cat $blockipfile | tr -s '\n' ' ' | sed "s/ /%0D%0A/g"` > $blockipfiletmp formelements="${formelements} -d SMTP-Accept/Config/BlockConnections=${blockconnections} -d @${blockipfiletmp}" echo " done." fi # If you enabled the e-mail address blocking, then the following part will be executed. In addition to newlines characters, I'm # also encoding some special other characters, just to be on the safe side. These characters are % (percentage), # (hash), $ (dollar) if [ ${blockaddresses} = "yes" ] then #"SMTP-Accept/Blocks/SenderAddresses" echo -n "Constructing list of e-mail addresses to block..." blockaddrfiletmp=${blockaddrfile}".tmp" echo "SMTP-Accept/Blocks/SenderAddresses="`cat $blockaddrfile | sed "s/%/%25/g" | sed "s/#/%23/g" | sed "s/$/%24/g" | tr -s '\n' ' ' | sed "s/ /%0D%0A/g"` > $blockaddrfiletmp formelements="${formelements} -d SMTP-Accept/Config/BlockSenderAddresses=${blockaddresses} -d @${blockaddrfiletmp}" echo " done." fi # If you enabled the domain name blocking, then the following part will be executed. if [ ${blockdomains} = "yes" ] then #"SMTP-Accept/Blocks/SenderDomains" echo -n "Constructing list of domain names to block..." blockdomainsfiletmp=${blockdomainsfile}".tmp" echo "SMTP-Accept/Blocks/SenderDomains="`cat $blockdomainsfile | tr -s '\n' ' ' | sed "s/ /%0D%0A/g"` > $blockdomainsfiletmp formelements="${formelements} -d SMTP-Accept/Config/BlockSenderDomains=${blockdomains} -d @${blockdomainsfiletmp}" echo " done." fi # If you enabled the user name blocking, then the following part will be executed. if [ ${blockuser} = "yes" ] then #"SMTP-Accept/Blocks/SenderLocalParts" echo -n "Constructing list of user names to block..." blockuserfiletmp=${blockuserfile}".tmp" echo "SMTP-Accept/Blocks/SenderLocalParts="`cat $blockuserfile | sed "s/%/%25/g" | sed "s/#/%23/g" | sed "s/$/%24/g" | tr -s '\n' ' ' | sed "s/ /%0D%0A/g"` > $blockuserfiletmp formelements="${formelements} -d SMTP-Accept/Config/BlockSenderLocalParts=${blockuser} -d @${blockuserfiletmp}" echo " done." fi echo "" echo "This is the curl command that will be used to update Post.Office:" echo "" echo "curl -s $formelements http://${postofficehost}/${sessionid}/MailBlockingOptions " echo "" # This is the most important part of the script: sending the form data to Post.Office. Hopefully it works on your server as well. # Please note the sed command. This will extract and display any error message generated by post.office. So you know if there was # an error. curl -s $formelements http://${postofficehost}/${sessionid}/MailBlockingOptions | sed -n '/ ERROR /,/ ERROR /p' echo "" # I'm removing any temporary files which I created previously. if [ ${removetmp} = "yes" ] then tmp="${blockipfiletmp} ${blockaddrfiletmp} ${blockdomainsfiletmp} ${blockuserfiletmp} ${easynettemp} ${sieverttemp}" for i in $tmp do if [ -f $i ] then rm -f $i fi done echo " done." fi #end