[PATCH] Add support for Dovecot to mod_auth_imap2

I’ve been using mod_auth_imap for authenticating against IMAP for several years. When I recently switched to Dovecot from Courier IMAP, mod_auth_imap2 stopped working.

In browsing through the source code for mod_auth_imap2, I came to two realizations:

  1. mod_auth_imap2 doesn’t expect the IMAP server to send an untagged response in reply to a LOGIN command, and
  2. there’s a bug in the code that skips untagged responses, so inserting it in the right spot wouldn’t have worked without fixing it first.

The following patch, which I sent to the author and went unanswered, corrects these problems. It’s not very pretty, but hey, neither is the original code.

UPDATE: My patch eventually made it into the www/mod_auth_imap2 FreeBSD port.

--- mod_auth_imap.c.orig        2006-05-07 18:22:43.000000000 -0500
+++ mod_auth_imap.c     2011-03-26 00:07:35.000000000 -0500
@@ -170,7 +170,7 @@
     tcp_gets(Sock,result,500);

     //skip lines that start with "*"
-    if (strncmp(result,"* ",2 == 0)) {
+    if (strncmp(result,"* ",2) == 0) {
        tcp_gets(Sock,result,500);
     }

@@ -188,6 +188,11 @@
     tcp_puts(Sock,buf);
     tcp_gets(Sock,result,500);

+    //skip lines that start with "*"
+    if (strncmp(result,"* ",2) == 0) {
+       tcp_gets(Sock,result,500);
+    }
+
     if (strncmp(result,"A002 OK",7) == 0) {
         if (logflag) {
             ap_log_rerror(APLOG_MARK,APLOG_WARNING|APLOG_NOERRNO,0,r,"mod_auth_imap: Verified login for user %s.", username);

Leave a Reply

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