Enable Spamassassin for all users

Trans-IX B.V.

Verified User
Joined
Mar 4, 2009
Messages
36
Location
The Netherlands
We are using some scripts to make sure all users are using Spamassassin.

We make sure spam=ON is in the
/usr/local/directadmin/data/users/USERNAME/user.conf

And we make sure that the following is set in the
/etc/virtual/DOMAINNAME/filter.conf

high_score=5
high_score_block=yes
where=delete

After that we run
echo "action=rewrite&value=filter" >> /usr/local/directadmin/data/task.queue

And I login in the username and I look under "Spamassassin Setup" it still says:

SpamAssassin is not currently enabled.
Click "Save" (below) to enable it.

How can I automate with a script that all the Spamassassins for all the users are being enabled?

Thanks
 
Thank you for the link, SeLLeRoNe. I have read somehwere before in the forum that you have edited the spamassassin settings for new domains. I have read this link http://help.directadmin.com/item.php?id=304

Would it be possible for you to share your custom domain_create_post.sh that give custom spamassassin settings for new domain, and some instructions for the setup? That would help a lot. It would be very kind of you. :)
 
The link i posted in post #2 is the way i did use... just changed to fit domain_create_post.sh

Here it is:

Code:
#!/bin/sh

\cp MY_CUSTOM_FILTER_FILE /etc/virtual/$domain/filter
\cp MY_CUSTOM_FILTER.CONF_FILE /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;

Regards
 
Thanks a lot! So the code you posted is placed in domain_create_post.sh file, or is it placed in user_create_post.sh? I just need a these example before I can be able to test, because I am not sure about this.
 
I am trying to look into this now, but have a hard time. Regarding this:

Code:
\cp MY_CUSTOM_FILTER_FILE /etc/virtual/$domain/filter
\cp MY_CUSTOM_FILTER.CONF_FILE /etc/virtual/$domain/filter.conf

1: Is this "\cp" correct, or did you make a mistake when pasting it in the forums? I have never seen the "\" used before. But don't know.

2: Regarding MY_CUSTOM_FILTER_FILE and MY_CUSTOM_FILTER.CONF_FILE, at what path have you added those two files?

3: Your code is very different from the code in the example at http://help.directadmin.com/item.php?id=117 - is it not needed to have this at the end?:
Code:
fi

Also you don't turn SpamAssassin on as default for new users in this code?

This is a little over my head, I think I need more detailed guide. All I want is that all user accounts created has SpamAssassin enabled as default, and that all domains added have my preferred SpamAssassin settings. But the example at http://help.directadmin.com/item.php?id=117 and your code, does not seem to be enough for me to understand all I need to do.
 
Well, the code i posted is for add standard filter to new domain, for new users you need to use the guide link i posted.

the "fi" statement is not needed since i dont use any if statement.

you can choose what path you want for the filter and filter.conf standard file

the \cp just mean that if destination file exist it overwrite without ask.

If you are not able to do those two file by yourself you should consider hire an administrator that do this job for you.

Me, Zeiter, nobaloney and other offer this kind of services as payd service.

Regards
 
Thank for for additional information. I will figure out the rest myself.
 
Here is a old, but useful forum post by John about .sh scripts etc: http://forum.directadmin.com/showthread.php?t=1715&p=10237#post10237 - however please note that in John's post, he say to chmod domain_create_post.sh to 755. I don't think that is needed. I have set my custom user_create_post.sh and domain_create_post.sh to 0700 on my servers, and it works.

I have tested a lot, and have setup custom user_create_post.sh and domain_create_post.sh to automatically enable SpamAssassin on new accounts, and automatically have my settings when customer add a new domain. It might help someone, so here I post my setup:

Create /root/.spamassassin/user_prefs and add this content in it:
Code:
required_score 7.5
report_safe     1

Create /root/.spamassassin/filter.conf and add this content in it:
Code:
high_score=15
high_score_block=yes
where=userspamfolder

Create /usr/local/directadmin/scripts/custom/user_create_post.sh , set owner/group to diradmin diradmin and chmod 0700, and add this content in it:
Code:
#!/bin/sh
if [ "$spam" = "ON" ]; then
DIR=/home/$username/.spamassassin
mkdir  $DIR
\cp /root/.spamassassin/user_prefs $DIR/user_prefs
chown  ${username}:mail $DIR
chmod 771 $DIR
chown $username:$username  $DIR/user_prefs
chmod 755 $DIR/user_prefs
fi
exit 0;

Create /usr/local/directadmin/scripts/custom/domain_create_post.sh , set owner/group to diradmin diradmin and chmod 0700, and add this content in it:
Code:
#!/bin/sh
\cp /root/.spamassassin/filter.conf /etc/virtual/$domain/filter.conf
chown mail:mail /etc/virtual/$domain/filter.conf
chmod 600 /etc/virtual/$domain/filter.conf
exit 0;

Thats it! Please note that in this guide http://help.directadmin.com/item.php?id=117 the example of user_create_post.sh has these three lines wich I did not add in my file:
Code:
touch $DIR/spam
chown  mail:$username $DIR/spam
chmod 660 $DIR/spam

The reason I did not add them, was because of initial testing I did, show that default it is not added a "spam" file when enabling SpamAssassin, therfor I think the example is outdated, and it is not needed to create that file. So I have removed those three lines from my code.
 
Hi ditto, nice job on updating the files!

I'm missing a script to update all the existing /etc/virtual/*/filter.conf files so I created one using your domain_create_post.sh:

So for everyone who wants to use it:

Code:
vi /opt/update_filterconf.sh

Paste:

Code:
#!/bin/sh
for i in `ls /etc/virtual`; do
{
  domain=$i /usr/local/directadmin/scripts/custom/domain_create_post.sh
};
done;
exit 0;

Save with :wq and run:

Code:
chmod 700 /opt/update_filterconf.sh

And run it:

Code:
/opt/update_filterconf.sh

Would there still be the need of running the following?

Code:
echo "action=rewrite&value=filter" >> /usr/local/directadmin/data/task.queue
 
Last edited:
[..]Would there still be the need of running the following?

Code:
echo "action=rewrite&value=filter" >> /usr/local/directadmin/data/task.queue

I don't know, but hopefully someone else knows and will answer ... I have a question related to spam management in DirectAdmin, hope somone knows:

When SpamAssassin settings in DirectAdmin is set to send spam to spamfolder (filter.conf has where=userspamfolder), what happens to the spam when a customer setup a email forwarder in DirectAdmin, is the spam that is sent to userspamfolder also forwarded, or is it not forwarded? I hope it is not forwarded to the forwarder email address, because if it is, I would have to change all customers SpamAssassin settings to delete the spam without sending it to spam folder.
 
My steps

Hello,

First thank you very much to all for your contributions.

I recently needed to do this myself and with the help of Directadmin, plus the contributions of you, I have successfully completed the task with a few modifications.

I share my steps:

Use an existing user account to activate and configure Spamassassin filters and actions as we want to be the default settings for all new and/or existing users.

Copy the configuration created by directadmin in sample user:

# cp /etc/virtual/USER_DOMAIN/filter /usr/local/directadmin/scripts/custom/default_filter
# cp /etc/virtual/USER_DOMAIN/filter.conf /usr/local/directadmin/scripts/custom/default_filter.conf
# cp /home/USER/.spamassassin/user_prefs /usr/local/directadmin/scripts/custom/default_user_prefs

We create the scripts that Directadmin use when a new user and a new domain is creared:

# cd /usr/local/directadmin/scripts/custom/

# nano user_create_post.sh


Code:
#!/bin/sh

if [ "$spam" = "ON" ]; then
        DIR=/home/$username/.spamassassin
        DEFAULTDIR=/usr/local/directadmin/scripts/custom
        mkdir $DIR
        \cp $DEFAULTDIR/default_user_prefs $DIR/user_prefs
        chown ${username}:mail $DIR
        chmod 771 $DIR
        chown $username:$username  $DIR/user_prefs
        chmod 755 $DIR/user_prefs
fi

exit 0;
# chmod +x user_create_post.sh
# chown diradmin.diradmin user_create_post.sh

# nano domain_create_post.sh


Code:
#!/bin/sh

        DEFAULTDIR=/usr/local/directadmin/scripts/custom
        \cp -f $DEFAULTDIR/default_filter /etc/virtual/$domain/filter
        \cp -f $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;
# chmod +x domain_create_post.sh
# chown diradmin.diradmin domain_create_post.sh


Now all new Users and domains that are created, have enabled by default Spamassassin and the setup we have set by default.

If you also want to enable Spamassassin and apply these settings to all existing users, these are the steps that I used.

We create the scripts for manually run Spamassassin activation and copy the default filters to existing users:

# nano spam.sh

Code:
#!/bin/sh

for i in `ls /usr/local/directadmin/data/users`; do
  {
        username=$i spam=ON /usr/local/directadmin/scripts/custom/user_create_post.sh
  };
  done;

exit 0;
# chmod +x spam.sh

# nano update_filters.sh


Code:
#!/bin/sh

for i in `cat /etc/virtual/domains`; do
  {
        domain=$i /usr/local/directadmin/scripts/custom/domain_create_post.sh
  };
  done;

exit 0;
# chmod +x update_filters.sh

And then run:

# ./spam.sh
# ./update_filters.sh


These steps have helped me to such purpose without problem and everything is working properly as I wanted (or so I think at the moment), but would appreciate any advice to improve the process or warnings of potential failures.

Thank you very much to all ...and sorry for my bad English!

Greetings.


NOTE: Ok at the moment I found a small problem that has been driving me crazy for a few minutes.

If you copy the files "filter" and "filter.conf" in the folder "/etc/virtual/hostname.domain.tld/" when a mail is sending from "[email protected]", CSF/LFD notifications for example, EXIM gives an error and can not send mail:
Code:
[COLOR=#000000][FONT=verdana]Failed to find user "" from expanded string "${lookup{$domain}lsearch{/etc/virtual/domainowners}{$value}}" for the domain_filter router[/FONT][/COLOR]
Delete these two files in this folder fix the problem, but would have to improve my method to get the list of domains to avoid this problem.

Sorry.
 
Last edited:
I'm a newcomer to DirectAdmin and have been looking at the best way to enable SpamAssassin for all users and domains, along with a default set of configuration options. The help article "How to enable SpamAssassin for new accounts" is excellent but it falls down when an existing user adds an additional domain to their account.

I've adapted it to add the required settings for new domains added to existing accounts. I've also adapted the script that allows you to run this process against all existing users and domains.

The following process generates the files required by SpamAssassin:
/home/<username>/.spamassassin/user_prefs
/etc/virtual/<domain>/filter.conf
/etc/virtual/<domain>/filter

1. Create the file
/usr/local/directadmin/scripts/custom/user_create_post.sh
and add:
Bash:
#!/bin/sh
if [ "$spam" = "ON" ]; then
  DIR=/home/$username/.spamassassin
  mkdir -p $DIR
  UP=$DIR/user_prefs
  if [ ! -s ${UP} ]; then
    echo 'required_score 5.0' > ${UP}
    echo 'rewrite_header subject ***SPAM***' >> ${UP}
    echo 'report_safe 0' >> ${UP}
    chown $username:$username ${UP}
    chmod 644 ${UP}
  fi
  chown ${username}:mail $DIR
  chmod 771 $DIR
fi
exit 0;
Set the permissions on this file as follows:
chmod 755 user_create_post.sh
This executes when a new user is created and generates the user_prefs file containing some default settings.

2. Create the file
/usr/local/directadmin/scripts/custom/domain_create_post.sh
and add:
Bash:
#!/bin/sh
if [ "${domain}" != "" ]; then
  FCONF=/etc/virtual/${domain}/filter.conf
  if [ ! -s ${FCONF} ]; then
    echo 'high_score=10' > ${FCONF}
    echo 'high_score_block=yes' >> ${FCONF}
    echo 'where=inbox' >> ${FCONF}
    chown mail:mail ${FCONF}
   
    echo "action=rewrite&value=filter&user=$username" >> /usr/local/directadmin/data/task.queue
  fi
fi
exit 0;
Set the permissions on this file as follows:
chmod 755 domain_create_post.sh
This executes when a new domain is added and generates the filter.conf file containing some default settings.

That's it. The first code executes when a new user is created, followed by the second for that user's main domain. If the user later adds an additional domain to their account, the second code will execute.

Note, the line echo "action=rewrite&value=filter&user=$username" >> /usr/local/directadmin/data/task.queue rewrites the filters for the user and this takes care of the filter file update - there is no need to edit this file manually.

To take care of existing accounts, create a file, e.g. spam.sh and add:
Bash:
#!/bin/sh
for i in `ls /usr/local/directadmin/data/users`; do
{
  username=$i spam=ON /usr/local/directadmin/scripts/custom/user_create_post.sh
  for d in `cat /usr/local/directadmin/data/users/$i/domains.list`; do
  {
    username=$i domain=$d /usr/local/directadmin/scripts/custom/domain_create_post.sh
  };
  done;
};
done;
exit 0;
Set the permissions on this file as follows:
chmod 755 spam.sh
Run this file once. Note, this will not overwrite any existing SpamAssassin settings that a user may have already configured.

Cheers,
Dan
 
Back
Top