Friday 4 October 2013

Dovecot Configuration for POP & IMAP with SSL +Sieve (Mail filtering) and IMAP Quota on SENDMAIL

1.       Install the dovecot package on Sendmail server.

[root@mail01 mail]# yum install dovecot*

[root@mail01 mail]# rpm -qa | grep dovecot
dovecot-2.0.9-5.el6.x86_64
dovecot-pigeonhole-2.0.9-5.el6.x86_64

2.       Open /etc/dovecot/dovecot.conf file and change the following parameters.

[root@mail01 mail]# vi /etc/dovecot/dovecot.conf

protocols = imap pop3 lmtp sieve
mail_location = maildir:~/Maildir

# disable_plaintext_auth  value is no if pop3 need to connect on 110 port  otherwise it will connect only on secure PLAIN connection 995 port. Comment out if need to connect on 110 port.


#disable_plaintext_auth = no


Sieve (Mail filtering Language) :

Sieve is a language for filtering e-mail messages. It support as a plug-in for Dovecot's Local Delivery Agent (LDA) and also for its LMTP service. The plug-in implements a Sieve interpreter, which filters incoming messages using a script specified in the Sieve language. The Sieve script is provided by the user through New Webmail UI and, using that Sieve script, the user can customize how incoming messages are handled. Messages can be delivered to specific folders, forwarded, rejected, discarded, etc.


The managesieve daemon will listen on port 2000 but some other version it listen on port 2000. request to kindly change 20-managesieve.conf  file if it is not listen on port 2000

update /etc/dovecot/conf.d/20-managesieve.conf
service managesieve-login {
  inet_listener sieve {
    port = 2000
  }
}

Sieve Interpreter Configuration:

The part of the Sieve interpreter configuration that is relevant for ManageSieve mainly consists of the settings that specify where the user's scripts are stored and where the active script is located. The ManageSieve service primarily uses the following Sieve interpreter settings in the plugin section of the Dovecot configuration(/etc/dovecot/conf.d) update /etc/dovecot/conf.d/90-sieve.conf :

sieve_dir = ~/sieve       

This specifies the path to the directory where the uploaded scripts are stored. Scripts are stored as separate files with extension '.sieve'.

sieve = ~/.dovecot.sieve
This specifies the location of the symbolic link pointing to the active script in the Sieve storage directory. The Sieve interpreter uses this setting to locate the main script file that needs to be executed upon delivery.



Enabling Sieve plugin:

To use Sieve, you will first need to make sure you are using Dovecot LDA or LMTP (this settings is already  has been defined in above 'Protocol Configuration' section)for delivering incoming mail to users' mailboxes. Then, you need to enable the Pigeonhole Sieve plugin in your configuration:


update /etc/dovecot/conf.d/15-lda.conf
protocol lda {
mail_plugins = $mail_plugins sieve
}
update /etc/dovecot/conf.d/20-lmtp.conf
protocol lmtp {
mail_plugins = $mail_plugins sieve
}

restart the dovecot (/etc/init.d/dovecot restart) and check

#telnet centre-imap-server 2000
it should be listen

Dovecot LDA with MTA (Sendmail, Postfix etc.):

The dovecot-lda is a local mail delivery agent which takes mail from an MTA (sendmail, postfix etc.) and delivers it to a user's mailbox, while keeping Dovecot index files up to date.
Main features of the dovecot-lda are:
1. Sieve language support by the Pigeonhole sieve plugin
2. Mailbox indexing during mail delivery, providing faster mailbox access later
3. Quota enforcing by the quota plugin
To configure dovecot lda with Sendmail kindly update below sendmail.cf file.
update the /etc/mail/sendmail.mc file by making following entry.
dnl # FEATURE(local_procmail, `', `procmail -t -Y -a $h -d $u')dnl
FEATURE(`local_procmail', `/usr/libexec/dovecot/dovecot-lda',`/usr/libexec/dovecot/dovecot-lda -d $u')
MODIFY_MAILER_FLAGS(`LOCAL', `-f')
MAILER(procmail)

 Convert the sendmail.mc file sendmail.cf using m4 language. And restart the sendmail daemon.

[root@mail01 certs]# cd /etc/mail
[root@mail01 mail]# m4 sendmail.mc > sendmail.cf
            [root@mail01 mail]# service sendmail restart
            [root@mail01 mail]# chkconfig sendmail on

----------------------------------------------------------------------------------------------------------------------------
sendmail.cf (the mails will deliver through dovecot-lda instead of procmail )

below 3 lines from sendmail.cf will be removed.


Mlocal,         P=/usr/bin/procmail, F=lsDFMAw5:/|@qSPfhn9, S=EnvFromL/HdrFromL, R=EnvToL/HdrToL,
                T=DNS/RFC822/X-Unix,
                A=procmail -t -Y -a $h -d $u

and below 3 lines will be added into sendmail.cf automatically.

Mlocal,                P=/usr/libexec/dovecot/dovecot-lda, F=lsDFMAw5:/|@qSPhn9, S=EnvFromL/HdrFromL, R=EnvToL/HdrToL, T=DNS/RFC822/X-Unix,
             A=/usr/libexec/dovecot/dovecot-lda -d $u

---------------------------------------------------------------------------------------------------------------------------------



Quota:

First you have the quota root backend configuration, this quota backend specifies the method how Dovecot keeps track of the current quota usage. They don't (usually) specify users' quota limits, that's done by returning extra fields from userdb.
To define quota backend:
update /etc/dovecot/conf.d/90-quota.conf file
plugin {

 quota = maildir:User quota

 }


There are different quota backends that Dovecot can use, but in this document we are defining maildir backend here in this document we   configure maildir backend:
·maildir: Store quota usage in Maildir++ maildirsize files. This is the most commonly used quota for virtual users.

Enabling quota plugins:

update /etc/dovecot/conf.d/10-mail.conf
mail_plugins = $mail_plugins quota

update /etc/dovecot/conf.d/20-imap.conf
mail_plugins = $mail_plugins imap_quota

It will reporting quota information via IMAP.

Per-user quota:
To configure per user basis quota, we need to maintain flat file (/etc/dovecot/users) in dovecot database
Note: After creating user's home directory at your mail server you need to add user information in  /etc/dovecot/users file.

(the users entries should be below like
username:x:uid:gid:Full Name:HOME DIR:BASH:userdb_quota_rule=*:storage=2G

example
jitendrakumar:x:4321:4321:Jitendra Kumar:/home/jitendrakumar:/bin/bash:userdb_quota_rule=*:storage=2G)

now define /etc/dovecot/users into configuration file

update /etc/dovecot/conf.d/auth-system.conf.ext
passdb {
  driver = pam
}

userdb {
   driver = passwd-file
  args = username_format=%u /etc/dovecot/users
}


Quota warnings:


Now configure Dovecot to run an external command when user's quota exceeds a specified limit. Note that the warning is ONLY executed at the exact time when the limit is being crossed.
update /opt/dovecot/etc/dovecot/conf.d/90-quota.conf

plugin {
 

  quota_warning = storage=75%% quota-warning 75 %u

  quota_warning2 = storage=90%% quota-warning 90 %u

}



service quota-warning {

 executable = script /usr/local/bin/quota-warning.sh

 unix_listener quota-warning {

  mode = 0666

    }

}


With the above example when user's quota exceeds 75%, quota-warning.sh is executed with parameter 75. The same goes for when quota exceeds 90%.
You have to create the quota-warning.sh
touch /usr/local/bin/quota-warning.sh
chmod 700 /usr/local/bin/quota-warning.sh
chmod +x  /usr/local/bin/quota-warning.sh

Here is an example that sends a mail to the user:
vim /usr/local/bin/quota-warning.sh

#!/bin/sh
PERCENT=$1
USER=$2
cat << EOF | /usr/libexec/dovecot/deliver -d $USER -o "plugin/quota=maildir:User quota"

From: mailadmin-centre@example.com
Subject: Mailbox Quota Warning: $PERCENT% Full.
Mailbox quota report:
    * Your mailbox is now $PERCENT% full, please clear some files for
      further mails.
EOF




Over Quota:
If user is over quota, you can configure following settings in /etc/dovecot/conf.d/15-lda.conf

quota_full_tempfail = yes (after exceeding quota,  mails will deliver to queue directory instead of bounce back, after deleting mails from users or increasing quota by mailadmin the mails will deliver  to user INBOX automatically)

quota_full_tempfail = no (after exceeding quota mails will be bounce back)

6 comments:

  1. Hello good afternoon!

    I would like to take out a doubt you, if you can help I appreciate!

    I'm implementing the quota system, using postfix + dovecot + ldap.

    I made the necessary settings to enable quota checking the logs it is reporting the share that I predefine the ldap user, but when the quota exceeds the limit messages are not blocked.

    ReplyDelete
    Replies
    1. Hello Friend,

      I never worked on postfix but i have configured quota in sendmail with using sendmail+dovecot+LDAP. Manage Sieve provide the features to enable quota on user's mailbox.

      You are using dovecot so this document will also work for postfix+dovecot configuration.

      The /etc/dovecot/users file keeps all quota information for users.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Obrigado pelas informações!!! Vou verificar essa gerencia sieve, tem algum script que tome a ação de bloqueio ?

    ReplyDelete
    Replies
    1. Please reply in English, I don't understand french.

      Jitendra Kumar

      Delete