FreeBSD, Dovecot and Sieve

 03.05.2013 -  Louis Kowolowski -  ~3 Minutes

This outlines the things I had to do on the backend to get sieve filtering working on Dovecot 2.x on FreeBSD   .

It should be portable to other platforms, but you may have to adjust paths. This won’t really discuss the details of setting up Dovecot, except where relevant to sieve. I will assume you already have Dovecot (2.x) server connected to an MTA (I use postfix, but others work just fine). I also run a virtual environment, so this will reflect some of those specifics as well.

Installation

You will need to install the pigeonhole managesieve   pkg linked off the dovecot wiki. In FreeBSD   , you can do

	pkg_add dovecot2-managesieve

or

	make install clean

from the dovecot2-managesieve directory in the ports tree.

Postfix transports

For reference, the postfix transport looks like this:

	virtual_transport = lmtp:unix:private/dovecot-lmtp

Dovecot config bits

You will need to define some things in your dovecot.conf:

	lmtp_save_to_detail_mailbox = yes
	mail_home = /usr/local/virtual/%d/%n
	mail_location = maildir:~/Maildir

This first part adds an entry in your received headers indicating the message traversed to the dovecot server over LMTP. The second part defines the virtual users homedir, and then where mail should be stored relative to the homedir.

	plugin {
	  autocreate = Trash
	  autocreate2 = Junk
	  autosubscribe = Trash
	  autosubscribe2 = Junk
	  sieve = /usr/local/virtual/%d/%n/dovecot.sieve
	  sieve_dir = /usr/local/virtual/%d/%n/
	  sieve_global_dir = /usr/local/virtual/sieve
	  sieve_global_path = /usr/local/virtual/sieve/globalfilter.sieve
	}

This allows us to dynamically create Trash and Junk folders and have them appear in the mail client. We want to do this so we don’t inadvertently filter into a non-existent location when we deploy a global filter. The sieve directives define where the per-user sieve files will be, and also where the global sieve files will be.

	protocols = imap pop3 lmtp sieve

Ensure we listen for sieve communication (you will want to disable unused protocols, and firewall sieve, from the outside world).

	service lmtp {
	  unix_listener /var/spool/postfix/private/dovecot-lmtp {
	    group = postfix
	    mode = 0660
	    user = postfix
	  }
	}
	protocol lmtp {
	  mail_plugins = " sieve"
	  postmaster_address = postmaster@txt.com
	  quota_full_tempfail = yes
	}

This defines the location of the lmtp unix socket dovecot will listen on. It also includes the sieve plugin, which allows us to filter messages at delivery time.

	service managesieve-login {
	  inet_listener sieve {
	    port = 4190
	  }
	  process_min_avail = 0
	  service_count = 1
	  vsz_limit = 64 M
	}
	service managesieve {
	  process_limit = 1024
	}
	protocol sieve {
	  mail_max_userip_connections = 10
	  managesieve_implementation_string = Dovecot Pigeonhole
	  managesieve_logout_format = bytes=%i/%o
	  managesieve_max_line_length = 65536
	}

These setup the sieve communication for users. Depending on how you allow sieve communication, you could offer direct interaction with the sieve server (such as telnet sieve_server 4190), or proxied communication with the sieve server (such as through a web interface like roundcube).