Skip to end of metadata
Go to start of metadata
Table of Contents


This solution does not take into account qmail-ldap clustering.

Also, I assume that all virtual users are under the same uid/gid and that the imap server is started from tcpserver


Since the imapd daemon is started from tcpserver with someting like this:

  /usr/lib/courier-imap/sbin/imaplogin \
    /var/qmail/bin/auth_imap \
    /usr/lib/courier-imap/bin/imapd "${MAILDIRNAME-Maildir}"

We "just" have to write a new authentication program, that we will call auth_cas, which is able to understand proxy tickets as valid authentication tokens. In particular, the proxy ticket will be used as the user password, and the perl client AuthCAS will be used to validate the proxy ticket and to retrieve the username of the user trying to log-in.

We immediately see a small complication: most webmail interfaces store the user password in some form (encrypted and stored in a browser cookie, or encrypted and stored in session data). So we have to use imapproxy (see ) to established a persisten imap connection with qmail-ldap. By using imapproxy, the webmail client passes the proxy ticket to imapproxy dameon, and this talk with the real imap daemon (which we are going to CASify).


Installing and configuring imapproxy is very simple, just follow the instruction on their website. The configuration I use is the following:

server_hostname localhost
 listen_port 800
server_port 843

cache_expiration_time 3600 # adjust according to your needs

So, imapproxy will be listening on, and will forward imap connections to, where qmail-ldap imap daemon is listening.


This is the script that does the real work. It validates proxy tickets, and launches the imapd daemon. If the proxy ticket validates correctly, we retrieve user information from the LDAP server, and set the environment variables needed for courier-imapd.


use AuthCAS;

use Net::LDAP;
use Net::LDAP::Util qw(ldap_error_text



my $cas = new AuthCAS(  casUrl => 'https://cas.server/url',
                        CAFile => '/etc/ca.pem',

while(<FH>) {
        if ($_ =~ m/^ST-/)  {
                my ($user, @proxies) = $cas->validatePT('{}INBOX',$PT);
#####         print LOG "User authenticated as $user via ".join(',',@proxies)."\n";

if (!$user) {
exit (1);


                my $ldap = Net::LDAP->new('');
                my $mesg =$ldap->bind();

                if($mesg->code()) {

                $result = $ldap->search(        base=> 'dc=cilea,dc=it',
                                        filter => "(&(objectClass=qmailUser)(accountStatus=active)(uid=$user))",
                if ($result->count == 1 ) {
                        foreach $entry ($result->all_entries) {

                                exec @ARGV;
                } else  { exit(1); }

} needs to be run as user /var/qmail/control/ldapuid and group /var/qmail/control/ldapgid. If your perl installation does not allow setuid perl script, you will have to compile a small wrapper auth_cas which is setuid and calls

 #define REAL_PATH "/usr/local/src/auth_cas_imap/"
main(ac, av)
     char **av;
       execv(REAL_PATH, av);

 Adjust the REAL_PATH, compile with gcc, setuid the executable and use this in the run file of qmail.

Webmail integration 

The final step is to integrate the webmail authentication with the casified imap daemon. If your webmail is php based, you will of course use phpCAS. For instance, for squirrelmail I use something like this: first I authenticate the users, the I build a form with username and proxy ticket as password, then this form is submitted to standard squirrelmail authentication page.




<form name="logincas" action="/webmail/src/redirect.php" method="post">

<input type="hidden" name="login_username" value="<?=$nomeutente?>">
<input type="hidden" name="secretkey" value="<?=$pt?>">



  • No labels