Wednesday, November 13, 2013

Install Quota and Sieve Plugin for Dovecot Mailserver

To use Quota, you will first need to make sure you are using Dovecot's deliver for delivering incoming mail to users' mailboxes. Then, you need to enable the Quota plugin in your dovecot.conf

First we will start with configuring PostfixAdmin To Enable Quota Limits,

vim /var/www/postfixadmin/config.inc.php

$CONF['quota'] = 'YES';
$CONF['used_quotas'] = 'YES';
$CONF['new_quota_table'] = 'YES';

These enable the quota configuration within PostfixAdmin.

The "quota" field in the table mailbox is the user quota limit which postfixadmin enters in Megabytes and is stored in bytes.
The "maxquota" field in the domain table is the domain quota limit.
The SuperAdmin within PostfixAdmin has control over these values.

Enabling Quota Mailbox Limits in Dovecot,

vim  /usr/local/etc/dovecot/dovecot-dict-sql-user.conf

 connect = host=localhost dbname=postfix user=postfix password=postfixpassword

 map {
  pattern = priv/quota/storage
  table = quota2
  username_field = username
  value_field = bytes
 }
 map {
  pattern = priv/quota/messages
  table = quota2
  username_field = username
  value_field = messages
 }


vim /usr/local/etc/dovecot/dovecot-dict-sql-domain.conf

 connect = host=localhost dbname=postfix user=postfix password=postfixpassword

 map {
    pattern = priv/quota/storage
    table = domain
    username_field = domain
    value_field = quota
 }

 map {
    pattern = priv/quota/messages
    table = quota2
    username_field = username
    value_field = messages
 }


These configurations enable dovecot to write the users and domain mailboxes current quota sizes to the quota2 doamin tables respectively.

Enable Quota Plugin,

vim /usr/local/etc/dovecot/dovecot.conf

 mail_plugins = $mail_plugins quota

 passdb {
    args = /usr/local/etc/dovecot/dovecot-sql.conf.ext
    driver = sql
 }

 service dict {
    unix_listener dict {
        mode = 0600
        user = dovecot
    }
 }

 protocol imap {
    mail_plugins = $mail_plugins imap_quota
 }


 plugin {
    # Using SQL Tables to store current quota size
    quota = dict:Quota:%d:proxy::sqldomainquota
    quota = dict:User Quota::proxy::sqluserquota

    # Allow 10% more for Trash Folder
    quota_rule2 = Trash:storage=+10%%

 }

 dict {
    sqluserquota = mysql:/usr/local/etc/dovecot/dovecot-dict-sql-user.conf
    sqldomainquota = mysql:/usr/local/etc/dovecot/dovecot-dict-sql-domain.conf

 }

Inorder to use quota support, we need to use userdb sql instead of userdb static.


vim /usr/local/etc/dovecot/conf.d/auth-sql.conf.ext

 userdb {
  driver = sql
  args = /usr/local/etc/dovecot/dovecot-sql.conf.ext
 }


vim /usr/local/etc/dovecot/dovecot-sql.conf.ext ( /home/vmail is my maildir folder )

 driver = mysql
 connect = host=127.0.0.1 dbname=postfix user=postfix password=postfixpassword
 default_pass_scheme = MD5

 user_query = SELECT CONCAT('/home/vmail/',maildir) as home, \
                    CONCAT('maildir:/home/vmail/',maildir) as mail, \
                    CONCAT('*:bytes=', \
                     IF(mailbox.quota = 0, domain.maxquota*1024000, mailbox.quota)) \
                    as quota_rule \
             FROM mailbox, domain \
             WHERE username = '%u' AND mailbox.active = '1' AND \
                   domain.domain = '%d' AND domain.active = '1'

 password_query = SELECT username as user, password, \
                        CONCAT('/home/vmail/',maildir) AS userdb_home, \
                        CONCAT('maildir:/home/vmail/',maildir) AS userdb_mail \
                 FROM mailbox WHERE username = '%u' AND active = '1'


To determine the quota limit, Dovecot retrieves these when it authenticates a POP3/IMAP account, against the mailbox and domain tables. And when exeeded the dovecot-lda will bounce it back to the sender.

Restart Dovecot and go through the logs for debugging ..

tailf /var/log/mail.log

Sieve Filering

To use Sieve, you will first need to make sure you are using Dovecot's deliver for delivering incoming mail to users' mailboxes. Then, you need to enable the Sieve plugin in your dovecot.conf

For sieve support to Dovecot 2.2 and higher, we need to download and install dovecot-2.2-pigeonhole package from pigeonhole project.

wget http://pigeonhole.dovecot.org/releases/2.2/dovecot-2.2-pigeonhole-0.4.2.tar.gz

tar -zxvf dovecot-2.2-pigeonhole-0.4.2.tar.gz
cd dovecot-2.2-pigeonhole-0.4.2

./configure --with-dovecot=/usr/local/lib/dovecot/
make
make install

Enable Sieve Plugin in dovecot,

vim /usr/local/etc/dovecot/dovecot.conf

 plugin {
 autocreate = INBOX.Spam
 autosubscribe = INBOX.Spam
 #The location of the user's active script:
   sieve = ~/.dovecot.sieve

   # If the user has no personal active script (i.e. if the file
   # indicated in sieve= does not exist), use this one:
   sieve_before = /usr/local/lib/dovecot/sieve/default.sieve

   # The include extension fetches the :personal scripts from this
   # directory. When ManageSieve is used, this is also where scripts
   # are uploaded.
   sieve_dir = ~/sieve

   # The include extension fetches the :global scripts from this
   # directory.
   sieve_global_dir = /usr/local/lib/dovecot/sieve/global/
 }

 lda_mailbox_autocreate = yes
 lda_mailbox_autosubscribe = yes

 protocol lda {
  postmaster_address = postmaster@test.com
  mail_plugins = $mail_plugins sieve
 }


vim /usr/local/etc/dovecot/conf.d/20-lmtp.conf

 protocol lmtp {
  mail_plugins = $mail_plugins sieve
 }

Now create global sieve filer file, Here I am using this for filering the Spam mails (Flagged by spamassassin) to Spam folder.

vim /usr/local/lib/dovecot/sieve/default.sieve

 require "fileinto";
 if header :contains "X-Spam-Flag" "YES" {
   fileinto "Spam";
 }

Compile the sieve file,
sievec /var/lib/dovecot/sieve/default.sieve

or

Restart dovecot and go through the logs for debugging ..
tailf /var/log/mail.log

Thanks for reading
Cheers /-

No comments:

Post a Comment