← cd ~/blog

less sieve-howto-postfixdovecot-in-ispconfig3-environment.md

sieve: howto postfix/dovecot in ispconfig3 environment

#Debian & Sysadmin#Ispconfig archive

# historical archive — written years ago, kept as it was. Some info is dated.

Today we’ll walk through together the steps to properly set up a server-side email filtering system using Dovecot Sieve and Roundcube.

By the end of this article we’ll have the ability to have our own automatic email filtering system with subsequent sorting into a specific folder based on filtering rules of our choice.

What’s under the hood?! (What is all this about?)

The project behind all this is known as Pigeonhole. The fantastic things were adding both support for the Sieve language (RFC 5228), and the ManageSieve protocol (RFC 5804) to the Dovecot Secure IMAP Server. On top of this, add the ability to manipulate your rules through the Roundcube web interface.

Before proceeding, as usual, let’s take care of starting a screen session:

screen -U -S sieve
Make sure your system is up to date! (if it isn’t… well … do it!!!)

LET’S ENABLE DOVECOT MANAGE-SIEVE

Let’s install what’s necessary

apt-get install dovecot-sieve dovecot-managesieved

Let’s add the correct directives to our configuration file

vi /etc/dovecot/dovecot.conf

Let’s make sure the directives are similar to these:

protocols = imap pop3 sieve

...

plugin {
  ...
  sieve_dir=/var/vmail/%d/%n/sieve
  sieve=/var/vmail/%d/%n/.sieve
  sieve_extensions = +spamtest +spamtestplus +relational +comparator-i;ascii-numeric
  sieve_global_path = /etc/sieve/conf.d/default.sieve 
  sieve_global_dir = /etc/sieve/
  sieve_before = /etc/sieve/conf.d/before
  sieve_after = /etc/sieve/conf.d/after
  ...
}

...

}
managesieve_notify_capability = mailto
managesieve_sieve_capability = fileinto reject envelope encoded-character Vacation subaddress comparator-i;ascii-numeric relational regex imap4flags copy include variables body enotify environment mailbox date

service managesieve-login {
  inet_listener sieve {
    port = 4190
  }
  service_count = 1
  process_min_avail = 1
  vsz_limit = 64M
}

service managesieve {
  process_limit = 10
}

protocol sieve {

    managesieve_max_line_length = 65536

    managesieve_implementation_string = dovecot

    log_path = /var/log/dovecot-sieve-errors.log

    info_log_path = /var/log/dovecot-sieve.log

    managesieve_logout_format = bytes ( in=%i : out=%o )
}

...

protocol lda {
  mail_plugins = sieve quota
}
...

I create the log files as indicated in the configuration file

touch /var/log/{dovecot-sieve-errors.log,dovecot-sieve.log}
touch /var/log/{dovecot-sieve-errors.log,dovecot-sieve.log}
mkdir -p /etc/sieve/{global,after,before}

chown vmail:vmail -R /etc/sieve/*
chown vmail:mail /var/log/dovecot-*

Let’s restart our main service so that our changes take effect

service dovecot restart

Now all that’s left is to check whether the service is up and running.

# ps aux | grep -v grep | grep managesieve-login
# netstat -an | grep LISTEN | grep :4190

The first command should return something like this

dovenull 29840  0.0  0.1   5136  2244 ?        S    10:53   0:00 dovecot/managesieve-login

while the second will tell us whether the listening port has been correctly set

tcp        0      0 0.0.0.0:4190            0.0.0.0:*               LISTEN
tcp6       0      0 :::4190                 :::*                    LISTEN

SET-UP OF THE GLOBAL FILTERS

The next step is to create a filter whose rule is to sort the emails marked as SPAM by our SpamAssassin into the relevant Spam/Junk folder. Let’s proceed with creating the following file:

vim /etc/sieve/global/default.sieve

and let’s add a couple of rules

require ["fileinto"];
# rule:[SPAM]
if header :contains "X-Spam-Flag" "YES" {
        fileinto "Spam";
}
# rule:[SPAM2]
elsif header :matches "Subject" ["*money*","*Viagra*","Cialis"] {
        fileinto "Spam";
}

LET’S ENABLE THE SIEVE PLUGIN IN ROUNDCUBE

In order to handle the server-side filtering rules through Roundcube, we need to enable the managesieve plugin by editing a few configuration files.

Let’s move into the ROOT Directory of RoundCube

vim +/PLUGINS config/main.inc.php
...
$rcmail_config['plugins'] = array( ... 'managesieve');

Next, in the plugin folder let’s copy the default configuration file and set it up as follows:

cd plugins/managesieve/
cp config.inc.php.dist config.inc.php
vim config.inc.php
...
$rcmail_config['managesieve_port'] = 4190;
...
$rcmail_config['managesieve_default'] = '/etc/dovecot/sieve/default.sieve';

Now you can proceed to log in through the Roundcube web interface and navigate through Settings > Filters and create new filter sets and/or rules.