]> granicus.if.org Git - curl/commitdiff
FTP: access files in root dir correctly
authorDaniel Stenberg <daniel@haxx.se>
Fri, 12 Apr 2013 13:29:28 +0000 (15:29 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Fri, 12 Apr 2013 20:43:13 +0000 (22:43 +0200)
Accessing a file with an absolute path in the root dir but with no
directory specified was not handled correctly. This fix comes with four
new test cases that verify it.

Bug: http://curl.haxx.se/mail/lib-2013-04/0142.html
Reported by: Sam Deane

lib/ftp.c
tests/data/Makefile.am
tests/data/test106
tests/data/test1224 [new file with mode: 0644]
tests/data/test1225 [new file with mode: 0644]
tests/data/test1226 [new file with mode: 0644]
tests/data/test1227 [new file with mode: 0644]

index dce23de751fbea564e4c0ff8ac719c70d73a85d3..5308b2fa217209744f56f0398e2b6489d01e9623 100644 (file)
--- a/lib/ftp.c
+++ b/lib/ftp.c
@@ -4316,13 +4316,17 @@ CURLcode ftp_parse_url_path(struct connectdata *conn)
     }
     slash_pos=strrchr(cur_pos, '/');
     if(slash_pos || !*cur_pos) {
+      size_t dirlen = slash_pos-cur_pos;
+
       ftpc->dirs = calloc(1, sizeof(ftpc->dirs[0]));
       if(!ftpc->dirs)
         return CURLE_OUT_OF_MEMORY;
 
+      if(!dirlen)
+        dirlen++;
+
       ftpc->dirs[0] = curl_easy_unescape(conn->data, slash_pos ? cur_pos : "/",
-                                         slash_pos ?
-                                         curlx_sztosi(slash_pos-cur_pos) : 1,
+                                         slash_pos ? curlx_sztosi(dirlen) : 1,
                                          NULL);
       if(!ftpc->dirs[0]) {
         freedirs(ftpc);
@@ -4377,6 +4381,15 @@ CURLcode ftp_parse_url_path(struct connectdata *conn)
         }
         else {
           cur_pos = slash_pos + 1; /* jump to the rest of the string */
+          if(!ftpc->dirdepth) {
+            /* path starts with a slash, add that as a directory */
+            ftpc->dirs[ftpc->dirdepth] = strdup("/");
+            if(!ftpc->dirs[ftpc->dirdepth++]) { /* run out of memory ... */
+              failf(data, "no memory");
+              freedirs(ftpc);
+              return CURLE_OUT_OF_MEMORY;
+            }
+          }
           continue;
         }
 
index d96ac02c77108a443fa6dc616106f2aee545657c..c3857d2f5eb233f71efece6fcaa455f74b23c2f1 100644 (file)
@@ -90,7 +90,7 @@ test1128 test1129 test1130 test1131 test1132 test1133 \
 test1200 test1201 test1202 test1203 test1204 test1205 test1206 test1207 \
 test1208 test1209 test1210 test1211 test1212 test1213 test1214 test1215 \
 test1216 test1217 test1218 \
-test1220 test1221 test1222 test1223 \
+test1220 test1221 test1222 test1223 test1224 test1225 test1226 test1227 \
 \
 test1300 test1301 test1302 test1303 test1304 test1305 test1306 test1307 \
 test1308 test1309 test1310 test1311 test1312 test1313 test1314 test1315 \
index d1b9a81b90d7fcd2457ddfd3ca0e69d83e9876ed..56eca64205e4269a681aa7508acef25f21d49dd3 100644 (file)
@@ -39,7 +39,8 @@ FTP GET with type=A style ASCII URL using %20 codes
 USER anonymous\r
 PASS ftp@example.com\r
 PWD\r
-CWD /path with  spaces\r
+CWD /\r
+CWD path with  spaces\r
 CWD and things2\r
 EPSV\r
 TYPE A\r
diff --git a/tests/data/test1224 b/tests/data/test1224
new file mode 100644 (file)
index 0000000..315936b
--- /dev/null
@@ -0,0 +1,49 @@
+<testcase>
+<info>
+<keywords>
+FTP
+PASV
+RETR
+</keywords>
+</info>
+# Server-side
+<reply>
+<data>
+data
+    to
+      see
+that FTP
+works
+  so does it?
+</data>
+</reply>
+
+# Client-side
+<client>
+<server>
+ftp
+</server>
+ <name>
+FTP fetch a file from the root directory
+ </name>
+ <command>
+ftp://%HOSTIP:%FTPPORT//1224
+</command>
+
+</client>
+
+# Verify data after the test has been "shot"
+<verify>
+<protocol>
+USER anonymous\r
+PASS ftp@example.com\r
+PWD\r
+CWD /\r
+EPSV\r
+TYPE I\r
+SIZE 1224\r
+RETR 1224\r
+QUIT\r
+</protocol>
+</verify>
+</testcase>
diff --git a/tests/data/test1225 b/tests/data/test1225
new file mode 100644 (file)
index 0000000..2b2519c
--- /dev/null
@@ -0,0 +1,57 @@
+<testcase>
+<info>
+<keywords>
+FTP
+PASV
+RETR
+</keywords>
+</info>
+# Server-side
+<reply>
+<data>
+data
+    to
+      see
+that FTP
+works
+  so does it?
+</data>
+</reply>
+
+# Client-side
+<client>
+<server>
+ftp
+</server>
+ <name>
+FTP fetch two files using absolute paths
+ </name>
+ <command>
+ftp://%HOSTIP:%FTPPORT//foo/1225 ftp://%HOSTIP:%FTPPORT//foo/bar/1225
+</command>
+
+</client>
+
+# Verify data after the test has been "shot"
+<verify>
+<protocol>
+USER anonymous\r
+PASS ftp@example.com\r
+PWD\r
+CWD /\r
+CWD foo\r
+EPSV\r
+TYPE I\r
+SIZE 1225\r
+RETR 1225\r
+CWD /\r
+CWD /\r
+CWD foo\r
+CWD bar\r
+EPSV\r
+SIZE 1225\r
+RETR 1225\r
+QUIT\r
+</protocol>
+</verify>
+</testcase>
diff --git a/tests/data/test1226 b/tests/data/test1226
new file mode 100644 (file)
index 0000000..6381b12
--- /dev/null
@@ -0,0 +1,49 @@
+<testcase>
+<info>
+<keywords>
+FTP
+PASV
+RETR
+</keywords>
+</info>
+# Server-side
+<reply>
+<data>
+data
+    to
+      see
+that FTP
+works
+  so does it?
+</data>
+</reply>
+
+# Client-side
+<client>
+<server>
+ftp
+</server>
+ <name>
+FTP fetch a file from the root directory with singlecwd
+ </name>
+ <command>
+ftp://%HOSTIP:%FTPPORT//1226 --ftp-method singlecwd
+</command>
+
+</client>
+
+# Verify data after the test has been "shot"
+<verify>
+<protocol>
+USER anonymous\r
+PASS ftp@example.com\r
+PWD\r
+CWD /\r
+EPSV\r
+TYPE I\r
+SIZE 1226\r
+RETR 1226\r
+QUIT\r
+</protocol>
+</verify>
+</testcase>
diff --git a/tests/data/test1227 b/tests/data/test1227
new file mode 100644 (file)
index 0000000..46b28d5
--- /dev/null
@@ -0,0 +1,48 @@
+<testcase>
+<info>
+<keywords>
+FTP
+PASV
+RETR
+</keywords>
+</info>
+# Server-side
+<reply>
+<data>
+data
+    to
+      see
+that FTP
+works
+  so does it?
+</data>
+</reply>
+
+# Client-side
+<client>
+<server>
+ftp
+</server>
+ <name>
+FTP fetch a file from the root directory with nocwd
+ </name>
+ <command>
+ftp://%HOSTIP:%FTPPORT//1227 --ftp-method nocwd
+</command>
+
+</client>
+
+# Verify data after the test has been "shot"
+<verify>
+<protocol>
+USER anonymous\r
+PASS ftp@example.com\r
+PWD\r
+EPSV\r
+TYPE I\r
+SIZE /1227\r
+RETR /1227\r
+QUIT\r
+</protocol>
+</verify>
+</testcase>