]> 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 f781105dd627d71882ba6d8592e4bf4857fd16ac..75b523189cdda24b9229eab2864cdaa3d4f70096 100644 (file)
--- a/pop3.c
+++ b/pop3.c
@@ -41,7 +41,7 @@ static char lastok[POPBUFSIZE+1];
 
 #define DOTLINE(s)     (s[0] == '.' && (s[1]=='\r'||s[1]=='\n'||s[1]=='\0'))
 
-int pop3_ok (int sock, char *argbuf)
+static int pop3_ok (int sock, char *argbuf)
 /* parse command response */
 {
     int ok;
@@ -49,8 +49,7 @@ int pop3_ok (int sock, char *argbuf)
     char *bufp;
 
     if ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
-    {
-       bufp = buf;
+    {  bufp = buf;
        if (*bufp == '+' || *bufp == '-')
            bufp++;
        else
@@ -71,7 +70,9 @@ 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", 
@@ -123,7 +124,7 @@ int pop3_ok (int sock, char *argbuf)
     return(ok);
 }
 
-int pop3_getauth(int sock, struct query *ctl, char *greeting)
+static int pop3_getauth(int sock, struct query *ctl, char *greeting)
 /* apply for connection authorization */
 {
     int ok;
@@ -132,6 +133,19 @@ int pop3_getauth(int sock, struct query *ctl, char *greeting)
 #if OPIE_ENABLE
     char *challenge;
 #endif /* OPIE_ENABLE */
+#if defined(GSSAPI)
+    flag has_gssapi = FALSE;
+#endif /* defined(GSSAPI) */
+#if defined(KERBEROS_V4) || defined(KERBEROS_V5)
+    flag has_kerberos = FALSE;
+#endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
+    flag has_cram = FALSE;
+#ifdef OPIE_ENABLE
+    flag has_otp = FALSE;
+#endif /* OPIE_ENABLE */
+#ifdef SSL_ENABLE
+    flag has_ssl = FALSE;
+#endif /* SSL_ENABLE */
 
 #ifdef SDPS_ENABLE
     /*
@@ -178,105 +192,141 @@ 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)
        {
-#if defined(GSSAPI)
-           flag has_gssapi = FALSE;
-#endif /* defined(GSSAPI) */
-#if defined(KERBEROS_V4) || defined(KERBEROS_V5)
-           flag has_kerberos = FALSE;
-#endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
-           flag has_cram = FALSE;
-#ifdef OPIE_ENABLE
-           flag has_otp = FALSE;
-#endif /* OPIE_ENABLE */
-           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) || defined(KERBEROS_V5)
-               if (strstr(buffer, "KERBEROS_V4"))
-                   has_kerberos = TRUE;
-#endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
+#if defined(KERBEROS_V4)
+                   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;
            }
-
-           /*
-            * Here's where we set priorities.  Note that we must do tests
-            * in *reverse* order of desirability.
-            */
-           if (has_cram)
-               ctl->server.authenticate = A_CRAM_MD5;
-#ifdef OPIE_ENABLE
-           if (has_otp)
-               ctl->server.authenticate = A_OTP;
-#endif /* OPIE_ENABLE */
-#if defined(GSSAPI)
-           if (has_gssapi)
-               ctl->server.authenticate = A_GSSAPI;
-#endif /* defined(GSSAPI) */
-#if defined(KERBEROS_V4) || defined(KERBEROS_V5)
-           if (has_kerberos)
-               ctl->server.authenticate = A_KERBEROS_V4;
-#endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
        }
 
+#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.
         */
-#if defined(KERBEROS_V4) || defined(KERBEROS_V5)
+#if defined(KERBEROS_V4)
        /* 
         * Servers doing KPOP have to go through a dummy login sequence
         * rather than doing SASL.
         */
-       if (
+       if (has_kerberos &&
 #if INET6_ENABLE
            ctl->server.service && (strcmp(ctl->server.service, KPOP_PORT)!=0)
 #else /* INET6_ENABLE */
            ctl->server.port != KPOP_PORT
 #endif /* INET6_ENABLE */
            && (ctl->server.authenticate == A_KERBEROS_V4
-            || ctl->server.authenticate == A_KERBEROS_V5))
-           return(do_rfc1731(sock, "AUTH", ctl->server.truename));
+            || ctl->server.authenticate == A_KERBEROS_V5
+            || ctl->server.authenticate == A_ANY))
+       {
+           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 (ctl->server.authenticate==A_GSSAPI)
-           return(do_gssauth(sock, "AUTH", 
-                             ctl->server.truename, ctl->remotename));
+       if (has_gssapi &&
+           (ctl->server.authenticate == A_GSSAPI ||
+            ctl->server.authenticate == A_ANY))
+       {
+           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 (ctl->server.authenticate == A_OTP)
-           do_otp(sock, "AUTH", ctl);
+       if (has_otp &&
+           (ctl->server.authenticate == A_OTP ||
+            ctl->server.authenticate == A_ANY))
+       {
+           ok = do_otp(sock, "AUTH", ctl);
+           if (ok == PS_SUCCESS || ctl->server.authenticate != A_ANY)
+               break;
+       }
 #endif /* OPIE_ENABLE */
-       if (ctl->server.authenticate == A_CRAM_MD5)
-           return(do_cram_md5(sock, "AUTH", ctl));
+
+       if (has_cram &&
+           (ctl->server.authenticate == A_CRAM_MD5 ||
+            ctl->server.authenticate == A_ANY))
+       {
+           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);
+       strcpy(shroud, ctl->password);
        ok = gen_transact(sock, "PASS %s", ctl->password);
+       shroud[0] = '\0';
        break;
 
     case P_APOP:
@@ -286,7 +336,7 @@ 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 @@ 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 @@ 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 @@ 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);
     }
@@ -346,8 +396,7 @@ int pop3_getauth(int sock, struct query *ctl, char *greeting)
     return(PS_SUCCESS);
 }
 
-static int
-pop3_gettopid( int sock, int num , char *id)
+static int pop3_gettopid( int sock, int num , char *id)
 {
     int ok;
     int got_it;
@@ -370,8 +419,7 @@ pop3_gettopid( int sock, int num , char *id)
     return 0;
 }
 
-static int
-pop3_slowuidl( int sock,  struct query *ctl, int *countp, int *newp)
+static int pop3_slowuidl( int sock,  struct query *ctl, int *countp, int *newp)
 {
     /* This approach tries to get the message headers from the
      * remote hosts and compares the message-id to the already known
@@ -435,7 +483,7 @@ 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;
            }
        } 
@@ -506,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);
@@ -519,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);
                }
            }
@@ -567,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);
@@ -706,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);
@@ -732,7 +803,6 @@ const static struct method pop3 =
     FALSE,             /* this is not a tagged protocol */
     TRUE,              /* this uses a message delimiter */
     pop3_ok,           /* parse command response */
-    NULL,              /* no password canonicalization */
     pop3_getauth,      /* get authorization */
     pop3_getrange,     /* query range of messages */
     pop3_getsizes,     /* we can get a list of sizes */
@@ -750,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 */