]> Pileus Git - ~andy/fetchmail/blobdiff - pop3.c
Bring us up to date with gnuplot 1.37.
[~andy/fetchmail] / pop3.c
diff --git a/pop3.c b/pop3.c
index 93c2516babe47cbac5b5ee6f0b47c57b06015ca4..75b523189cdda24b9229eab2864cdaa3d4f70096 100644 (file)
--- a/pop3.c
+++ b/pop3.c
@@ -70,7 +70,9 @@ static int pop3_ok (int sock, char *argbuf)
        }
        else if (strncmp(buf,"-ERR", 4) == 0)
        {
-           if (stage > STAGE_GETAUTH) 
+           if (stage == STAGE_FETCH)
+               ok = PS_TRANSIENT;
+           else if (stage > STAGE_GETAUTH)
                ok = PS_PROTOCOL;
            /*
             * We're checking for "lock busy", "unable to lock", 
@@ -141,6 +143,9 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
 #ifdef OPIE_ENABLE
     flag has_otp = FALSE;
 #endif /* OPIE_ENABLE */
+#ifdef SSL_ENABLE
+    flag has_ssl = FALSE;
+#endif /* SSL_ENABLE */
 
 #ifdef SDPS_ENABLE
     /*
@@ -187,43 +192,81 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
         * latches the server's authentication type, so that in daemon mode
         * the CAPA check only needs to be done once at start of run.
         *
-        * APOP was introduced in RFC 1450, and CAPA not until
-        * RFC2449. So the < check is an easy way to prevent CAPA from
-        * being sent to the more primitive POP3 servers dating from
-        * RFC 1081 and RFC 1225, which seem more likely to choke on
-        * it.  This certainly catches IMAP-2000's POP3 gateway.
-        * 
+        * If CAPA fails, then force the authentication method to PASSORD
+        * and repoll immediately.
+        *
         * These authentication methods are blessed by RFC1734,
         * describing the POP3 AUTHentication command.
         */
-       if (ctl->server.authenticate == A_ANY 
-           && strchr(greeting, '<') 
-           && gen_transact(sock, "CAPA") == 0)
+       if (ctl->server.authenticate == A_ANY)
        {
-           char buffer[64];
-
-           /* determine what authentication methods we have available */
-           while ((ok = gen_recv(sock, buffer, sizeof(buffer))) == 0)
+           ok = gen_transact(sock, "CAPA");
+           if (ok == PS_SUCCESS)
            {
-               if (DOTLINE(buffer))
-                   break;
+               char buffer[64];
+
+               /* determine what authentication methods we have available */
+               while ((ok = gen_recv(sock, buffer, sizeof(buffer))) == 0)
+               {
+                   if (DOTLINE(buffer))
+                       break;
+#ifdef SSL_ENABLE
+                   if (strstr(buffer, "STLS"))
+                       has_ssl = TRUE;
+#endif /* SSL_ENABLE */
 #if defined(GSSAPI)
-               if (strstr(buffer, "GSSAPI"))
-                   has_gssapi = TRUE;
+                   if (strstr(buffer, "GSSAPI"))
+                       has_gssapi = TRUE;
 #endif /* defined(GSSAPI) */
 #if defined(KERBEROS_V4)
-               if (strstr(buffer, "KERBEROS_V4"))
-                   has_kerberos = TRUE;
+                   if (strstr(buffer, "KERBEROS_V4"))
+                       has_kerberos = TRUE;
 #endif /* defined(KERBEROS_V4)  */
 #ifdef OPIE_ENABLE
-               if (strstr(buffer, "X-OTP"))
-                   has_otp = TRUE;
+                   if (strstr(buffer, "X-OTP"))
+                       has_otp = TRUE;
 #endif /* OPIE_ENABLE */
-               if (strstr(buffer, "CRAM-MD5"))
-                   has_cram = TRUE;
+                   if (strstr(buffer, "CRAM-MD5"))
+                       has_cram = TRUE;
+               }
+           }
+           /* we are in STAGE_GETAUTH! */
+           else if (ok == PS_AUTHFAIL ||
+               /* Some servers directly close the socket. However, if we
+                * have already authenticated before, then a previous CAPA
+                * must have succeeded. In that case, treat this as a
+                * genuine socket error and do not change the auth method.
+                */
+               (ok == PS_SOCKET && !ctl->wehaveauthed))
+           {
+               ctl->server.authenticate = A_PASSWORD;
+               /* repoll immediately */
+               ok = PS_REPOLL;
+               break;
            }
        }
 
+#ifdef SSL_ENABLE
+       if (has_ssl && !ctl->use_ssl &&
+#if INET6_ENABLE
+           ctl->server.service && (strcmp(ctl->server.service, "pop3s"))
+#else /* INET6_ENABLE */
+           ctl->server.port != 995
+#endif /* INET6_ENABLE */
+           )
+       {
+           char *realhost;
+
+           realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;           gen_transact(sock, "STLS");
+           if (SSLOpen(sock,ctl->sslcert,ctl->sslkey,ctl->sslproto,ctl->sslcertck, ctl->sslcertpath,ctl->sslfingerprint,realhost,ctl->server.pollname) == -1)
+           {
+               report(stderr,
+                      GT_("SSL connection failed.\n"));
+               return(PS_AUTHFAIL);
+           }
+       }
+#endif /* SSL_ENABLE */
+
        /*
         * OK, we have an authentication type now.
         */
@@ -241,36 +284,43 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
            && (ctl->server.authenticate == A_KERBEROS_V4
             || ctl->server.authenticate == A_KERBEROS_V5
             || ctl->server.authenticate == A_ANY))
-           if(ok = do_rfc1731(sock, "AUTH", ctl->server.truename))
-               if(ctl->server.authenticate != A_ANY)
-                    break;
+       {
+           ok = do_rfc1731(sock, "AUTH", ctl->server.truename);
+           if (ok == PS_SUCCESS || ctl->server.authenticate != A_ANY)
+               break;
+       }
 #endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
 
 #if defined(GSSAPI)
        if (has_gssapi &&
            (ctl->server.authenticate == A_GSSAPI ||
             ctl->server.authenticate == A_ANY))
-           if(ok = do_gssauth(sock, "AUTH", 
-                              ctl->server.truename, ctl->remotename))
-               if(ctl->server.authenticate != A_ANY)
-                    break;
+       {
+           ok = do_gssauth(sock,"AUTH",ctl->server.truename,ctl->remotename);
+           if (ok == PS_SUCCESS || ctl->server.authenticate != A_ANY)
+               break;
+       }
 #endif /* defined(GSSAPI) */
 
 #ifdef OPIE_ENABLE
        if (has_otp &&
            (ctl->server.authenticate == A_OTP ||
             ctl->server.authenticate == A_ANY))
-           if(ok = do_otp(sock, "AUTH", ctl))
-               if(ctl->server.authenticate != A_ANY)
-                    break;
+       {
+           ok = do_otp(sock, "AUTH", ctl);
+           if (ok == PS_SUCCESS || ctl->server.authenticate != A_ANY)
+               break;
+       }
 #endif /* OPIE_ENABLE */
 
        if (has_cram &&
            (ctl->server.authenticate == A_CRAM_MD5 ||
             ctl->server.authenticate == A_ANY))
-           if(ok = do_cram_md5(sock, "AUTH", ctl))
-               if(ctl->server.authenticate != A_ANY)
-                    break;
+       {
+           ok = do_cram_md5(sock, "AUTH", ctl, NULL);
+           if (ok == PS_SUCCESS || ctl->server.authenticate != A_ANY)
+               break;
+       }
 
        /* ordinary validation, no one-time password or RPA */ 
        gen_transact(sock, "USER %s", ctl->remotename);
@@ -286,7 +336,7 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
            continue;
        if (*start == 0) {
            report(stderr,
-                  _("Required APOP timestamp not found in greeting\n"));
+                  GT_("Required APOP timestamp not found in greeting\n"));
            return(PS_AUTHFAIL);
        }
 
@@ -295,7 +345,7 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
            continue;
        if (*end == 0 || end == start + 1) {
            report(stderr, 
-                  _("Timestamp syntax error in greeting\n"));
+                  GT_("Timestamp syntax error in greeting\n"));
            return(PS_AUTHFAIL);
        }
        else
@@ -317,7 +367,7 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
        break;
 
     default:
-       report(stderr, _("Undefined protocol request in POP3_auth\n"));
+       report(stderr, GT_("Undefined protocol request in POP3_auth\n"));
        ok = PS_ERROR;
     }
 
@@ -325,7 +375,7 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
     {
        /* maybe we detected a lock-busy condition? */
         if (ok == PS_LOCKBUSY)
-           report(stderr, _("lock busy!  Is another session active?\n")); 
+           report(stderr, GT_("lock busy!  Is another session active?\n")); 
 
        return(ok);
     }
@@ -433,7 +483,7 @@ static int pop3_slowuidl( int sock,  struct query *ctl, int *countp, int *newp)
                }
            } else {
                report(stderr, 
-                      _("Messages inserted into list on server. Cannot handle this.\n"));
+                      GT_("Messages inserted into list on server. Cannot handle this.\n"));
                return -1;
            }
        } 
@@ -504,7 +554,7 @@ static int pop3_getrange(int sock,
        {
            if (sscanf(buf, "%d", &last) == 0)
            {
-               report(stderr, _("protocol error\n"));
+               report(stderr, GT_("protocol error\n"));
                return(PS_ERROR);
            }
            *newp = (*countp - last);
@@ -517,7 +567,7 @@ static int pop3_getrange(int sock,
                /* don't worry, yet! do it the slow way */
                if((ok = pop3_slowuidl( sock, ctl, countp, newp))!=0)
                {
-                   report(stderr, _("protocol error while fetching UIDLs\n"));
+                   report(stderr, GT_("protocol error while fetching UIDLs\n"));
                    return(PS_ERROR);
                }
            }
@@ -565,12 +615,17 @@ static int pop3_getsizes(int sock, int count, int *sizes)
 
        while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
        {
-           int num, size;
+           unsigned int num, size;
 
            if (DOTLINE(buf))
                break;
-           else if (sscanf(buf, "%d %d", &num, &size) == 2)
-               sizes[num - 1] = size;
+           else if (sscanf(buf, "%u %u", &num, &size) == 2) {
+               if (num > 0 && num <= count)
+                   sizes[num - 1] = size;
+               else
+                   /* warn about possible attempt to induce buffer overrun */
+                   report(stderr, "Warning: ignoring bogus data for message sizes returned by server.\n");
+           }
        }
 
        return(ok);
@@ -704,6 +759,24 @@ static int pop3_logout(int sock, struct query *ctl)
 {
     int ok;
 
+#ifdef __UNUSED__
+    /*
+     * We used to do this in case the server marks messages deleted when seen.
+     * (Yes, this has been reported, in the MercuryP/NLM server.
+     * It's even legal under RFC 1939 (section 8) as a site policy.)
+     * It interacted badly with UIDL, though.  Thomas Zajic wrote:
+     * "Running 'fetchmail -F -v' and checking the logs, I found out
+     * that fetchmail did in fact flush my mailbox properly, but sent
+     * a RSET just before sending QUIT to log off.  This caused the
+     * POP3 server to undo/forget about the previous DELEs, resetting
+     * my mailbox to its original (ie.  unflushed) state. The
+     * ~/.fetchids file did get flushed though, so the next time
+     * fetchmail was run it saw all the old messages as new ones ..."
+     */
+     if (ctl->keep)
+       gen_transact(sock, "RSET");
+#endif /* __UNUSED__ */
+
     ok = gen_transact(sock, "QUIT");
     if (!ok)
        expunge_uids(ctl);
@@ -747,7 +820,7 @@ int doPOP3 (struct query *ctl)
 {
 #ifndef MBOX
     if (ctl->mailboxes->id) {
-       fprintf(stderr,_("Option --remote is not supported with POP3\n"));
+       fprintf(stderr,GT_("Option --remote is not supported with POP3\n"));
        return(PS_SYNTAX);
     }
 #endif /* MBOX */