[2019-12-31]

Quotas

Now this is a feature that caused me quite some headache. But in the end it was pretty simple. Quotas are size limits for users. You can make sure that users do not waste infinite amounts of disk space but are forced to clean up old emails every now and then.

The magic happens in two places. Postfix needs to reject new emails if the user’s mailbox is over quota. And Dovecot needs to keep track of the quota and how much the user has already used up of it.

Dovecot quota policy service

Let’s start with Dovecot. Find the file /etc/dovecot/conf.d/90-quota.conf and edit it. There are several “plugin {…}” sections. Take one and make it look like:

plugin {
  quota = maildir:User quota

  quota_status_success = DUNNO
  quota_status_nouser = DUNNO
  quota_status_overquota = "452 4.2.2 Mailbox is full and cannot receive any more emails"
}

The first line defines that you want to calculate the used space in a user’s maildir. There are several backends like that but the maildir is the best choice in this context. The string “User quota” is just some random string that may be queried from a mail user agent.

The lines starting with “quota_status_…” set return values for the service that will set up in a minute. It will tell Postfix that it will not interfere (DUNNO – colloquial way to say “don’t know”). And it will return a string with a return code 452 if the user is over quota. Codes starting with “4” mean temporary errors. It will tell the sending party that it is worth retrying at a later time. However if the user does not resolve the issue it will lead to a bounce error email after three days.

In the same file (90-quota.conf) add a new section:

service quota-status {
  executable = /usr/lib/dovecot/quota-status -p postfix
  unix_listener /var/spool/postfix/private/quota-status {
    user = postfix
  }
}

This creates a new Dovecot service responding to requests from other processes. You surely recognize that we put it into the jail that Postfix runs in (/var/spool/postfix). So Postfix can access it.

Time to restart Dovecot:

systemctl restart dovecot

Take a look at the /var/spool/postfix/private directory. If all went as intended you will find a socket file called quota-status there. Otherwise please check the /var/log/mail.log file for errors.

Postfix recipient restrictions

If we stopped here then Dovecot would reject emails for users who have no space left. However Postfix would happily receive new emails and only later get rejected when talking to Dovecot via LMTP. It will then keep the email in its queue and retry for a while. In the end it will send a bounce back to the sender telling them about the problem. So why is this bad?

  1. The sender will assume that the email was delivered while it is stuck in the queue for up to three days.
  2. Spam emails use forged senders. So at the time that Postfix generates the bounce email it will likely send it to an innocent person. This is called backscatter and considered a mail server misconfiguration. Such a problem may get your mail server blacklisted. You don’t need that.

So the next logical step is to make Postfix check whether a mailbox is over quota whenever a new email arrives. Let’s hook up into the “RCPT TO” phase of the SMTP dialog when a new email comes in. Postfix checks its smtpd_recipient_restrictions configuration at this stage. Run this command in the shell:

postconf "smtpd_recipient_restrictions = \
     reject_unauth_destination \
     check_policy_service unix:private/quota-status"

This adds two checks:

  1. reject_unauth_destination checks whether the mail server is the final destination for the recipient’s email address. This is pretty much the default behavior if you do not define any restrictions.
  2. check_policy_service connects to the socket file at /var/spool/postfix/private/quota-status that was put there by Dovecot. It will use it to ask Dovecot whether the user is over quota in which case the email would get rejected.

Test it

If you are curious to see this working, then set John’s mailbox to just a few KB and send him several test emails using swaks. After a few emails you will see the rejection message:

-> RCPT TO:john@example.org
 <** 552 5.2.2 john@example.org: Recipient address rejected: Mailbox is full and cannot receive any more emails

Your users may complain that they have deleted many emails but are still over quota. Let them check if they actually emptied the Trash folder. Of course emails in that folder also contribute to the disk space usage. Once the Trash folder is expunged the problem should be gone.

Automatic warning emails

The last step is to inform the poor users if they accidentally went over quota. After all they do not necessarily reccognize that on their own. Let’s do that by sending them an email with a warning. Yes, we will make sure that the email gets through even if the quota is reached.

You still have the 90-quota.conf open in your editor? Good. Add this section to the file (derived from the Dovecot documentation):

plugin {
   quota_warning = storage=95%% quota-warning 95 %u
   quota_warning2 = storage=80%% quota-warning 80 %u
   quota_warning3 = -storage=100%% quota-warning below %u
}
service quota-warning {
   executable = script /usr/local/bin/quota-warning.sh
   unix_listener quota-warning {
     group = dovecot
     mode = 0660
   }
 }

This section defines three automatic quota warnings. The first (quota_warning) is triggered if the user reaches 95% of the quota. The second (quota_warning2) at 80%. And the third if the user had reached 100% but has removed enough emails to have gone below 100%. These lines follow this schema:

  • Trigger (e.g. “storage=95%”). The “%” sign needs to be used twice if you want to emit a literal percent sign. So this is not a typo.
  • The socket you want to call in that case. Our socket is the “service quota-warning” that calls a shell script.
  • Additional parameters that are passed to the shell script in our case. They tell the script the percentage that has been reached (e.g. 95) and the address of the user who should get the warning.

Apparently we need the script to run. So please create a new file at /usr/local/bin/quota-warning.sh and put these lines into it:

#!/bin/sh
PERCENT=$1
USER=$2
cat << EOF | /usr/lib/dovecot/dovecot-lda -d $USER -o "plugin/quota=maildir:User quota:noenforcing"
From: postmaster@webmail.example.org
Subject: Quota warning - $PERCENT% reached

Your mailbox can only store a limited amount of emails.
Currently it is $PERCENT% full. If you reach 100% then
new emails cannot be stored. Thanks for your understanding.
EOF

Make this file executable:

chmod +x /usr/local/bin/quota-warning.sh

Time to restart Dovecot again:

systemctl restart dovecot

Recalculate quota

If you directly remove files from a user’s Maildir instead of properly accessing the mailbox using IMAP then you will mix up the quota calculation. To force Dovecot to recalculate the quota you will have to run:

doveadm quota recalc -u john@example.org

38 thoughts on “Quotas

  • 2019-12-31 at 14:33
    Permalink

    I think (!) the proper error code for temporary rejection due to quota is

    quota_status_overquota = “452 4.2.2 Mailbox is full and cannot receive any more emails”

    Maybe add this for completeness. 🙂

    Reply
    • 2019-12-31 at 21:03
      Permalink

      I would agree. SMTP error code 422 seems more appropriate from my research as well.

      Reply
      • 2020-01-01 at 11:22
        Permalink

        The RFC seems to refer to “disk full” with 4.2.2 responses but not “user over quota”. However a 4.x.x error seems to be more appropriate because the error is not necessarily permanent and retrying is a good idea. I’ll fix that.

        Reply
        • 2020-05-09 at 18:55
          Permalink

          How about: “552 Requested mail action aborted: exceeded storage allocation”?

          Also, I am wondering about the other code “4.2.2”, what does that refer to?

          Reply
          • 2020-05-10 at 12:59
            Permalink

            Ignore me; realised that’d be understood as a permanent error…

  • 2020-01-02 at 04:42
    Permalink

    Dovecot was failing for me until I move the { onto the same line as unix_listener: doveconf: Fatal: Error in configuration file /etc/dovecot/conf.d/90-quota.conf line 95: Expecting ‘{‘

    This worked:
    service quota-status {
    executable = /usr/lib/dovecot/quota-status -p postfix
    unix_listener /var/spool/postfix/private/quota-status {
    user = postfix
    }
    }

    Reply
    • 2020-01-02 at 11:57
      Permalink

      Good. Fixed in the documentation, too.

      Reply
      • 2020-01-03 at 15:52
        Permalink

        So if I follow your guide and set 1 073 741 is it something like a 1 G ?

        Reply
        • 2020-01-03 at 16:20
          Permalink

          Without any spaces, but yes, that should do its job.

          Reply
          • 2020-01-16 at 10:25
            Permalink

            Honestly I don’t think so – if its really “Bytes” then it should be 1073741824…. and with an Int(11) in the database the maximum value would be 2147483648 which means it would only support around 2 GB…. nowadays that’s not that much if you imagine how much storage you could get on free mail hoster. Wouldn’t it be biter (if I’m not wrong) to change either the database structure to bigint or to use kilobytes ?

  • 2020-01-06 at 09:50
    Permalink

    This page is missing its “next” button

    Reply
    • 2020-01-06 at 10:40
      Permalink

      Thanks, fixed.

      Reply
  • 2020-01-27 at 11:57
    Permalink

    Hi Christoph,

    I have a problem. With smtpd_recipient_restrictions = reject_unauth_destination I can send email to my virtual domain only. If I try to send mail to external domain, I receive a relay denied, even if I put my source ip in mynetworks variable.
    It’s resolvible? Thank You so much

    Reply
    • 2020-01-27 at 16:08
      Permalink

      I have the exact same problem.
      Could it be possible that the smtpd_recipient_restrictions should be empty or commented out like in the relaying-through-postfix chapter it is said? Or that the smtpd_relay_restrictions replaces the smtpd_recipient_restrictions?

      Reply
      • 2020-01-27 at 19:31
        Permalink

        I was wrong. The trick is to remove the ‘#’ in front of ‘-o smtpd_recipient_restrictions=’ in the submission section of /etc/postfix/master.cf. After that you can send out e-mails to other domains/mail-servers.
        The only question now is if you can remove the standard ‘smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination’ in /etc/postfix/main.cf, because there is ‘smtpd_recipient_restrictions = reject_unauth_destination check_policy_service unix:private/quota-status’ or if you can remove the latter one and set ‘smtpd_relay_restrictions’ to ‘smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination check_policy_service unix:private/quota-status’? I dont think that you need both of them or at least only one of them needs a *_unauth_destination option in it.

        Reply
    • 2020-04-22 at 18:40
      Permalink

      Hi,

      I think the solution is to only have

      smtpd_recipient_restrictions = check_policy_service unix:private/quota-status

      From http://www.postfix.org/postconf.5.html#smtpd_recipient_restrictions:
      Optional restrictions that the Postfix SMTP server applies in the context of a client RCPT TO command, after smtpd_relay_restrictions. See SMTPD_ACCESS_README, section “Delayed evaluation of SMTP access restriction lists” for a discussion of evaluation context and time.

      With this, the reject_unauth_destination is not present anymore. Relaying is already stopped by smtpd_relay_restrictions, so I think there should be no harm done there.

      Best regards
      Christof

      Reply
  • 2020-01-28 at 19:36
    Permalink

    The quota-warning service did not work for me:
    lmtp(john@example.org): Error: program unix:/var/run/dovecot/quota-warning: net_connect_unix(/var/run/dovecot/quota-warning) failed: Permission denied (euid=5000(vmail) egid=5000(vmail) missing +r perm: /var/run/dovecot/quota-warning, dir owned by 0:0 mode=0755)

    I needed to switch to ‘group = vmail’ in ‘service quota-warning’.

    Also I like to use two additional quota rules, one for additional space in trash and another one to limit the number of emails:
    quota_rule2 = Trash:storage=+100M
    quota_rule3 = *:messages=100000

    Reply
  • 2020-02-22 at 23:17
    Permalink

    Hi,

    I changed the “mail_plugins” line in /etc/dovecot/conf.d/10-mail.conf
    from
    mail_plugins = quota
    to
    mail_plugins = quota imap_quota

    The result is, that I can see the quota setting of a Mailbox and the current usage of it in IMAP Clients (e.g. Thunderbird). Is there any reason why this setting shoud not be used?

    Reply
    • 2020-02-23 at 10:37
      Permalink

      Correction:
      I had to add “imap_quota” to 20-imap.conf.
      So the “mail_plugins” line in /etc/dovecot/conf.d/10-mail.conf stays
      mail_plugins = quota
      I added the following line to to /etc/dovecot/conf.d/to 20-imap.conf:
      mail_plugins = $mail_plugins imap_quota
      in the “protocol imap” section.

      Reply
      • 2020-03-22 at 08:11
        Permalink

        Thanks Christian… it was so usefull

        Reply
  • 2020-03-03 at 22:57
    Permalink

    Hello,

    I have tested this attractive solution, but alias does not work because Postfix does alias map too late when using “check_policy_service” (you can check it enabling verbose mode with “smtpd -v”).

    So only original “TO: ” address is used, i.e. alias, which is unknown for Dovecot.

    Tinkering Dovecot to include alias is not the solution because it’s the job of Postfix.

    Does anyone have a solution?

    Reply
    • 2020-05-24 at 20:28
      Permalink

      Hello,

      I confirm the issues of k049873. I have the same here. A soon as I have in main.cf the line
      “smtpd_recipient_restrictions = reject_unauth_destination check_policy_service unix:private/quota-status” the server does not accept virtual aliases emails anymore and reject them with “Recipient address rejected: Unknown user;”
      The only way to get it to work, change it to the “smtpd_recipient_restrictions = reject_unauth_destination ”

      Any idea, how to get quota to work?

      Reply
      • 2020-05-25 at 21:24
        Permalink

        I finally got it to work. Quota was not setup correctly. Now dovecot still shows “dovecot: auth: unknown user”, but the emails are delivered correctly for aliases.

        Reply
        • 2020-08-07 at 05:28
          Permalink

          Hi,

          I don’t know if you still need the solution, but here it is.

          I had the same problem. I solved it by following the ‘Optional: Catch-all aliases’ section on ‘Making Postfix get its information …’ chapter.

          Apparently, the user still needs mapping to itself to be recognized.

          I hope the author can help with some clarification.

          Reply
    • 2020-03-08 at 22:39
      Permalink

      That should work. You could also –attach a larger file.

      Reply
  • 2020-03-11 at 20:54
    Permalink

    if anyone wants to get the quota information of all mailboxes, you will get the following error:

    $ doveadm quota get -A
    Error: User listing returned failure
    doveadm: Error: Failed to iterate through some users
    Username Quota name Type Value Limit %

    to fix that, you sould edit the /etc/dovecot/dovecot-sql.conf.ext

    and add this line at the end of the file:
    iterate_query = SELECT email AS user FROM virtual_users

    Reply
    • 2020-03-22 at 15:09
      Permalink

      That is the last line of text to be added to the bottom of the /etc/dovecot/dovecot-sql.conf.ext file as presented on the “Setting up Dovecot” page of the tutorial.

      Reply
  • 2020-03-31 at 08:08
    Permalink

    If anyone is following this guide to configure a mail server on Centos 8, you need to replace
    executable = /usr/lib/dovecot/quota-status -p postfix
    with
    executable = /usr/libexec/dovecot/quota-status -p postfix

    Reply
    • 2020-05-29 at 11:58
      Permalink

      I’m using this guide on Slackware and had exactly the same issue.

      “Fatal: service(quota-status) access(/usr/lib/dovecot/quota-status) failed: No such file or directory”

      executable = /usr/libexec/dovecot/quota-status -p postfix fixed this problem for me too

      Reply
  • 2020-07-21 at 08:37
    Permalink

    You set:

    quota_status_overquota = “452 4.2.2 Mailbox is full and cannot receive any more emails”

    yet in your test example you get a completely different error code:

    <** 552 5.2.2 john@example.org: Recipient address rejected: Mailbox is full and cannot receive any more emails

    Googling some SMTP error codes, it seems that 552 5.2.2 is preferred for this case, even Dovecot uses it in its example at https://doc.dovecot.org/configuration_manual/quota_plugin/, so that's what I'm going to use.

    Reply
  • 2020-08-26 at 12:24
    Permalink

    Hi!
    given the fact that ” check_policy_service unix” comes last:

    postconf “smtpd_recipient_restrictions = \
    reject_unauth_destination \
    check_policy_service unix:private/quota-status”

    What would happen if the policy server returns “action=DEFER_IF_PERMIT Temporary internal error” . Will the email be REJECTed ?

    Reply
  • 2020-09-14 at 11:44
    Permalink

    Does this service provide status in realtime ? Is there any cron or config that updates the users quotas ?

    Reply
  • 2020-10-02 at 10:48
    Permalink

    Hi, if I want to set the quota to 5 Gb, how should I do it?

    Reply
    • 2020-10-05 at 14:22
      Permalink

      Dovecot understands shortcuts like 5G. But we are using a BIGINT field in this guide. So you would have to convert 5 GB to bytes.

      Reply
  • 2020-10-23 at 07:30
    Permalink

    We use warning emails to notify you of quotas.

    In rare cases, the email address has changed to the logged-in user.
    So I couldn’t send the email with “Recipient address rejected: User unknown in local recipient table”.
    why?

    Reply

Leave a Reply to Markus Cancel reply

Your email address will not be published. Required fields are marked *