Quotas

Your mail server doesn't have infinite space. Especially if you use IMAP then users appreciate the comfort of leaving their emails on the server. And there are even IMAP users who are not aware of emails that are just marked for deletion but are still occupying space. So unless you have very few or just highly disciplined users you may want to limit the space a user can occupy. Dovecot can store the size of a user's mailbox and the number of mails therein along with the virtual folder.

Enabling the quota plugin in Dovecot

There are three locations in the /etc/dovecot/dovecot.conf file where you need to enable the quota plugin (taken from the Dovecot documentation):

protocol imap {
  mail_plugins = quota imap_quota
}
protocol pop3 {
  mail_plugins = quota
}
protocol lda {
  mail_plugins = quota
} 

Setting global quota

The simplest case is a general quota limit for all users. Say you grant every user 1 GB of space with no more than 1000 emails. This would be the configuration in your /etc/dovecot/dovecot.conf file:

plugin {
  quota = maildir:storage=1000000:messages=1000
} 

Keep in mind that the storage parameter refers to KB. You can even omit the messages parameter if you don't intend to limit the number of messages.

Setting per-user quota

If you have certain users who have different quotas from your global quota setting then you can store the quota settings in your virtual_users table. Use this SQL statement to add two columns to the virtual_users table:

mysql>
ALTER TABLE `virtual_users` ADD `quota_kb` INT NOT NULL,
ADD `quota_messages` INT NOT NULL ;

And you will have to enable the user_query in your /etc/dovecot/dovecot-sql.conf. Throughout this tutorial it was suggested you use the "userdb static" approach in your /etc/dovecot/dovecot.conf. But this assumes that all users have equal settings. So in this per-user quota situation you will need to disable "userdb static" again and enable "userdb sql" to make Dovecot make an extra query to fetch the user's data from the database:

#userdb static {
#  args = uid=5000 gid=5000 home=/var/vmail/%d/%n allow_all_users=yes
#

userdb sql {
    args = /etc/dovecot/dovecot-sql.conf
}

And in your /etc/dovecot/dovecot-sql.conf add this line:

user_query = SELECT CONCAT('/var/vmail/',CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1))) AS home, 5000 AS uid, 5000 AS gid, CONCAT('maildir:storage=',quota_kb,':messages=',quota_messages) AS quota FROM virtual_users WHERE email='%u';

This query may look a bit weird but it does the same as the above

args = uid=5000 gid=5000 home=/var/vmail/%d/%n allow_all_users=yes

line and in addition gets the quota_kb and quota_messages information.

What happens if a user is over quota

Quotas in Dovecot aren't especially user-friendly. For example the recipient does not get a warning at a soft quota limit. Just the sender gets a bounce email with the subject reading "Automatically rejected mail" saying "Your message to <john@example.com> was automatically rejected: Quota exceeded"

Comments

Debian Lenny has the version

Debian Lenny has the version 1.0.15 of Dovecot. The feature you are quoting is not supported before version 1.1. So this wouldn't work unless the Debian package has patches for that functionality. And from a quick glance over the source package it doesn't.

Hi Christoph Hass, you're wrong

>Hi Christoph, you're wrong. Take a look on the 1.0 dovecot quota page http://wiki.dovecot.org/Quota/1.0?highlight=quota Best Regards, Jhanssen Oliveira.

What's your point?

What's your point?

maildir

fi you use a thunderbird Pluginn called "display quota" and only a few email

users on your server - use

quota = dirsize:storage=1000000:messages=1000
instead of

quota = maildir:storage=1000000:messages=1000

with "maildir" the level of my mailbox isn't displayed correct. It is only displaying the level in trash.

If anybody get another option to do this - plz tell. High CPU load isn't good

1.2 compatability mod

Add to dovecot.conf

mail_uid = vmail
mail_gid = vmail

change to the following:

  mechanisms = plain login digest-md5 cram-md5

  #passdb passwd-file {
    # File contains a list of usernames, one per line
    #args = /etc/dovecot.deny
  #}
  passdb sql {
    args = /etc/dovecot/dovecot-sql.conf
  }
  #userdb passwd {
    # [blocking=yes] - By default the lookups are done in the main dovecot-auth
    # process. This setting causes the lookups to be done in auth worker
    # proceses. Useful with remote NSS lookups that may block.
    # NOTE: Be sure to use this setting with nss_ldap or users might get
    # logged in as each others!
    #args = 
  #}

  #userdb passwd-file {
    # Path for passwd-file
    #args
  #}
  #userdb static {
    # Template for the fields. Can return anything a userdb could normally
    # return. For example:
    #
    #  args = uid=5000 gid=5000 home=/var/mail/%d/%n allow_all_users=yes
    #
    # If you use deliver, it needs to look up users only from the userdb. This
    # of course doesn't work with static because there is no list of users.
    # Normally static userdb handles this by doing a passdb lookup. This works
    # with most passdbs, with PAM being the most notable exception. If you do
    # the user verification another way, you can add allow_all_users=yes to
    # the args in which case the passdb lookup is skipped.
    #
    #args =
  #}

  userdb sql {
    # Path for SQL configuration file
    args = /etc/dovecot/dovecot-sql.conf
  }
protocol lda {
    log_path = /var/log/dovecot.log
    auth_socket_path = /var/run/dovecot/auth-master
    postmaster_address = postmaster@domain.com
    mail_plugins = quota sieve
    global_script_path = /var/mail/globalsieverc
}

plugin {
  quota = maildir:User quota
  quota_rule = *:storage=75M
  quota_rule2 = Trash:storage=50M

  quota_warning = storage=95%% /var/mail/quota-warning.sh 95
  quota_warning2 = storage=80%% /var/mail/quota-warning.sh 80

}

and in /var/mail/quota-warning.sh
#!/bin/sh

PERCENT=$1
FROM="postmaster@domain.org"
qwf="/tmp/quota.warning.$$"

echo "From: $FROM
To: $USER
To: postmaster@fyn.nl
Subject: Your email quota is $PERCENT% full
Content-Type: text/plain; charset="UTF-8"

Your mailbox is now $PERCENT%+ full, please make some more room or contact your administrator" >> $qwf

cat $qwf | /usr/sbin/sendmail -f $FROM "$USER"
rm -f $qwf

exit 0

And don't forget to chown that one to vmail:vmail.

Now for dovecot-sql.conf:

driver = mysql
connect = host=127.0.0.1 dbname=provider  user=provider  password=<password>
user_query = SELECT CONCAT('/var/mail/',CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1))) AS home, 5000 AS uid, 5000 AS gid, CONCAT('*:bytes=',quota_kb) AS quota_rule FROM virtual_users WHERE email='%u';
default_pass_scheme = plain login crypt digest-md5 cram-md5

Details....

Edit the quota_kb to allow varchar and insert the limits like 500K, 100M, 1G etc.

Details......

password_query = SELECT email as user, password, 5000 AS userdb_uid, 5000 AS userdb_gid, CONCAT('*:bytes=',quota_kb) AS userdb_quota_rule FROM virtual_users WHERE email='%u' ;


To finish the deal

Does not work for me

I also had the idea to handle it that way, but it just won't work for me.
I always get this error:

 

Jul 14 14:13:32 MY_HOST dovecot: IMAP(sebastian.buettner@MY_DOMAIN): maildir quota: Unknown setting: User quota
 

Is there any point I could start solving this problem?

 

Thanks and cheers,

Sebastian

Bravo!

Great job! Really great!

I'm interested in finding out (if you have the time, that is) how to put per-domain quota.

That feature would really make my day! :)

 

Best wishes!

Dovecot 1.1

Thanks Christoph. I've made it this far without a problem. The best Linux Tut I've seen by far!

I followed the instructions above, restarted dovecot and bang, it crashed. Checked the version to find that it is
1.1.11 and not 1.0 as you indicated earlier. It must be a new(ish) update as I only installed Lenny about 10 days
ago.

I'm having a go at the instructions at http://wiki.dovecot.org/Quota/1.1 but a simplified version in your inimitable
style would really help.

Regards
 Grant

Dovecot 1.1

Oh boy, my bad.


I started by installing Lenny but couldn't get gnome to work with my nvidia card so ended up installing
Ubuntu Karmic.

The tutorial was working so well I forgot :(

Quota exceeded with recipient's address shown

Great tutorial.  I'm a newbie.  I followed your tutorial on debian etch and am using dovecot 1.0.rc15.  Everything works fine except for quota.  When a mailbox quota exceeds, all the sender gets is this bounce e-mail:

"Your message was automatically rejected by Dovecot Mail Delivery Agent.

The following reason was given:
Quota exceeded"

Can the recipient e-mail address be added to the above message without upgrading dovecot?

I've seen something called "backports" when googling.  If I really have to upgrade dovecot, is it the way to go?  And will the new dovecot recognize the existing 'dovecot.conf' and others?
 

problem with schema mysql to upgrade dovecot 1.2

Hi , 

I have a problem with upgrade dovecot 1.2 . Dovecot 1.2 use a new schema for mysql quota . i create a new table as here

CREATE TABLE quota (

 

username varchar(100) not null,
bytes bigint not null default 0,
messages integer not null default 0,
primary key (username)
);

In my file dovecot-sql.conf i add this line 

user_query = SELECT [/var/vmail/] AS home, 5000 AS uid, 5000 AS gid, CONCAT('*:bytes=', CAST(quota AS CHAR)) AS quota_rule FROM mailbox WHERE username = '%u';

but after reload dovecot  the quota don't work for my all account . 

yet i use to avoid the problem . 

In my dovecot.conf 

 

plugin {
  sieve=~/.dovecot.sieve
  sieve_dir=~/sieve
  quota = dict:user::proxy::quotadict
  quota_rule = *:storage=100M
  quota_warning = storage=95%% /usr/local/bin/quota-warning.sh 95
}
the problem with this method , the quota are not personalized .
 
Are you a idea to resolve my problem ?