How To Install & Set Up Dovecot Mail Server With Sieve And Virtual Users

Channel: Linux
Abstract: /var/mail/folders/user4/usr/local/etc/dovecot.passdb user1
How To Install & Set Up Dovecot Mail Server With Sieve And Virtual Users

This document describes how to install the Dovecot mail server from source as an imap / pop3 mail server for your domain and how to set up the sieve plugin so your clients can use the sieve mail filtering language for their mail accounts.

 

1. Get the Sources

We can download Dovecot from http://dovecot.org. I always prefer stable versions so:

wget -c http://dovecot.org/releases/1.0/dovecot-1.0.13.tar.gz

We also need the sieve plugin source so:

wget -c http://dovecot.org/releases/sieve/dovecot-sieve-1.0.2.tar.gz

 

2. Install Dovecot

At this point we can continue with the installation of dovecot mail server

tar zxfv dovecot-1.0.13.tar.gz
cd dovecot
./configure
make
make install

And now we need to install the sieve plugin

tar zxfv dovecot-sieve-1.0.2.tar.gz
cd dovecot-sieve-1.0.2
./configure --with-dovecot=../dovecot-1.0.13
make
make install

 

3. Configuration

The basic configuration file of dovecot is /usr/local/etc/dovecot.conf and we can create it by doing this:

cp -pi /usr/local/etc/dovecot-example.conf /usr/local/etc/dovecot.conf

The main configuration of dovecot is here : http://wiki.dovecot.org/MainConfig

The below config is a basic configuration file for these features: Imap Protocol, Local Delivery Agent with Sieve plugin, Virtual Users from file and support other programs to authenticate with the dovecot mail server.

# Dovecot configuration file
base_dir=/usr/local/var/run/
protocols = imap
listen = *:143
# We can use plain text passwords
disable_plaintext_auth = no
# Logging
log_path = /var/log/dovecot.log
info_log_path = /var/log/dovecot.info.log
log_timestamp = "%b %d %H:%M:%S "
# SSL settings
# Without ssl
ssl_disable = yes
# Login processes
login_user = dovecot
login_greeting = Hi buddy, have an account ?
login_log_format = %$: %s
# Mailbox locations and namespaces
mail_extra_groups = mail
# Mailbox locations and namespaces
# Clients Inbox at /var/mail, Clients Folders at /var/mail/folders/username/
mail_location = mbox:/var/mail/folders/%u/:INBOX=/var/mail/%u
# Mail processes
mail_debug = yes
mail_log_prefix = "%Us(%u): "
verbose_proctitle = yes
first_valid_uid = 1000
last_valid_uid = 5000
max_mail_processes = 2048
# mbox-specific settings
mbox_read_locks = dotlock fcntl
mbox_write_locks = dotlock fcntl
# IMAP specific settings
protocol imap {
}
# LDA specific settings
protocol lda {
        postmaster_address = [email protected]
        hostname = mydomain.org
# Sieve plugin for local delivery agent
        mail_plugins = cmusieve
        log_path = /var/log/dovecot-local-deliver.log
        auth_socket_path = /usr/local/var/run/dovecot-auth-master
}
# Authentication processes
auth_verbose = yes
auth_debug = yes
auth_debug_passwords = yes
auth default {
        mechanisms = plain
        passdb passwd-file {
        # Virtual Users from file
            args = /usr/local/etc/dovecot.passdb
        }
        userdb passwd-file {
        # Virtual Users from file
                args = /usr/local/etc/dovecot.passdb
        }
        user = root
# It's possible to export the authentication interface to other programs:
# For example getmail with MDA external
        socket listen {
          master {
            path = /usr/local/var/run/dovecot-auth-master
            mode = 0660
            user = dovecot
            group = mail
          }
        }
}

We need to create the configuration file that dovecot reads to authenticate virtual users,

/usr/local/etc/dovecot.passdb
user1:{PLAIN}pass1:1001:1001:User 1 Name:/var/mail/folders/user1:: mail_plugins=cmusieve
user2:{PLAIN}pass2:1002:1002:User 2 Name:/var/mail/folders/user2:: mail_plugins=cmusieve
user3:{PLAIN}pass3:1003:1003:User 3 Name:/var/mail/folders/user3:: mail_plugins=cmusieve
user4:{PLAIN}pass4:1004:1004:User 4 Name:/var/mail/folders/user4:: mail_plugins=cmusieve

Now we are ready to test our imap mail server.

 

4. Begin & Test Dovecot

To start the dovecot mail server just type:

 /usr/local/sbin/dovecot
  ps -ef | grep dovecot

If there are any problems just see the log files.

tail -f /usr/local/dovecot*

To verify the config of running dovecot:

/usr/local/sbin/dovecot -n
# 1.0.13: /usr/local/etc/dovecot.conf
base_dir: /usr/local/var/run/
log_path: /var/log/dovecot.log
info_log_path: /var/log/dovecot.info.log
protocols: imap
listen: *:143
ssl_disable: yes
disable_plaintext_auth: no
login_dir: /usr/local/var/run//login
login_executable: /usr/local/libexec/dovecot/imap-login
login_greeting: Hi buddy, have an account ?
max_mail_processes: 2048
verbose_proctitle: yes
first_valid_uid: 1000
last_valid_uid: 5000
mail_extra_groups: mail
mail_location: mbox:/var/mail/folders/%u/:INBOX=/var/mail/%u
mail_debug: yes
mbox_read_locks: dotlock fcntl
auth default:
  verbose: yes
  debug: yes
  debug_passwords: yes
  passdb:
    driver: passwd-file
    args: /usr/local/etc/dovecot.passdb
  userdb:
    driver: passwd-file
    args: /usr/local/etc/dovecot.passdb
  socket:
    type: listen
    master:
      path: /usr/local/var/run/dovecot-auth-master
      mode: 432
      user: dovecot
      group: mail

If everything till now are running ok, you should check the mail server via telnet. To do this just type the below:

# telnet localhost 143
1 login username password

If you use the above dovecot.conf then you should test it like this:

# telnet mydomain.org 143
1 login user1 pass1

And to check the INBOX:

# telnet mydomain.org 143

* OK Hi buddy, have an account ?
1 login user1 pass1
1 OK Logged in.
2 select inbox
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft $MDNSent $Label1 NonJunk $Forwarded)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft $MDNSent $Label1 NonJunk $Forwarded \*)] Flags permitted.
* 8737 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1174418788] UIDs valid
* OK [UIDNEXT 112330] Predicted next UID
2 OK [READ-WRITE] Select completed.

And if you want to check that it finds other mailboxes:

# telnet mydomain.org 143

* OK Hi buddy, have an account ?
1 login ebal test
1 OK Logged in.
2 SELECT INBOX
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft $MDNSent $Label1 NonJunk $Forwarded $Label3 $Label2 $Label4 $Label5 Junk)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft $MDNSent $Label1 NonJunk $Forwarded $Label3 $Label2 $Label4 $Label5 Junk \*)] Flags permitted.
* 8737 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1174418788] UIDs valid
* OK [UIDNEXT 112330] Predicted next UID
2 OK [READ-WRITE] Select completed.
2 LIST "" *
* LIST (\NoInferiors \UnMarked) "/" "draft"
* LIST (\NoInferiors \UnMarked) "/" "Trash"
* LIST (\NoInferiors \UnMarked) "/" "INBOX"
2 OK List completed.

 

5. Use Sieve

The Sieve is a Mail Filtering Language and you can find everything here: http://tools.ietf.org/html/rfc3028. Dovecot plugin reads the .dovecot.sieve file from our mail location. The above dovecot.conf sets the mail location at /var/mail/folders/usename. So you need to create the file:

touch /var/mail/folders/user1/.dovecot.sieve

According to rfc you can write your rules. An example is here:

require "fileinto";
if header :comparator "i;ascii-casemap" :contains "Subject" "**SPAM**"  {
        fileinto "Trash";
        stop;
}

This sieve rule delivers every email where the header subject contains "**SPAM**" to the Trash folder.

If you want to validate your sieve rules here is the perfect project:http://libsieve-php.sourceforge.net/.

If you have apache installed you can set up web mail clients like Horde Groupware Webmail Edition or IlohaMail.

6. Usefull Links
  • Dovecot
  • Sieve
  • libsieve-php
  • Horde Groupware Webmail Edition
  • My wiki for dovecot

Ref From: howtoforge
Channels: email

Related articles