]> Pileus Git - ~andy/fetchmail/blobdiff - imap.c
Note Earl's regression fix for SSL_CTX_clear_options() on older OpenSSL.
[~andy/fetchmail] / imap.c
diff --git a/imap.c b/imap.c
index fac7cae0ef326732a0ce2acde61905ecccea176e..cb87eda5378f258f20955c3524f2f84c6d29cd0e 100644 (file)
--- a/imap.c
+++ b/imap.c
 #include  "config.h"
 #include  <stdio.h>
 #include  <string.h>
+#include  <strings.h>
 #include  <ctype.h>
 #if defined(STDC_HEADERS)
 #include  <stdlib.h>
 #include  <limits.h>
+#include  <errno.h>
 #endif
 #include  "fetchmail.h"
 #include  "socket.h"
 
 #include  "i18n.h"
 
-#if OPIE_ENABLE
-#endif /* OPIE_ENABLE */
-
-#ifndef strstr         /* glibc-2.1 declares this as a macro */
-extern char *strstr(); /* needed on sysV68 R3V7.1. */
-#endif /* strstr */
-
 /* imap_version values */
 #define IMAP2          -1      /* IMAP2 or IMAP2BIS, RFC1176 */
 #define IMAP4          0       /* IMAP4 rev 0, RFC1730 */
 #define IMAP4rev1      1       /* IMAP4 rev 1, RFC2060 */
 
-static int count = 0, recentcount = 0, unseen = 0, deletions = 0;
-static unsigned int startcount = 1;
-static int expunged, expunge_period, saved_timeout = 0;
-static int imap_version, preauth;
-static flag do_idle, has_idle;
+/* global variables: please reinitialize them explicitly for proper
+ * working in daemon mode */
+
+/* TODO: session variables to be initialized before server greeting */
+static int preauth = FALSE;
+
+/* session variables initialized in capa_probe() or imap_getauth() */
 static char capabilities[MSGBUFSIZE+1];
+static int imap_version = IMAP4;
+static flag do_idle = FALSE, has_idle = FALSE;
+static int expunge_period = 1;
+
+/* mailbox variables initialized in imap_getrange() */
+static int count = 0, oldcount = 0, recentcount = 0, unseen = 0, deletions = 0;
+static unsigned int startcount = 1;
+static int expunged = 0;
 static unsigned int *unseen_messages;
 
-static int imap_ok(int sock, char *argbuf)
+/* for "IMAP> EXPUNGE" */
+static int actual_deletions = 0;
+
+/* for "IMAP> IDLE" */
+static int saved_timeout = 0, idle_timeout = 0;
+static time_t idle_start_time = 0;
+
+static int imap_untagged_response(int sock, const char *buf)
+/* interpret untagged status responses */
+{
+    /* For each individual check, use a BLANK before the word to avoid
+     * confusion with the \Recent flag or similar */
+    if (stage == STAGE_GETAUTH
+           && !strncmp(buf, "* CAPABILITY", 12))
+    {
+       strlcpy(capabilities, buf + 12, sizeof(capabilities));
+    }
+    else if (stage == STAGE_GETAUTH
+           && !strncmp(buf, "* PREAUTH", 9))
+    {
+       preauth = TRUE;
+    }
+    else if (stage != STAGE_LOGOUT
+           && !strncmp(buf, "* BYE", 5))
+    {
+       /* log the unexpected bye from server as we expect the
+        * connection to be cut-off after this */
+       if (outlevel > O_SILENT)
+           report(stderr, GT_("Received BYE response from IMAP server: %s"), buf + 5);
+    }
+    else if (strstr(buf, " EXISTS"))
+    {
+       char *t; unsigned long u;
+       errno = 0;
+       u = strtoul(buf+2, &t, 10);
+       /*
+        * Don't trust the message count passed by the server.
+        * Without this check, it might be possible to do a
+        * DNS-spoofing attack that would pass back a ridiculous 
+        * count, and allocate a malloc area that would overlap
+        * a portion of the stack.
+        */
+       if (errno /* strtoul failed */
+               || t == buf+2 /* no valid data */
+               || u > (unsigned long)(INT_MAX/sizeof(int)) /* too large */)
+       {
+           report(stderr, GT_("bogus message count in \"%s\"!"), buf);
+           return(PS_PROTOCOL);
+       }
+       count = u; /* safe as long as count <= INT_MAX - checked above */
+
+       if ((recentcount = count - oldcount) < 0)
+           recentcount = 0;
+
+       /*
+        * Nasty kluge to handle RFC2177 IDLE.  If we know we're idling
+        * we can't wait for the tag matching the IDLE; we have to tell the
+        * server the IDLE is finished by shipping back a DONE when we
+        * see an EXISTS.  Only after that will a tagged response be
+        * shipped.  The idling flag also gets cleared on a timeout.
+        */
+       if (stage == STAGE_IDLE)
+       {
+           /* If IDLE isn't supported, we were only sending NOOPs anyway. */
+           if (has_idle)
+           {
+               /* we do our own write and report here to disable tagging */
+               SockWrite(sock, "DONE\r\n", 6);
+               if (outlevel >= O_MONITOR)
+                   report(stdout, "IMAP> DONE\n");
+           }
+
+           mytimeout = saved_timeout;
+           stage = STAGE_GETRANGE;
+       }
+    }
+    /* we now compute recentcount as a difference between
+     * new and old EXISTS, hence disable RECENT check */
+# if 0
+    else if (strstr(buf, " RECENT"))
+    {
+       /* fixme: use strto[u]l and error checking */
+       recentcount = atoi(buf+2);
+    }
+# endif
+    else if (strstr(buf, " EXPUNGE"))
+    {
+       unsigned long u; char *t;
+       /* the response "* 10 EXPUNGE" means that the currently
+        * tenth (i.e. only one) message has been deleted */
+       errno = 0;
+       u = strtoul(buf+2, &t, 10);
+       if (errno /* conversion error */ || t == buf+2 /* no number found */) {
+           report(stderr, GT_("bogus EXPUNGE count in \"%s\"!"), buf);
+           return PS_PROTOCOL;
+       }
+       if (u > 0)
+       {
+           if (count > 0)
+               count--;
+           if (oldcount > 0)
+               oldcount--;
+           /* We do expect an EXISTS response immediately
+            * after this, so this updation of recentcount is
+            * just a precaution! */
+           if ((recentcount = count - oldcount) < 0)
+               recentcount = 0;
+           actual_deletions++;
+       }
+    }
+    /*
+     * The server may decide to make the mailbox read-only, 
+     * which causes fetchmail to go into a endless loop
+     * fetching the same message over and over again. 
+     * 
+     * However, for check_only, we use EXAMINE which will
+     * mark the mailbox read-only as per the RFC.
+     * 
+     * This checks for the condition and aborts if 
+     * the mailbox is read-only. 
+     *
+     * See RFC 2060 section 6.3.1 (SELECT).
+     * See RFC 2060 section 6.3.2 (EXAMINE).
+     */ 
+    else if (stage == STAGE_GETRANGE
+           && !check_only && strstr(buf, "[READ-ONLY]"))
+    {
+       return(PS_LOCKBUSY);
+    }
+    else
+    {
+       return(PS_UNTAGGED);
+    }
+    return(PS_SUCCESS);
+}
+
+static int imap_response(int sock, char *argbuf, struct RecvSplit *rs)
 /* parse command response */
 {
     char buf[MSGBUFSIZE+1];
@@ -47,81 +188,45 @@ static int imap_ok(int sock, char *argbuf)
        int     ok;
        char    *cp;
 
-       if ((ok = gen_recv(sock, buf, sizeof(buf))))
+       if (rs)
+           ok = gen_recv_split(sock, buf, sizeof(buf), rs);
+       else
+           ok = gen_recv(sock, buf, sizeof(buf));
+       if (ok != PS_SUCCESS)
            return(ok);
 
        /* all tokens in responses are caseblind */
        for (cp = buf; *cp; cp++)
-           if (islower(*cp))
-               *cp = toupper(*cp);
-
-       /* interpret untagged status responses */
-       if (strstr(buf, "* CAPABILITY"))
-       {
-           strncpy(capabilities, buf + 12, sizeof(capabilities));
-           capabilities[sizeof(capabilities)-1] = '\0';
-       }
-       else if (strstr(buf, "EXISTS"))
-       {
-           count = atoi(buf+2);
-           /*
-            * Don't trust the message count passed by the server.
-            * Without this check, it might be possible to do a
-            * DNS-spoofing attack that would pass back a ridiculous 
-            * count, and allocate a malloc area that would overlap
-            * a portion of the stack.
-            */
-           if (count > INT_MAX/sizeof(int))
-           {
-               report(stderr, "bogus message count!");
-               return(PS_PROTOCOL);
-           }
+           if (islower((unsigned char)*cp))
+               *cp = toupper((unsigned char)*cp);
 
-           /*
-            * Nasty kluge to handle RFC2177 IDLE.  If we know we're idling
-            * we can't wait for the tag matching the IDLE; we have to tell the
-            * server the IDLE is finished by shipping back a DONE when we
-            * see an EXISTS.  Only after that will a tagged response be
-            * shipped.  The idling flag also gets cleared on a timeout.
-            */
-           if (stage == STAGE_IDLE)
+       /* untagged responses start with "* " */
+       if (buf[0] == '*' && buf[1] == ' ') {
+           ok = imap_untagged_response(sock, buf);
+           if (ok == PS_UNTAGGED)
            {
-               /* If IDLE isn't supported, we were only sending NOOPs anyway. */
-               if (has_idle)
+               if (argbuf && stage != STAGE_IDLE && tag[0] != '\0')
                {
-                   /* we do our own write and report here to disable tagging */
-                   SockWrite(sock, "DONE\r\n", 6);
-                   if (outlevel >= O_MONITOR)
-                       report(stdout, "IMAP> DONE\n");
+                   /* if there is an unmatched response, pass it back to
+                    * the calling function for further analysis. The
+                    * calling function should call imap_response() again
+                    * to read the remaining response */
+                   strcpy(argbuf, buf);
+                   return(ok);
                }
-
-               mytimeout = saved_timeout;
-               stage = STAGE_FETCH;
            }
+           else if (ok != PS_SUCCESS)
+               return(ok);
        }
-       /* a space is required to avoid confusion with the \Recent flag */
-       else if (strstr(buf, " RECENT"))
+
+       if (stage == STAGE_IDLE)
        {
-           recentcount = atoi(buf+2);
+           /* reduce the timeout: servers may not reset their timeout
+            * when they send some information asynchronously */
+           mytimeout = idle_timeout - (time((time_t *) NULL) - idle_start_time);
+           if (mytimeout <= 0)
+               return(PS_IDLETIMEOUT);
        }
-       else if (strstr(buf, "PREAUTH"))
-           preauth = TRUE;
-       /*
-        * The server may decide to make the mailbox read-only, 
-        * which causes fetchmail to go into a endless loop
-        * fetching the same message over and over again. 
-        * 
-        * However, for check_only, we use EXAMINE which will
-        * mark the mailbox read-only as per the RFC.
-        * 
-        * This checks for the condition and aborts if 
-        * the mailbox is read-only. 
-        *
-        * See RFC 2060 section 6.3.1 (SELECT).
-        * See RFC 2060 section 6.3.2 (EXAMINE).
-        */ 
-       else if (!check_only && strstr(buf, "[READ-ONLY]"))
-           return(PS_LOCKBUSY);
     } while
        (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
 
@@ -136,9 +241,9 @@ static int imap_ok(int sock, char *argbuf)
        char    *cp;
 
        /* skip the tag */
-       for (cp = buf; !isspace(*cp); cp++)
+       for (cp = buf; !isspace((unsigned char)*cp); cp++)
            continue;
-       while (isspace(*cp))
+       while (isspace((unsigned char)*cp))
            cp++;
 
         if (strncasecmp(cp, "OK", 2) == 0)
@@ -153,6 +258,8 @@ static int imap_ok(int sock, char *argbuf)
        {
            if (stage == STAGE_GETAUTH) 
                return(PS_AUTHFAIL);    /* RFC2060, 6.2.2 */
+           else if (stage == STAGE_GETSIZES)
+               return(PS_SUCCESS);     /* see comments in imap_getpartialsizes() */
            else
                return(PS_ERROR);
        }
@@ -161,12 +268,18 @@ static int imap_ok(int sock, char *argbuf)
     }
 }
 
-#if NTLM_ENABLE
-#include "ntlm.h"
+static int imap_ok(int sock, char *argbuf)
+/* parse command response */
+{
+    int ok;
 
-static tSmbNtlmAuthRequest   request;             
-static tSmbNtlmAuthChallenge challenge;
-static tSmbNtlmAuthResponse  response;
+    while ((ok = imap_response(sock, argbuf, NULL)) == PS_UNTAGGED)
+       ; /* wait for the tagged response */
+    return(ok);
+}
+
+#ifdef NTLM_ENABLE
+#include "ntlm.h"
 
 /*
  * NTLM support by Grant Edwards.
@@ -181,67 +294,25 @@ static tSmbNtlmAuthResponse  response;
 
 static int do_imap_ntlm(int sock, struct query *ctl)
 {
-    char msgbuf[2048];
-    int result,len;
-  
+    int result;
+
     gen_send(sock, "AUTHENTICATE NTLM");
 
-    if ((result = gen_recv(sock, msgbuf, sizeof msgbuf)))
-       return result;
-  
-    if (msgbuf[0] != '+')
-       return PS_AUTHFAIL;
-  
-    buildSmbNtlmAuthRequest(&request,ctl->remotename,NULL);
-
-    if (outlevel >= O_DEBUG)
-       dumpSmbNtlmAuthRequest(stdout, &request);
-
-    memset(msgbuf,0,sizeof msgbuf);
-    to64frombits (msgbuf, (unsigned char*)&request, SmbLength(&request));
-  
-    if (outlevel >= O_MONITOR)
-       report(stdout, "IMAP> %s\n", msgbuf);
-  
-    strcat(msgbuf,"\r\n");
-    SockWrite (sock, msgbuf, strlen (msgbuf));
-
-    if ((gen_recv(sock, msgbuf, sizeof msgbuf)))
-       return result;
-  
-    len = from64tobits ((char*)&challenge, msgbuf, sizeof(challenge));
-    
-    if (outlevel >= O_DEBUG)
-       dumpSmbNtlmAuthChallenge(stdout, &challenge);
-    
-    buildSmbNtlmAuthResponse(&challenge, &response,ctl->remotename,ctl->password);
-  
-    if (outlevel >= O_DEBUG)
-       dumpSmbNtlmAuthResponse(stdout, &response);
-  
-    memset(msgbuf,0,sizeof msgbuf);
-    to64frombits (msgbuf, (unsigned char*)&response, SmbLength(&response));
-
-    if (outlevel >= O_MONITOR)
-       report(stdout, "IMAP> %s\n", msgbuf);
-      
-    strcat(msgbuf,"\r\n");
-    SockWrite (sock, msgbuf, strlen (msgbuf));
-  
-    if ((result = gen_recv (sock, msgbuf, sizeof msgbuf)))
+    if ((result = ntlm_helper(sock, ctl, "IMAP")))
        return result;
-  
-    if (strstr (msgbuf, "OK"))
+
+    result = imap_ok (sock, NULL);
+    if (result == PS_SUCCESS)
        return PS_SUCCESS;
     else
        return PS_AUTHFAIL;
 }
 #endif /* NTLM */
 
-static int imap_canonicalize(char *result, char *raw, int maxlen)
+static void imap_canonicalize(char *result, char *raw, size_t maxlen)
 /* encode an IMAP password as per RFC1730's quoting conventions */
 {
-    int i, j;
+    size_t i, j;
 
     j = 0;
     for (i = 0; i < strlen(raw) && i < maxlen; i++)
@@ -251,11 +322,9 @@ static int imap_canonicalize(char *result, char *raw, int maxlen)
        result[j++] = raw[i];
     }
     result[j] = '\0';
-
-    return(i);
 }
 
-static void capa_probe(int sock, struct query *ctl)
+static int capa_probe(int sock, struct query *ctl)
 /* set capability variables from a CAPA probe */
 {
     int        ok;
@@ -268,7 +337,7 @@ static void capa_probe(int sock, struct query *ctl)
 
        /* capability checks are supposed to be caseblind */
        for (cp = capabilities; *cp; cp++)
-           *cp = toupper(*cp);
+           *cp = toupper((unsigned char)*cp);
 
        /* UW-IMAP server 10.173 notifies in all caps, but RFC2060 says we
           should expect a response in mixed-case */
@@ -291,32 +360,52 @@ static void capa_probe(int sock, struct query *ctl)
        if (outlevel >= O_DEBUG)
            report(stdout, GT_("Protocol identified as IMAP2 or IMAP2BIS\n"));
     }
+    else
+       return ok;
 
     /* 
      * Handle idling.  We depend on coming through here on startup
      * and after each timeout (including timeouts during idles).
      */
+    do_idle = ctl->idle;
     if (ctl->idle)
     {
-       do_idle = TRUE;
        if (strstr(capabilities, "IDLE"))
-       {
            has_idle = TRUE;
-       }
+       else
+           has_idle = FALSE;
        if (outlevel >= O_VERBOSE)
            report(stdout, GT_("will idle after poll\n"));
     }
 
     peek_capable = (imap_version >= IMAP4);
+
+    return PS_SUCCESS;
+}
+
+static int do_authcert (int sock, const char *command, const char *name)
+/* do authentication "external" (authentication provided by client cert) */
+{
+    char buf[256];
+
+    if (name && name[0])
+    {
+        size_t len = strlen(name);
+        if ((len / 3) + ((len % 3) ? 4 : 0)  < sizeof(buf))
+            to64frombits (buf, name, strlen(name));
+        else
+            return PS_AUTHFAIL; /* buffer too small. */
+    }
+    else
+        buf[0]=0;
+    return gen_transact(sock, "%s EXTERNAL %s",command,buf);
 }
 
 static int imap_getauth(int sock, struct query *ctl, char *greeting)
 /* apply for connection authorization */
 {
     int ok = 0;
-#ifdef SSL_ENABLE
-    flag did_stls = FALSE;
-#endif /* SSL_ENABLE */
+    (void)greeting;
 
     /*
      * Assumption: expunges are cheap, so we want to do them
@@ -327,7 +416,8 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
     else
        expunge_period = 1;
 
-    capa_probe(sock, ctl);
+    if ((ok = capa_probe(sock, ctl)))
+       return ok;
 
     /* 
      * If either (a) we saw a PREAUTH token in the greeting, or
@@ -340,44 +430,68 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
     }
 
 #ifdef SSL_ENABLE
-    if ((!ctl->sslproto || !strcmp(ctl->sslproto,"tls1"))
-        && !ctl->use_ssl
-        && strstr(capabilities, "STARTTLS"))
-    {
-           char *realhost;
-
-           realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
-           ok = gen_transact(sock, "STARTTLS");
-
-           /* We use "tls1" instead of ctl->sslproto, as we want STARTTLS,
-            * not other SSL protocols
-            */
-           if (ok == PS_SUCCESS &&
-              SSLOpen(sock,ctl->sslcert,ctl->sslkey,"tls1",ctl->sslcertck, ctl->sslcertpath,ctl->sslfingerprint,realhost,ctl->server.pollname) == -1)
-           {
-              if (!ctl->sslproto && !ctl->wehaveauthed)
-              {
-                  ctl->sslproto = xstrdup("");
-                  /* repoll immediately */
-                  return(PS_REPOLL);
-              }
-               report(stderr,
-                      GT_("SSL connection failed.\n"));
-               return(PS_AUTHFAIL);
-           }
-          did_stls = TRUE;
-
-          /*
-           * RFC 2595 says this:
-           *
-           * "Once TLS has been started, the client MUST discard cached
-           * information about server capabilities and SHOULD re-issue the
-           * CAPABILITY command.  This is necessary to protect against
-           * man-in-the-middle attacks which alter the capabilities list prior
-           * to STARTTLS.  The server MAY advertise different capabilities
-           * after STARTTLS."
-           */
-          capa_probe(sock, ctl);
+    if (maybe_tls(ctl)) {
+       char *commonname;
+
+       commonname = ctl->server.pollname;
+       if (ctl->server.via)
+           commonname = ctl->server.via;
+       if (ctl->sslcommonname)
+           commonname = ctl->sslcommonname;
+
+       if (strstr(capabilities, "STARTTLS")
+               || must_tls(ctl)) /* if TLS is mandatory, ignore capabilities */
+       {
+           /* Use "tls1" rather than ctl->sslproto because tls1 is the only
+            * protocol that will work with STARTTLS.  Don't need to worry
+            * whether TLS is mandatory or opportunistic unless SSLOpen() fails
+            * (see below). */
+           if (gen_transact(sock, "STARTTLS") == PS_SUCCESS
+                   && (set_timeout(mytimeout), SSLOpen(sock, ctl->sslcert, ctl->sslkey, "tls1", ctl->sslcertck,
+                       ctl->sslcertfile, ctl->sslcertpath, ctl->sslfingerprint, commonname,
+                       ctl->server.pollname, &ctl->remotename)) != -1)
+           {
+               /*
+                * RFC 2595 says this:
+                *
+                * "Once TLS has been started, the client MUST discard cached
+                * information about server capabilities and SHOULD re-issue the
+                * CAPABILITY command.  This is necessary to protect against
+                * man-in-the-middle attacks which alter the capabilities list prior
+                * to STARTTLS.  The server MAY advertise different capabilities
+                * after STARTTLS."
+                *
+                * Now that we're confident in our TLS connection we can
+                * guarantee a secure capability re-probe.
+                */
+               if ((ok = capa_probe(sock, ctl)))
+                   return ok;
+               if (outlevel >= O_VERBOSE)
+               {
+                   report(stdout, GT_("%s: upgrade to TLS succeeded.\n"), commonname);
+               }
+           } else if (must_tls(ctl)) {
+               /* Config required TLS but we couldn't guarantee it, so we must
+                * stop. */
+               set_timeout(0);
+               report(stderr, GT_("%s: upgrade to TLS failed.\n"), commonname);
+               return PS_SOCKET;
+           } else {
+               set_timeout(0);
+               if (outlevel >= O_VERBOSE) {
+                   report(stdout, GT_("%s: opportunistic upgrade to TLS failed, trying to continue\n"), commonname);
+               }
+               /* We don't know whether the connection is in a working state, so
+                * test by issuing a NOOP. */
+               if (gen_transact(sock, "NOOP") != PS_SUCCESS) {
+                   /* Not usable.  Empty sslproto to force an unencrypted
+                    * connection on the next attempt, and repoll. */
+                   ctl->sslproto = xstrdup("");
+                   return PS_REPOLL;
+               }
+               /* Usable.  Proceed with authenticating insecurely. */
+           }
+       }
     }
 #endif /* SSL_ENABLE */
 
@@ -387,19 +501,45 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
      */
     ok = PS_AUTHFAIL;
 
-#ifdef GSSAPI
+    /* Yahoo hack - we'll just try ID if it was offered by the server,
+     * and IGNORE errors. */
+    {
+       char *tmp = strstr(capabilities, " ID");
+       if (tmp && !isalnum((unsigned char)tmp[3]) && strstr(ctl->server.via ? ctl->server.via : ctl->server.pollname, "yahoo.com")) {
+               (void)gen_transact(sock, "ID (\"guid\" \"1\")");
+       }
+    }
+
     if ((ctl->server.authenticate == A_ANY 
+         || ctl->server.authenticate == A_EXTERNAL)
+       && strstr(capabilities, "AUTH=EXTERNAL"))
+    {
+        ok = do_authcert(sock, "AUTHENTICATE", ctl->remotename);
+       if (ok)
+        {
+            /* SASL cancellation of authentication */
+            gen_send(sock, "*");
+            if (ctl->server.authenticate != A_ANY)
+                return ok;
+        } else {
+            return ok;
+       }
+    }
+
+#ifdef GSSAPI
+    if (((ctl->server.authenticate == A_ANY && check_gss_creds("imap", ctl->server.truename) == PS_SUCCESS)
         || ctl->server.authenticate == A_GSSAPI)
        && strstr(capabilities, "AUTH=GSSAPI"))
-       if(ok = do_gssauth(sock, "AUTHENTICATE", ctl->server.truename, ctl->remotename))
+    {
+       if ((ok = do_gssauth(sock, "AUTHENTICATE", "imap",
+                       ctl->server.truename, ctl->remotename)))
        {
-           /* SASL cancellation of authentication */
-           gen_send(sock, "*");
-           if(ctl->server.authenticate != A_ANY)
+           if (ctl->server.authenticate != A_ANY)
                 return ok;
-       }
-       else
+       } else  {
            return ok;
+       }
+    }
 #endif /* GSSAPI */
 
 #ifdef KERBEROS_V4
@@ -430,8 +570,6 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
     {
        if ((ok = do_cram_md5 (sock, "AUTHENTICATE", ctl, NULL)))
        {
-           /* SASL cancellation of authentication */
-           gen_send(sock, "*");
            if(ctl->server.authenticate != A_ANY)
                 return ok;
        }
@@ -439,19 +577,20 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
            return ok;
     }
 
-#if OPIE_ENABLE
+#ifdef OPIE_ENABLE
     if ((ctl->server.authenticate == A_ANY 
         || ctl->server.authenticate == A_OTP)
-       && strstr(capabilities, "AUTH=X-OTP"))
+       && strstr(capabilities, "AUTH=X-OTP")) {
        if ((ok = do_otp(sock, "AUTHENTICATE", ctl)))
        {
            /* SASL cancellation of authentication */
            gen_send(sock, "*");
            if(ctl->server.authenticate != A_ANY)
                 return ok;
-       }
-       else
+       } else {
            return ok;
+       }
+    }
 #else
     if (ctl->server.authenticate == A_OTP)
     {
@@ -466,8 +605,6 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
        && strstr (capabilities, "AUTH=NTLM")) {
        if ((ok = do_imap_ntlm(sock, ctl)))
        {
-           /* SASL cancellation of authentication */
-           gen_send(sock, "*");
            if(ctl->server.authenticate != A_ANY)
                 return ok;
        }
@@ -502,34 +639,25 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
        || ctl->server.authenticate == A_PASSWORD)
     {
        /* these sizes guarantee no buffer overflow */
-       char    remotename[NAMELEN*2+1], password[PASSWORDLEN*2+1];
+       char *remotename, *password;
+       size_t rnl, pwl;
+       rnl = 2 * strlen(ctl->remotename) + 1;
+       pwl = 2 * strlen(ctl->password) + 1;
+       remotename = (char *)xmalloc(rnl);
+       password = (char *)xmalloc(pwl);
 
-       imap_canonicalize(remotename, ctl->remotename, NAMELEN);
-       imap_canonicalize(password, ctl->password, PASSWORDLEN);
+       imap_canonicalize(remotename, ctl->remotename, rnl);
+       imap_canonicalize(password, ctl->password, pwl);
 
-#ifdef HAVE_SNPRINTF
        snprintf(shroud, sizeof (shroud), "\"%s\"", password);
-#else
-       strcpy(shroud, "\"");
-       strcat(shroud, password);
-       strcat(shroud, "\"");
-#endif
        ok = gen_transact(sock, "LOGIN \"%s\" \"%s\"", remotename, password);
+       memset(shroud, 0x55, sizeof(shroud));
        shroud[0] = '\0';
-#ifdef SSL_ENABLE
-       /* this is for servers which claim to support TLS, but actually
-        * don't! */
-       if (did_stls && ok == PS_SOCKET && !ctl->sslproto && !ctl->wehaveauthed)
-       {
-           ctl->sslproto = xstrdup("");
-           /* repoll immediately */
-           ok = PS_REPOLL;
-       }
-#endif
+       memset(password, 0x55, strlen(password));
+       free(password);
+       free(remotename);
        if (ok)
        {
-           /* SASL cancellation of authentication */
-           gen_send(sock, "*");
            if(ctl->server.authenticate != A_ANY)
                 return ok;
        }
@@ -545,9 +673,27 @@ static int internal_expunge(int sock)
 {
     int        ok;
 
+    actual_deletions = 0;
+
     if ((ok = gen_transact(sock, "EXPUNGE")))
        return(ok);
 
+    /* if there is a mismatch between the number of mails which should
+     * have been expunged and the number of mails actually expunged,
+     * another email client may be deleting mails. Quit here,
+     * otherwise fetchmail gets out-of-sync with the imap server,
+     * reports the wrong size to the SMTP server on MAIL FROM: and
+     * triggers a "message ... was not the expected length" error on
+     * every subsequent mail */
+    if (deletions > 0 && deletions != actual_deletions)
+    {
+       report(stderr,
+               GT_("mail expunge mismatch (%d actual != %d expected)\n"),
+               actual_deletions, deletions);
+       deletions = 0;
+       return(PS_ERROR);
+    }
+
     expunged += deletions;
     deletions = 0;
 
@@ -563,14 +709,15 @@ static int imap_idle(int sock)
 {
     int ok;
 
-    stage = STAGE_IDLE;
     saved_timeout = mytimeout;
 
     if (has_idle) {
        /* special timeout to terminate the IDLE and re-issue it
         * at least every 28 minutes:
         * (the server may have an inactivity timeout) */
-       mytimeout = 1680; /* 28 min */
+       mytimeout = idle_timeout = 1680; /* 28 min */
+       time(&idle_start_time);
+       stage = STAGE_IDLE;
        /* enter IDLE mode */
        ok = gen_transact(sock, "IDLE");
 
@@ -579,37 +726,151 @@ static int imap_idle(int sock)
            SockWrite(sock, "DONE\r\n", 6);
            if (outlevel >= O_MONITOR)
                report(stdout, "IMAP> DONE\n");
-       } else
-           /* not idle timeout */
-           return ok;
+           /* reset stage and timeout here: we are not idling any more */
+           mytimeout = saved_timeout;
+           stage = STAGE_GETRANGE;
+           /* get OK IDLE message */
+           ok = imap_ok(sock, NULL);
+       }
     } else {  /* no idle support, fake it */
-       /* when faking an idle, we can't assume the server will
-        * send us the new messages out of the blue (RFC2060);
-        * this timeout is potentially the delay before we notice
-        * new mail (can be small since NOOP checking is cheap) */
-       mytimeout = 28;
+       /* Note: stage and timeout have not been changed here as NOOP
+        * does not idle */
        ok = gen_transact(sock, "NOOP");
-       /* if there's an error (not likely) or we just found mail (stage 
-        * has changed, timeout has also been restored), we're done */
-       if (ok != 0 || stage != STAGE_IDLE)
-           return(ok);
 
-       /* wait (briefly) for an unsolicited status update */
-       ok = imap_ok(sock, NULL);
-       /* again, this is new mail or an error */
-       if (ok != PS_IDLETIMEOUT)
-           return(ok);
+       /* no error, but no new mail either */
+       if (ok == PS_SUCCESS && recentcount == 0)
+       {
+           /* There are some servers who do send new mail
+            * notification out of the blue. This is in compliance
+            * with RFC 2060 section 5.3. Wait for that with a low
+            * timeout */
+           mytimeout = idle_timeout = 28;
+           time(&idle_start_time);
+           stage = STAGE_IDLE;
+           /* We are waiting for notification; no tag needed */
+           tag[0] = '\0';
+           /* wait (briefly) for an unsolicited status update */
+           ok = imap_ok(sock, NULL);
+           if (ok == PS_IDLETIMEOUT) {
+               /* no notification came; ok */
+               ok = PS_SUCCESS;
+           }
+       }
     }
 
     /* restore normal timeout value */
+    set_timeout(0);
     mytimeout = saved_timeout;
-    stage = STAGE_FETCH;
+    stage = STAGE_GETRANGE;
 
-    /* get OK IDLE message */
-    if (has_idle)
-        return imap_ok(sock, NULL);
+    return(ok);
+}
 
-    return PS_SUCCESS;
+static int imap_search(int sock, struct query *ctl, int count)
+/* search for unseen messages */
+{
+    int ok;
+    char buf[MSGBUFSIZE+1], *cp;
+
+    /* Don't count deleted messages. Enabled only for IMAP4 servers or
+     * higher and only when keeping mails. This flag will have an
+     * effect only when user has marked some unread mails for deletion
+     * using another e-mail client. */
+    flag skipdeleted = (imap_version >= IMAP4) && ctl->keep;
+    const char *undeleted;
+
+    /* structure to keep the end portion of the incomplete response */
+    struct RecvSplit rs;
+
+    /* startcount is higher than count so that if there are no
+     * unseen messages, imap_getsizes() will not need to do
+     * anything! */
+    startcount = count + 1;
+
+    for (;;)
+    {
+       undeleted = (skipdeleted ? " UNDELETED" : "");
+       gen_send(sock, "SEARCH UNSEEN%s", undeleted);
+       gen_recv_split_init("* SEARCH", &rs);
+       while ((ok = imap_response(sock, buf, &rs)) == PS_UNTAGGED)
+       {
+           if ((cp = strstr(buf, "* SEARCH")))
+           {
+               char    *ep;
+
+               cp += 8;        /* skip "* SEARCH" */
+               while (*cp && unseen < count)
+               {
+                   /* skip whitespace */
+                   while (*cp && isspace((unsigned char)*cp))
+                       cp++;
+                   if (*cp) 
+                   {
+                       unsigned long um;
+
+                       errno = 0;
+                       um = strtoul(cp,&ep,10);
+                       if (errno == 0 && ep > cp
+                               && um <= INT_MAX && um <= (unsigned)count)
+                       {
+                           unseen_messages[unseen++] = um;
+                           if (outlevel >= O_DEBUG)
+                               report(stdout, GT_("%lu is unseen\n"), um);
+                           if (startcount > um)
+                               startcount = um;
+                       }
+                       cp = ep;
+                   }
+               }
+           }
+       }
+       if (ok != PS_ERROR) /* success or non-protocol error */
+           return(ok);
+
+       /* there is a protocol error. try a different search command. */
+       if (skipdeleted)
+       {
+           /* retry with "SEARCH UNSEEN" */
+           skipdeleted = FALSE;
+           continue;
+       }
+       /* try with "FETCH 1:n FLAGS" */
+       break;
+    }
+
+    if (count == 1)
+       gen_send(sock, "FETCH %d FLAGS", count);
+    else
+       gen_send(sock, "FETCH %d:%d FLAGS", 1, count);
+    while ((ok = imap_response(sock, buf, NULL)) == PS_UNTAGGED)
+    {
+       unsigned int num;
+       int consumed;
+
+       /* expected response format:
+        * IMAP< * 1 FETCH (FLAGS (\Seen))
+        * IMAP< * 2 FETCH (FLAGS (\Seen \Deleted))
+        * IMAP< * 3 FETCH (FLAGS ())
+        * IMAP< * 4 FETCH (FLAGS (\Recent))
+        * IMAP< * 5 FETCH (UID 10 FLAGS (\Recent))
+        */
+       if (unseen < count
+               && sscanf(buf, "* %u %n", &num, &consumed) == 1
+               && 0 == strncasecmp(buf+consumed, "FETCH", 5)
+               && isspace((unsigned char)buf[consumed+5])
+               && num >= 1 && num <= (unsigned)count
+               && strstr(buf, "FLAGS ")
+               && !strstr(buf, "\\SEEN")
+               && !strstr(buf, "\\DELETED"))
+       {
+           unseen_messages[unseen++] = num;
+           if (outlevel >= O_DEBUG)
+               report(stdout, GT_("%u is unseen\n"), num);
+           if (startcount > num)
+               startcount = num;
+       }
+    }
+    return(ok);
 }
 
 static int imap_getrange(int sock, 
@@ -619,40 +880,20 @@ static int imap_getrange(int sock,
 /* get range of messages to be fetched */
 {
     int ok;
-    char buf[MSGBUFSIZE+1], *cp;
 
     /* find out how many messages are waiting */
     *bytes = -1;
 
     if (pass > 1)
     {
-       /* 
-        * We have to have an expunge here, otherwise the re-poll will
-        * infinite-loop picking up un-expunged messages -- unless the
-        * expunge period is one and we've been nuking each message 
-        * just after deletion.
-        */
-       ok = 0;
-       if (deletions) {
-           ok = internal_expunge(sock);
-           if (ok)
-           {
-               report(stderr, GT_("expunge failed\n"));
-               return(ok);
-           }
-       }
-
-       /*
+       /* deleted mails have already been expunged by
+        * end_mailbox_poll().
+        *
         * recentcount is already set here by the last imap command which
-        * returned RECENT on detecting new mail. if recentcount is 0, wait
+        * returned EXISTS on detecting new mail. if recentcount is 0, wait
         * for new mail.
-        */
-
-       /* some servers do not report RECENT after an EXPUNGE. this check
-        * forces an incorrect recentcount to be ignored. */
-       if (recentcount > count)
-           recentcount = 0;
-       /* this is a while loop because imap_idle() might return on other
+        *
+        * this is a while loop because imap_idle() might return on other
         * mailbox changes also */
        while (recentcount == 0 && do_idle) {
            smtp_close(ctl, 1);
@@ -667,31 +908,30 @@ static int imap_getrange(int sock,
        if (recentcount == 0)
                count = 0;
        if (outlevel >= O_DEBUG)
-           report(stdout, GT_("%d messages waiting after re-poll\n"), count);
+           report(stdout, ngettext("%d message waiting after re-poll\n",
+                                   "%d messages waiting after re-poll\n",
+                                   count), count);
     }
     else
     {
-       count = 0;
+       oldcount = count = 0;
        ok = gen_transact(sock, 
                          check_only ? "EXAMINE \"%s\"" : "SELECT \"%s\"",
                          folder ? folder : "INBOX");
+       /* imap_ok returns PS_LOCKBUSY for READ-ONLY folders,
+        * which we can safely use in fetchall keep only */
+       if (ok == PS_LOCKBUSY && ctl->fetchall && ctl-> keep)
+           ok = 0;
+
        if (ok != 0)
        {
            report(stderr, GT_("mailbox selection failed\n"));
            return(ok);
        }
        else if (outlevel >= O_DEBUG)
-           report(stdout, GT_("%d messages waiting after first poll\n"), count);
-
-       /* no messages?  then we may need to idle until we get some */
-       while (count == 0 && do_idle) {
-           ok = imap_idle(sock);
-           if (ok)
-           {
-               report(stderr, GT_("re-poll failed\n"));
-               return(ok);
-           }
-       }
+           report(stdout, ngettext("%d message waiting after first poll\n",
+                                   "%d messages waiting after first poll\n",
+                                   count), count);
 
        /*
         * We should have an expunge here to
@@ -707,11 +947,30 @@ static int imap_getrange(int sock,
                return(ok);
            }
            if (outlevel >= O_DEBUG)
-               report(stdout, GT_("%d messages waiting after expunge\n"), count);
+               report(stdout, ngettext("%d message waiting after expunge\n",
+                                       "%d messages waiting after expunge\n",
+                                       count), count);
+       }
+
+       if (count == 0 && do_idle)
+       {
+           /* no messages?  then we may need to idle until we get some */
+           while (count == 0) {
+               ok = imap_idle(sock);
+               if (ok)
+               {
+                   report(stderr, GT_("re-poll failed\n"));
+                   return(ok);
+               }
+           }
+           if (outlevel >= O_DEBUG)
+               report(stdout, ngettext("%d message waiting after re-poll\n",
+                                       "%d messages waiting after re-poll\n",
+                                       count), count);
        }
     }
 
-    *countp = count;
+    *countp = oldcount = count;
     recentcount = 0;
     startcount = 1;
 
@@ -720,55 +979,16 @@ static int imap_getrange(int sock,
     {
        if (unseen_messages)
            free(unseen_messages);
-       unseen_messages = xmalloc(count * sizeof(unsigned int));
+       unseen_messages = (unsigned int *)xmalloc(count * sizeof(unsigned int));
        memset(unseen_messages, 0, count * sizeof(unsigned int));
        unseen = 0;
 
-       gen_send(sock, "SEARCH UNSEEN");
-       do {
-           ok = gen_recv(sock, buf, sizeof(buf));
-           if (ok != 0)
-           {
-               report(stderr, GT_("search for unseen messages failed\n"));
-               return(PS_PROTOCOL);
-           }
-           else if ((cp = strstr(buf, "* SEARCH")))
-           {
-               char    *ep;
-
-               cp += 8;        /* skip "* SEARCH" */
-               /* startcount is higher than count so that if there are no
-                * unseen messages, imap_getsizes() will not need to do
-                * anything! */
-               startcount = count + 1;
-
-               while (*cp && unseen < count)
-               {
-                   /* skip whitespace */
-                   while (*cp && isspace(*cp))
-                       cp++;
-                   if (*cp) 
-                   {
-                       unsigned int um;
-                       /*
-                        * Message numbers are between 1 and 2^32 inclusive,
-                        * so unsigned int is large enough.
-                        */
-                       um=(unsigned int)strtol(cp,&ep,10);
-                       if (um <= count)
-                       {
-                           unseen_messages[unseen++] = um;
-                           if (outlevel >= O_DEBUG)
-                               report(stdout, GT_("%u is unseen\n"), um);
-                           if (startcount > um)
-                               startcount = um;
-                       }
-                       cp = ep;
-                   }
-               }
-           }
-       } while
-           (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
+       ok = imap_search(sock, ctl, count);
+       if (ok != 0)
+       {
+           report(stderr, GT_("search for unseen messages failed\n"));
+           return(ok);
+       }
 
        if (outlevel >= O_DEBUG && unseen > 0)
            report(stdout, GT_("%u is first unseen\n"), startcount);
@@ -776,17 +996,17 @@ static int imap_getrange(int sock,
        unseen = -1;
 
     *newp = unseen;
-    count = 0;
     expunged = 0;
     deletions = 0;
 
     return(PS_SUCCESS);
 }
 
-static int imap_getsizes(int sock, int count, int *sizes)
-/* capture the sizes of all messages */
+static int imap_getpartialsizes(int sock, int first, int last, int *sizes)
+/* capture the sizes of messages #first-#last */
 {
     char buf [MSGBUFSIZE+1];
+    int ok;
 
     /*
      * Some servers (as in, PMDF5.1-9.1 under OpenVMS 6.1)
@@ -818,46 +1038,53 @@ static int imap_getsizes(int sock, int count, int *sizes)
      * * 1 FETCH (BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 35 3))
      * A006 OK FETCH completed.
      *
-     * To get around this, we terminate the read loop on a NO and count
+     * To get around this, we treat the final NO as success and count
      * on the fact that the sizes array has been preinitialized with a
      * known-bad size value.
      */
-    /* if fetchall is specified, startcount is 1;
-     * else if there is new mail, startcount is first unseen message;
-     * else startcount is greater than count.
-     */
-    if (count == startcount)
-       gen_send(sock, "FETCH %d RFC822.SIZE", count);
-    else if (count > startcount)
-       gen_send(sock, "FETCH %d:%d RFC822.SIZE", startcount, count);
+
+    /* expunges change the fetch numbers */
+    first -= expunged;
+    last -= expunged;
+
+    if (last == first)
+       gen_send(sock, "FETCH %d RFC822.SIZE", last);
+    else if (last > first)
+       gen_send(sock, "FETCH %d:%d RFC822.SIZE", first, last);
     else /* no unseen messages! */
        return(PS_SUCCESS);
-    for (;;)
+    while ((ok = imap_response(sock, buf, NULL)) == PS_UNTAGGED)
     {
-       unsigned int num, size;
-       int ok;
-       char *cp;
-
-       if ((ok = gen_recv(sock, buf, sizeof(buf))))
-           return(ok);
-       /* we want response matching to be case-insensitive */
-       for (cp = buf; *cp; cp++)
-           *cp = toupper(*cp);
-       /* an untagged NO means that a message was not readable */
-       if (strstr(buf, "* NO"))
-           ;
-       else if (strstr(buf, "OK") || strstr(buf, "NO"))
-           break;
-       else if (sscanf(buf, "* %u FETCH (RFC822.SIZE %u)", &num, &size) == 2) 
+       unsigned int size;
+       int num;
+       int consumed;
+       char *ptr;
+
+       /* expected response formats:
+        * IMAP> A0005 FETCH 1 RFC822.SIZE
+        * IMAP< * 1 FETCH (RFC822.SIZE 1187)
+        * IMAP< * 1 FETCH (UID 16 RFC822.SIZE 1447)
+        */
+       if (sscanf(buf, "* %d %n", &num, &consumed) == 1
+           && 0 == strncasecmp(buf + consumed, "FETCH", 5)
+           && isspace((unsigned char)buf[consumed + 5])
+               && (ptr = strstr(buf, "RFC822.SIZE "))
+               && sscanf(ptr, "RFC822.SIZE %u", &size) == 1)
        {
-           if (num > 0 && num <= count)
-               sizes[num - 1] = size;
+           if (num >= first && num <= last)
+               sizes[num - first] = size;
            else
-               report(stderr, "Warning: ignoring bogus data for message sizes returned by the server.\n");
+               report(stderr,
+                       GT_("Warning: ignoring bogus data for message sizes returned by the server.\n"));
        }
     }
+    return(ok);
+}
 
-    return(PS_SUCCESS);
+static int imap_getsizes(int sock, int count, int *sizes)
+/* capture the sizes of all messages */
+{
+    return imap_getpartialsizes(sock, 1, count, sizes);
 }
 
 static int imap_is_old(int sock, struct query *ctl, int number)
@@ -866,6 +1093,8 @@ static int imap_is_old(int sock, struct query *ctl, int number)
     flag seen = TRUE;
     int i;
 
+    (void)sock;
+    (void)ctl;
     /* 
      * Expunges change the fetch numbers, but unseen_messages contains
      * indices from before any expungees were done.  So neither the
@@ -874,7 +1103,7 @@ static int imap_is_old(int sock, struct query *ctl, int number)
 
     seen = TRUE;
     for (i = 0; i < unseen; i++)
-       if (unseen_messages[i] == number)
+       if (unseen_messages[i] == (unsigned)number)
        {
            seen = FALSE;
            break;
@@ -883,20 +1112,25 @@ static int imap_is_old(int sock, struct query *ctl, int number)
     return(seen);
 }
 
+#if 0
 static char *skip_token(char *ptr)
 {
-    while(isspace(*ptr)) ptr++;
-    while(!isspace(*ptr) && !iscntrl(*ptr)) ptr++;
-    while(isspace(*ptr)) ptr++;
+    while(isspace((unsigned char)*ptr)) ptr++;
+    while(!isspace((unsigned char)*ptr) && !iscntrl((unsigned char)*ptr)) ptr++;
+    while(isspace((unsigned char)*ptr)) ptr++;
     return(ptr);
 }
+#endif
 
 static int imap_fetch_headers(int sock, struct query *ctl,int number,int *lenp)
 /* request headers of nth message */
 {
     char buf [MSGBUFSIZE+1];
     int        num;
+    int ok;
+    char *ptr;
 
+    (void)ctl;
     /* expunges change the fetch numbers */
     number -= expunged;
 
@@ -907,37 +1141,50 @@ static int imap_fetch_headers(int sock, struct query *ctl,int number,int *lenp)
     gen_send(sock, "FETCH %d RFC822.HEADER", number);
 
     /* looking for FETCH response */
-    for (;;) 
+    if ((ok = imap_response(sock, buf, NULL)) == PS_UNTAGGED)
     {
-       int     ok;
-       char    *ptr;
-
-       if ((ok = gen_recv(sock, buf, sizeof(buf))))
-           return(ok);
-       ptr = skip_token(buf);  /* either "* " or "AXXXX " */
-       if (sscanf(ptr, "%d FETCH (%*s {%d}", &num, lenp) == 2)
-           break;
-       /* try to recover from chronically fucked-up M$ Exchange servers */
-       else if (!strncmp(ptr, "NO", 2))
+               int consumed;
+       /* expected response formats:
+        * IMAP> A0006 FETCH 1 RFC822.HEADER
+        * IMAP< * 1 FETCH (RFC822.HEADER {1360}
+        * IMAP< * 1 FETCH (UID 16 RFC822.HEADER {1360}
+        * IMAP< * 1 FETCH (UID 16 RFC822.SIZE 4029 RFC822.HEADER {1360}
+        */
+       if (sscanf(buf, "* %d %n", &num, &consumed) == 1
+           && 0 == strncasecmp(buf + consumed, "FETCH", 5)
+           && isspace((unsigned char)buf[5+consumed])
+               && num == number
+               && (ptr = strstr(buf, "RFC822.HEADER"))
+               && sscanf(ptr, "RFC822.HEADER {%d}%n", lenp, &consumed) == 1
+               && ptr[consumed-1] == '}')
        {
-           /* wait for a tagged response */
-           if (strstr (buf, "* NO"))
-               imap_ok (sock, 0);
-           return(PS_TRANSIENT);
+           return(PS_SUCCESS);
        }
-       else if (!strncmp(ptr, "BAD", 3))
+
+       /* wait for a tagged response */
+       imap_ok (sock, 0);
+
+       /* try to recover for some responses */
+       if (!strncmp(buf, "* NO", 4) ||
+               !strncmp(buf, "* BAD", 5) ||
+               strstr(buf, "FETCH ()"))
        {
-           /* wait for a tagged response */
-           if (strstr (buf, "* BAD"))
-               imap_ok (sock, 0);
-           return(PS_TRANSIENT);
+           return(PS_TRANSIENT);
        }
-    }
 
-    if (num != number)
+       /* a response which does not match any of the above */
+       if (outlevel > O_SILENT)
+           report(stderr, GT_("Incorrect FETCH response: %s.\n"), buf);
        return(PS_ERROR);
-    else
-       return(PS_SUCCESS);
+    }
+    else if (ok == PS_SUCCESS)
+    {
+       /* an unexpected tagged response */
+       if (outlevel > O_SILENT)
+           report(stderr, GT_("Incorrect FETCH response: %s.\n"), buf);
+       return(PS_TRANSIENT);
+    }
+    return(ok);
 }
 
 static int imap_fetch_body(int sock, struct query *ctl, int number, int *lenp)
@@ -946,6 +1193,7 @@ static int imap_fetch_body(int sock, struct query *ctl, int number, int *lenp)
     char buf [MSGBUFSIZE+1], *cp;
     int        num;
 
+    (void)ctl;
     /* expunges change the fetch numbers */
     number -= expunged;
 
@@ -996,46 +1244,69 @@ static int imap_fetch_body(int sock, struct query *ctl, int number, int *lenp)
     if (num != number)
        return(PS_ERROR);
 
+    /* Understand "NIL" as length => no body present
+     * (MS Exchange, BerliOS Bug #11980) */
+    if (strstr(buf+10, "NIL)")) {
+           *lenp = 0;
+           return PS_SUCCESS;
+    }
+
+    /* Understand the empty string. Seen on Yahoo. */
+    /* XXX FIXME: we should be able to handle strings here. */
+    if (strstr(buf+10, "\"\")")) {
+           *lenp = 0;
+           return PS_SUCCESS;
+    }
+
     /*
      * Try to extract a length from the FETCH response.  RFC2060 requires
      * it to be present, but at least one IMAP server (Novell GroupWise)
-     * botches this.
+     * botches this.  The overflow check is needed because of a broken
+     * server called dbmail that returns huge garbage lengths.
      */
-    if ((cp = strchr(buf, '{')))
-       *lenp = atoi(cp + 1);
-    else
+    if ((cp = strchr(buf, '{'))) {
+       long l; char *t;
+        errno = 0;
+       ++ cp;
+       l = strtol(cp, &t, 10);
+        if (errno || t == cp || (t && !strchr(t, '}')) /* parse error */
+                   || l < 0 || l > INT_MAX /* range check */) {
+           *lenp = -1;
+       } else {
+           *lenp = l;
+       }
+    } else {
        *lenp = -1;     /* missing length part in FETCH reponse */
+    }
 
-    return(PS_SUCCESS);
+    return PS_SUCCESS;
 }
 
-static int imap_trail(int sock, struct query *ctl, int number)
+static int imap_trail(int sock, struct query *ctl, const char *tag)
 /* discard tail of FETCH response after reading message text */
 {
     /* expunges change the fetch numbers */
     /* number -= expunged; */
 
-    for (;;)
-    {
-       char buf[MSGBUFSIZE+1];
-       int ok;
-
-       if ((ok = gen_recv(sock, buf, sizeof(buf))))
-           return(ok);
-
-       /* UW IMAP returns "OK FETCH", Cyrus returns "OK Completed" */
-       if (strstr(buf, "OK"))
-           break;
-    }
+    (void)ctl;
+    (void)tag;
 
-    return(PS_SUCCESS);
+    return imap_ok(sock, NULL);
 }
 
 static int imap_delete(int sock, struct query *ctl, int number)
 /* set delete flag for given message */
 {
     int        ok;
+    /* Select which flags to set on message deletion: */
+    const char delflags_seen[] = "\\Seen \\Deleted";
+    static const char *delflags;
+    /* Which environment variable to look for: */
+
+    /* DEFAULT since many fetchmail versions <= 6.3.X */
+    delflags = delflags_seen;
 
+    (void)ctl;
     /* expunges change the fetch numbers */
     number -= expunged;
 
@@ -1043,17 +1314,18 @@ static int imap_delete(int sock, struct query *ctl, int number)
      * Use SILENT if possible as a minor throughput optimization.
      * Note: this has been dropped from IMAP4rev1.
      *
-     * We set Seen because there are some IMAP servers (notably HP
-     * OpenMail) that do message-receipt DSNs, but only when the seen
-     * bit is set.  This is the appropriate time -- we get here right
+     * We set \Seen because there are some IMAP servers (notably HP
+     * OpenMail and MS Exchange) do message-receipt DSNs,
+     * but only when the seen bit gets set.
+     * This is the appropriate time -- we get here right
      * after the local SMTP response that says delivery was
      * successful.
      */
     if ((ok = gen_transact(sock,
                        imap_version == IMAP4 
-                               ? "STORE %d +FLAGS.SILENT (\\Seen \\Deleted)"
-                               : "STORE %d +FLAGS (\\Seen \\Deleted)", 
-                       number)))
+                               ? "STORE %d +FLAGS.SILENT (%s)"
+                               : "STORE %d +FLAGS (%s)",
+                       number, delflags)))
        return(ok);
     else
        deletions++;
@@ -1065,7 +1337,10 @@ static int imap_delete(int sock, struct query *ctl, int number)
      * the next session.
      */
     if (NUM_NONZERO(expunge_period) && (deletions % expunge_period) == 0)
-       internal_expunge(sock);
+    {
+       if ((ok = internal_expunge(sock)))
+           return(ok);
+    }
 
     return(PS_SUCCESS);
 }
@@ -1073,6 +1348,11 @@ static int imap_delete(int sock, struct query *ctl, int number)
 static int imap_mark_seen(int sock, struct query *ctl, int number)
 /* mark the given message as seen */
 {
+    (void)ctl;
+
+    /* expunges change the message numbers */
+    number -= expunged;
+
     return(gen_transact(sock,
        imap_version == IMAP4
        ? "STORE %d +FLAGS.SILENT (\\Seen)"
@@ -1080,9 +1360,19 @@ static int imap_mark_seen(int sock, struct query *ctl, int number)
        number));
 }
 
+static int imap_end_mailbox_poll(int sock, struct query *ctl)
+/* cleanup mailbox before we idle or switch to another one */
+{
+    (void)ctl;
+    if (deletions)
+       internal_expunge(sock);
+    return(PS_SUCCESS);
+}
+
 static int imap_logout(int sock, struct query *ctl)
 /* send logout command */
 {
+    (void)ctl;
     /* if any un-expunged deletions remain, ship an expunge now */
     if (deletions)
        internal_expunge(sock);
@@ -1096,28 +1386,25 @@ static int imap_logout(int sock, struct query *ctl)
     return(gen_transact(sock, "LOGOUT"));
 }
 
-const static struct method imap =
+static const struct method imap =
 {
     "IMAP",            /* Internet Message Access Protocol */
-#if INET6_ENABLE
-    "imap",
-    "imaps",
-#else /* INET6_ENABLE */
-    143,                /* standard IMAP2bis/IMAP4 port */
-    993,                /* ssl IMAP2bis/IMAP4 port */
-#endif /* INET6_ENABLE */
+    "imap",            /* service (plain and TLS) */
+    "imaps",           /* service (SSL) */
     TRUE,              /* this is a tagged protocol */
     FALSE,             /* no message delimiter */
     imap_ok,           /* parse command response */
     imap_getauth,      /* get authorization */
     imap_getrange,     /* query range of messages */
     imap_getsizes,     /* get sizes of messages (used for ESMTP SIZE option) */
+    imap_getpartialsizes,      /* get sizes of subset of messages (used for ESMTP SIZE option) */
     imap_is_old,       /* no UID check */
     imap_fetch_headers,        /* request given message headers */
     imap_fetch_body,   /* request given message body */
     imap_trail,                /* eat message trailer */
     imap_delete,       /* delete the message */
     imap_mark_seen,    /* how to mark a message as seen */
+    imap_end_mailbox_poll,     /* end-of-mailbox processing */
     imap_logout,       /* expunge and exit */
     TRUE,              /* yes, we can re-poll */
 };