]> Pileus Git - ~andy/fetchmail/blobdiff - imap.c
Compiler warnings fixes, preprocessor and minor general cleanup.
[~andy/fetchmail] / imap.c
diff --git a/imap.c b/imap.c
index 3e6d80fc8ed4a45d6413b7235a4dde3af80e59d8..d7f0b83f14da7318e1b0f9072f1ec5bc3f538baa 100644 (file)
--- a/imap.c
+++ b/imap.c
 #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
+#ifdef OPIE_ENABLE
 #endif /* OPIE_ENABLE */
 
 #ifndef strstr         /* glibc-2.1 declares this as a macro */
-extern char *strstr(); /* needed on sysV68 R3V7.1. */
+extern char *strstr(const char *, const char *);       /* needed on sysV68 R3V7.1. */
 #endif /* strstr */
 
 /* imap_version values */
@@ -34,7 +35,7 @@ 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;
+static flag do_idle, has_idle;
 static char capabilities[MSGBUFSIZE+1];
 static unsigned int *unseen_messages;
 
@@ -86,10 +87,14 @@ static int imap_ok(int sock, char *argbuf)
             */
            if (stage == STAGE_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");
+               /* 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_FETCH;
@@ -157,7 +162,7 @@ static int imap_ok(int sock, char *argbuf)
     }
 }
 
-#if NTLM_ENABLE
+#ifdef NTLM_ENABLE
 #include "ntlm.h"
 
 static tSmbNtlmAuthRequest   request;             
@@ -292,12 +297,18 @@ static void capa_probe(int sock, struct query *ctl)
      * Handle idling.  We depend on coming through here on startup
      * and after each timeout (including timeouts during idles).
      */
-    if (strstr(capabilities, "IDLE") && ctl->idle)
+    if (ctl->idle)
     {
        do_idle = TRUE;
+       if (strstr(capabilities, "IDLE"))
+       {
+           has_idle = TRUE;
+       }
        if (outlevel >= O_VERBOSE)
            report(stdout, GT_("will idle after poll\n"));
     }
+
+    peek_capable = (imap_version >= IMAP4);
 }
 
 static int imap_getauth(int sock, struct query *ctl, char *greeting)
@@ -308,6 +319,15 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
     flag did_stls = FALSE;
 #endif /* SSL_ENABLE */
 
+    /*
+     * Assumption: expunges are cheap, so we want to do them
+     * after every message unless user said otherwise.
+     */
+    if (NUM_SPECIFIED(ctl->expunge))
+       expunge_period = NUM_VALUE_OUT(ctl->expunge);
+    else
+       expunge_period = 1;
+
     capa_probe(sock, ctl);
 
     /* 
@@ -319,44 +339,6 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
         preauth = FALSE;  /* reset for the next session */
         return(PS_SUCCESS);
     }
-    /*
-     * Time to authenticate the user.
-     * Try the protocol variants that don't require passwords first.
-     */
-    ok = PS_AUTHFAIL;
-
-#ifdef GSSAPI
-    if ((ctl->server.authenticate == A_ANY 
-        || ctl->server.authenticate == A_GSSAPI)
-       && strstr(capabilities, "AUTH=GSSAPI"))
-       if(ok = do_gssauth(sock, "AUTHENTICATE", ctl->server.truename, ctl->remotename))
-       {
-           /* SASL cancellation of authentication */
-           gen_send(sock, "*");
-           if(ctl->server.authenticate != A_ANY)
-                return ok;
-       }
-       else
-           return ok;
-#endif /* GSSAPI */
-
-#ifdef KERBEROS_V4
-    if ((ctl->server.authenticate == A_ANY 
-        || ctl->server.authenticate == A_KERBEROS_V4
-        || ctl->server.authenticate == A_KERBEROS_V5) 
-       && strstr(capabilities, "AUTH=KERBEROS_V4"))
-    {
-       if ((ok = do_rfc1731(sock, "AUTHENTICATE", ctl->server.truename)))
-       {
-           /* SASL cancellation of authentication */
-           gen_send(sock, "*");
-           if(ctl->server.authenticate != A_ANY)
-                return ok;
-       }
-       else
-           return ok;
-    }
-#endif /* KERBEROS_V4 */
 
 #ifdef SSL_ENABLE
     if ((!ctl->sslproto || !strcmp(ctl->sslproto,"tls1"))
@@ -400,16 +382,44 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
     }
 #endif /* SSL_ENABLE */
 
-    peek_capable = (imap_version >= IMAP4);
-
-    /* 
-     * Assumption: expunges are cheap, so we want to do them
-     * after every message unless user said otherwise.
+    /*
+     * Time to authenticate the user.
+     * Try the protocol variants that don't require passwords first.
      */
-    if (NUM_SPECIFIED(ctl->expunge))
-       expunge_period = NUM_VALUE_OUT(ctl->expunge);
-    else
-       expunge_period = 1;
+    ok = PS_AUTHFAIL;
+
+#ifdef GSSAPI
+    if ((ctl->server.authenticate == A_ANY 
+        || ctl->server.authenticate == A_GSSAPI)
+       && strstr(capabilities, "AUTH=GSSAPI"))
+       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)
+                return ok;
+       }
+       else
+           return ok;
+#endif /* GSSAPI */
+
+#ifdef KERBEROS_V4
+    if ((ctl->server.authenticate == A_ANY 
+        || ctl->server.authenticate == A_KERBEROS_V4
+        || ctl->server.authenticate == A_KERBEROS_V5) 
+       && strstr(capabilities, "AUTH=KERBEROS_V4"))
+    {
+       if ((ok = do_rfc1731(sock, "AUTHENTICATE", ctl->server.truename)))
+       {
+           /* SASL cancellation of authentication */
+           gen_send(sock, "*");
+           if(ctl->server.authenticate != A_ANY)
+                return ok;
+       }
+       else
+           return ok;
+    }
+#endif /* KERBEROS_V4 */
 
     /*
      * No such luck.  OK, now try the variants that mask your password
@@ -430,7 +440,7 @@ 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"))
@@ -498,7 +508,13 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
        imap_canonicalize(remotename, ctl->remotename, NAMELEN);
        imap_canonicalize(password, ctl->password, PASSWORDLEN);
 
-       strcpy(shroud, password);
+#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);
        shroud[0] = '\0';
 #ifdef SSL_ENABLE
@@ -544,35 +560,57 @@ static int internal_expunge(int sock)
 }
 
 static int imap_idle(int sock)
-/* start an RFC2177 IDLE */
+/* start an RFC2177 IDLE, or fake one if unsupported */
 {
     int ok;
 
-    /* special timeout to terminate the IDLE and re-issue it
-     * at least every 28 minutes:
-     * (the server may have an inactivity timeout) */
     stage = STAGE_IDLE;
     saved_timeout = mytimeout;
-    mytimeout = 1680; /* 28 min */
 
-    /* enter IDLE mode */
-    ok = gen_transact(sock, "IDLE");
+    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 */
+       /* enter IDLE mode */
+       ok = gen_transact(sock, "IDLE");
+
+       if (ok == PS_IDLETIMEOUT) {
+           /* send "DONE" continuation */
+           SockWrite(sock, "DONE\r\n", 6);
+           if (outlevel >= O_MONITOR)
+               report(stdout, "IMAP> DONE\n");
+       } else
+           /* not idle timeout */
+           return ok;
+    } 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;
+       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);
 
-    if(ok == PS_IDLETIMEOUT) {
-       /* send "DONE" continuation */
-       SockWrite(sock, "DONE\r\n", 6);
-       if (outlevel >= O_MONITOR)
-           report(stdout, "IMAP> DONE\n");
+       /* 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);
+    }
 
-       /* restore normal timeout value */
-       mytimeout = saved_timeout;
-       stage = STAGE_FETCH;
+    /* restore normal timeout value */
+    mytimeout = saved_timeout;
+    stage = STAGE_FETCH;
 
-       /* get OK IDLE message */
-       return imap_ok(sock, NULL);
-    } else
-       /* not idle timeout */
-       return ok;
+    /* get OK IDLE message */
+    if (has_idle)
+        return imap_ok(sock, NULL);
+
+    return PS_SUCCESS;
 }
 
 static int imap_getrange(int sock, 
@@ -687,7 +725,8 @@ static int imap_getrange(int sock,
        memset(unseen_messages, 0, count * sizeof(unsigned int));
        unseen = 0;
 
-       gen_send(sock, "SEARCH UNSEEN");
+       /* don't count deleted messages, in case user enabled keep last time */
+       gen_send(sock, "SEARCH UNSEEN NOT DELETED");
        do {
            ok = gen_recv(sock, buf, sizeof(buf));
            if (ok != 0)
@@ -746,8 +785,8 @@ static int imap_getrange(int sock,
     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];
 
@@ -785,14 +824,15 @@ static int imap_getsizes(int sock, int count, int *sizes)
      * 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 (;;)
@@ -813,8 +853,8 @@ static int imap_getsizes(int sock, int count, int *sizes)
            break;
        else if (sscanf(buf, "* %u FETCH (RFC822.SIZE %u)", &num, &size) == 2) 
        {
-           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");
        }
@@ -823,6 +863,12 @@ static int imap_getsizes(int sock, int count, int *sizes)
     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)
 /* is the given message old? */
 {
@@ -962,10 +1008,15 @@ static int imap_fetch_body(int sock, struct query *ctl, int number, int *lenp)
     /*
      * 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);
+    if ((cp = strchr(buf, '{'))) {
+        errno = 0;
+       *lenp = (int)strtol(cp + 1, (char **)NULL, 10);
+        if (errno == ERANGE && (*lenp == LONG_MAX || *lenp == LONG_MIN))
+            *lenp = -1;    /* length is too big/small for us to handle */
+    }
     else
        *lenp = -1;     /* missing length part in FETCH reponse */
 
@@ -1059,10 +1110,10 @@ 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
+#ifdef INET6_ENABLE
     "imap",
     "imaps",
 #else /* INET6_ENABLE */
@@ -1075,6 +1126,7 @@ const static struct method imap =
     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 */