Postfix per domain quota

dovecotemail-serverpostfix

We are using a combination of Postfix 2.11.0 and Dovecot 2.2.9 as MTA/MDA. As a frontend for managing mail domains and mailboxes postfix.admin is used.

Since allowing users to add mailboxes to their domains can get out of control pretty easily I'd like to set a maximum mailboxes size per domain.

So for example:

domain example.com >> Domain quoata = 20000 MB

This will then allow users to setup their mailboxes freely with any desired size but will not allow them to use more than 20 GB in total (spread through all mailboxes).

To clarify I don't want to setup a per mailbox quota which is forced with this. Admins get x MB of space and can assign it to their liking.

My first thought would be adding this feature to postfix.admin on my own in the way of adding a new column to the domains table which stores the domain quota. Then each time when adding a new mailbox to a specific domain, the column containing the current mailbox quotas will be queried and summed up and then check with this sample code (this is no real code!):

if (SUM(mailbox_quota) + to_add_mailbox_quota > domain_qouta) {
    throw_warning and don't proceed
} 

But maybe I am missing out some feature, I am happy to get to know several approaches to this.

EDIT:

By evaluating postfix.admins code I found this:

 # TODO: detailed error message ("domain quota exceeded", "mailbox quota too big" etc.) via flash_error? Or "available quota: xxx MB"?
        if ( !Config::bool('domain_quota') ) {
            return true; # enforcing domain_quota is disabled - just allow it
        } elseif ($limit['quota'] <= 0) { # TODO: CHECK - 0 (unlimited) is fine, not sure about <= -1 (disabled)...
            $rval = true;
        } elseif ($quota == 0) { # trying to create an unlimited mailbox, but domain quota is set
            return false;
        } else {
            $table_mailbox = table_by_key('mailbox');
            $query = "SELECT SUM(quota) FROM $table_mailbox WHERE domain = '" . escape_string($domain) . "'";
            $query .= " AND username != '" . escape_string($this->id) . "'";
            $result = db_query ($query);
            $row = db_row ($result['result']);
            $cur_quota_total = divide_quota($row[0]); # convert to MB
            if ( ($quota + $cur_quota_total) > $limit['quota'] ) {
                $rval = false;
            } else {
                $rval = true;
            }
        }

        return $rval;

This shall if I understand it correct, do exactly what I stated above if it's set like this in the config (which it is):

// If you want to enforce domain-level quotas set this to 'YES'.
$CONF['domain_quota'] = 'YES';

But sill I can add mailboxes with 4 GB quota to a domain which has a enforced 2 GB quota, but why?

Best Answer

SOLUTION FOUND!

To use the domain quota feature of postfix 2.3.8 you have to not only force the domain quouta but also have to force quota for mailbox users in general.

This is really badly documented / undocumented / unclear from a admins perspective. I hope this helps someone in the future.

This needs to be in your config.inc.php:

// When you want to enforce quota for your mailbox users set this to 'YES'.
$CONF['quota'] = 'YES';
// If you want to enforce domain-level quotas set this to 'YES'.
$CONF['domain_quota'] = 'YES';