Default values for SpamAssassin

patrik

Verified User
Joined
Sep 6, 2006
Messages
128
Hi,
I wrote this in another thread but when I noticed the thread was rather old I though I might dedicated a new thread for the scripts.

I have extended the scripts for enabling SpamAssassin by default for new users. I wanted to also be able to add default values for where a spam should be sent (Inbox, spam, user_spam/spambox or deleted).

This is the modified script that goes through every existing user / domain. Note: This will re-set the SpamAssassin configuration for EVERY user. I have called this script enable_spamassassin.sh

Code:
#!/bin/sh
for i in `ls /usr/local/directadmin/data/users`; do
{
     SPAMDIR=/home/$i/.spamassassin
     DEFAULTDIR=/usr/local/directadmin/scripts/custom
     mkdir -p $SPAMDIR
     cp $DEFAULTDIR/default_user_prefs $SPAMDIR/user_prefs
     chown ${i}:mail $SPAMDIR
     chmod 771 $SPAMDIR
     chown $i:$i $SPAMDIR/user_prefs
     chmod 755 $SPAMDIR/user_prefs
     touch $SPAMDIR/spam
     chown mail:$i $SPAMDIR/spam
     chmod 660 $SPAMDIR/spam

     mkdir -p $SPAMDIR/user_spam
     chown ${i}:mail $SPAMDIR/user_spam
     chmod 771 $SPAMDIR/user_spam

     for j in `cat /usr/local/directadmin/data/users/$i/domains.list`; do
     {
       cp $DEFAULTDIR/default_filter /etc/virtual/$j/filter
       cp $DEFAULTDIR/default_filter.conf /etc/virtual/$j/filter.conf
       chown mail:mail /etc/virtual/$j/filter
       chown mail:mail /etc/virtual/$j/filter.conf
       chmod 640 /etc/virtual/$j/filter
       chmod 600 /etc/virtual/$j/filter.conf
       
       # Create every spam box in user_spam
        for k in `ls /home/$i/mail/$j`; do
        {
           touch $SPAMDIR/user_spam/$k@$j
        } done;
     } done;


};
done;
exit 0;

This is the slightly modified user_create_post.sh script which is run when a new user has been created. It copies default filter values:
Code:
#!/bin/sh
if [ "$spam" = "ON" ]; then
   SPAMDIR=/home/$username/.spamassassin
   DEFAULTDIR=/usr/local/directadmin/scripts/custom
   mkdir $SPAMDIR
   cp $DEFAULTDIR/default_user_prefs $DIR/user_prefs
   chown ${username}:mail $SPAMDIR
   chmod 771 $SPAMDIR
   chown $username:$username $SPAMDIR/user_prefs
   chmod 755 $SPAMDIR/user_prefs
   touch $SPAMDIR/spam
   chown mail:$username $SPAMDIR/spam
   chmod 660 $SPAMDIR/spam

   # Copy the default filter and filter.conf
   cp $DEFAULTDIR/default_filter /etc/virtual/$domain/filter
   cp $DEFAULTDIR/default_filter.conf /etc/virtual/$domain/filter.conf
   chown mail:mail /etc/virtual/$domain/filter
   chown mail:mail /etc/virtual/$domain/filter.conf
   chmod 640 /etc/virtual/$domain/filter
   chmod 600 /etc/virtual/$domain/filter.conf
fi
exit 0;
Here's the original http://help.directadmin.com/item.php?id=117

And here is my domain_create_post.sh which is run when a new domain is created and copies default filter values:
Code:
#!/bin/sh

DEFAULTDIR=/usr/local/directadmin/scripts/custom

# Copy the default filter and filter.conf
cp $DEFAULTDIR/default_filter /etc/virtual/$domain/filter
cp $DEFAULTDIR/default_filter.conf /etc/virtual/$domain/filter.conf
chown mail:mail /etc/virtual/$domain/filter
chown mail:mail /etc/virtual/$domain/filter.conf
chmod 640 /etc/virtual/$domain/filter
chmod 600 /etc/virtual/$domain/filter.conf
exit 0;

The default_filter and default_filter.conf could be created by yourself to suit your needs. An easy way to do this is to enable SpamAssassin on a user and configure it as you want the default values to be. Then go inside /etc/virtual/DOMAIN.TLD/ and copy filter and filter.conf.

Please tell me if you find a bug.


Cheers,

17/10 - 2006: A few changes made in enable_spamassassin.sh
 
Last edited:
Hi,

I get this when i run enable_spam.

cp: cannot stat `/usr/local/directadmin/scripts/custom/default_user_prefs': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter.conf': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter.conf': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter.conf': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter.conf': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_user_prefs': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter.conf': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter.conf': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_filter.conf': No such file or directory
cp: cannot stat `/usr/local/directadmin/scripts/custom/default_user_prefs': No such file or directory
 
powerdomein,
You need those three files to exist inside /usr/local/directadmin/scripts/custom/.

The default_filter and default_filter.conf could be created by yourself to suit your needs. An easy way to do this is to enable SpamAssassin on a user and configure it as you want the default values to be. Then go inside /etc/virtual/DOMAIN.TLD/ and copy filter and filter.conf.
Copy them to /usr/local/directadmin/scripts/custom/ and rename them to default_filter and default_filter.conf respectively. You will also need a default_user_prefs which you can find in /home/USERNAME/.spamassassin/user_prefs if the user USERNAME has spamassassin enabled. Copy user_prefs to /usr/local/directadmin/scripts/custom/ and rename it to default_user_prefs and you should be able to run the script.
 
Hi,

Do you know exactly what are the options available in the file default_user_prefs? From the user_prefs file from existing users, only 2 options are found in the file:

required_score 5.0
report_safe 1

How can I define the option for "where to save the spam" and "whether to rename the email subject"? I can't find any option file throughout the server.

Thanks,
Anthony
 
Hi,
Check, /etc/virtual/domain.tld/filter and filter.conf
 
Hi patrik,

I wonder if you have any way to handle the following situation, that is, when the user first login, he disabled spamassassin. Later on, he enables it back. When this happens, what he see in the spamassassin session are not the default values we set at the first place. Any help on this?

Thanks,
Anthony :)
 
There seems to have a problem. In the file "filter", there are some lines like the ones below:

if
$h_X-Spam-Status: contains "Yes,"
then
if
$local_part is "hostspeedy"
then

save /home/hostXXX/.spamassassin/spam 660
else
save /home/hostXXX/.spamassassin/user_spam/$local_part@$domain 660

endif
finish
endif

hostXXX is the username of the account I copied the default file from. It seems that this method of copying will lead to errors as the username in the file is incorrect? Is it? :confused:
 
After reading, I found that all you need is just 3 lines from the 6 you wrote ;) , because the file "filter" is meanlingless to be copied. This file is just a temp file for writing stuff, nothing is read from it by DA, so the default empty file created by DA will do the job. Moreover, you don't have to include the lines in user_create_post.sh, you just need them in domain_create_post.sh, since domain_create_post.sh will be called anyway after a user is created.

cp $DEFAULTDIR/default_filter.conf /etc/virtual/$domain/filter.conf
chown mail:mail /etc/virtual/$domain/filter.conf
chmod 600 /etc/virtual/$domain/filter.conf

:D
 
Last edited:
ak17_hk said:
There seems to have a problem. In the file "filter", there are some lines like the ones below:



hostXXX is the username of the account I copied the default file from. It seems that this method of copying will lead to errors as the username in the file is incorrect? Is it? :confused:
Maybe I wasn't clear enough.
Of course you can't just copy a file and use it as it is, but you can look at it when determine how the default filter should look like.
 
ak17_hk said:
After reading, I found that all you need is just 3 lines from the 6 you wrote ;) , because the file "filter" is meanlingless to be copied. This file is just a temp file for writing stuff, nothing is read from it by DA, so the default empty file created by DA will do the job. Moreover, you don't have to include the lines in user_create_post.sh, you just need them in domain_create_post.sh, since domain_create_post.sh will be called anyway after a user is created.:D
Hmm. First of all, what do you mean by saying that the filter file is just a temp file? It's not a temp file it contains the filter process which is normally created by DirectAdmin when the user changes his/her Spam filter configuration.
It might be redundant to copy the filter and filter.conf in user_create_post.sh if the domain script is also invoked, I haven't checked that but if you say so. At least it doesn't hurt.

I have modified those scripts a bit since last time. The filter is not copied from a default_filter anymore but rather dynamically created from the shell script.

user_create_post.sh
Code:
#!/bin/sh
if [ "$spam" = "ON" ]; then
   DIR=/home/$username/.spamassassin
   mkdir $DIR
   cp /usr/local/directadmin/scripts/custom/default_user_prefs $DIR/user_prefs
   chown ${username}:mail $DIR
   chmod 771 $DIR
   chown $username:$username $DIR/user_prefs
   chmod 755 $DIR/user_prefs
   touch $DIR/spam
   chown mail:$username $DIR/spam
   chmod 660 $DIR/spam

   # Create the default filter and copy filter.conf
   echo "# Exim Filter" > /etc/virtual/$domain/filter
   echo "# Do not modify this file" >> /etc/virtual/$domain/filter
   echo "" >> /etc/virtual/$domain/filter
   echo "if error_message then finish endif" >> /etc/virtual/$domain/filter
   echo "if" >> /etc/virtual/$domain/filter
   echo "   \$h_X-Spam-Status: contains \"Yes,\"" >> /etc/virtual/$domain/filter
   echo "then" >> /etc/virtual/$domain/filter
   echo "   if" >> /etc/virtual/$domain/filter
   echo "      \$local_part is \"${username}\"" >> /etc/virtual/$domain/filter
   echo "   then" >> /etc/virtual/$domain/filter
   echo "      save $DIR/spam 660" >> /etc/virtual/$domain/filter
   echo "   else" >> /etc/virtual/$domain/filter
   echo "      save $DIR/user_spam/\$local_part@\$domain 660" >> /etc/virtual/$domain/filter
   echo "   endif" >> /etc/virtual/$domain/filter
   echo "   finish" >> /etc/virtual/$domain/filter
   echo "endif" >> /etc/virtual/$domain/filter
   echo "" >> /etc/virtual/$domain/filter
   echo "# End Filter" >> /etc/virtual/$domain/filter


   #cp /usr/local/directadmin/scripts/custom/default_filter /etc/virtual/$domain/filter
   cp /usr/local/directadmin/scripts/custom/default_filter.conf /etc/virtual/$domain/filter.conf
   chown mail:mail /etc/virtual/$domain/filter
   chown mail:mail /etc/virtual/$domain/filter.conf
   chmod 640 /etc/virtual/$domain/filter
   chmod 600 /etc/virtual/$domain/filter.conf
fi
exit 0;

domain_create_post.sh
Code:
#!/bin/sh

DEFAULTDIR=/usr/local/directadmin/scripts/custom
DIR=/home/$username/.spamassassin

# Create the default filter and copy filter.conf
echo "# Exim Filter" > /etc/virtual/$domain/filter
echo "# Do not modify this file" >> /etc/virtual/$domain/filter
echo "" >> /etc/virtual/$domain/filter
echo "if error_message then finish endif" >> /etc/virtual/$domain/filter
echo "if" >> /etc/virtual/$domain/filter
echo "   \$h_X-Spam-Status: contains \"Yes,\"" >> /etc/virtual/$domain/filter
echo "then" >> /etc/virtual/$domain/filter
echo "   if" >> /etc/virtual/$domain/filter
echo "      \$local_part is \"${username}\"" >> /etc/virtual/$domain/filter
echo "   then" >> /etc/virtual/$domain/filter
echo "      save $DIR/spam 660" >> /etc/virtual/$domain/filter
echo "   else" >> /etc/virtual/$domain/filter
echo "      save $DIR/user_spam/\$local_part@\$domain 660" >> /etc/virtual/$domain/filter
echo "   endif" >> /etc/virtual/$domain/filter
echo "   finish" >> /etc/virtual/$domain/filter
echo "endif" >> /etc/virtual/$domain/filter
echo "" >> /etc/virtual/$domain/filter
echo "# End Filter" >> /etc/virtual/$domain/filter

#cp $DEFAULTDIR/default_filter /etc/virtual/$domain/filter
cp $DEFAULTDIR/default_filter.conf /etc/virtual/$domain/filter.conf
chown mail:mail /etc/virtual/$domain/filter
chown mail:mail /etc/virtual/$domain/filter.conf
chmod 640 /etc/virtual/$domain/filter
chmod 600 /etc/virtual/$domain/filter.conf
exit 0;
 
Just take a look at the file "filter" from one of the existing user, at the top it says:

# created by DirectAdmin, version 1.28.1
# Do not modify this file as any changes will be
# overwritten when the user makes a change.
# (data is only written to this file, not read)

In order words, the content in this file is not important at all for setting up default values for the users, as *NOTHING* will be read from it. I guess it's just a transition file used. :D
 
Does it make sense to just write to a file and never read from it?

I'm quite sure they mean that DA will never read the file. Because Exim will and that's for sure! Check the domain_filter director in exim.conf if you don't believe me.
It's still important that you alter the filter file!
 
ak17_hk said:
Hi patrik,

I wonder if you have any way to handle the following situation, that is, when the user first login, he disabled spamassassin. Later on, he enables it back. When this happens, what he see in the spamassassin session are not the default values we set at the first place. Any help on this?

Thanks,
Anthony :)
Sorry I missed this post completly. Hmm, No I have no good solution for this. The correct (but hard) way to solve this is probably to remove the SpamAssassin link from DirectAdmin and then create a simple plugin with just the option to turn on / off SpamAssassin.
 
Hi,

I contacted John and he promised to solve this problem in the next release~ :D

Anthony.
 
Sorry for the reply to this old post, but i don't know how to correct/fix this

ls: /home/<user>/mail/<domain>.<tld>: No such file or directory

caused by

for k in `ls /home/$i/mail/$j`; do

I think it's because i use the newer mbox type
 
hahah mbox is not newer...mbox is older

There is no /home/user/mail anyways it should be /home/user/imap

I dunno why you are following an old guide of a bad script.
 
Last edited:
Well actually the baseline of that script is not that bad, is for sure needed to adjust but have a defautl spamassassin setup and a default filter list for each new user/domain is actually nice.

Im using it, not this one, a customized one.. and is working great

Regards
 
Back
Top