Quota
=====

There are different quota backends that Dovecot can use:

 * <fs> [Quota.FS.txt]: Filesystem quota.
 * <dirsize> [Quota.Dirsize.txt]: The simplest and slowest quota backend.
 * <dict> [Quota.Dict.txt]: Store quota in a dictionary (e.g. SQL).
 * <maildir> [Quota.Maildir.txt]: Maildir++ quota. This is the most commonly
   used quota for virtual users.

See <Quota.New.txt> for Dovecot v1.1 / quota-rewrite patch quota configuration.

Enabling quota plugins
----------------------

There are currently two quota related plugins:

 * quota: Implements the actual quota handling and includes also all the quota
   backends.
 * imap_quota: For reporting quota information via IMAP.

Usually you'd enable these by adding them to the 'mail_plugins' settings in the
config file:

---%<-------------------------------------------------------------------------
protocol imap {
  mail_plugins = quota imap_quota
}
protocol pop3 {
  mail_plugins = quota
}
# In case you're using deliver:
protocol lda {
  mail_plugins = quota
}
---%<-------------------------------------------------------------------------

Configuring quota
-----------------

Most of the quota backends have very similar configuration. They support two
kinds of quota limits:

 * *storage*: Quota limit in kilobytes.
 * *messages*: Quota limit in number of messages. This isn't probably very
   useful.

You can configure quota globally by placing the settings in plugin section in
'dovecot.conf' and you can give per-user limits by having your <userdb>
[UserDatabase.txt] return the quota setting as an <extra field>
[UserDatabase.ExtraFields.txt]. The userdb quota setting always overrides the
global plugin setting.

The important thing to remember is to *use the correct format for quota
setting*. You can't just return a numeric quota field from userdb and expect it
to work. Dovecot wouldn't then know what quota backend to use.

Here is an example global quota configuration:

---%<-------------------------------------------------------------------------
plugin {
  # 10 MB + 1000 messages quota limit
  quota = maildir:storage=10240:messages=1000
}
---%<-------------------------------------------------------------------------

Now if you want to override this for some users, make your userdb return quota
field *in the exact same format*. See below for some examples.

Quota and Trash mailbox
-----------------------

Standard way to expunge messages with IMAP works by:

 1. Marking message with \Deleted flag
 2. Actually expunging the message using EXPUNGE command

Both of these commands can be successfully used while user's quota is full.
However many clients use a "move-to-Trash" feature, which works by:

 1. COPY the message to Trash mailbox
 2. Mark the message with \Deleted
 3. Expunge the message from the original mailbox.
 4. (Maybe later expunge the message from Trash when "clean trash" feature is
    used)

If user is over quota, the first COPY command will fail and user may get an
unintuitive message about not being able to delete messages because user is
over quota. The possible solutions for this are:

 * Disable move-to-trash feature from client
 * Dovecot v1.0 + <Maildir++> [Quota.Maildir.txt] quota: You can completely
   ignore Trash mailbox from quota calculation by appending ':ignore=Trash' to
   the quota line. Note that this would allow users to store messages
   infinitely to the mailbox.
 * Dovecot v1.1 or <v1.0 quota rewrite> [Quota.New.txt]: You can give a
   separate quota rule giving Trash mailbox somewhat more quota.

To make sure users don't start keeping messages permanently in Trash you can
use a nightly<cronjob or expire plugin (v1.1)> [Plugins.Expire.txt] to expunge
old messages from Trash mailbox.

Examples
--------

SQL
---

---%<-------------------------------------------------------------------------
# MySQL, quota in kilobytes:
user_query = SELECT home, uid, gid, concat('maildir:storage=', quota_kb) AS
quota FROM users WHERE userid = '%u'

# MySQL, quota in bytes:
user_query = SELECT home, uid, gid, concat('maildir:storage=',
floor(quota/1024)) AS quota FROM users WHERE userid = '%u'

# PostgreSQL, SQLite, quota in kilobytes:
user_query = SELECT home, uid, gid, 'maildir:storage=' || quota_kb AS quota
FROM users WHERE userid = '%u'
---%<-------------------------------------------------------------------------

LDAP
----

The easiest way from Dovecot's point of view is if you already have the quota
in Dovecot's format in LDAP (e.g.'maildir:storage=102400'. Then you can use a
configuration like this:

---%<-------------------------------------------------------------------------
user_attrs = homeDirectory=home,uidNumber=uid,gidNumber=gid,quotaDovecot=quota
---%<-------------------------------------------------------------------------

Unfortunately usually this isn't the case. So if you have the quota in
kilobytes in LDAP, you can use it in a bit kludgy way:

---%<-------------------------------------------------------------------------
user_attrs =
homeDirectory=home,uidNumber=uid,gidNumber=gid,quotaKb=quota=maildir:storage
---%<-------------------------------------------------------------------------

If you have the quota stored as bytes, you'll need to use a <post-login
scripting> [PostLoginScripting.txt] trick to use them. Something like:

---%<-------------------------------------------------------------------------
# quotaBytes is exported to $QUOTA_BYTES environment
user_attrs =
homeDirectory=home,uidNumber=uid,gidNumber=gid,quotaBytes=quota_bytes
---%<-------------------------------------------------------------------------

And make imap's 'mail_executable' point to a script:

---%<-------------------------------------------------------------------------
#!/bin/sh

export QUOTA=maildir:storage=`expr $QUOTA_BYTES / 1024`
exec /usr/local/libexec/dovecot/imap
---%<-------------------------------------------------------------------------

This post-login trick unfortunately doesn't work with <deliver> [LDA.txt]. If
you need it, you're pretty much out of luck for now.<v1.1 quota>
[Quota.New.txt] makes this possible.

(This file was created from the wiki on 2008-05-04 04:42)
