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