]> Pileus Git - ~andy/fetchmail/blobdiff - pop3.c
Merge Rainer Weikusat's P-Tree implementation for uid.
[~andy/fetchmail] / pop3.c
diff --git a/pop3.c b/pop3.c
index b86cab5407e6949926640e7fafb2805673efb510..dd8e46041e74728ee5286122351d25d482d5fd2f 100644 (file)
--- a/pop3.c
+++ b/pop3.c
 #include  <stdio.h>
 #include  <string.h>
 #include  <ctype.h>
-#if defined(HAVE_UNISTD_H)
 #include <unistd.h>
-#endif
-#if defined(STDC_HEADERS)
 #include  <stdlib.h>
-#endif
 #include  <errno.h>
 
 #include  "fetchmail.h"
 #include  "socket.h"
-#include  "i18n.h"
+#include  "gettext.h"
 #include  "uid_db.h"
 
 #ifdef OPIE_ENABLE
@@ -46,9 +42,9 @@ flag done_capa = FALSE;
 #if defined(GSSAPI)
 flag has_gssapi = FALSE;
 #endif /* defined(GSSAPI) */
-#if defined(KERBEROS_V4) || defined(KERBEROS_V5)
+#if defined(KERBEROS_V5)
 flag has_kerberos = FALSE;
-#endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
+#endif /* defined(KERBEROS_V5) */
 static flag has_cram = FALSE;
 #ifdef OPIE_ENABLE
 flag has_otp = FALSE;
@@ -206,9 +202,9 @@ static int capa_probe(int sock)
 #if defined(GSSAPI)
     has_gssapi = FALSE;
 #endif /* defined(GSSAPI) */
-#if defined(KERBEROS_V4) || defined(KERBEROS_V5)
+#if defined(KERBEROS_V5)
     has_kerberos = FALSE;
-#endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
+#endif /* defined(KERBEROS_V5) */
     has_cram = FALSE;
 #ifdef OPIE_ENABLE
     has_otp = FALSE;
@@ -238,11 +234,6 @@ static int capa_probe(int sock)
                has_gssapi = TRUE;
 #endif /* defined(GSSAPI) */
 
-#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;
@@ -288,9 +279,9 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
 #if defined(GSSAPI)
     has_gssapi = FALSE;
 #endif /* defined(GSSAPI) */
-#if defined(KERBEROS_V4) || defined(KERBEROS_V5)
+#if defined(KERBEROS_V5)
     has_kerberos = FALSE;
-#endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
+#endif /* defined(KERBEROS_V5) */
     has_cram = FALSE;
 #ifdef OPIE_ENABLE
     has_otp = FALSE;
@@ -390,7 +381,6 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
         */
        if ((ctl->server.authenticate == A_ANY) ||
                (ctl->server.authenticate == A_GSSAPI) ||
-               (ctl->server.authenticate == A_KERBEROS_V4) ||
                (ctl->server.authenticate == A_KERBEROS_V5) ||
                (ctl->server.authenticate == A_OTP) ||
                (ctl->server.authenticate == A_CRAM_MD5) ||
@@ -499,22 +489,6 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
        /*
         * OK, we have an authentication type now.
         */
-#if defined(KERBEROS_V4)
-       /* 
-        * Servers doing KPOP have to go through a dummy login sequence
-        * rather than doing SASL.
-        */
-       if (has_kerberos &&
-           ctl->server.service && (strcmp(ctl->server.service, KPOP_PORT)!=0)
-           && (ctl->server.authenticate == A_KERBEROS_V4
-            || 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 (has_gssapi &&
@@ -601,7 +575,6 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
         * users switching *to* KPOP accidentally revealing their
         * password */
        if ((ctl->server.authenticate == A_ANY
-                   || ctl->server.authenticate == A_KERBEROS_V4
                    || ctl->server.authenticate == A_KERBEROS_V5)
                && (ctl->server.service != NULL
                    && strcmp(ctl->server.service, KPOP_PORT) == 0))
@@ -669,15 +642,6 @@ static int pop3_getauth(int sock, struct query *ctl, char *greeting)
        ok = gen_transact(sock, "APOP %s %s", ctl->remotename, (char *)ctl->digest);
        break;
 
-    case P_RPOP:
-       if ((ok = gen_transact(sock,"USER %s", ctl->remotename)) == 0) {
-           strlcpy(shroud, ctl->password, sizeof(shroud));
-           ok = gen_transact(sock, "RPOP %s", ctl->password);
-           memset(shroud, 0x55, sizeof(shroud));
-           shroud[0] = '\0';
-       }
-       break;
-
     default:
        report(stderr, GT_("Undefined protocol request in POP3_auth\n"));
        ok = PS_ERROR;
@@ -731,39 +695,6 @@ static void trim(char *s) {
     s[0] = '\0';
 }
 
-/* XXX FIXME: using the Message-ID is unsafe, some messages (spam,
- * broken messages) do not have Message-ID headers, and messages without
- * those appear to break this code and cause fetchmail (at least version
- * 6.2.3) to not delete such messages properly after retrieval.
- * See Sourceforge Bug #780933.
- *
- * The other problem is that the TOP command itself is optional, too... */
-static int pop3_gettopid(int sock, int num , char *id, size_t idsize)
-{
-    int ok;
-    int got_it;
-    char buf [POPBUFSIZE+1];
-    snprintf(buf, sizeof(buf), "TOP %d 1", num);
-    if ((ok = gen_transact(sock, "%s", buf)) != 0)
-       return ok;
-    got_it = 0;
-    while (gen_recv(sock, buf, sizeof(buf)) == 0)
-    {
-       if (DOTLINE(buf))
-           break;
-       if (!got_it && 0 == strncasecmp("Message-Id:", buf, 11)) {
-           char *p = buf + 11;
-           got_it = 1;
-           p += strspn(p, POSIX_space);
-           strlcpy(id, p, idsize);
-           trim(id);
-       }
-    }
-    /* XXX FIXME: do not return success here if no Message-ID header was
-     * found. */
-    return 0;
-}
-
 /** Parse the UID response (leading +OK must have been
  * stripped off) in buf, store the number in gotnum, and store the ID
  * into the caller-provided buffer "id" of size "idsize".
@@ -867,103 +798,6 @@ static int pop3_fastuidl( int sock,  struct query *ctl, unsigned int count, int
     return 0;
 }
 
-static int pop3_slowuidl( int sock,  struct query *ctl, int *countp, int *newp)
-{
-    /* XXX FIXME: this code is severely broken. A Cc:d mailing list
-     * message will arrive twice with the same Message-ID, so this
-     * slowuidl code will break. Same goes for messages without
-     * Message-ID headers at all. This code would best be removed. */
-    /* This approach tries to get the message headers from the
-     * remote hosts and compares the message-id to the already known
-     * ones:
-     *  + if the first message containes a new id, all messages on
-     *    the server will be new
-     *  + if the first is known, try to estimate the last known message
-     *    on the server and check. If this works you know the total number
-     *    of messages to get.
-     *  + Otherwise run a binary search to determine the last known message
-     */
-    struct uid_db_record *rec;
-    int ok, nolinear = 0;
-    int first_nr, n_recs, try_id, try_nr, add_id;
-    int num;
-    char id [IDLEN+1];
-
-    if ((ok = pop3_gettopid(sock, 1, id, sizeof(id))) != 0)
-       return ok;
-
-    rec = first_uid_in_db(&ctl->oldsaved, id);
-    if (!rec) {
-       /* the first message is unknown -> all messages are new */
-       *newp = *countp;        
-       return 0;
-    }
-    first_nr = rec->pos;
-
-    /* check where we expect the latest known message */
-    n_recs = uid_db_n_records(&ctl->oldsaved);
-    try_id = n_recs  - first_nr; /* -1 + 1 */
-    if( try_id > 1 ) {
-       if( try_id <= *countp ) {
-           if ((ok = pop3_gettopid(sock, try_id, id, sizeof(id))) != 0)
-               return ok;
-
-           rec = last_uid_in_db(&ctl->oldsaved, id);
-           try_nr = rec ? rec->pos : -1;
-       } else {
-           try_id = *countp+1;
-           try_nr = -1;
-       }
-       if( try_nr != n_recs -1 ) {
-           /* some messages inbetween have been deleted... */
-           if( try_nr == -1 ) {
-               nolinear = 1;
-
-               for( add_id = 1<<30; add_id > try_id-1; add_id >>= 1 )
-                   ;
-               for( ; add_id; add_id >>= 1 ) {
-                   if( try_nr == -1 ) {
-                       if( try_id - add_id <= 1 ) {
-                           continue;
-                       }
-                       try_id -= add_id;
-                   } else 
-                       try_id += add_id;
-                   
-                   if ((ok = pop3_gettopid(sock, try_id, id, sizeof(id))) != 0)
-                       return ok;
-
-                   rec = find_uid_by_id(&ctl->oldsaved, id);
-                   try_nr = rec ? rec->pos : -1;
-               }
-               if( try_nr == -1 ) {
-                   try_id--;
-               }
-           } else {
-               report(stderr, 
-                      GT_("Messages inserted into list on server. Cannot handle this.\n"));
-               return -1;
-           }
-       } 
-    }
-    /* the first try_id messages are known -> copy them to the newsaved list */
-    for( num = first_nr; num < n_recs; num++ )
-    {
-       rec = find_uid_by_pos(&ctl->oldsaved, num);
-
-       rec = uid_db_insert(&ctl->newsaved, rec->id, rec->status);
-       set_uid_db_num(&ctl->newsaved, rec, num - first_nr + 1);
-    }
-
-    if( nolinear ) {
-       clear_uid_db(&ctl->oldsaved);
-       last = try_id;
-    }
-
-    *newp = *countp - try_id;
-    return 0;
-}
-
 static int pop3_getrange(int sock, 
                         struct query *ctl,
                         const char *folder,
@@ -1052,12 +886,8 @@ static int pop3_getrange(int sock,
            /* grab the mailbox's UID list */
            if (gen_transact(sock, "UIDL") != 0)
            {
-               /* don't worry, yet! do it the slow way */
-               if (pop3_slowuidl(sock, ctl, countp, newp))
-               {
                    report(stderr, GT_("protocol error while fetching UIDLs\n"));
                    return(PS_ERROR);
-               }
            }
            else
            {
@@ -1223,29 +1053,6 @@ static int pop3_is_old(int sock, struct query *ctl, int num)
     }
 }
 
-#ifdef UNUSED
-/*
- * We could use this to fetch headers only as we do for IMAP.  The trouble 
- * is that there's no way to fetch the body only.  So the following RETR 
- * would have to re-fetch the header.  Enough messages have longer headers
- * than bodies to make this a net loss.
- */
-static int pop_fetch_headers(int sock, struct query *ctl,int number,int *lenp)
-/* request headers of nth message */
-{
-    int ok;
-    char buf[POPBUFSIZE+1];
-
-    gen_send(sock, "TOP %d 0", number);
-    if ((ok = pop3_ok(sock, buf)) != 0)
-       return(ok);
-
-    *lenp = -1;                /* we got sizes from the LIST response */
-
-    return(PS_SUCCESS);
-}
-#endif /* UNUSED */
-
 static int pop3_fetch(int sock, struct query *ctl, int number, int *lenp)
 /* request nth message */
 {
@@ -1380,24 +1187,6 @@ 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);