c260e0
From 3f411052825386a95d039435eb139a63859c3c73 Mon Sep 17 00:00:00 2001
c260e0
From: Daniel Stenberg <daniel@haxx.se>
c260e0
Date: Mon, 5 Aug 2013 23:49:53 +0200
c260e0
Subject: [PATCH] FTP: when EPSV gets a 229 but fails to connect, retry with PASV
c260e0
c260e0
This is a regression as this logic used to work. It isn't clear when it
c260e0
broke, but I'm assuming in 7.28.0 when we went all-multi internally.
c260e0
c260e0
This likely never worked with the multi interface. As the failed
c260e0
connection is detected once the multi state has reached DO_MORE, the
c260e0
Curl_do_more() function was now expanded somewhat so that the
c260e0
ftp_do_more() function can request to go "back" to the previous state
c260e0
when it makes another attempt - using PASV.
c260e0
c260e0
Added test case 1233 to verify this fix. It has the little issue that it
c260e0
assumes no service is listening/accepting connections on port 1...
c260e0
c260e0
Reported-by: byte_bucket in the #curl IRC channel
c260e0
c260e0
[upstream commit 7cc00d9a832c42a330888aa5c11a2abad1bd5ac0]
c260e0
c260e0
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c260e0
---
c260e0
 lib/ftp.c              |   64 ++++++++++++++++++++++++++++-------------------
c260e0
 lib/multi.c            |   11 ++++++--
c260e0
 lib/url.c              |   10 ++++---
c260e0
 lib/url.h              |    4 +-
c260e0
 lib/urldata.h          |    2 +-
c260e0
 tests/data/Makefile.am |    2 +-
c260e0
 tests/data/test1233    |   46 ++++++++++++++++++++++++++++++++++
c260e0
 7 files changed, 102 insertions(+), 37 deletions(-)
c260e0
 create mode 100644 tests/data/test1233
c260e0
c260e0
diff --git a/lib/ftp.c b/lib/ftp.c
c260e0
index 469b887..4501116 100644
c260e0
--- a/lib/ftp.c
c260e0
+++ b/lib/ftp.c
c260e0
@@ -136,7 +136,7 @@ static CURLcode ftp_done(struct connectdata *conn,
c260e0
                          CURLcode, bool premature);
c260e0
 static CURLcode ftp_connect(struct connectdata *conn, bool *done);
c260e0
 static CURLcode ftp_disconnect(struct connectdata *conn, bool dead_connection);
c260e0
-static CURLcode ftp_do_more(struct connectdata *conn, bool *completed);
c260e0
+static CURLcode ftp_do_more(struct connectdata *conn, int *completed);
c260e0
 static CURLcode ftp_multi_statemach(struct connectdata *conn, bool *done);
c260e0
 static int ftp_getsock(struct connectdata *conn, curl_socket_t *socks,
c260e0
                        int numsocks);
c260e0
@@ -1794,15 +1794,15 @@ static CURLcode ftp_state_quote(struct connectdata *conn,
c260e0
 static CURLcode ftp_epsv_disable(struct connectdata *conn)
c260e0
 {
c260e0
   CURLcode result = CURLE_OK;
c260e0
-  infof(conn->data, "got positive EPSV response, but can't connect. "
c260e0
-        "Disabling EPSV\n");
c260e0
+  infof(conn->data, "Failed EPSV attempt. Disabling EPSV\n");
c260e0
   /* disable it for next transfer */
c260e0
   conn->bits.ftp_use_epsv = FALSE;
c260e0
   conn->data->state.errorbuf = FALSE; /* allow error message to get
c260e0
                                          rewritten */
c260e0
   PPSENDF(&conn->proto.ftpc.pp, "PASV", NULL);
c260e0
   conn->proto.ftpc.count1++;
c260e0
-  /* remain in the FTP_PASV state */
c260e0
+  /* remain in/go to the FTP_PASV state */
c260e0
+  state(conn, FTP_PASV);
c260e0
   return result;
c260e0
 }
c260e0
 
c260e0
@@ -1931,15 +1931,7 @@ static CURLcode ftp_state_pasv_resp(struct connectdata *conn,
c260e0
   }
c260e0
   else if(ftpc->count1 == 0) {
c260e0
     /* EPSV failed, move on to PASV */
c260e0
-
c260e0
-    /* disable it for next transfer */
c260e0
-    conn->bits.ftp_use_epsv = FALSE;
c260e0
-    infof(data, "disabling EPSV usage\n");
c260e0
-
c260e0
-    PPSENDF(&ftpc->pp, "PASV", NULL);
c260e0
-    ftpc->count1++;
c260e0
-    /* remain in the FTP_PASV state */
c260e0
-    return result;
c260e0
+    return ftp_epsv_disable(conn);
c260e0
   }
c260e0
   else {
c260e0
     failf(data, "Bad PASV/EPSV response: %03d", ftpcode);
c260e0
@@ -2018,14 +2010,17 @@ static CURLcode ftp_state_pasv_resp(struct connectdata *conn,
c260e0
   case CURLPROXY_SOCKS5_HOSTNAME:
c260e0
     result = Curl_SOCKS5(conn->proxyuser, conn->proxypasswd, newhost, newport,
c260e0
                          SECONDARYSOCKET, conn);
c260e0
+    connected = TRUE;
c260e0
     break;
c260e0
   case CURLPROXY_SOCKS4:
c260e0
     result = Curl_SOCKS4(conn->proxyuser, newhost, newport,
c260e0
                          SECONDARYSOCKET, conn, FALSE);
c260e0
+    connected = TRUE;
c260e0
     break;
c260e0
   case CURLPROXY_SOCKS4A:
c260e0
     result = Curl_SOCKS4(conn->proxyuser, newhost, newport,
c260e0
                          SECONDARYSOCKET, conn, TRUE);
c260e0
+    connected = TRUE;
c260e0
     break;
c260e0
   case CURLPROXY_HTTP:
c260e0
   case CURLPROXY_HTTP_1_0:
c260e0
@@ -2077,8 +2072,7 @@ static CURLcode ftp_state_pasv_resp(struct connectdata *conn,
c260e0
     }
c260e0
   }
c260e0
 
c260e0
-  conn->bits.tcpconnect[SECONDARYSOCKET] = TRUE;
c260e0
-
c260e0
+  conn->bits.tcpconnect[SECONDARYSOCKET] = connected;
c260e0
   conn->bits.do_more = TRUE;
c260e0
   state(conn, FTP_STOP); /* this phase is completed */
c260e0
 
c260e0
@@ -3664,20 +3658,23 @@ static CURLcode ftp_range(struct connectdata *conn)
c260e0
  *
c260e0
  * This function shall be called when the second FTP (data) connection is
c260e0
  * connected.
c260e0
+ *
c260e0
+ * 'complete' can return 0 for incomplete, 1 for done and -1 for go back
c260e0
+ * (which basically is only for when PASV is being sent to retry a failed
c260e0
+ * EPSV).
c260e0
  */
c260e0
 
c260e0
-static CURLcode ftp_do_more(struct connectdata *conn, bool *complete)
c260e0
+static CURLcode ftp_do_more(struct connectdata *conn, int *completep)
c260e0
 {
c260e0
   struct SessionHandle *data=conn->data;
c260e0
   struct ftp_conn *ftpc = &conn->proto.ftpc;
c260e0
   CURLcode result = CURLE_OK;
c260e0
   bool connected = FALSE;
c260e0
+  bool complete = FALSE;
c260e0
 
c260e0
   /* the ftp struct is inited in ftp_connect() */
c260e0
   struct FTP *ftp = data->state.proto.ftp;
c260e0
 
c260e0
-  *complete = FALSE;
c260e0
-
c260e0
   /* if the second connection isn't done yet, wait for it */
c260e0
   if(!conn->bits.tcpconnect[SECONDARYSOCKET]) {
c260e0
     if(conn->tunnel_state[SECONDARYSOCKET] == TUNNEL_CONNECT) {
c260e0
@@ -3694,14 +3691,22 @@ static CURLcode ftp_do_more(struct connectdata *conn, bool *complete)
c260e0
     if(connected) {
c260e0
       DEBUGF(infof(data, "DO-MORE connected phase starts\n"));
c260e0
     }
c260e0
-    else
c260e0
+    else {
c260e0
+      if(result && (ftpc->count1 == 0)) {
c260e0
+        *completep = -1; /* go back to DOING please */
c260e0
+        /* this is a EPSV connect failing, try PASV instead */
c260e0
+        return ftp_epsv_disable(conn);
c260e0
+      }
c260e0
       return result;
c260e0
+    }
c260e0
   }
c260e0
 
c260e0
   if(ftpc->state) {
c260e0
     /* already in a state so skip the intial commands.
c260e0
        They are only done to kickstart the do_more state */
c260e0
-    result = ftp_multi_statemach(conn, complete);
c260e0
+    result = ftp_multi_statemach(conn, &complete);
c260e0
+
c260e0
+    *completep = (int)complete;
c260e0
 
c260e0
     /* if we got an error or if we don't wait for a data connection return
c260e0
        immediately */
c260e0
@@ -3712,7 +3717,7 @@ static CURLcode ftp_do_more(struct connectdata *conn, bool *complete)
c260e0
       /* if we reach the end of the FTP state machine here, *complete will be
c260e0
          TRUE but so is ftpc->wait_data_conn, which says we need to wait for
c260e0
          the data connection and therefore we're not actually complete */
c260e0
-      *complete = FALSE;
c260e0
+      *completep = 0;
c260e0
   }
c260e0
 
c260e0
   if(ftp->transfer <= FTPTRANSFER_INFO) {
c260e0
@@ -3735,6 +3740,9 @@ static CURLcode ftp_do_more(struct connectdata *conn, bool *complete)
c260e0
 
c260e0
         if(result)
c260e0
           return result;
c260e0
+
c260e0
+        *completep = 1; /* this state is now complete when the server has
c260e0
+                           connected back to us */
c260e0
       }
c260e0
     }
c260e0
     else if(data->set.upload) {
c260e0
@@ -3742,7 +3750,8 @@ static CURLcode ftp_do_more(struct connectdata *conn, bool *complete)
c260e0
       if(result)
c260e0
         return result;
c260e0
 
c260e0
-      result = ftp_multi_statemach(conn, complete);
c260e0
+      result = ftp_multi_statemach(conn, &complete);
c260e0
+      *completep = (int)complete;
c260e0
     }
c260e0
     else {
c260e0
       /* download */
c260e0
@@ -3770,7 +3779,8 @@ static CURLcode ftp_do_more(struct connectdata *conn, bool *complete)
c260e0
           return result;
c260e0
       }
c260e0
 
c260e0
-      result = ftp_multi_statemach(conn, complete);
c260e0
+      result = ftp_multi_statemach(conn, &complete);
c260e0
+      *completep = (int)complete;
c260e0
     }
c260e0
     return result;
c260e0
   }
c260e0
@@ -3782,7 +3792,7 @@ static CURLcode ftp_do_more(struct connectdata *conn, bool *complete)
c260e0
 
c260e0
   if(!ftpc->wait_data_conn) {
c260e0
     /* no waiting for the data connection so this is now complete */
c260e0
-    *complete = TRUE;
c260e0
+    *completep = 1;
c260e0
     DEBUGF(infof(data, "DO-MORE phase ends with %d\n", (int)result));
c260e0
   }
c260e0
 
c260e0
@@ -3825,7 +3835,9 @@ CURLcode ftp_perform(struct connectdata *conn,
c260e0
   /* run the state-machine */
c260e0
   result = ftp_multi_statemach(conn, dophase_done);
c260e0
 
c260e0
-  *connected = conn->bits.tcpconnect[FIRSTSOCKET];
c260e0
+  *connected = conn->bits.tcpconnect[SECONDARYSOCKET];
c260e0
+
c260e0
+  infof(conn->data, "ftp_perform ends with SECONDARY: %d\n", *connected);
c260e0
 
c260e0
   if(*dophase_done)
c260e0
     DEBUGF(infof(conn->data, "DO phase is complete1\n"));
c260e0
@@ -4445,7 +4457,7 @@ static CURLcode ftp_dophase_done(struct connectdata *conn,
c260e0
   struct ftp_conn *ftpc = &conn->proto.ftpc;
c260e0
 
c260e0
   if(connected) {
c260e0
-    bool completed;
c260e0
+    int completed;
c260e0
     CURLcode result = ftp_do_more(conn, &completed);
c260e0
 
c260e0
     if(result) {
c260e0
diff --git a/lib/multi.c b/lib/multi.c
c260e0
index 706df23..9a8e68e 100644
c260e0
--- a/lib/multi.c
c260e0
+++ b/lib/multi.c
c260e0
@@ -906,6 +906,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
c260e0
   struct SingleRequest *k;
c260e0
   struct SessionHandle *data;
c260e0
   long timeout_ms;
c260e0
+  int control;
c260e0
 
c260e0
   if(!GOOD_EASY_HANDLE(easy->easy_handle))
c260e0
     return CURLM_BAD_EASY_HANDLE;
c260e0
@@ -1323,13 +1324,17 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
c260e0
       /*
c260e0
        * When we are connected, DO MORE and then go DO_DONE
c260e0
        */
c260e0
-      easy->result = Curl_do_more(easy->easy_conn, &dophase_done);
c260e0
+      easy->result = Curl_do_more(easy->easy_conn, &control);
c260e0
 
c260e0
       /* No need to remove this handle from the send pipeline here since that
c260e0
          is done in Curl_done() */
c260e0
       if(CURLE_OK == easy->result) {
c260e0
-        if(dophase_done) {
c260e0
-          multistate(easy, CURLM_STATE_DO_DONE);
c260e0
+        if(control) {
c260e0
+          /* if positive, advance to DO_DONE
c260e0
+             if negative, go back to DOING */
c260e0
+          multistate(easy, control==1?
c260e0
+                     CURLM_STATE_DO_DONE:
c260e0
+                     CURLM_STATE_DOING);
c260e0
           result = CURLM_CALL_MULTI_PERFORM;
c260e0
         }
c260e0
         else
c260e0
diff --git a/lib/url.c b/lib/url.c
c260e0
index b269027..52f7e27 100644
c260e0
--- a/lib/url.c
c260e0
+++ b/lib/url.c
c260e0
@@ -5394,18 +5394,20 @@ CURLcode Curl_do(struct connectdata **connp, bool *done)
c260e0
  *
c260e0
  * TODO: A future libcurl should be able to work away this state.
c260e0
  *
c260e0
+ * 'complete' can return 0 for incomplete, 1 for done and -1 for go back to
c260e0
+ * DOING state there's more work to do!
c260e0
  */
c260e0
 
c260e0
-CURLcode Curl_do_more(struct connectdata *conn, bool *completed)
c260e0
+CURLcode Curl_do_more(struct connectdata *conn, int *complete)
c260e0
 {
c260e0
   CURLcode result=CURLE_OK;
c260e0
 
c260e0
-  *completed = FALSE;
c260e0
+  *complete = 0;
c260e0
 
c260e0
   if(conn->handler->do_more)
c260e0
-    result = conn->handler->do_more(conn, completed);
c260e0
+    result = conn->handler->do_more(conn, complete);
c260e0
 
c260e0
-  if(!result && *completed)
c260e0
+  if(!result && (*complete == 1))
c260e0
     /* do_complete must be called after the protocol-specific DO function */
c260e0
     do_complete(conn);
c260e0
 
c260e0
diff --git a/lib/url.h b/lib/url.h
c260e0
index a026e90..c0d9c38 100644
c260e0
--- a/lib/url.h
c260e0
+++ b/lib/url.h
c260e0
@@ -7,7 +7,7 @@
c260e0
  *                            | (__| |_| |  _ <| |___
c260e0
  *                             \___|\___/|_| \_\_____|
c260e0
  *
c260e0
- * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
c260e0
+ * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
c260e0
  *
c260e0
  * This software is licensed as described in the file COPYING, which
c260e0
  * you should have received as part of this distribution. The terms
c260e0
@@ -37,7 +37,7 @@ CURLcode Curl_close(struct SessionHandle *data); /* opposite of curl_open() */
c260e0
 CURLcode Curl_connect(struct SessionHandle *, struct connectdata **,
c260e0
                       bool *async, bool *protocol_connect);
c260e0
 CURLcode Curl_do(struct connectdata **, bool *done);
c260e0
-CURLcode Curl_do_more(struct connectdata *, bool *completed);
c260e0
+CURLcode Curl_do_more(struct connectdata *, int *completed);
c260e0
 CURLcode Curl_done(struct connectdata **, CURLcode, bool premature);
c260e0
 CURLcode Curl_disconnect(struct connectdata *, bool dead_connection);
c260e0
 CURLcode Curl_protocol_connect(struct connectdata *conn, bool *done);
c260e0
diff --git a/lib/urldata.h b/lib/urldata.h
c260e0
index 7a275da..2be467b 100644
c260e0
--- a/lib/urldata.h
c260e0
+++ b/lib/urldata.h
c260e0
@@ -550,7 +550,7 @@ struct Curl_async {
c260e0
 /* These function pointer types are here only to allow easier typecasting
c260e0
    within the source when we need to cast between data pointers (such as NULL)
c260e0
    and function pointers. */
c260e0
-typedef CURLcode (*Curl_do_more_func)(struct connectdata *, bool *);
c260e0
+typedef CURLcode (*Curl_do_more_func)(struct connectdata *, int *);
c260e0
 typedef CURLcode (*Curl_done_func)(struct connectdata *, CURLcode, bool);
c260e0
 
c260e0
 
c260e0
diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am
c260e0
index 3e8dae0..3f6a047 100644
c260e0
--- a/tests/data/Makefile.am
c260e0
+++ b/tests/data/Makefile.am
c260e0
@@ -78,7 +78,7 @@ test1118 test1119 test1120 test1121 test1122 test1123 test1124 test1125	\
c260e0
 test1126 test1127 test1128 test1129 test1130 test1131 test1132 test1133 \
c260e0
 test1200 test1201 test1202 test1203 test1204 test1205 test1206 test1207 \
c260e0
 test1208 test1209 test1210 test1211 test1216 test1218 \
c260e0
-test1220 test1221 test1222 test1223 \
c260e0
+test1220 test1221 test1222 test1223 test1233 \
c260e0
 test1300 test1301 test1302 test1303 test1304 test1305	\
c260e0
 test1306 test1307 test1308 test1309 test1310 test1311 test1312 test1313 \
c260e0
 test1314 test1315 test1316 test1317 test1318 test1319 test1320 test1321 \
c260e0
diff --git a/tests/data/test1233 b/tests/data/test1233
c260e0
new file mode 100644
c260e0
index 0000000..caf0527
c260e0
--- /dev/null
c260e0
+++ b/tests/data/test1233
c260e0
@@ -0,0 +1,46 @@
c260e0
+<testcase>
c260e0
+<info>
c260e0
+<keywords>
c260e0
+FTP
c260e0
+</keywords>
c260e0
+</info>
c260e0
+
c260e0
+# Server-side
c260e0
+<reply>
c260e0
+<servercmd>
c260e0
+# Assuming there's nothing listening on port 1
c260e0
+REPLY EPSV 229 Entering Passiv Mode (|||1|)
c260e0
+</servercmd>
c260e0
+<data>
c260e0
+here are some bytes
c260e0
+</data>
c260e0
+</reply>
c260e0
+
c260e0
+# Client-side
c260e0
+<client>
c260e0
+<server>
c260e0
+ftp
c260e0
+</server>
c260e0
+ <name>
c260e0
+FTP failing to connect to EPSV port, switching to PASV
c260e0
+ </name>
c260e0
+ <command>
c260e0
+ftp://%HOSTIP:%FTPPORT/1233
c260e0
+</command>
c260e0
+</client>
c260e0
+
c260e0
+# Verify data after the test has been "shot"
c260e0
+<verify>
c260e0
+<protocol>
c260e0
+USER anonymous
c260e0
+PASS ftp@example.com
c260e0
+PWD
c260e0
+EPSV
c260e0
+PASV
c260e0
+TYPE I
c260e0
+SIZE 1233
c260e0
+RETR 1233
c260e0
+QUIT
c260e0
+</protocol>
c260e0
+</verify>
c260e0
+</testcase>
c260e0
-- 
c260e0
1.7.1
c260e0