Ever wanted to run certain packages from one version of the operating system ( in this case Debian ) without having to upgrade (or downgrade) the entire system ?
Well, the “Pin” option in the Apt package management system lets you do that. It’s pretty simple too!
1. Add the repositories for the two versions you want to have packages from in /etc/apt/sources.list . In this example we have Debian Stable and Testing:
#Stable
deb http://ftp.us.debian.org/debian stable main non-free contrib
deb http://non-us.debian.org/debian-non-US stable/non-US main contrib non-free
#Testing
deb http://ftp.us.debian.org/debian testing main non-free contrib
deb http://non-us.debian.org/debian-non-US testing/non-US main contrib non-free
Run apt-get update to add the repositories to apt.
2. Next, modify the file /etc/apt/preferences. This is where the actual “pinning” takes place. By default, apt installs the highest version available. So let’s say we would have the two repositories and we would like to install the package “dovecot-common”. Apt will take the highest version of that package which is naturally available in the “Testing” repository. But say we wanted to run the older of version of “dovecot-common” (for our own reasons, doesn’t matter why), which is available in the “Stable” repository. We would have this in our /etc/apt/preferences file:
Package: dovecot-common
Pin: release a=stable
Pin-Priority: 700
Package: dovecot-common
Pin: release a=testing
Pin-Priority: 600
Notice that we have a higher priority number for “dovecot-common” in the “stable” release. In Apt, the package with the highest priority wins, no matter the package version.
So now that you know this, you can use it to mix-match between packages from different repositories, while using strictly apt, no forcing of install and keeping them updated (if a newer version appears in that repository, apt will install it).
We have recently upgraded our mail servers running Debian Etch to Lenny and noticing that Dovecot 1.2 has been backported to Lenny backports, we decided to upgrade to Dovecot to take advantage of its fixes, improved security and quota settings.
Setting up quotas and warnings for any Dovecot above 1.0 is very simple.
1) First step is to enable the quota plugin for all protocols enabled in Dovecot ( pop3, imap and lda if you use it). This is done by just adding the line:
mail_plugins = quota
and for the IMAP protocol:
mail_plugins = quota imap_quota
2) Setup the plugin:
# Quota plugin settings
plugin {
quota = maildir:User quota
#Quota limit is 1GB
quota_rule = *:storage=1G
#We add 100Mb more for Trash
quota_rule2 = Trash:storage=100M
#We set up warnings at 75% and 90%
quota_warning = storage=75%% /opt/mail.sh 75 %u
quota_warning2 = storage=90%% /opt/mail.sh 90 %u
}
We can add as many rules and warnings as we want, just by adding and incrementing a number at the end of the warning or rule ( like above: quota_rule, quota_rule2 and so on ).
The first variable, “quota”, is the quota root and is a name that is sent to the IMAP client and can be anything you want.
The second variable is the quota itself and you can set it up using several limits: storage,bytes,messages,backend and ignore (this ignores quota for a specific mailbox). All variables support besides numbers the b/k/M/G/T/% suffixes. For example: 100M, 1G, 30% and so on.
The third variable sets up the quota warning. Using the limit you set up above in the rule you set up the warning at what level you want, by using a fixed number or percent. In the above example we used percent. In this case you need the double % ( %% ) so it can be escaped by dovecot. After the warning limit is set, in the same line, separated by space, is the command to run when that limit is reached. In this case it is a custom script that takes two command line arguments: the first one is the percent and the second one is the user that has reached the limit and to which to send the warning.
We set up the second argument (the user) as dovecot’s %u variable which is set in the ldap configuration, in the filter string. You can make this anything you want, taking the user dynamically through whichever system you have configured as your user database and whichever variable you have set up as you user.
For example, in my dovecot-ldap.conf this is what we have:
pass_filter = (&(objectClass=person)(userPrincipalName=%u))
and this is where I take my %u from.
And finally this is the script I use to send the warning. It uses the default sendmail binary, a simple text file and the two arguments taken from the command line:
#!/bin/bash
PERCENT=$1
USER=$2
echo “From: postmaster@domain.org
To: $USER
Subject: Your email quota is $PERCENT% full
Content-Type: text/plain; charset=”UTF-8″
This is an automatic message to warn that your mailbox is now $PERCENT% full.” > /tmp/quota.email.$USER
cat /tmp/quota.email.$USER | /usr/sbin/sendmail -f postmaster@domain.org $USER
rm /tmp/quota.email.$USER
That’s it! Whenever the user reaches its defined quotas he / she will receive a warning email. This quota are global, any user will have the same quota. If you want per-user quotas and / or soft-quotas check out my later edit below!
LATER EDIT:
If you want to make your quotas non-enforcing, that means the user will still receive the warnings but if he or she reaches the quota, their e-mails will not be blocked and they will still be able to receive, you must modify your “quota” variable in the plugin’s settings (the quota root). Make your settings look like this:
# Quota plugin settings
plugin {
quota = dict:user::noenforcing:file:/opt/data/mailboxes/%u/Maildir/dovecot-quota
#Quota limit is 1GB
quota_rule = *:storage=1G
#We add 100Mb more for Trash
quota_rule2 = Trash:storage=100M
#We set up warnings at 75% and 90%
quota_warning = storage=75%% /opt/mail.sh 75 %u
quota_warning2 = storage=90%% /opt/mail.sh 90 %u
}
Notice the only thing changed is the first variable: “quota”.
Also, if you want to use LDAP attributes to change your users’ quotas on a per-user basis, you need to do the following:
1) Rename your dovecot-ldap.conf file to dovecot-passdb-ldap.conf :
mv /etc/dovecot/dovecot-ldap.conf /etc/dovecot/dovecot-passdb-ldap.conf
2) Create a symlink to dovecot-passdb-ldap.conf called dovecot-userdb-ldap.conf :
ln -s /etc/dovecot/dovecot-passdb-ldap.conf /etc/dovecot/dovecot-userdb-ldap.conf
3) Modify your dovecot.conf file to point to these files as DBs for users and passwords:
passdb ldap {
args = /etc/dovecot/dovecot-passdb-ldap.conf
}
userdb ldap {
args = /etc/dovecot/dovecot-userdb-ldap.conf
}
4) And finally make sure your ” /etc/dovecot/dovecot-passdb-ldap.conf ” file looks like this:
hosts =
dn =
dnpass =
auth_bind = yes
ldap_version = 3
base = dc=example, dc=org
pass_filter = (&(objectClass=person)(userPrincipalName=%u))
user_filter = (&(objectClass=person)(userPrincipalName=%u))
user_attrs = otherPager=quota_rule=*:bytes=%$, userPrincipalName=home=/opt/data/mailboxes/%u
Notice we use the ” userPrincipalName ” as the attribute for username in dovecot (the username the users will also use to authenticate to dovecot). If you want, you can change this to whatever you want, like sAMAccountName.
Also, if you look carefully, notice that we used the ” otherPager ” attribute from LDAP as the attribute for user quota. We just modify this attribute for whatever we want to override the default quota for a certain user. For example we want to modify for user jon.doe, we just put in that attribute: ” 2G ” and the user will have 2 gigabytes. You can use any existing attribute that is not used and will not be or you can add your own to all the users in the LDAP tree.
That’s it! Restart Dovecot and it should work!
Posted on: November 2nd, 2009 More Vim tips
I will continue my previous post with another few Vim tips I recently found in my quest to improve my Vim experience!
1) For those of you annoyed by the auto-comment feature of Vim (the feature that automatically inserts a comment leader in front of a line after your previous line was also commented), just enter this:
:set fo-=r
2) If you are like me and like to use Ctrl+E and Ctrl+A in your command line and want to also use this in Vim, here is a simple way to map end of line and beginning of line to Ctrl+E and Ctrl+A in Vim:
” map CTRL-E to end-of-line (insert mode)
imap $i
” map CTRL-A to beginning-of-line (insert mode)
imap 0i
” map CTRL-E to end-of-line (normal mode)
nmap $
” map CTRL-A to beginning-of-line (normal mode)
nmap 0
3) This is for the lazy ones or the ones that just can not get adjusted to Vim’s shortcuts, here is a trick to map Ctrl+C to copy to clipboard, Ctrl+V to paste and Ctrl+X to cut:
” map CTRL-C to copy (visual mode)
vmap y
” map CTRL-X to cut (visual mode)
vmap x
” map CTRL-V to paste (insert mode)
imap P
Add them to your .vimrc and that’s it!