Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | #include "urldata.h" |
26 | | |
27 | | #ifndef CURL_DISABLE_FTP |
28 | | |
29 | | #ifdef HAVE_NETINET_IN_H |
30 | | #include <netinet/in.h> |
31 | | #endif |
32 | | #ifdef HAVE_ARPA_INET_H |
33 | | #include <arpa/inet.h> |
34 | | #endif |
35 | | #ifdef HAVE_NETDB_H |
36 | | #include <netdb.h> |
37 | | #endif |
38 | | #ifdef __VMS |
39 | | #include <in.h> |
40 | | #include <inet.h> |
41 | | #endif |
42 | | |
43 | | #include "sendf.h" |
44 | | #include "curl_addrinfo.h" |
45 | | #include "curl_trc.h" |
46 | | #include "if2ip.h" |
47 | | #include "hostip.h" |
48 | | #include "progress.h" |
49 | | #include "transfer.h" |
50 | | #include "escape.h" |
51 | | #include "ftp.h" |
52 | | #include "ftp-int.h" |
53 | | #include "ftplistparser.h" |
54 | | #include "curl_range.h" |
55 | | #include "strcase.h" |
56 | | #include "vtls/vtls.h" |
57 | | #include "cfilters.h" |
58 | | #include "cf-socket.h" |
59 | | #include "connect.h" |
60 | | #include "curlx/inet_ntop.h" |
61 | | #include "curlx/inet_pton.h" |
62 | | #include "select.h" |
63 | | #include "parsedate.h" /* for the week day and month names */ |
64 | | #include "sockaddr.h" /* required for Curl_sockaddr_storage */ |
65 | | #include "multiif.h" |
66 | | #include "url.h" |
67 | | #include "http_proxy.h" |
68 | | #include "curlx/strdup.h" |
69 | | #include "curlx/strerr.h" |
70 | | #include "curlx/strparse.h" |
71 | | #include "curl_ctype.h" |
72 | | |
73 | | #ifndef NI_MAXHOST |
74 | | #define NI_MAXHOST 1025 |
75 | | #endif |
76 | | #ifndef INET_ADDRSTRLEN |
77 | | #define INET_ADDRSTRLEN 16 |
78 | | #endif |
79 | | |
80 | | /* macro to check for a three-digit ftp status code at the start of the |
81 | | given string */ |
82 | | #define STATUSCODE(line) \ |
83 | 73.2k | (ISDIGIT((line)[0]) && ISDIGIT((line)[1]) && ISDIGIT((line)[2])) |
84 | | |
85 | | /* macro to check for the last line in an FTP server response */ |
86 | 550k | #define LASTLINE(line) (STATUSCODE(line) && (' ' == (line)[3])) |
87 | | |
88 | | #ifdef CURLVERBOSE |
89 | | /* for tracing purposes */ |
90 | | static const char * const ftp_state_names[] = { |
91 | | "STOP", |
92 | | "WAIT220", |
93 | | "AUTH", |
94 | | "USER", |
95 | | "PASS", |
96 | | "ACCT", |
97 | | "PBSZ", |
98 | | "PROT", |
99 | | "CCC", |
100 | | "PWD", |
101 | | "SYST", |
102 | | "NAMEFMT", |
103 | | "QUOTE", |
104 | | "RETR_PREQUOTE", |
105 | | "STOR_PREQUOTE", |
106 | | "LIST_PREQUOTE", |
107 | | "POSTQUOTE", |
108 | | "CWD", |
109 | | "MKD", |
110 | | "MDTM", |
111 | | "TYPE", |
112 | | "LIST_TYPE", |
113 | | "RETR_LIST_TYPE", |
114 | | "RETR_TYPE", |
115 | | "STOR_TYPE", |
116 | | "SIZE", |
117 | | "RETR_SIZE", |
118 | | "STOR_SIZE", |
119 | | "REST", |
120 | | "RETR_REST", |
121 | | "PORT", |
122 | | "PRET", |
123 | | "PASV", |
124 | | "LIST", |
125 | | "RETR", |
126 | | "STOR", |
127 | | "QUIT" |
128 | | }; |
129 | | #define FTP_CSTATE(ftpc) ((ftpc) ? ftp_state_names[(ftpc)->state] : "???") |
130 | | |
131 | | #endif /* CURLVERBOSE */ |
132 | | |
133 | | /* This is the ONLY way to change FTP state! */ |
134 | | static void ftp_state_low(struct Curl_easy *data, |
135 | | struct ftp_conn *ftpc, |
136 | | ftpstate newstate |
137 | | #ifdef DEBUGBUILD |
138 | | , int lineno |
139 | | #endif |
140 | | ) |
141 | 16.7k | { |
142 | 16.7k | if(ftpc->state != newstate) { |
143 | 15.9k | #ifdef DEBUGBUILD |
144 | 15.9k | NOVERBOSE((void)lineno); |
145 | 15.9k | CURL_TRC_FTP(data, "[%s] -> [%s] (line %d)", FTP_CSTATE(ftpc), |
146 | 15.9k | ftp_state_names[newstate], lineno); |
147 | | #else |
148 | | CURL_TRC_FTP(data, "[%s] -> [%s]", FTP_CSTATE(ftpc), |
149 | | ftp_state_names[newstate]); |
150 | | #endif |
151 | 15.9k | } |
152 | 16.7k | ftpc->state = newstate; |
153 | 16.7k | } |
154 | | |
155 | | /* Local API functions */ |
156 | | #ifndef DEBUGBUILD |
157 | | #define ftp_state(x, y, z) ftp_state_low(x, y, z) |
158 | | #else /* !DEBUGBUILD */ |
159 | 16.7k | #define ftp_state(x, y, z) ftp_state_low(x, y, z, __LINE__) |
160 | | #endif /* DEBUGBUILD */ |
161 | | |
162 | | static CURLcode ftp_state_mdtm(struct Curl_easy *data, |
163 | | struct ftp_conn *ftpc, |
164 | | struct FTP *ftp); |
165 | | static CURLcode ftp_state_quote(struct Curl_easy *data, |
166 | | struct ftp_conn *ftpc, |
167 | | struct FTP *ftp, |
168 | | bool init, ftpstate instate); |
169 | | static CURLcode ftp_nb_type(struct Curl_easy *data, |
170 | | struct ftp_conn *ftpc, |
171 | | struct FTP *ftp, |
172 | | bool ascii, ftpstate newstate); |
173 | | static CURLcode getftpresponse(struct Curl_easy *data, size_t *nreadp, |
174 | | int *ftpcodep); |
175 | | |
176 | | static void freedirs(struct ftp_conn *ftpc) |
177 | 5.80k | { |
178 | 5.80k | curlx_safefree(ftpc->dirs); |
179 | 5.80k | ftpc->dirdepth = 0; |
180 | 5.80k | curlx_safefree(ftpc->rawpath); |
181 | 5.80k | ftpc->file = NULL; |
182 | 5.80k | } |
183 | | |
184 | | static size_t numof_slashes(const char *str) |
185 | 1.89k | { |
186 | 1.89k | const char *slashPos; |
187 | 1.89k | size_t num = 0; |
188 | 2.93k | do { |
189 | 2.93k | slashPos = strchr(str, '/'); |
190 | 2.93k | if(slashPos) { |
191 | 1.03k | ++num; |
192 | 1.03k | str = slashPos + 1; |
193 | 1.03k | } |
194 | 2.93k | } while(slashPos); |
195 | 1.89k | return num; |
196 | 1.89k | } |
197 | | |
198 | 1.89k | #define FTP_MAX_DIR_DEPTH 1000 |
199 | | |
200 | | /*********************************************************************** |
201 | | * |
202 | | * ftp_parse_url_path() |
203 | | * |
204 | | * Parse the URL path into separate path components. |
205 | | * |
206 | | */ |
207 | | static CURLcode ftp_parse_url_path(struct Curl_easy *data, |
208 | | struct ftp_conn *ftpc, |
209 | | struct FTP *ftp) |
210 | 1.90k | { |
211 | 1.90k | const char *slashPos = NULL; |
212 | 1.90k | const char *fileName = NULL; |
213 | 1.90k | CURLcode result = CURLE_OK; |
214 | 1.90k | const char *rawPath = NULL; /* URL-decoded "raw" path */ |
215 | 1.90k | size_t pathLen = 0; |
216 | | |
217 | 1.90k | ftpc->ctl_valid = FALSE; |
218 | 1.90k | ftpc->cwdfail = FALSE; |
219 | | |
220 | 1.90k | if(ftpc->rawpath) |
221 | 7 | freedirs(ftpc); |
222 | | /* URL-decode ftp path before further evaluation */ |
223 | 1.90k | result = Curl_urldecode(ftp->path, 0, &ftpc->rawpath, &pathLen, REJECT_CTRL); |
224 | 1.90k | if(result) { |
225 | 2 | failf(data, "path contains control characters"); |
226 | 2 | return result; |
227 | 2 | } |
228 | 1.90k | rawPath = ftpc->rawpath; |
229 | | |
230 | 1.90k | switch(data->set.ftp_filemethod) { |
231 | 5 | case FTPFILE_NOCWD: /* fastest, but less standard-compliant */ |
232 | | |
233 | 5 | if((pathLen > 0) && (rawPath[pathLen - 1] != '/')) |
234 | 1 | fileName = rawPath; /* this is a full file path */ |
235 | | /* |
236 | | else: ftpc->file is not used anywhere other than for operations on |
237 | | a file. In other words, never for directory operations, |
238 | | so we can safely leave filename as NULL here and use it as a |
239 | | argument in dir/file decisions. |
240 | | */ |
241 | 5 | break; |
242 | | |
243 | 3 | case FTPFILE_SINGLECWD: |
244 | 3 | slashPos = strrchr(rawPath, '/'); |
245 | 3 | if(slashPos) { |
246 | | /* get path before last slash, except for / */ |
247 | 2 | size_t dirlen = slashPos - rawPath; |
248 | 2 | if(dirlen == 0) |
249 | 1 | dirlen = 1; |
250 | | |
251 | 2 | ftpc->dirs = curlx_calloc(1, sizeof(ftpc->dirs[0])); |
252 | 2 | if(!ftpc->dirs) |
253 | 0 | return CURLE_OUT_OF_MEMORY; |
254 | | |
255 | 2 | ftpc->dirs[0].start = 0; |
256 | 2 | ftpc->dirs[0].len = (int)dirlen; |
257 | 2 | ftpc->dirdepth = 1; /* we consider it to be a single directory */ |
258 | 2 | fileName = slashPos + 1; /* rest is filename */ |
259 | 2 | } |
260 | 1 | else |
261 | 1 | fileName = rawPath; /* filename only (or empty) */ |
262 | 3 | break; |
263 | | |
264 | 3 | default: /* allow pretty much anything */ |
265 | 1.89k | case FTPFILE_MULTICWD: { |
266 | | /* current position: begin of next path component */ |
267 | 1.89k | const char *curPos = rawPath; |
268 | | |
269 | | /* number of entries to allocate for the 'dirs' array */ |
270 | 1.89k | size_t dirAlloc = numof_slashes(rawPath); |
271 | | |
272 | 1.89k | if(dirAlloc >= FTP_MAX_DIR_DEPTH) |
273 | | /* suspiciously deep directory hierarchy */ |
274 | 0 | return CURLE_URL_MALFORMAT; |
275 | | |
276 | 1.89k | if(dirAlloc) { |
277 | 202 | ftpc->dirs = curlx_calloc(dirAlloc, sizeof(ftpc->dirs[0])); |
278 | 202 | if(!ftpc->dirs) |
279 | 0 | return CURLE_OUT_OF_MEMORY; |
280 | | |
281 | | /* parse the URL path into separate path components */ |
282 | 1.23k | while(dirAlloc--) { |
283 | 1.03k | const char *spos = strchr(curPos, '/'); |
284 | 1.03k | size_t clen = spos - curPos; |
285 | | |
286 | | /* path starts with a slash: add that as a directory */ |
287 | 1.03k | if(!clen && (ftpc->dirdepth == 0)) |
288 | 33 | ++clen; |
289 | | |
290 | | /* we skip empty path components, like "x//y" since the FTP command |
291 | | CWD requires a parameter and a non-existent parameter a) does not |
292 | | work on many servers and b) has no effect on the others. */ |
293 | 1.03k | if(clen) { |
294 | 367 | ftpc->dirs[ftpc->dirdepth].start = (int)(curPos - rawPath); |
295 | 367 | ftpc->dirs[ftpc->dirdepth].len = (int)clen; |
296 | 367 | ftpc->dirdepth++; |
297 | 367 | } |
298 | 1.03k | curPos = spos + 1; |
299 | 1.03k | } |
300 | 202 | } |
301 | 1.89k | fileName = curPos; /* the rest is the filename (or empty) */ |
302 | 1.89k | } |
303 | 0 | break; |
304 | 1.90k | } /* switch */ |
305 | | |
306 | 1.90k | if(fileName && *fileName) |
307 | 974 | ftpc->file = fileName; |
308 | 929 | else |
309 | 929 | ftpc->file = NULL; /* instead of point to a zero byte, |
310 | | we make it a NULL pointer */ |
311 | | |
312 | 1.90k | if(data->state.upload && !ftpc->file && (ftp->transfer == PPTRANSFER_BODY)) { |
313 | | /* We need a filename when uploading. Return error! */ |
314 | 2 | failf(data, "Uploading to a URL without a filename"); |
315 | 2 | return CURLE_URL_MALFORMAT; |
316 | 2 | } |
317 | | |
318 | 1.90k | ftpc->cwddone = FALSE; /* default to not done */ |
319 | | |
320 | 1.90k | if((data->set.ftp_filemethod == FTPFILE_NOCWD) && (rawPath[0] == '/')) |
321 | 1 | ftpc->cwddone = TRUE; /* skip CWD for absolute paths */ |
322 | 1.90k | else { /* newly created FTP connections are already in entry path */ |
323 | 1.90k | const char *oldPath = data->conn->bits.reuse ? ftpc->prevpath : ""; |
324 | 1.90k | if(oldPath) { |
325 | 1.90k | size_t n = pathLen; |
326 | 1.90k | if(data->set.ftp_filemethod == FTPFILE_NOCWD) |
327 | 4 | n = 0; /* CWD to entry for relative paths */ |
328 | 1.89k | else |
329 | 1.89k | n -= ftpc->file ? strlen(ftpc->file) : 0; |
330 | | |
331 | 1.90k | if((strlen(oldPath) == n) && rawPath && !strncmp(rawPath, oldPath, n)) { |
332 | 1.69k | infof(data, "Request has same path as previous transfer"); |
333 | 1.69k | ftpc->cwddone = TRUE; |
334 | 1.69k | } |
335 | 1.90k | } |
336 | 1.90k | } |
337 | | |
338 | 1.90k | return CURLE_OK; |
339 | 1.90k | } |
340 | | |
341 | | /*********************************************************************** |
342 | | * |
343 | | * ftp_need_type() |
344 | | * |
345 | | * Returns TRUE if we in the current situation should send TYPE |
346 | | */ |
347 | | static int ftp_need_type(struct ftp_conn *ftpc, |
348 | | bool ascii_wanted) |
349 | 37 | { |
350 | 37 | return ftpc->transfertype != (ascii_wanted ? 'A' : 'I'); |
351 | 37 | } |
352 | | |
353 | | static void close_secondarysocket(struct Curl_easy *data, |
354 | | struct ftp_conn *ftpc) |
355 | 2.22k | { |
356 | 2.22k | (void)ftpc; |
357 | 2.22k | CURL_TRC_FTP(data, "[%s] closing DATA connection", FTP_CSTATE(ftpc)); |
358 | 2.22k | Curl_conn_close(data, SECONDARYSOCKET); |
359 | 2.22k | Curl_conn_cf_discard_all(data, data->conn, SECONDARYSOCKET); |
360 | 2.22k | } |
361 | | |
362 | | #ifdef CURL_PREFER_LF_LINEENDS |
363 | | /* |
364 | | * Lineend Conversions |
365 | | * On ASCII transfers, e.g. directory listings, we might get lines |
366 | | * ending in '\r\n' and we prefer '\n'. |
367 | | * We might also get a lonely '\r' which we convert into a '\n'. |
368 | | */ |
369 | | struct ftp_cw_lc_ctx { |
370 | | struct Curl_cwriter super; |
371 | | bool newline_pending; |
372 | | }; |
373 | | |
374 | | static CURLcode ftp_cw_lc_write(struct Curl_easy *data, |
375 | | struct Curl_cwriter *writer, int type, |
376 | | const char *buf, size_t blen) |
377 | 199k | { |
378 | 199k | static const char nl = '\n'; |
379 | 199k | struct ftp_cw_lc_ctx *ctx = writer->ctx; |
380 | 199k | struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN); |
381 | | |
382 | 199k | if(!ftpc) |
383 | 0 | return CURLE_FAILED_INIT; |
384 | | |
385 | 199k | if(!(type & CLIENTWRITE_BODY) || ftpc->transfertype != 'A') |
386 | 198k | return Curl_cwriter_write(data, writer->next, type, buf, blen); |
387 | | |
388 | | /* ASCII mode BODY data, convert lineends */ |
389 | 214k | while(blen) { |
390 | | /* do not pass EOS when writing parts */ |
391 | 213k | int chunk_type = (type & ~CLIENTWRITE_EOS); |
392 | 213k | const char *cp; |
393 | 213k | size_t chunk_len; |
394 | 213k | CURLcode result; |
395 | | |
396 | 213k | if(ctx->newline_pending) { |
397 | 213k | if(buf[0] != '\n') { |
398 | | /* previous chunk ended in '\r' and we do not see a '\n' in this one, |
399 | | * need to write a newline. */ |
400 | 212k | result = Curl_cwriter_write(data, writer->next, chunk_type, &nl, 1); |
401 | 212k | if(result) |
402 | 0 | return result; |
403 | 212k | } |
404 | | /* either we wrote the newline or it is part of the next chunk of bytes |
405 | | * we write. */ |
406 | 213k | ctx->newline_pending = FALSE; |
407 | 213k | } |
408 | | |
409 | 213k | cp = memchr(buf, '\r', blen); |
410 | 213k | if(!cp) |
411 | 566 | break; |
412 | | |
413 | | /* write the bytes before the '\r', excluding the '\r' */ |
414 | 213k | chunk_len = cp - buf; |
415 | 213k | if(chunk_len) { |
416 | 23.8k | result = Curl_cwriter_write(data, writer->next, chunk_type, |
417 | 23.8k | buf, chunk_len); |
418 | 23.8k | if(result) |
419 | 0 | return result; |
420 | 23.8k | } |
421 | | /* skip the '\r', we now have a newline pending */ |
422 | 213k | buf = cp + 1; |
423 | 213k | blen = blen - chunk_len - 1; |
424 | 213k | ctx->newline_pending = TRUE; |
425 | 213k | } |
426 | | |
427 | | /* Any remaining data does not contain a '\r' */ |
428 | 1.22k | if(blen) { |
429 | 566 | DEBUGASSERT(!ctx->newline_pending); |
430 | 566 | return Curl_cwriter_write(data, writer->next, type, buf, blen); |
431 | 566 | } |
432 | 661 | else if(type & CLIENTWRITE_EOS) { |
433 | | /* EndOfStream, if we have a trailing cr, now is the time to write it */ |
434 | 553 | if(ctx->newline_pending) { |
435 | 27 | ctx->newline_pending = FALSE; |
436 | 27 | return Curl_cwriter_write(data, writer->next, type, &nl, 1); |
437 | 27 | } |
438 | | /* Always pass on the EOS type indicator */ |
439 | 526 | return Curl_cwriter_write(data, writer->next, type, buf, 0); |
440 | 553 | } |
441 | 108 | return CURLE_OK; |
442 | 1.22k | } |
443 | | |
444 | | static const struct Curl_cwtype ftp_cw_lc = { |
445 | | "ftp-lineconv", |
446 | | NULL, |
447 | | Curl_cwriter_def_init, |
448 | | ftp_cw_lc_write, |
449 | | Curl_cwriter_def_close, |
450 | | sizeof(struct ftp_cw_lc_ctx) |
451 | | }; |
452 | | |
453 | | #endif /* CURL_PREFER_LF_LINEENDS */ |
454 | | |
455 | | /*********************************************************************** |
456 | | * |
457 | | * ftp_check_ctrl_on_data_wait() |
458 | | * |
459 | | */ |
460 | | static CURLcode ftp_check_ctrl_on_data_wait(struct Curl_easy *data, |
461 | | struct ftp_conn *ftpc) |
462 | 0 | { |
463 | 0 | struct connectdata *conn = data->conn; |
464 | 0 | curl_socket_t ctrl_sock = conn->sock[FIRSTSOCKET]; |
465 | 0 | struct pingpong *pp = &ftpc->pp; |
466 | 0 | size_t nread; |
467 | 0 | int ftpcode; |
468 | 0 | bool response = FALSE; |
469 | | |
470 | | /* First check whether there is a cached response from server */ |
471 | 0 | if(curlx_dyn_len(&pp->recvbuf)) { |
472 | 0 | const char *l = curlx_dyn_ptr(&pp->recvbuf); |
473 | 0 | if(!ISDIGIT(*l) || (*l > '3')) { |
474 | | /* Data connection could not be established, let's return */ |
475 | 0 | infof(data, "There is negative response in cache while serv connect"); |
476 | 0 | (void)getftpresponse(data, &nread, &ftpcode); |
477 | 0 | return CURLE_FTP_ACCEPT_FAILED; |
478 | 0 | } |
479 | 0 | } |
480 | | |
481 | 0 | if(pp->overflow) |
482 | | /* there is pending control data still in the buffer to read */ |
483 | 0 | response = TRUE; |
484 | 0 | else { |
485 | 0 | int socketstate = SOCKET_READABLE(ctrl_sock, 0); |
486 | | /* see if the connection request is already here */ |
487 | 0 | switch(socketstate) { |
488 | 0 | case -1: /* error */ |
489 | | /* let's die here */ |
490 | 0 | failf(data, "Error while waiting for server connect"); |
491 | 0 | return CURLE_FTP_ACCEPT_FAILED; |
492 | 0 | default: |
493 | 0 | if(socketstate & CURL_CSELECT_IN) |
494 | 0 | response = TRUE; |
495 | 0 | break; |
496 | 0 | } |
497 | 0 | } |
498 | | |
499 | 0 | if(response) { |
500 | 0 | infof(data, "Ctrl conn has data while waiting for data conn"); |
501 | 0 | if(pp->overflow > 3) { |
502 | 0 | const char *r = curlx_dyn_ptr(&pp->recvbuf); |
503 | 0 | size_t len = curlx_dyn_len(&pp->recvbuf); |
504 | |
|
505 | 0 | DEBUGASSERT((pp->overflow + pp->nfinal) <= curlx_dyn_len(&pp->recvbuf)); |
506 | | /* move over the most recently handled response line */ |
507 | 0 | r += pp->nfinal; |
508 | 0 | len -= pp->nfinal; |
509 | |
|
510 | 0 | if((len > 3) && LASTLINE(r)) { |
511 | 0 | curl_off_t status; |
512 | 0 | if(!curlx_str_number(&r, &status, 999) && (status == 226)) { |
513 | | /* funny timing situation where we get the final message on the |
514 | | control connection before traffic on the data connection has been |
515 | | noticed. Leave the 226 in there and use this as a trigger to read |
516 | | the data socket. */ |
517 | 0 | infof(data, "Got 226 before data activity"); |
518 | 0 | return CURLE_OK; |
519 | 0 | } |
520 | 0 | } |
521 | 0 | } |
522 | | |
523 | 0 | (void)getftpresponse(data, &nread, &ftpcode); |
524 | |
|
525 | 0 | infof(data, "FTP code: %03d", ftpcode); |
526 | |
|
527 | 0 | if(ftpcode / 100 > 3) |
528 | 0 | return CURLE_FTP_ACCEPT_FAILED; |
529 | | |
530 | 0 | return CURLE_WEIRD_SERVER_REPLY; |
531 | 0 | } |
532 | | |
533 | 0 | return CURLE_OK; |
534 | 0 | } |
535 | | |
536 | | /*********************************************************************** |
537 | | * |
538 | | * ftp_initiate_transfer() |
539 | | * |
540 | | * After connection from server is accepted this function is called to |
541 | | * setup transfer parameters and initiate the data transfer. |
542 | | * |
543 | | */ |
544 | | static CURLcode ftp_initiate_transfer(struct Curl_easy *data, |
545 | | struct ftp_conn *ftpc) |
546 | 954 | { |
547 | 954 | CURLcode result = CURLE_OK; |
548 | 954 | bool connected; |
549 | | |
550 | 954 | CURL_TRC_FTP(data, "ftp_initiate_transfer()"); |
551 | 954 | result = Curl_conn_connect(data, SECONDARYSOCKET, TRUE, &connected); |
552 | 954 | if(result || !connected) |
553 | 0 | return result; |
554 | | |
555 | 954 | if(data->state.upload) { |
556 | | /* When we know we are uploading a specified file, we can get the file |
557 | | size prior to the actual upload. */ |
558 | 45 | Curl_pgrsSetUploadSize(data, data->state.infilesize); |
559 | | |
560 | | /* FTP upload, shutdown DATA, ignore shutdown errors, as we rely |
561 | | * on the server response on the CONTROL connection. */ |
562 | 45 | Curl_xfer_setup_send(data, SECONDARYSOCKET); |
563 | 45 | Curl_xfer_set_shutdown(data, TRUE, TRUE); |
564 | 45 | } |
565 | 909 | else { |
566 | | /* FTP download, shutdown, do not ignore errors */ |
567 | 909 | Curl_xfer_setup_recv(data, SECONDARYSOCKET, data->req.size); |
568 | 909 | Curl_xfer_set_shutdown(data, TRUE, FALSE); |
569 | 909 | } |
570 | | |
571 | 954 | ftpc->pp.pending_resp = TRUE; /* expect server response */ |
572 | 954 | ftp_state(data, ftpc, FTP_STOP); |
573 | | |
574 | 954 | return CURLE_OK; |
575 | 954 | } |
576 | | |
577 | | static bool ftp_endofresp(struct Curl_easy *data, struct connectdata *conn, |
578 | | const char *line, size_t len, int *code) |
579 | 514k | { |
580 | 514k | curl_off_t status; |
581 | 514k | (void)data; |
582 | 514k | (void)conn; |
583 | | |
584 | 514k | if((len > 3) && LASTLINE(line) && !curlx_str_number(&line, &status, 999)) { |
585 | 18.0k | *code = (int)status; |
586 | 18.0k | return TRUE; |
587 | 18.0k | } |
588 | | |
589 | 496k | return FALSE; |
590 | 514k | } |
591 | | |
592 | | static CURLcode ftp_readresp(struct Curl_easy *data, |
593 | | struct ftp_conn *ftpc, |
594 | | int sockindex, |
595 | | struct pingpong *pp, |
596 | | int *ftpcodep, /* return the ftp-code if done */ |
597 | | size_t *size) /* size of the response */ |
598 | 21.5k | { |
599 | 21.5k | int code; |
600 | 21.5k | CURLcode result = Curl_pp_readresp(data, sockindex, pp, &code, size); |
601 | 21.5k | DEBUGASSERT(ftpcodep); |
602 | | |
603 | | /* store the latest code for later retrieval, except during shutdown */ |
604 | 21.5k | if(!ftpc->shutdown) |
605 | 21.5k | data->info.httpcode = code; |
606 | | |
607 | 21.5k | *ftpcodep = code; |
608 | | |
609 | 21.5k | if(code == 421) { |
610 | | /* 421 means "Service not available, closing control connection." and FTP |
611 | | * servers use it to signal that idle session timeout has been exceeded. |
612 | | * If we ignored the response, it could end up hanging in some cases. |
613 | | * |
614 | | * This response code can come at any point so having it treated |
615 | | * generically is a good idea. |
616 | | */ |
617 | 3 | infof(data, "We got a 421 - timeout"); |
618 | 3 | ftp_state(data, ftpc, FTP_STOP); |
619 | 3 | return CURLE_OPERATION_TIMEDOUT; |
620 | 3 | } |
621 | | |
622 | 21.5k | return result; |
623 | 21.5k | } |
624 | | |
625 | | /* --- parse FTP server responses --- */ |
626 | | |
627 | | /* |
628 | | * getftpresponse() is a BLOCKING function to read the full response from a |
629 | | * server after a command. |
630 | | * |
631 | | */ |
632 | | static CURLcode getftpresponse(struct Curl_easy *data, |
633 | | size_t *nreadp, /* return number of bytes |
634 | | read */ |
635 | | int *ftpcodep) /* return the ftp-code */ |
636 | 819 | { |
637 | | /* |
638 | | * We cannot read one byte per read() and then go back to select() as the |
639 | | * OpenSSL read() does not grok that properly. |
640 | | * |
641 | | * Alas, read as much as possible, split up into lines, use the ending |
642 | | * line in a response or continue reading. */ |
643 | | |
644 | 819 | struct connectdata *conn = data->conn; |
645 | 819 | curl_socket_t sockfd = conn->sock[FIRSTSOCKET]; |
646 | 819 | CURLcode result = CURLE_OK; |
647 | 819 | struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN); |
648 | 819 | struct pingpong *pp = &ftpc->pp; |
649 | 819 | size_t nread; |
650 | 819 | int cache_skip = 0; |
651 | 819 | DEBUGASSERT(ftpcodep); |
652 | | |
653 | 819 | CURL_TRC_FTP(data, "getftpresponse start"); |
654 | 819 | *nreadp = 0; |
655 | 819 | *ftpcodep = 0; /* 0 for errors */ |
656 | | |
657 | 819 | if(!ftpc) |
658 | 0 | return CURLE_FAILED_INIT; |
659 | | |
660 | 4.04k | while(!*ftpcodep && !result) { |
661 | | /* check and reset timeout value every lap */ |
662 | 3.93k | timediff_t timeout = Curl_pp_state_timeleft_ms(data, pp); |
663 | 3.93k | timediff_t interval_ms; |
664 | | |
665 | 3.93k | if(timeout <= 0) { |
666 | 13 | failf(data, "FTP response timeout"); |
667 | 13 | return CURLE_OPERATION_TIMEDOUT; /* already too little time */ |
668 | 13 | } |
669 | | |
670 | 3.92k | interval_ms = 1000; /* use 1 second timeout intervals */ |
671 | 3.92k | if(timeout < interval_ms) |
672 | 3.92k | interval_ms = timeout; |
673 | | |
674 | | /* |
675 | | * Since this function is blocking, we need to wait here for input on the |
676 | | * connection and only then we call the response reading function. We do |
677 | | * timeout at least every second to make the timeout check run. |
678 | | * |
679 | | * A caution here is that the ftp_readresp() function has a cache that may |
680 | | * contain pieces of a response from the previous invoke and we need to |
681 | | * make sure we do not wait for input while there is unhandled data in |
682 | | * that cache. Also, if the cache is there, we call ftp_readresp() and |
683 | | * the cache was not good enough to continue we must not busy-loop around |
684 | | * this function. |
685 | | * |
686 | | */ |
687 | | |
688 | 3.92k | if(curlx_dyn_len(&pp->recvbuf) && (cache_skip < 2)) { |
689 | | /* |
690 | | * There is a cache left since before. We then skipping the wait for |
691 | | * socket action, unless this is the same cache like the previous round |
692 | | * as then the cache was deemed not enough to act on and we then need to |
693 | | * wait for more data anyway. |
694 | | */ |
695 | 1.72k | } |
696 | 2.20k | else if(!Curl_conn_data_pending(data, FIRSTSOCKET)) { |
697 | 2.20k | curl_socket_t wsock = Curl_pp_needs_flush(data, pp) ? |
698 | 2.20k | sockfd : CURL_SOCKET_BAD; |
699 | 2.20k | int ev = Curl_socket_check(sockfd, CURL_SOCKET_BAD, wsock, interval_ms); |
700 | 2.20k | if(ev < 0) { |
701 | 0 | failf(data, "FTP response aborted due to select/poll error: %d", |
702 | 0 | SOCKERRNO); |
703 | 0 | return CURLE_RECV_ERROR; |
704 | 0 | } |
705 | 2.20k | else if(ev == 0) { |
706 | 13 | result = Curl_pgrsUpdate(data); |
707 | 13 | continue; /* continue in our loop for the timeout duration */ |
708 | 13 | } |
709 | 2.20k | } |
710 | | |
711 | 3.91k | if(Curl_pp_needs_flush(data, pp)) { |
712 | 0 | result = Curl_pp_flushsend(data, pp); |
713 | 0 | if(result) |
714 | 0 | break; |
715 | 0 | } |
716 | | |
717 | 3.91k | result = ftp_readresp(data, ftpc, FIRSTSOCKET, pp, ftpcodep, &nread); |
718 | 3.91k | if(result) |
719 | 696 | break; |
720 | | |
721 | 3.21k | if(!nread && curlx_dyn_len(&pp->recvbuf)) |
722 | | /* bump cache skip counter as on repeated skips we must wait for more |
723 | | data */ |
724 | 2.91k | cache_skip++; |
725 | 305 | else |
726 | | /* when we got data or there is no cache left, we reset the cache skip |
727 | | counter */ |
728 | 305 | cache_skip = 0; |
729 | | |
730 | 3.21k | *nreadp += nread; |
731 | | |
732 | 3.21k | } /* while there is buffer left and loop is requested */ |
733 | | |
734 | 806 | pp->pending_resp = FALSE; |
735 | 806 | CURL_TRC_FTP(data, "getftpresponse -> result=%d, nread=%zu, ftpcode=%d", |
736 | 806 | result, *nreadp, *ftpcodep); |
737 | | |
738 | 806 | return result; |
739 | 819 | } |
740 | | |
741 | | static CURLcode ftp_state_user(struct Curl_easy *data, |
742 | | struct ftp_conn *ftpc, |
743 | | struct connectdata *conn) |
744 | 184 | { |
745 | 184 | CURLcode result = Curl_pp_sendf(data, &ftpc->pp, "USER %s", |
746 | 184 | conn->user ? conn->user : ""); |
747 | 184 | if(!result) { |
748 | 184 | ftpc->ftp_trying_alternative = FALSE; |
749 | 184 | ftp_state(data, ftpc, FTP_USER); |
750 | 184 | } |
751 | 184 | return result; |
752 | 184 | } |
753 | | |
754 | | static CURLcode ftp_state_pwd(struct Curl_easy *data, |
755 | | struct ftp_conn *ftpc) |
756 | 1.99k | { |
757 | 1.99k | CURLcode result; |
758 | 1.99k | #ifdef DEBUGBUILD |
759 | 1.99k | if(!data->id && getenv("CURL_FTP_PWD_STOP")) |
760 | 0 | return CURLE_OK; |
761 | 1.99k | #endif |
762 | 1.99k | result = Curl_pp_sendf(data, &ftpc->pp, "%s", "PWD"); |
763 | 1.99k | if(!result) |
764 | 1.99k | ftp_state(data, ftpc, FTP_PWD); |
765 | | |
766 | 1.99k | return result; |
767 | 1.99k | } |
768 | | |
769 | | /* For the FTP "protocol connect" and "doing" phases only */ |
770 | | static CURLcode ftp_pollset(struct Curl_easy *data, |
771 | | struct easy_pollset *ps) |
772 | 6.82k | { |
773 | 6.82k | struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN); |
774 | 6.82k | return ftpc ? Curl_pp_pollset(data, &ftpc->pp, ps) : CURLE_OK; |
775 | 6.82k | } |
776 | | |
777 | | /* For the FTP "DO_MORE" phase only */ |
778 | | static CURLcode ftp_domore_pollset(struct Curl_easy *data, |
779 | | struct easy_pollset *ps) |
780 | 2.54k | { |
781 | 2.54k | struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN); |
782 | | |
783 | 2.54k | if(!ftpc) |
784 | 0 | return CURLE_OK; |
785 | | |
786 | | /* When in DO_MORE state, we could be either waiting for us to connect to a |
787 | | * remote site, or we could wait for that site to connect to us. Or handle |
788 | | * ordinary commands. |
789 | | */ |
790 | 2.54k | CURL_TRC_FTP(data, "[%s] ftp_domore_pollset()", FTP_CSTATE(ftpc)); |
791 | | |
792 | 2.54k | if(FTP_STOP == ftpc->state) { |
793 | | /* if stopped and still in this state, then we are also waiting for a |
794 | | connect on the secondary connection */ |
795 | 1 | DEBUGASSERT(data->conn->sock[SECONDARYSOCKET] != CURL_SOCKET_BAD || |
796 | 1 | (data->conn->cfilter[SECONDARYSOCKET] && |
797 | 1 | !Curl_conn_is_connected(data->conn, SECONDARYSOCKET))); |
798 | | /* An unconnected SECONDARY will add its socket by itself |
799 | | * via its adjust_pollset() */ |
800 | 1 | return Curl_pollset_add_in(data, ps, data->conn->sock[FIRSTSOCKET]); |
801 | 1 | } |
802 | 2.54k | return Curl_pp_pollset(data, &ftpc->pp, ps); |
803 | 2.54k | } |
804 | | |
805 | | static int pathlen(struct ftp_conn *ftpc, int num) |
806 | 260 | { |
807 | 260 | DEBUGASSERT(ftpc->dirs); |
808 | 260 | DEBUGASSERT(ftpc->dirdepth > num); |
809 | 260 | return ftpc->dirs[num].len; |
810 | 260 | } |
811 | | |
812 | | static const char *pathpiece(struct ftp_conn *ftpc, int num) |
813 | 260 | { |
814 | 260 | DEBUGASSERT(ftpc->dirs); |
815 | 260 | DEBUGASSERT(ftpc->dirdepth > num); |
816 | 260 | return &ftpc->rawpath[ftpc->dirs[num].start]; |
817 | 260 | } |
818 | | |
819 | | /* This is called after the FTP_QUOTE state is passed. |
820 | | |
821 | | ftp_state_cwd() sends the range of CWD commands to the server to change to |
822 | | the correct directory. It may also need to send MKD commands to create |
823 | | missing ones, if that option is enabled. |
824 | | */ |
825 | | static CURLcode ftp_state_cwd(struct Curl_easy *data, |
826 | | struct ftp_conn *ftpc, |
827 | | struct FTP *ftp) |
828 | 1.90k | { |
829 | 1.90k | CURLcode result = CURLE_OK; |
830 | | |
831 | 1.90k | if(ftpc->cwddone) |
832 | | /* already done and fine */ |
833 | 1.69k | result = ftp_state_mdtm(data, ftpc, ftp); |
834 | 202 | else { |
835 | | /* FTPFILE_NOCWD with full path: expect ftpc->cwddone! */ |
836 | 202 | DEBUGASSERT((data->set.ftp_filemethod != FTPFILE_NOCWD) || |
837 | 202 | !(ftpc->dirdepth && ftpc->rawpath[0] == '/')); |
838 | | |
839 | 202 | ftpc->count2 = 0; /* count2 counts failed CWDs */ |
840 | | |
841 | 202 | if(data->conn->bits.reuse && ftpc->entrypath && |
842 | | /* no need to go to entrypath when we have an absolute path */ |
843 | 0 | !(ftpc->dirdepth && ftpc->rawpath[0] == '/')) { |
844 | | /* This is a reused connection. Since we change directory to where the |
845 | | transfer is taking place, we must first get back to the original dir |
846 | | where we ended up after login: */ |
847 | 0 | ftpc->cwdcount = 0; /* we count this as the first path, then we add one |
848 | | for all upcoming ones in the ftp->dirs[] array */ |
849 | 0 | result = Curl_pp_sendf(data, &ftpc->pp, "CWD %s", ftpc->entrypath); |
850 | 0 | if(!result) |
851 | 0 | ftp_state(data, ftpc, FTP_CWD); |
852 | 0 | } |
853 | 202 | else { |
854 | 202 | if(ftpc->dirdepth) { |
855 | 202 | ftpc->cwdcount = 1; |
856 | | /* issue the first CWD, the rest is sent when the CWD responses are |
857 | | received... */ |
858 | 202 | result = Curl_pp_sendf(data, &ftpc->pp, "CWD %.*s", |
859 | 202 | pathlen(ftpc, 0), pathpiece(ftpc, 0)); |
860 | 202 | if(!result) |
861 | 202 | ftp_state(data, ftpc, FTP_CWD); |
862 | 202 | } |
863 | 0 | else { |
864 | | /* No CWD necessary */ |
865 | 0 | result = ftp_state_mdtm(data, ftpc, ftp); |
866 | 0 | } |
867 | 202 | } |
868 | 202 | } |
869 | 1.90k | return result; |
870 | 1.90k | } |
871 | | |
872 | | typedef enum { |
873 | | EPRT, |
874 | | PORT, |
875 | | DONE |
876 | | } ftpport; |
877 | | |
878 | | /* |
879 | | * Parse the CURLOPT_FTPPORT string |
880 | | * "(ipv4|ipv6|domain|interface)?(:port(-range)?)?" |
881 | | * and extract addr/addrlen and port_min/port_max. |
882 | | */ |
883 | | static CURLcode ftp_port_parse_string(struct Curl_easy *data, |
884 | | struct connectdata *conn, |
885 | | const char *string_ftpport, |
886 | | struct Curl_sockaddr_storage *ss, |
887 | | unsigned short *port_minp, |
888 | | unsigned short *port_maxp, |
889 | | const char **hostp, |
890 | | char *hbuf, size_t hbuflen) |
891 | 19 | { |
892 | 19 | const char *ip_end = NULL; |
893 | 19 | const char *addr = NULL; |
894 | 19 | size_t addrlen = 0; |
895 | 19 | unsigned short port_min = 0; |
896 | 19 | unsigned short port_max = 0; |
897 | 19 | char ipstr[50]; |
898 | | #ifndef USE_IPV6 |
899 | | (void)conn; |
900 | | (void)ss; |
901 | | #endif |
902 | | |
903 | | /* default to nothing */ |
904 | 19 | *hostp = NULL; |
905 | 19 | *port_minp = *port_maxp = 0; |
906 | | |
907 | 19 | if(!string_ftpport || (strlen(string_ftpport) <= 1)) |
908 | 1 | goto done; |
909 | | |
910 | 18 | #ifdef USE_IPV6 |
911 | 18 | if(*string_ftpport == '[') { |
912 | | /* [ipv6]:port(-range) */ |
913 | 2 | const char *ip_start = string_ftpport + 1; |
914 | 2 | ip_end = strchr(ip_start, ']'); |
915 | 2 | if(ip_end) { |
916 | 1 | addrlen = ip_end - ip_start; |
917 | 1 | addr = ip_start; |
918 | 1 | } |
919 | 2 | } |
920 | 16 | else |
921 | 16 | #endif |
922 | 16 | if(*string_ftpport == ':') { |
923 | | /* :port */ |
924 | 3 | ip_end = string_ftpport; |
925 | 3 | } |
926 | 13 | else { |
927 | 13 | ip_end = strchr(string_ftpport, ':'); |
928 | 13 | addr = string_ftpport; |
929 | 13 | if(ip_end) { |
930 | 9 | #ifdef USE_IPV6 |
931 | 9 | struct sockaddr_in6 * const sa6 = (void *)ss; |
932 | 9 | #endif |
933 | | /* either ipv6 or (ipv4|domain|interface):port(-range) */ |
934 | 9 | addrlen = ip_end - string_ftpport; |
935 | 9 | #ifdef USE_IPV6 |
936 | 9 | if(curlx_inet_pton(AF_INET6, string_ftpport, &sa6->sin6_addr) == 1) { |
937 | | /* ipv6 */ |
938 | 1 | addrlen = strlen(string_ftpport); |
939 | 1 | ip_end = NULL; /* this got no port ! */ |
940 | 1 | } |
941 | 9 | #endif |
942 | 9 | } |
943 | 4 | else |
944 | | /* ipv4|interface */ |
945 | 4 | addrlen = strlen(string_ftpport); |
946 | 13 | } |
947 | | |
948 | | /* parse the port */ |
949 | 18 | if(ip_end) { |
950 | 12 | const char *portp = strchr(ip_end, ':'); |
951 | 12 | if(portp) { |
952 | 11 | curl_off_t start; |
953 | 11 | curl_off_t end; |
954 | 11 | portp++; |
955 | 11 | if(!curlx_str_number(&portp, &start, 0xffff)) { |
956 | 4 | port_min = (unsigned short)start; |
957 | 4 | if(!curlx_str_single(&portp, '-') && |
958 | 2 | !curlx_str_number(&portp, &end, 0xffff)) |
959 | 1 | port_max = (unsigned short)end; |
960 | 3 | else |
961 | 3 | port_max = port_min; |
962 | 4 | } |
963 | 11 | } |
964 | 12 | } |
965 | | |
966 | | /* correct errors like :1234-1230 or :-4711 */ |
967 | 18 | if(port_min > port_max) |
968 | 1 | port_min = port_max = 0; |
969 | | |
970 | 18 | if(addrlen) { |
971 | 14 | const struct Curl_sockaddr_ex *remote_addr = |
972 | 14 | Curl_conn_get_remote_addr(data, FIRSTSOCKET); |
973 | | |
974 | 14 | DEBUGASSERT(remote_addr); |
975 | 14 | DEBUGASSERT(addr); |
976 | 14 | if(!remote_addr || (addrlen >= sizeof(ipstr)) || (addrlen >= hbuflen)) |
977 | 1 | return CURLE_FTP_PORT_FAILED; |
978 | 13 | memcpy(ipstr, addr, addrlen); |
979 | 13 | ipstr[addrlen] = 0; |
980 | | |
981 | | /* attempt to get the address of the given interface name */ |
982 | 13 | switch(Curl_if2ip(remote_addr->family, |
983 | 13 | #ifdef USE_IPV6 |
984 | 13 | Curl_ipv6_scope(&remote_addr->curl_sa_addr), |
985 | 13 | conn->scope_id, |
986 | 13 | #endif |
987 | 13 | ipstr, hbuf, hbuflen)) { |
988 | 12 | case IF2IP_NOT_FOUND: |
989 | | /* not an interface, use the string as hostname instead */ |
990 | 12 | memcpy(hbuf, addr, addrlen); |
991 | 12 | hbuf[addrlen] = 0; |
992 | 12 | *hostp = hbuf; |
993 | 12 | break; |
994 | 0 | case IF2IP_AF_NOT_SUPPORTED: |
995 | 0 | return CURLE_FTP_PORT_FAILED; |
996 | 1 | case IF2IP_FOUND: |
997 | 1 | *hostp = hbuf; /* use the hbuf for hostname */ |
998 | 1 | break; |
999 | 13 | } |
1000 | 13 | } |
1001 | | /* else: only a port(-range) given, leave host as NULL */ |
1002 | | |
1003 | 18 | done: |
1004 | 18 | *port_minp = port_min; |
1005 | 18 | *port_maxp = port_max; |
1006 | 18 | return CURLE_OK; |
1007 | 18 | } |
1008 | | |
1009 | | /* |
1010 | | * If no host was derived from the FTPPORT string, fall back to the IP address |
1011 | | * of the control connection's local socket. |
1012 | | */ |
1013 | | static CURLcode ftp_port_default_host(struct Curl_easy *data, |
1014 | | struct connectdata *conn, |
1015 | | struct Curl_sockaddr_storage *ss, |
1016 | | curl_socklen_t *sslenp, |
1017 | | const char **hostp, |
1018 | | char *hbuf, size_t hbuflen, |
1019 | | bool *non_localp) |
1020 | 5 | { |
1021 | 5 | struct sockaddr *sa = (struct sockaddr *)ss; |
1022 | 5 | struct sockaddr_in * const sa4 = (void *)sa; |
1023 | 5 | #ifdef USE_IPV6 |
1024 | 5 | struct sockaddr_in6 * const sa6 = (void *)sa; |
1025 | 5 | #endif |
1026 | 5 | char buffer[STRERROR_LEN]; |
1027 | 5 | const char *r; |
1028 | | |
1029 | 5 | *sslenp = sizeof(*ss); |
1030 | 5 | if(getsockname(conn->sock[FIRSTSOCKET], sa, sslenp)) { |
1031 | 0 | failf(data, "getsockname() failed: %s", |
1032 | 0 | curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); |
1033 | 0 | return CURLE_FTP_PORT_FAILED; |
1034 | 0 | } |
1035 | 5 | switch(sa->sa_family) { |
1036 | 0 | #ifdef USE_IPV6 |
1037 | 0 | case AF_INET6: |
1038 | 0 | r = curlx_inet_ntop(sa->sa_family, &sa6->sin6_addr, hbuf, hbuflen); |
1039 | 0 | break; |
1040 | 0 | #endif |
1041 | 5 | default: |
1042 | 5 | r = curlx_inet_ntop(sa->sa_family, &sa4->sin_addr, hbuf, hbuflen); |
1043 | 5 | break; |
1044 | 5 | } |
1045 | 5 | if(!r) |
1046 | 5 | return CURLE_FTP_PORT_FAILED; |
1047 | | |
1048 | 0 | *hostp = hbuf; |
1049 | 0 | *non_localp = FALSE; /* we know it is local now */ |
1050 | 0 | return CURLE_OK; |
1051 | 5 | } |
1052 | | |
1053 | | /* |
1054 | | * Resolve the host string to a list of addresses. |
1055 | | */ |
1056 | | static CURLcode ftp_port_resolve_host(struct Curl_easy *data, |
1057 | | struct connectdata *conn, |
1058 | | const char *host, |
1059 | | struct Curl_dns_entry **dns_entryp, |
1060 | | const struct Curl_addrinfo **resp) |
1061 | 13 | { |
1062 | 13 | CURLcode result; |
1063 | | |
1064 | 13 | *resp = NULL; |
1065 | 13 | result = Curl_resolv_blocking( |
1066 | 13 | data, Curl_resolv_dns_queries(data, conn->ip_version), |
1067 | 13 | host, 0, Curl_conn_get_transport(data, conn), dns_entryp); |
1068 | 13 | if(result) |
1069 | 3 | failf(data, "failed to resolve the address provided to PORT: %s", host); |
1070 | 10 | else { |
1071 | 10 | DEBUGASSERT(*dns_entryp); |
1072 | 10 | *resp = (*dns_entryp)->addr; |
1073 | 10 | } |
1074 | 13 | return result; |
1075 | 13 | } |
1076 | | |
1077 | | /* |
1078 | | * Open a TCP socket for the resolved address family. |
1079 | | */ |
1080 | | static CURLcode ftp_port_open_socket(struct Curl_easy *data, |
1081 | | struct connectdata *conn, |
1082 | | const struct Curl_addrinfo *res, |
1083 | | const struct Curl_addrinfo **aip, |
1084 | | curl_socket_t *portsockp) |
1085 | 10 | { |
1086 | 10 | char buffer[STRERROR_LEN]; |
1087 | 10 | int error = 0; |
1088 | 10 | const struct Curl_addrinfo *ai; |
1089 | 10 | CURLcode result = CURLE_FTP_PORT_FAILED; |
1090 | | |
1091 | 10 | for(ai = res; ai; ai = ai->ai_next) { |
1092 | 10 | result = |
1093 | 10 | Curl_socket_open(data, ai, NULL, |
1094 | 10 | Curl_conn_get_transport(data, conn), portsockp); |
1095 | 10 | if(result) { |
1096 | 0 | if(result == CURLE_OUT_OF_MEMORY) |
1097 | 0 | return result; |
1098 | 0 | result = CURLE_FTP_PORT_FAILED; |
1099 | 0 | error = SOCKERRNO; |
1100 | 0 | continue; |
1101 | 0 | } |
1102 | 10 | break; |
1103 | 10 | } |
1104 | 10 | if(!ai) { |
1105 | 0 | failf(data, "socket failure: %s", |
1106 | 0 | curlx_strerror(error, buffer, sizeof(buffer))); |
1107 | 0 | return CURLE_FTP_PORT_FAILED; |
1108 | 0 | } |
1109 | 10 | *aip = ai; |
1110 | 10 | return result; |
1111 | 10 | } |
1112 | | |
1113 | | /* |
1114 | | * Bind the socket to a local address and port within the requested range. |
1115 | | * Falls back to the control-connection address if the user-requested address |
1116 | | * is non-local. |
1117 | | */ |
1118 | | static CURLcode ftp_port_bind_socket(struct Curl_easy *data, |
1119 | | struct connectdata *conn, |
1120 | | curl_socket_t portsock, |
1121 | | const struct Curl_addrinfo *ai, |
1122 | | struct Curl_sockaddr_storage *ss, |
1123 | | curl_socklen_t *sslen_io, |
1124 | | unsigned short port_min, |
1125 | | unsigned short port_max, |
1126 | | bool non_local) |
1127 | 10 | { |
1128 | 10 | struct sockaddr *sa = (struct sockaddr *)ss; |
1129 | 10 | struct sockaddr_in * const sa4 = (void *)sa; |
1130 | 10 | #ifdef USE_IPV6 |
1131 | 10 | struct sockaddr_in6 * const sa6 = (void *)sa; |
1132 | 10 | #endif |
1133 | 10 | char buffer[STRERROR_LEN]; |
1134 | 10 | unsigned short port; |
1135 | 10 | int error; |
1136 | | |
1137 | 10 | memcpy(sa, ai->ai_addr, ai->ai_addrlen); |
1138 | 10 | *sslen_io = ai->ai_addrlen; |
1139 | | |
1140 | 10 | for(port = port_min; port <= port_max;) { |
1141 | 10 | if(sa->sa_family == AF_INET) |
1142 | 9 | sa4->sin_port = htons(port); |
1143 | 1 | #ifdef USE_IPV6 |
1144 | 1 | else |
1145 | 1 | sa6->sin6_port = htons(port); |
1146 | 10 | #endif |
1147 | 10 | if(bind(portsock, sa, *sslen_io)) { |
1148 | 10 | error = SOCKERRNO; |
1149 | 10 | if(non_local && (error == SOCKEADDRNOTAVAIL)) { |
1150 | | /* The requested bind address is not local. Use the address used for |
1151 | | * the control connection instead and restart the port loop. |
1152 | | */ |
1153 | 0 | infof(data, "bind(port=%hu) on non-local address failed: %s", port, |
1154 | 0 | curlx_strerror(error, buffer, sizeof(buffer))); |
1155 | |
|
1156 | 0 | *sslen_io = sizeof(*ss); |
1157 | 0 | if(getsockname(conn->sock[FIRSTSOCKET], sa, sslen_io)) { |
1158 | 0 | failf(data, "getsockname() failed: %s", |
1159 | 0 | curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); |
1160 | 0 | return CURLE_FTP_PORT_FAILED; |
1161 | 0 | } |
1162 | 0 | port = port_min; |
1163 | 0 | non_local = FALSE; /* do not try this again */ |
1164 | 0 | continue; |
1165 | 0 | } |
1166 | 10 | if(error != SOCKEADDRINUSE && error != SOCKEACCES) { |
1167 | 10 | failf(data, "bind(port=%hu) failed: %s", port, |
1168 | 10 | curlx_strerror(error, buffer, sizeof(buffer))); |
1169 | 10 | return CURLE_FTP_PORT_FAILED; |
1170 | 10 | } |
1171 | 10 | } |
1172 | 0 | else |
1173 | 0 | break; |
1174 | | |
1175 | | /* check if port is the maximum value here, because it might be 0xffff |
1176 | | and then the increment below will wrap the 16-bit counter */ |
1177 | 0 | if(port == port_max) { |
1178 | 0 | failf(data, "bind() failed, ran out of ports"); |
1179 | 0 | return CURLE_FTP_PORT_FAILED; |
1180 | 0 | } |
1181 | 0 | port++; |
1182 | 0 | } |
1183 | | |
1184 | | /* re-read the name so we can extract the actual port chosen */ |
1185 | 0 | *sslen_io = sizeof(*ss); |
1186 | 0 | if(getsockname(portsock, sa, sslen_io)) { |
1187 | 0 | failf(data, "getsockname() failed: %s", |
1188 | 0 | curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); |
1189 | 0 | return CURLE_FTP_PORT_FAILED; |
1190 | 0 | } |
1191 | 0 | CURL_TRC_FTP(data, "ftp_port_bind_socket(), socket bound to port %d", |
1192 | 0 | port); |
1193 | 0 | return CURLE_OK; |
1194 | 0 | } |
1195 | | |
1196 | | /* |
1197 | | * Start listening on the data socket. |
1198 | | */ |
1199 | | static CURLcode ftp_port_listen(struct Curl_easy *data, curl_socket_t portsock) |
1200 | 0 | { |
1201 | 0 | char buffer[STRERROR_LEN]; |
1202 | |
|
1203 | 0 | if(listen(portsock, 1)) { |
1204 | 0 | failf(data, "socket failure: %s", |
1205 | 0 | curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); |
1206 | 0 | return CURLE_FTP_PORT_FAILED; |
1207 | 0 | } |
1208 | 0 | CURL_TRC_FTP(data, "ftp_port_listen(), listening on port"); |
1209 | 0 | return CURLE_OK; |
1210 | 0 | } |
1211 | | |
1212 | | /* |
1213 | | * Send the EPRT or PORT command to the server. |
1214 | | */ |
1215 | | static CURLcode ftp_port_send_command(struct Curl_easy *data, |
1216 | | struct ftp_conn *ftpc, |
1217 | | struct connectdata *conn, |
1218 | | struct Curl_sockaddr_storage *ss, |
1219 | | const struct Curl_addrinfo *ai, |
1220 | | ftpport fcmd) |
1221 | 0 | { |
1222 | 0 | static const char mode[][5] = { "EPRT", "PORT" }; |
1223 | 0 | struct sockaddr *sa = (struct sockaddr *)ss; |
1224 | 0 | struct sockaddr_in * const sa4 = (void *)sa; |
1225 | 0 | #ifdef USE_IPV6 |
1226 | 0 | struct sockaddr_in6 * const sa6 = (void *)sa; |
1227 | 0 | #endif |
1228 | 0 | char myhost[MAX_IPADR_LEN + 1] = ""; |
1229 | 0 | unsigned short port; |
1230 | 0 | CURLcode result; |
1231 | | |
1232 | | /* Get a plain printable version of the numerical address to work with. This |
1233 | | logic uses the address provided by the FTPPORT option, which at times |
1234 | | might differ from the address in 'ss' used to bind to: when a user asks |
1235 | | the server to connect to a specific address knowing that it works, but |
1236 | | curl instead selects to listen to the local address because it cannot use |
1237 | | the provided address. FTP is strange. */ |
1238 | 0 | Curl_printable_address(ai, myhost, sizeof(myhost)); |
1239 | |
|
1240 | 0 | #ifdef USE_IPV6 |
1241 | 0 | if(!conn->bits.ftp_use_eprt && conn->bits.ipv6) |
1242 | | /* EPRT is disabled but we are connected to an IPv6 host, so we ignore the |
1243 | | request and enable EPRT again! */ |
1244 | 0 | conn->bits.ftp_use_eprt = TRUE; |
1245 | 0 | #endif |
1246 | |
|
1247 | 0 | for(; fcmd != DONE; fcmd++) { |
1248 | |
|
1249 | 0 | if(!conn->bits.ftp_use_eprt && (EPRT == fcmd)) |
1250 | | /* if disabled, goto next */ |
1251 | 0 | continue; |
1252 | | |
1253 | 0 | if((PORT == fcmd) && sa->sa_family != AF_INET) |
1254 | | /* PORT is IPv4 only */ |
1255 | 0 | continue; |
1256 | | |
1257 | 0 | switch(sa->sa_family) { |
1258 | 0 | case AF_INET: |
1259 | 0 | port = ntohs(sa4->sin_port); |
1260 | 0 | break; |
1261 | 0 | #ifdef USE_IPV6 |
1262 | 0 | case AF_INET6: |
1263 | 0 | port = ntohs(sa6->sin6_port); |
1264 | 0 | break; |
1265 | 0 | #endif |
1266 | 0 | default: |
1267 | 0 | continue; /* might as well skip this */ |
1268 | 0 | } |
1269 | | |
1270 | 0 | if(EPRT == fcmd) { |
1271 | | /* |
1272 | | * Two fine examples from RFC2428; |
1273 | | * |
1274 | | * EPRT |1|132.235.1.2|6275| |
1275 | | * |
1276 | | * EPRT |2|1080::8:800:200C:417A|5282| |
1277 | | */ |
1278 | 0 | result = Curl_pp_sendf(data, &ftpc->pp, "%s |%d|%s|%hu|", mode[fcmd], |
1279 | 0 | sa->sa_family == AF_INET ? 1 : 2, |
1280 | 0 | myhost, port); |
1281 | 0 | if(result) { |
1282 | 0 | failf(data, "Failure sending EPRT command: %s", |
1283 | 0 | curl_easy_strerror(result)); |
1284 | 0 | return result; |
1285 | 0 | } |
1286 | 0 | break; |
1287 | 0 | } |
1288 | 0 | if(PORT == fcmd) { |
1289 | | /* large enough for [IP address],[num],[num] */ |
1290 | 0 | char target[sizeof(myhost) + 20]; |
1291 | 0 | const char *source = myhost; |
1292 | 0 | char *dest = target; |
1293 | | |
1294 | | /* translate x.x.x.x to x,x,x,x */ |
1295 | 0 | while(*source) { |
1296 | 0 | if(*source == '.') |
1297 | 0 | *dest = ','; |
1298 | 0 | else |
1299 | 0 | *dest = *source; |
1300 | 0 | dest++; |
1301 | 0 | source++; |
1302 | 0 | } |
1303 | 0 | *dest = 0; |
1304 | 0 | curl_msnprintf(dest, 20, ",%d,%d", (int)(port >> 8), (int)(port & 0xff)); |
1305 | |
|
1306 | 0 | result = Curl_pp_sendf(data, &ftpc->pp, "%s %s", mode[fcmd], target); |
1307 | 0 | if(result) { |
1308 | 0 | failf(data, "Failure sending PORT command: %s", |
1309 | 0 | curl_easy_strerror(result)); |
1310 | 0 | return result; |
1311 | 0 | } |
1312 | 0 | break; |
1313 | 0 | } |
1314 | 0 | } |
1315 | | |
1316 | | /* store which command was sent */ |
1317 | 0 | ftpc->count1 = fcmd; |
1318 | 0 | ftp_state(data, ftpc, FTP_PORT); |
1319 | 0 | return CURLE_OK; |
1320 | 0 | } |
1321 | | |
1322 | | /* |
1323 | | * ftp_state_use_port() |
1324 | | * |
1325 | | * Set up an active-mode FTP data connection (using PORT or EPRT) and start |
1326 | | * listening for the server's incoming connection on SECONDARYSOCKET. |
1327 | | */ |
1328 | | static CURLcode ftp_state_use_port(struct Curl_easy *data, |
1329 | | struct ftp_conn *ftpc, |
1330 | | ftpport fcmd) /* start with this */ |
1331 | 19 | { |
1332 | 19 | CURLcode result = CURLE_FTP_PORT_FAILED; |
1333 | 19 | struct connectdata *conn = data->conn; |
1334 | 19 | curl_socket_t portsock = CURL_SOCKET_BAD; |
1335 | | |
1336 | 19 | struct Curl_sockaddr_storage ss; |
1337 | 19 | curl_socklen_t sslen; |
1338 | 19 | char hbuf[NI_MAXHOST]; |
1339 | 19 | const char *host = NULL; |
1340 | 19 | const char *string_ftpport = data->set.str[STRING_FTPPORT]; |
1341 | 19 | struct Curl_dns_entry *dns_entry = NULL; |
1342 | 19 | const struct Curl_addrinfo *res = NULL; |
1343 | 19 | const struct Curl_addrinfo *ai = NULL; |
1344 | 19 | unsigned short port_min = 0; |
1345 | 19 | unsigned short port_max = 0; |
1346 | 19 | bool non_local = TRUE; |
1347 | | |
1348 | | /* parse the FTPPORT string for address and port range */ |
1349 | 19 | result = ftp_port_parse_string(data, conn, string_ftpport, |
1350 | 19 | &ss, &port_min, &port_max, |
1351 | 19 | &host, hbuf, sizeof(hbuf)); |
1352 | 19 | if(!result && !host) |
1353 | | /* if no host was specified, use the control connection's local IP */ |
1354 | 5 | result = ftp_port_default_host(data, conn, &ss, &sslen, &host, |
1355 | 5 | hbuf, sizeof(hbuf), &non_local); |
1356 | | |
1357 | | /* resolve host string to address list */ |
1358 | 19 | if(!result) |
1359 | 13 | result = ftp_port_resolve_host(data, conn, host, &dns_entry, &res); |
1360 | | |
1361 | | /* Open a TCP socket for the data connection */ |
1362 | 19 | if(!result) |
1363 | 10 | result = ftp_port_open_socket(data, conn, res, &ai, &portsock); |
1364 | 19 | if(!result) { |
1365 | 10 | CURL_TRC_FTP(data, "[%s] ftp_state_use_port(), opened socket", |
1366 | 10 | FTP_CSTATE(ftpc)); |
1367 | | |
1368 | | /* bind to a suitable local address / port */ |
1369 | 10 | result = ftp_port_bind_socket(data, conn, portsock, ai, &ss, &sslen, |
1370 | 10 | port_min, port_max, non_local); |
1371 | 10 | } |
1372 | | |
1373 | | /* listen */ |
1374 | 19 | if(!result) |
1375 | 0 | result = ftp_port_listen(data, portsock); |
1376 | | |
1377 | | /* send the PORT / EPRT command */ |
1378 | 19 | if(!result) |
1379 | 0 | result = ftp_port_send_command(data, ftpc, conn, &ss, ai, fcmd); |
1380 | | |
1381 | | /* replace any filter on SECONDARY with one listening on this socket */ |
1382 | 19 | if(!result) |
1383 | 0 | result = Curl_conn_tcp_listen_set(data, conn, SECONDARYSOCKET, &portsock); |
1384 | | |
1385 | 19 | if(!result) |
1386 | 0 | portsock = CURL_SOCKET_BAD; /* now held in filter */ |
1387 | | |
1388 | | /* cleanup */ |
1389 | | |
1390 | 19 | if(dns_entry) |
1391 | 10 | Curl_dns_entry_unlink(data, &dns_entry); |
1392 | 19 | if(result) { |
1393 | 19 | ftp_state(data, ftpc, FTP_STOP); |
1394 | 19 | } |
1395 | 0 | else { |
1396 | | /* successfully set up the listen socket filter. SSL needed? */ |
1397 | 0 | if(conn->bits.ftp_use_data_ssl && data->set.ftp_use_port && |
1398 | 0 | !Curl_conn_is_ssl(conn, SECONDARYSOCKET)) { |
1399 | 0 | result = Curl_ssl_cfilter_add(data, conn, SECONDARYSOCKET); |
1400 | 0 | } |
1401 | 0 | conn->bits.do_more = FALSE; |
1402 | 0 | Curl_pgrsTime(data, TIMER_STARTACCEPT); |
1403 | 0 | Curl_expire(data, (data->set.accepttimeout > 0) ? |
1404 | 0 | data->set.accepttimeout: DEFAULT_ACCEPT_TIMEOUT, |
1405 | 0 | EXPIRE_FTP_ACCEPT); |
1406 | 0 | } |
1407 | 19 | if(portsock != CURL_SOCKET_BAD) |
1408 | 10 | Curl_socket_close(data, conn, portsock); |
1409 | 19 | return result; |
1410 | 19 | } |
1411 | | |
1412 | | static CURLcode ftp_state_use_pasv(struct Curl_easy *data, |
1413 | | struct ftp_conn *ftpc, |
1414 | | struct connectdata *conn) |
1415 | 1.68k | { |
1416 | 1.68k | CURLcode result = CURLE_OK; |
1417 | | /* |
1418 | | Here's the executive summary on what to do: |
1419 | | |
1420 | | PASV is RFC959, expect: |
1421 | | 227 Entering Passive Mode (a1,a2,a3,a4,p1,p2) |
1422 | | |
1423 | | LPSV is RFC1639, expect: |
1424 | | 228 Entering Long Passive Mode (4,4,a1,a2,a3,a4,2,p1,p2) |
1425 | | |
1426 | | EPSV is RFC2428, expect: |
1427 | | 229 Entering Extended Passive Mode (|||port|) |
1428 | | |
1429 | | */ |
1430 | | |
1431 | 1.68k | static const char mode[][5] = { "EPSV", "PASV" }; |
1432 | 1.68k | int modeoff; |
1433 | | |
1434 | 1.68k | #ifdef PF_INET6 |
1435 | 1.68k | if(!conn->bits.ftp_use_epsv && conn->bits.ipv6) |
1436 | | /* EPSV is disabled but we are connected to an IPv6 host, so we ignore the |
1437 | | request and enable EPSV again! */ |
1438 | 0 | conn->bits.ftp_use_epsv = TRUE; |
1439 | 1.68k | #endif |
1440 | | |
1441 | 1.68k | modeoff = conn->bits.ftp_use_epsv ? 0 : 1; |
1442 | | |
1443 | 1.68k | result = Curl_pp_sendf(data, &ftpc->pp, "%s", mode[modeoff]); |
1444 | 1.68k | if(!result) { |
1445 | 1.68k | ftpc->count1 = modeoff; |
1446 | 1.68k | ftp_state(data, ftpc, FTP_PASV); |
1447 | 1.68k | infof(data, "Connect data stream passively"); |
1448 | 1.68k | } |
1449 | 1.68k | return result; |
1450 | 1.68k | } |
1451 | | |
1452 | | /* |
1453 | | * ftp_state_prepare_transfer() starts PORT, PASV or PRET etc. |
1454 | | * |
1455 | | * REST is the last command in the chain of commands when a "head"-like |
1456 | | * request is made. Thus, if an actual transfer is to be made this is where we |
1457 | | * take off for real. |
1458 | | */ |
1459 | | static CURLcode ftp_state_prepare_transfer(struct Curl_easy *data, |
1460 | | struct ftp_conn *ftpc, |
1461 | | struct FTP *ftp) |
1462 | 1.81k | { |
1463 | 1.81k | CURLcode result = CURLE_OK; |
1464 | 1.81k | struct connectdata *conn = data->conn; |
1465 | | |
1466 | 1.81k | if(ftp->transfer != PPTRANSFER_BODY) { |
1467 | | /* does not transfer any data */ |
1468 | | |
1469 | | /* still possibly do PRE QUOTE jobs */ |
1470 | 107 | ftp_state(data, ftpc, FTP_RETR_PREQUOTE); |
1471 | 107 | result = ftp_state_quote(data, ftpc, ftp, TRUE, FTP_RETR_PREQUOTE); |
1472 | 107 | } |
1473 | 1.70k | else if(data->set.ftp_use_port) { |
1474 | | /* We have chosen to use the PORT (or similar) command */ |
1475 | 19 | result = ftp_state_use_port(data, ftpc, EPRT); |
1476 | 19 | } |
1477 | 1.68k | else { |
1478 | | /* We have chosen (this is default) to use the PASV (or similar) command */ |
1479 | 1.68k | if(data->set.ftp_use_pret) { |
1480 | | /* The user has requested that we send a PRET command |
1481 | | to prepare the server for the upcoming PASV */ |
1482 | 4 | if(!ftpc->file) |
1483 | 2 | result = Curl_pp_sendf(data, &ftpc->pp, "PRET %s", |
1484 | 2 | data->set.str[STRING_CUSTOMREQUEST] ? |
1485 | 0 | data->set.str[STRING_CUSTOMREQUEST] : |
1486 | 2 | (data->state.list_only ? "NLST" : "LIST")); |
1487 | 2 | else if(data->state.upload) |
1488 | 1 | result = Curl_pp_sendf(data, &ftpc->pp, "PRET STOR %s", ftpc->file); |
1489 | 1 | else |
1490 | 1 | result = Curl_pp_sendf(data, &ftpc->pp, "PRET RETR %s", ftpc->file); |
1491 | 4 | if(!result) |
1492 | 4 | ftp_state(data, ftpc, FTP_PRET); |
1493 | 4 | } |
1494 | 1.68k | else |
1495 | 1.68k | result = ftp_state_use_pasv(data, ftpc, conn); |
1496 | 1.68k | } |
1497 | 1.81k | return result; |
1498 | 1.81k | } |
1499 | | |
1500 | | static CURLcode ftp_state_rest(struct Curl_easy *data, |
1501 | | struct ftp_conn *ftpc, |
1502 | | struct FTP *ftp) |
1503 | 1.83k | { |
1504 | 1.83k | CURLcode result = CURLE_OK; |
1505 | | |
1506 | 1.83k | if((ftp->transfer != PPTRANSFER_BODY) && ftpc->file) { |
1507 | | /* if a "head"-like request is being made (on a file) */ |
1508 | | |
1509 | | /* Determine if server can respond to REST command and therefore |
1510 | | whether it supports range */ |
1511 | 32 | result = Curl_pp_sendf(data, &ftpc->pp, "REST %d", 0); |
1512 | 32 | if(!result) |
1513 | 32 | ftp_state(data, ftpc, FTP_REST); |
1514 | 32 | } |
1515 | 1.80k | else |
1516 | 1.80k | result = ftp_state_prepare_transfer(data, ftpc, ftp); |
1517 | | |
1518 | 1.83k | return result; |
1519 | 1.83k | } |
1520 | | |
1521 | | static CURLcode ftp_state_size(struct Curl_easy *data, |
1522 | | struct ftp_conn *ftpc, |
1523 | | struct FTP *ftp) |
1524 | 1.84k | { |
1525 | 1.84k | CURLcode result = CURLE_OK; |
1526 | | |
1527 | 1.84k | if((ftp->transfer == PPTRANSFER_INFO) && ftpc->file) { |
1528 | | /* if a "head"-like request is being made (on a file) */ |
1529 | | |
1530 | | /* we know ftpc->file is a valid pointer to a filename */ |
1531 | 34 | result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file); |
1532 | 34 | if(!result) |
1533 | 34 | ftp_state(data, ftpc, FTP_SIZE); |
1534 | 34 | } |
1535 | 1.80k | else |
1536 | 1.80k | result = ftp_state_rest(data, ftpc, ftp); |
1537 | | |
1538 | 1.84k | return result; |
1539 | 1.84k | } |
1540 | | |
1541 | | static CURLcode ftp_state_list(struct Curl_easy *data, |
1542 | | struct ftp_conn *ftpc, |
1543 | | struct FTP *ftp) |
1544 | 567 | { |
1545 | 567 | CURLcode result = CURLE_OK; |
1546 | | |
1547 | | /* If this output is to be machine-parsed, the NLST command might be better |
1548 | | to use, since the LIST command output is not specified or standard in any |
1549 | | way. It has turned out that the NLST list output is not the same on all |
1550 | | servers either... */ |
1551 | | |
1552 | | /* |
1553 | | if FTPFILE_NOCWD was specified, we should add the path |
1554 | | as argument for the LIST / NLST / or custom command. |
1555 | | Whether the server will support this, is uncertain. |
1556 | | |
1557 | | The other ftp_filemethods will CWD into dir/dir/ first and |
1558 | | then do LIST (in that case: nothing to do here) |
1559 | | */ |
1560 | 567 | const char *lstArg = NULL; |
1561 | 567 | int lstArglen = 0; |
1562 | 567 | char *cmd; |
1563 | | |
1564 | 567 | if((data->set.ftp_filemethod == FTPFILE_NOCWD) && ftp->path) { |
1565 | | /* URL-decode before evaluation: e.g. paths starting/ending with %2f */ |
1566 | 2 | const char *rawPath = ftpc->rawpath; |
1567 | 2 | const char *slashPos = strrchr(rawPath, '/'); |
1568 | 2 | if(slashPos) { |
1569 | | /* chop off the file part if format is dir/file otherwise remove |
1570 | | the trailing slash for dir/dir/ except for absolute path / */ |
1571 | 1 | size_t n = slashPos - rawPath; |
1572 | 1 | if(n == 0) |
1573 | 1 | ++n; |
1574 | | |
1575 | 1 | lstArg = rawPath; |
1576 | 1 | lstArglen = (int)n; |
1577 | 1 | } |
1578 | 2 | } |
1579 | | |
1580 | 567 | cmd = curl_maprintf("%s%s%.*s", |
1581 | 567 | data->set.str[STRING_CUSTOMREQUEST] ? |
1582 | 1 | data->set.str[STRING_CUSTOMREQUEST] : |
1583 | 567 | (data->state.list_only ? "NLST" : "LIST"), |
1584 | 567 | lstArg ? " " : "", |
1585 | 567 | lstArglen, lstArg ? lstArg : ""); |
1586 | | |
1587 | 567 | if(!cmd) |
1588 | 0 | return CURLE_OUT_OF_MEMORY; |
1589 | | |
1590 | 567 | result = Curl_pp_sendf(data, &ftpc->pp, "%s", cmd); |
1591 | 567 | curlx_free(cmd); |
1592 | | |
1593 | 567 | if(!result) |
1594 | 567 | ftp_state(data, ftpc, FTP_LIST); |
1595 | | |
1596 | 567 | return result; |
1597 | 567 | } |
1598 | | |
1599 | | static CURLcode ftp_state_list_prequote(struct Curl_easy *data, |
1600 | | struct ftp_conn *ftpc, |
1601 | | struct FTP *ftp) |
1602 | 0 | { |
1603 | | /* We have sent the TYPE, now we must send the list of prequote strings */ |
1604 | 0 | return ftp_state_quote(data, ftpc, ftp, TRUE, FTP_LIST_PREQUOTE); |
1605 | 0 | } |
1606 | | |
1607 | | static CURLcode ftp_state_retr_prequote(struct Curl_easy *data, |
1608 | | struct ftp_conn *ftpc, |
1609 | | struct FTP *ftp) |
1610 | 674 | { |
1611 | | /* We have sent the TYPE, now we must send the list of prequote strings */ |
1612 | 674 | return ftp_state_quote(data, ftpc, ftp, TRUE, FTP_RETR_PREQUOTE); |
1613 | 674 | } |
1614 | | |
1615 | | static CURLcode ftp_state_stor_prequote(struct Curl_easy *data, |
1616 | | struct ftp_conn *ftpc, |
1617 | | struct FTP *ftp) |
1618 | 83 | { |
1619 | | /* We have sent the TYPE, now we must send the list of prequote strings */ |
1620 | 83 | return ftp_state_quote(data, ftpc, ftp, TRUE, FTP_STOR_PREQUOTE); |
1621 | 83 | } |
1622 | | |
1623 | | static CURLcode ftp_state_type(struct Curl_easy *data, |
1624 | | struct ftp_conn *ftpc, |
1625 | | struct FTP *ftp) |
1626 | 1.84k | { |
1627 | 1.84k | CURLcode result = CURLE_OK; |
1628 | | |
1629 | | /* If we have selected NOBODY and HEADER, it means that we only want file |
1630 | | information. Which in FTP cannot be much more than the file size and |
1631 | | date. */ |
1632 | 1.84k | if(data->req.no_body && ftpc->file && |
1633 | 37 | ftp_need_type(ftpc, (bool)data->state.prefer_ascii)) { |
1634 | | /* The SIZE command is _not_ RFC 959 specified, and therefore many servers |
1635 | | may not support it! It is however the only way we have to get a file's |
1636 | | size! */ |
1637 | | |
1638 | 37 | ftp->transfer = PPTRANSFER_INFO; |
1639 | | /* this means no actual transfer will be made */ |
1640 | | |
1641 | | /* Some servers return different sizes for different modes, and thus we |
1642 | | must set the proper type before we check the size */ |
1643 | 37 | result = ftp_nb_type(data, ftpc, ftp, (bool)data->state.prefer_ascii, |
1644 | 37 | FTP_TYPE); |
1645 | 37 | if(result) |
1646 | 0 | return result; |
1647 | 37 | } |
1648 | 1.80k | else |
1649 | 1.80k | result = ftp_state_size(data, ftpc, ftp); |
1650 | | |
1651 | 1.84k | return result; |
1652 | 1.84k | } |
1653 | | |
1654 | | /* This is called after the CWD commands have been done in the beginning of |
1655 | | the DO phase */ |
1656 | | static CURLcode ftp_state_mdtm(struct Curl_easy *data, |
1657 | | struct ftp_conn *ftpc, |
1658 | | struct FTP *ftp) |
1659 | 1.84k | { |
1660 | 1.84k | CURLcode result = CURLE_OK; |
1661 | | |
1662 | | /* Requested time of file or time-depended transfer? */ |
1663 | 1.84k | if((data->set.get_filetime || data->set.timecondition) && ftpc->file) { |
1664 | | |
1665 | | /* we have requested to get the modified-time of the file, this is a white |
1666 | | spot as the MDTM is not mentioned in RFC959 */ |
1667 | 47 | result = Curl_pp_sendf(data, &ftpc->pp, "MDTM %s", ftpc->file); |
1668 | | |
1669 | 47 | if(!result) |
1670 | 47 | ftp_state(data, ftpc, FTP_MDTM); |
1671 | 47 | } |
1672 | 1.79k | else |
1673 | 1.79k | result = ftp_state_type(data, ftpc, ftp); |
1674 | | |
1675 | 1.84k | return result; |
1676 | 1.84k | } |
1677 | | |
1678 | | /* This is called after the TYPE and possible quote commands have been sent */ |
1679 | | static CURLcode ftp_state_ul_setup(struct Curl_easy *data, |
1680 | | struct ftp_conn *ftpc, |
1681 | | struct FTP *ftp, |
1682 | | bool sizechecked) |
1683 | 83 | { |
1684 | 83 | CURLcode result = CURLE_OK; |
1685 | 83 | curl_bit append = data->set.remote_append; |
1686 | | |
1687 | 83 | if((data->state.resume_from && !sizechecked) || |
1688 | 45 | ((data->state.resume_from > 0) && sizechecked)) { |
1689 | | /* we are about to continue the uploading of a file |
1690 | | 1. get already existing file's size. We use the SIZE command for this |
1691 | | which may not exist in the server! The SIZE command is not in |
1692 | | RFC959. |
1693 | | |
1694 | | 2. This used to set REST, but since we can do append, we issue no |
1695 | | another ftp command. Skip the source file offset and APPEND the rest |
1696 | | on the file instead |
1697 | | |
1698 | | 3. pass file-size number of bytes in the source file |
1699 | | 4. lower the infilesize counter */ |
1700 | | /* => transfer as usual */ |
1701 | 38 | int seekerr = CURL_SEEKFUNC_OK; |
1702 | | |
1703 | 38 | if(data->state.resume_from < 0) { |
1704 | | /* Got no given size to start from, figure it out */ |
1705 | 0 | result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file); |
1706 | 0 | if(!result) |
1707 | 0 | ftp_state(data, ftpc, FTP_STOR_SIZE); |
1708 | 0 | return result; |
1709 | 0 | } |
1710 | | |
1711 | | /* enable append */ |
1712 | 38 | append = TRUE; |
1713 | | |
1714 | | /* Let's read off the proper amount of bytes from the input. */ |
1715 | 38 | if(data->set.seek_func) { |
1716 | 0 | Curl_set_in_callback(data, TRUE); |
1717 | 0 | seekerr = data->set.seek_func(data->set.seek_client, |
1718 | 0 | data->state.resume_from, SEEK_SET); |
1719 | 0 | Curl_set_in_callback(data, FALSE); |
1720 | 0 | } |
1721 | | |
1722 | 38 | if(seekerr != CURL_SEEKFUNC_OK) { |
1723 | 0 | curl_off_t passed = 0; |
1724 | 0 | if(seekerr != CURL_SEEKFUNC_CANTSEEK) { |
1725 | 0 | failf(data, "Could not seek stream"); |
1726 | 0 | return CURLE_FTP_COULDNT_USE_REST; |
1727 | 0 | } |
1728 | | /* seekerr == CURL_SEEKFUNC_CANTSEEK (cannot seek to offset) */ |
1729 | 0 | do { |
1730 | 0 | char scratch[4 * 1024]; |
1731 | 0 | size_t readthisamountnow = |
1732 | 0 | (data->state.resume_from - passed > (curl_off_t)sizeof(scratch)) ? |
1733 | 0 | sizeof(scratch) : |
1734 | 0 | curlx_sotouz(data->state.resume_from - passed); |
1735 | |
|
1736 | 0 | size_t actuallyread = |
1737 | 0 | data->state.fread_func(scratch, 1, readthisamountnow, |
1738 | 0 | data->state.in); |
1739 | |
|
1740 | 0 | passed += actuallyread; |
1741 | 0 | if((actuallyread == 0) || (actuallyread > readthisamountnow)) { |
1742 | | /* this checks for greater-than only to make sure that the |
1743 | | CURL_READFUNC_ABORT return code still aborts */ |
1744 | 0 | failf(data, "Failed to read data"); |
1745 | 0 | return CURLE_FTP_COULDNT_USE_REST; |
1746 | 0 | } |
1747 | 0 | } while(passed < data->state.resume_from); |
1748 | 0 | } |
1749 | | /* now, decrease the size of the read */ |
1750 | 38 | if(data->state.infilesize > 0) { |
1751 | 35 | data->state.infilesize -= data->state.resume_from; |
1752 | | |
1753 | 35 | if(data->state.infilesize <= 0) { |
1754 | 34 | infof(data, "File already completely uploaded"); |
1755 | | |
1756 | | /* no data to transfer */ |
1757 | 34 | Curl_xfer_setup_nop(data); |
1758 | | |
1759 | | /* Set ->transfer so that we will not get any error in |
1760 | | * ftp_done() because we did not transfer anything! */ |
1761 | 34 | ftp->transfer = PPTRANSFER_NONE; |
1762 | | |
1763 | 34 | ftp_state(data, ftpc, FTP_STOP); |
1764 | 34 | return CURLE_OK; |
1765 | 34 | } |
1766 | 35 | } |
1767 | | /* we have passed, proceed as normal */ |
1768 | 38 | } /* resume_from */ |
1769 | | |
1770 | 49 | result = Curl_pp_sendf(data, &ftpc->pp, append ? "APPE %s" : "STOR %s", |
1771 | 49 | ftpc->file); |
1772 | 49 | if(!result) |
1773 | 49 | ftp_state(data, ftpc, FTP_STOR); |
1774 | | |
1775 | 49 | return result; |
1776 | 83 | } |
1777 | | |
1778 | | static CURLcode ftp_state_retr(struct Curl_easy *data, |
1779 | | struct ftp_conn *ftpc, |
1780 | | struct FTP *ftp, |
1781 | | curl_off_t filesize) |
1782 | 666 | { |
1783 | 666 | CURLcode result = CURLE_OK; |
1784 | | |
1785 | 666 | CURL_TRC_FTP(data, "[%s] ftp_state_retr()", FTP_CSTATE(ftpc)); |
1786 | 666 | if(data->set.max_filesize && (filesize > data->set.max_filesize)) { |
1787 | 35 | failf(data, "Maximum file size exceeded"); |
1788 | 35 | return CURLE_FILESIZE_EXCEEDED; |
1789 | 35 | } |
1790 | 631 | ftp->downloadsize = filesize; |
1791 | | |
1792 | 631 | if(data->state.resume_from) { |
1793 | | /* We always (attempt to) get the size of downloads, so it is done before |
1794 | | this even when not doing resumes. */ |
1795 | 252 | if(filesize == -1) { |
1796 | 51 | infof(data, "ftp server does not support SIZE"); |
1797 | | /* We could not get the size and therefore we cannot know if there |
1798 | | really is a part of the file left to get, although the server will |
1799 | | close the connection when we start the connection so it will not |
1800 | | cause us any harm, not make us exit as nicely. */ |
1801 | 51 | } |
1802 | 201 | else { |
1803 | | /* We got a file size report, so we check that there actually is a |
1804 | | part of the file left to get, or else we go home. */ |
1805 | 201 | if(data->state.resume_from < 0) { |
1806 | | /* We are supposed to download the last abs(from) bytes */ |
1807 | 108 | if(filesize < -data->state.resume_from) { |
1808 | 74 | failf(data, "Offset (%" FMT_OFF_T |
1809 | 74 | ") was beyond file size (%" FMT_OFF_T ")", |
1810 | 74 | data->state.resume_from, filesize); |
1811 | 74 | return CURLE_BAD_DOWNLOAD_RESUME; |
1812 | 74 | } |
1813 | | /* convert to size to download */ |
1814 | 34 | ftp->downloadsize = -data->state.resume_from; |
1815 | | /* download from where? */ |
1816 | 34 | data->state.resume_from = filesize - ftp->downloadsize; |
1817 | 34 | } |
1818 | 93 | else { |
1819 | 93 | if(filesize < data->state.resume_from) { |
1820 | 14 | failf(data, "Offset (%" FMT_OFF_T |
1821 | 14 | ") was beyond file size (%" FMT_OFF_T ")", |
1822 | 14 | data->state.resume_from, filesize); |
1823 | 14 | return CURLE_BAD_DOWNLOAD_RESUME; |
1824 | 14 | } |
1825 | | /* Now store the number of bytes we are expected to download */ |
1826 | 79 | ftp->downloadsize = filesize - data->state.resume_from; |
1827 | 79 | } |
1828 | 201 | } |
1829 | | |
1830 | 164 | if(ftp->downloadsize == 0) { |
1831 | | /* no data to transfer */ |
1832 | 2 | Curl_xfer_setup_nop(data); |
1833 | 2 | infof(data, "File already completely downloaded"); |
1834 | | |
1835 | | /* Set ->transfer so that we will not get any error in ftp_done() |
1836 | | * because we did not transfer the any file */ |
1837 | 2 | ftp->transfer = PPTRANSFER_NONE; |
1838 | 2 | ftp_state(data, ftpc, FTP_STOP); |
1839 | 2 | return CURLE_OK; |
1840 | 2 | } |
1841 | | |
1842 | | /* Set resume file transfer offset */ |
1843 | 162 | infof(data, "Instructs server to resume from offset %" FMT_OFF_T, |
1844 | 162 | data->state.resume_from); |
1845 | | |
1846 | 162 | result = Curl_pp_sendf(data, &ftpc->pp, "REST %" FMT_OFF_T, |
1847 | 162 | data->state.resume_from); |
1848 | 162 | if(!result) |
1849 | 162 | ftp_state(data, ftpc, FTP_RETR_REST); |
1850 | 162 | } |
1851 | 379 | else { |
1852 | | /* no resume */ |
1853 | 379 | result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file); |
1854 | 379 | if(!result) |
1855 | 379 | ftp_state(data, ftpc, FTP_RETR); |
1856 | 379 | } |
1857 | | |
1858 | 541 | return result; |
1859 | 631 | } |
1860 | | |
1861 | | static CURLcode ftp_state_quote(struct Curl_easy *data, |
1862 | | struct ftp_conn *ftpc, |
1863 | | struct FTP *ftp, |
1864 | | bool init, |
1865 | | ftpstate instate) |
1866 | 2.76k | { |
1867 | 2.76k | CURLcode result = CURLE_OK; |
1868 | 2.76k | bool quote = FALSE; |
1869 | 2.76k | struct curl_slist *item; |
1870 | | |
1871 | 2.76k | switch(instate) { |
1872 | 1.90k | case FTP_QUOTE: |
1873 | 1.90k | default: |
1874 | 1.90k | item = data->set.quote; |
1875 | 1.90k | break; |
1876 | 781 | case FTP_RETR_PREQUOTE: |
1877 | 864 | case FTP_STOR_PREQUOTE: |
1878 | 864 | case FTP_LIST_PREQUOTE: |
1879 | 864 | item = data->set.prequote; |
1880 | 864 | break; |
1881 | 0 | case FTP_POSTQUOTE: |
1882 | 0 | item = data->set.postquote; |
1883 | 0 | break; |
1884 | 2.76k | } |
1885 | | |
1886 | | /* |
1887 | | * This state uses: |
1888 | | * 'count1' to iterate over the commands to send |
1889 | | * 'count2' to store whether to allow commands to fail |
1890 | | */ |
1891 | | |
1892 | 2.76k | if(init) |
1893 | 2.76k | ftpc->count1 = 0; |
1894 | 0 | else |
1895 | 0 | ftpc->count1++; |
1896 | | |
1897 | 2.76k | if(item) { |
1898 | 0 | int i = 0; |
1899 | | |
1900 | | /* Skip count1 items in the linked list */ |
1901 | 0 | while((i < ftpc->count1) && item) { |
1902 | 0 | item = item->next; |
1903 | 0 | i++; |
1904 | 0 | } |
1905 | 0 | if(item) { |
1906 | 0 | const char *cmd = item->data; |
1907 | 0 | if(cmd[0] == '*') { |
1908 | 0 | cmd++; |
1909 | 0 | ftpc->count2 = 1; /* the sent command is allowed to fail */ |
1910 | 0 | } |
1911 | 0 | else |
1912 | 0 | ftpc->count2 = 0; /* failure means cancel operation */ |
1913 | |
|
1914 | 0 | result = Curl_pp_sendf(data, &ftpc->pp, "%s", cmd); |
1915 | 0 | if(result) |
1916 | 0 | return result; |
1917 | 0 | ftp_state(data, ftpc, instate); |
1918 | 0 | quote = TRUE; |
1919 | 0 | } |
1920 | 0 | } |
1921 | | |
1922 | 2.76k | if(!quote) { |
1923 | | /* No more quote to send, continue to ... */ |
1924 | 2.76k | switch(instate) { |
1925 | 1.90k | case FTP_QUOTE: |
1926 | 1.90k | default: |
1927 | 1.90k | result = ftp_state_cwd(data, ftpc, ftp); |
1928 | 1.90k | break; |
1929 | 781 | case FTP_RETR_PREQUOTE: |
1930 | 781 | if(ftp->transfer != PPTRANSFER_BODY) |
1931 | 107 | ftp_state(data, ftpc, FTP_STOP); |
1932 | 674 | else { |
1933 | 674 | if(ftpc->known_filesize != -1) { |
1934 | 0 | Curl_pgrsSetDownloadSize(data, ftpc->known_filesize); |
1935 | 0 | result = ftp_state_retr(data, ftpc, ftp, ftpc->known_filesize); |
1936 | 0 | } |
1937 | 674 | else { |
1938 | 674 | if(data->set.ignorecl || data->state.prefer_ascii) { |
1939 | | /* 'ignorecl' is used to support download of growing files. It |
1940 | | prevents the state machine from requesting the file size from |
1941 | | the server. With an unknown file size the download continues |
1942 | | until the server terminates it, otherwise the client stops if |
1943 | | the received byte count exceeds the reported file size. Set |
1944 | | option CURLOPT_IGNORE_CONTENT_LENGTH to 1 to enable this |
1945 | | behavior. |
1946 | | |
1947 | | In addition: asking for the size for 'TYPE A' transfers is not |
1948 | | constructive since servers do not report the converted size. |
1949 | | Thus, skip it. |
1950 | | */ |
1951 | 3 | result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file); |
1952 | 3 | if(!result) |
1953 | 3 | ftp_state(data, ftpc, FTP_RETR); |
1954 | 3 | } |
1955 | 671 | else { |
1956 | 671 | result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file); |
1957 | 671 | if(!result) |
1958 | 671 | ftp_state(data, ftpc, FTP_RETR_SIZE); |
1959 | 671 | } |
1960 | 674 | } |
1961 | 674 | } |
1962 | 781 | break; |
1963 | 83 | case FTP_STOR_PREQUOTE: |
1964 | 83 | result = ftp_state_ul_setup(data, ftpc, ftp, FALSE); |
1965 | 83 | break; |
1966 | 0 | case FTP_POSTQUOTE: |
1967 | 0 | break; |
1968 | 0 | case FTP_LIST_PREQUOTE: |
1969 | 0 | ftp_state(data, ftpc, FTP_LIST_TYPE); |
1970 | 0 | result = ftp_state_list(data, ftpc, ftp); |
1971 | 0 | break; |
1972 | 2.76k | } |
1973 | 2.76k | } |
1974 | | |
1975 | 2.76k | return result; |
1976 | 2.76k | } |
1977 | | |
1978 | | /* called from ftp_state_pasv_resp to switch to PASV in case of EPSV |
1979 | | problems */ |
1980 | | static CURLcode ftp_epsv_disable(struct Curl_easy *data, |
1981 | | struct ftp_conn *ftpc, |
1982 | | struct connectdata *conn) |
1983 | 735 | { |
1984 | 735 | CURLcode result = CURLE_OK; |
1985 | | |
1986 | 735 | if(conn->bits.ipv6 |
1987 | 0 | #ifndef CURL_DISABLE_PROXY |
1988 | 0 | && !(conn->bits.tunnel_proxy || conn->bits.socksproxy) |
1989 | 735 | #endif |
1990 | 735 | ) { |
1991 | | /* We cannot disable EPSV when doing IPv6, so this is instead a fail */ |
1992 | 0 | failf(data, "Failed EPSV attempt, exiting"); |
1993 | 0 | return CURLE_WEIRD_SERVER_REPLY; |
1994 | 0 | } |
1995 | | |
1996 | 735 | infof(data, "Failed EPSV attempt. Disabling EPSV"); |
1997 | | /* disable it for next transfer */ |
1998 | 735 | conn->bits.ftp_use_epsv = FALSE; |
1999 | 735 | close_secondarysocket(data, ftpc); |
2000 | 735 | data->state.errorbuf = FALSE; /* allow error message to get |
2001 | | rewritten */ |
2002 | 735 | result = Curl_pp_sendf(data, &ftpc->pp, "%s", "PASV"); |
2003 | 735 | if(!result) { |
2004 | 735 | ftpc->count1++; |
2005 | | /* remain in/go to the FTP_PASV state */ |
2006 | 735 | ftp_state(data, ftpc, FTP_PASV); |
2007 | 735 | } |
2008 | 735 | return result; |
2009 | 735 | } |
2010 | | |
2011 | | static CURLcode ftp_control_addr_dup(struct Curl_easy *data, char **newhostp) |
2012 | 1.50k | { |
2013 | 1.50k | struct connectdata *conn = data->conn; |
2014 | 1.50k | struct ip_quadruple ipquad; |
2015 | 1.50k | bool is_ipv6; |
2016 | | |
2017 | | /* Returns the control connection IP address. |
2018 | | If a proxy tunnel is used, returns the original hostname instead, because |
2019 | | the effective control connection address is the proxy address, |
2020 | | not the ftp host. */ |
2021 | 1.50k | #ifndef CURL_DISABLE_PROXY |
2022 | 1.50k | if(conn->bits.tunnel_proxy || conn->bits.socksproxy) |
2023 | 6 | *newhostp = curlx_strdup(conn->host.name); |
2024 | 1.49k | else |
2025 | 1.49k | #endif |
2026 | 1.49k | if(!Curl_conn_get_ip_info(data, conn, FIRSTSOCKET, &is_ipv6, &ipquad) && |
2027 | 1.49k | *ipquad.remote_ip) |
2028 | 1.49k | *newhostp = curlx_strdup(ipquad.remote_ip); |
2029 | 3 | else { |
2030 | | /* failed to get the remote_ip of the DATA connection */ |
2031 | 3 | failf(data, "unable to get peername of DATA connection"); |
2032 | 3 | *newhostp = NULL; |
2033 | 3 | return CURLE_FTP_CANT_GET_HOST; |
2034 | 3 | } |
2035 | 1.50k | return *newhostp ? CURLE_OK : CURLE_OUT_OF_MEMORY; |
2036 | 1.50k | } |
2037 | | |
2038 | | static bool match_pasv_6nums(const char *p, |
2039 | | unsigned int *array) /* 6 numbers */ |
2040 | 21.4k | { |
2041 | 21.4k | int i; |
2042 | 30.9k | for(i = 0; i < 6; i++) { |
2043 | 30.2k | curl_off_t num; |
2044 | 30.2k | if(i) { |
2045 | 8.82k | if(*p != ',') |
2046 | 4.51k | return FALSE; |
2047 | 4.31k | p++; |
2048 | 4.31k | } |
2049 | 25.7k | if(curlx_str_number(&p, &num, 0xff)) |
2050 | 16.2k | return FALSE; |
2051 | 9.50k | array[i] = (unsigned int)num; |
2052 | 9.50k | } |
2053 | 678 | return TRUE; |
2054 | 21.4k | } |
2055 | | |
2056 | | static CURLcode ftp_state_pasv_resp(struct Curl_easy *data, |
2057 | | struct ftp_conn *ftpc, |
2058 | | int ftpcode) |
2059 | 2.29k | { |
2060 | 2.29k | struct connectdata *conn = data->conn; |
2061 | 2.29k | CURLcode result; |
2062 | 2.29k | struct Curl_dns_entry *dns = NULL; |
2063 | 2.29k | unsigned short connectport; /* the local port connect() should use! */ |
2064 | 2.29k | const struct pingpong *pp = &ftpc->pp; |
2065 | 2.29k | char *newhost = NULL; |
2066 | 2.29k | unsigned short newport = 0; |
2067 | 2.29k | const char *str = curlx_dyn_ptr(&pp->recvbuf) + 4; /* start on the first |
2068 | | letter */ |
2069 | 2.29k | if((ftpc->count1 == 0) && |
2070 | 1.57k | (ftpcode == 229)) { |
2071 | | /* positive EPSV response */ |
2072 | 843 | const char *ptr = strchr(str, '('); |
2073 | 843 | if(ptr) { |
2074 | 841 | char sep; |
2075 | 841 | ptr++; |
2076 | | /* |||12345| */ |
2077 | 841 | sep = ptr[0]; |
2078 | 841 | if((ptr[1] == sep) && (ptr[2] == sep) && ISDIGIT(ptr[3])) { |
2079 | 828 | const char *p = &ptr[3]; |
2080 | 828 | curl_off_t num; |
2081 | 828 | if(curlx_str_number(&p, &num, 0xffff) || (*p != sep)) { |
2082 | 2 | failf(data, "Illegal port number in EPSV reply"); |
2083 | 2 | return CURLE_FTP_WEIRD_PASV_REPLY; |
2084 | 2 | } |
2085 | 826 | newport = (unsigned short)num; |
2086 | 826 | result = ftp_control_addr_dup(data, &newhost); |
2087 | 826 | if(result) |
2088 | 1 | return result; |
2089 | 826 | } |
2090 | 13 | else |
2091 | 13 | ptr = NULL; |
2092 | 841 | } |
2093 | 840 | if(!ptr) { |
2094 | 15 | failf(data, "Weirdly formatted EPSV reply"); |
2095 | 15 | return CURLE_FTP_WEIRD_PASV_REPLY; |
2096 | 15 | } |
2097 | 840 | } |
2098 | 1.45k | else if((ftpc->count1 == 1) && |
2099 | 724 | (ftpcode == 227)) { |
2100 | | /* positive PASV response */ |
2101 | 709 | unsigned int ip[6]; |
2102 | | |
2103 | | /* |
2104 | | * Scan for a sequence of six comma-separated numbers and use them as |
2105 | | * IP+port indicators. |
2106 | | * |
2107 | | * Found reply-strings include: |
2108 | | * "227 Entering Passive Mode (127,0,0,1,4,51)" |
2109 | | * "227 Data transfer will passively listen to 127,0,0,1,4,51" |
2110 | | * "227 Entering passive mode. 127,0,0,1,4,51" |
2111 | | */ |
2112 | 21.4k | while(*str) { |
2113 | 21.4k | if(match_pasv_6nums(str, ip)) |
2114 | 678 | break; |
2115 | 20.7k | str++; |
2116 | 20.7k | } |
2117 | | |
2118 | 709 | if(!*str) { |
2119 | 31 | failf(data, "Could not interpret the 227-response"); |
2120 | 31 | return CURLE_FTP_WEIRD_227_FORMAT; |
2121 | 31 | } |
2122 | | |
2123 | | /* we got OK from server */ |
2124 | 678 | if(data->set.ftp_skip_ip) { |
2125 | | /* told to ignore the remotely given IP but instead use the host we used |
2126 | | for the control connection */ |
2127 | 677 | infof(data, "Skip %u.%u.%u.%u for data connection, reuse %s instead", |
2128 | 677 | ip[0], ip[1], ip[2], ip[3], conn->host.name); |
2129 | 677 | result = ftp_control_addr_dup(data, &newhost); |
2130 | 677 | if(result) |
2131 | 2 | return result; |
2132 | 677 | } |
2133 | 1 | else |
2134 | 1 | newhost = curl_maprintf("%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]); |
2135 | | |
2136 | 676 | if(!newhost) |
2137 | 0 | return CURLE_OUT_OF_MEMORY; |
2138 | | |
2139 | 676 | newport = (unsigned short)(((ip[4] << 8) + ip[5]) & 0xffff); |
2140 | 676 | } |
2141 | 742 | else if(ftpc->count1 == 0) { |
2142 | | /* EPSV failed, move on to PASV */ |
2143 | 727 | return ftp_epsv_disable(data, ftpc, conn); |
2144 | 727 | } |
2145 | 15 | else { |
2146 | 15 | failf(data, "Bad PASV/EPSV response: %03d", ftpcode); |
2147 | 15 | return CURLE_FTP_WEIRD_PASV_REPLY; |
2148 | 15 | } |
2149 | | |
2150 | 1.50k | #ifndef CURL_DISABLE_PROXY |
2151 | 1.50k | if(conn->bits.proxy) { |
2152 | | /* This connection uses a proxy and we need to connect to the proxy again |
2153 | | * here. We do not want to rely on a former host lookup that might have |
2154 | | * expired now, instead we remake the lookup here and now! */ |
2155 | 6 | struct ip_quadruple ipquad; |
2156 | 6 | bool is_ipv6; |
2157 | 6 | const char * const host_name = conn->bits.socksproxy ? |
2158 | 6 | conn->socks_proxy.host.name : conn->http_proxy.host.name; |
2159 | | |
2160 | 6 | result = Curl_conn_get_ip_info(data, data->conn, FIRSTSOCKET, |
2161 | 6 | &is_ipv6, &ipquad); |
2162 | 6 | if(result) |
2163 | 0 | goto error; |
2164 | | |
2165 | 6 | (void)Curl_resolv_blocking( |
2166 | 6 | data, is_ipv6 ? CURL_DNSQ_AAAA : CURL_DNSQ_A, |
2167 | 6 | host_name, ipquad.remote_port, Curl_conn_get_transport(data, conn), |
2168 | 6 | &dns); |
2169 | | /* we connect to the proxy's port */ |
2170 | 6 | connectport = (unsigned short)ipquad.remote_port; |
2171 | | |
2172 | 6 | if(!dns) { |
2173 | 0 | failf(data, "cannot resolve proxy host %s:%hu", host_name, connectport); |
2174 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
2175 | 0 | goto error; |
2176 | 0 | } |
2177 | 6 | } |
2178 | 1.49k | else |
2179 | 1.49k | #endif |
2180 | 1.49k | { |
2181 | | /* normal, direct, ftp connection */ |
2182 | 1.49k | DEBUGASSERT(newhost); |
2183 | | |
2184 | | /* postponed address resolution in case of tcp fastopen */ |
2185 | 1.49k | if(conn->bits.tcp_fastopen && !conn->bits.reuse && !newhost[0]) { |
2186 | 0 | curlx_free(newhost); |
2187 | 0 | result = ftp_control_addr_dup(data, &newhost); |
2188 | 0 | if(result) |
2189 | 0 | goto error; |
2190 | 0 | } |
2191 | | |
2192 | 1.49k | (void)Curl_resolv_blocking( |
2193 | 1.49k | data, Curl_resolv_dns_queries(data, conn->ip_version), |
2194 | 1.49k | newhost, newport, Curl_conn_get_transport(data, conn), &dns); |
2195 | 1.49k | connectport = newport; /* we connect to the remote port */ |
2196 | | |
2197 | 1.49k | if(!dns) { |
2198 | 1 | failf(data, "cannot resolve new host %s:%hu", newhost, connectport); |
2199 | 1 | result = CURLE_FTP_CANT_GET_HOST; |
2200 | 1 | goto error; |
2201 | 1 | } |
2202 | 1.49k | } |
2203 | | |
2204 | 1.50k | DEBUGASSERT(newhost); |
2205 | 1.50k | curlx_free(conn->secondaryhostname); |
2206 | 1.50k | conn->secondary_port = newport; |
2207 | 1.50k | conn->secondaryhostname = newhost; |
2208 | 1.50k | newhost = NULL; |
2209 | | |
2210 | 1.50k | result = Curl_conn_setup(data, conn, SECONDARYSOCKET, dns, |
2211 | 1.50k | conn->bits.ftp_use_data_ssl ? |
2212 | 1.50k | CURL_CF_SSL_ENABLE : CURL_CF_SSL_DISABLE); |
2213 | | |
2214 | 1.50k | if(result) { |
2215 | 0 | if((result != CURLE_OUT_OF_MEMORY) && |
2216 | 0 | (ftpc->count1 == 0) && (ftpcode == 229)) { |
2217 | 0 | result = ftp_epsv_disable(data, ftpc, conn); |
2218 | 0 | } |
2219 | 0 | goto error; |
2220 | 0 | } |
2221 | | |
2222 | | /* |
2223 | | * When this is used from the multi interface, this might have returned with |
2224 | | * the 'connected' set to FALSE and thus we are now awaiting a non-blocking |
2225 | | * connect to connect. |
2226 | | */ |
2227 | | |
2228 | 1.50k | #ifdef CURLVERBOSE |
2229 | 1.50k | if(data->set.verbose) { |
2230 | | /* Dump information about this second connection when we have issued a PASV |
2231 | | * command before and thus we have connected to a possibly new IP address. |
2232 | | */ |
2233 | 0 | char buf[256]; |
2234 | 0 | Curl_printable_address(dns->addr, buf, sizeof(buf)); |
2235 | 0 | infof(data, "Connecting to %s (%s) port %d", |
2236 | 0 | conn->secondaryhostname, buf, connectport); |
2237 | 0 | } |
2238 | 1.50k | #endif |
2239 | | |
2240 | 1.50k | conn->bits.do_more = TRUE; |
2241 | 1.50k | ftp_state(data, ftpc, FTP_STOP); /* this phase is completed */ |
2242 | | |
2243 | 1.50k | error: |
2244 | 1.50k | Curl_dns_entry_unlink(data, &dns); |
2245 | 1.50k | curlx_free(newhost); |
2246 | 1.50k | return result; |
2247 | 1.50k | } |
2248 | | |
2249 | | /* called repeatedly until done from multi.c */ |
2250 | | static CURLcode ftp_statemach(struct Curl_easy *data, |
2251 | | struct ftp_conn *ftpc, |
2252 | | bool *done) |
2253 | 18.3k | { |
2254 | 18.3k | CURLcode result = Curl_pp_statemach(data, &ftpc->pp, FALSE, FALSE); |
2255 | | |
2256 | | /* Check for the state outside of the Curl_socket_check() return code checks |
2257 | | since at times we are in fact already in this state when this function |
2258 | | gets called. */ |
2259 | 18.3k | *done = (ftpc->state == FTP_STOP); |
2260 | | |
2261 | 18.3k | return result; |
2262 | 18.3k | } |
2263 | | |
2264 | | /* |
2265 | | * ftp_do_more() |
2266 | | * |
2267 | | * This function shall be called when the second FTP (data) connection is |
2268 | | * connected. |
2269 | | * |
2270 | | * 'complete' can return 0 for incomplete, 1 for done and -1 for go back |
2271 | | * (which is for when PASV is being sent to retry a failed EPSV). |
2272 | | */ |
2273 | | static CURLcode ftp_do_more(struct Curl_easy *data, int *completep) |
2274 | 4.04k | { |
2275 | 4.04k | struct connectdata *conn = data->conn; |
2276 | 4.04k | struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN); |
2277 | 4.04k | struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY); |
2278 | 4.04k | CURLcode result = CURLE_OK; |
2279 | 4.04k | bool connected = FALSE; |
2280 | 4.04k | bool complete = FALSE; |
2281 | | /* the ftp struct is inited in ftp_connect(). If we are connecting to an HTTP |
2282 | | * proxy then the state will not be valid until after that connection is |
2283 | | * complete */ |
2284 | | |
2285 | 4.04k | if(!ftpc || !ftp) |
2286 | 0 | return CURLE_FAILED_INIT; |
2287 | | |
2288 | 4.04k | *completep = 0; /* default to stay in the state */ |
2289 | | |
2290 | | /* if the second connection has been set up, try to connect it fully |
2291 | | * to the remote host. This may not complete at this time, for several |
2292 | | * reasons: |
2293 | | * - we do EPTR and the server will not connect to our listen socket |
2294 | | * until we send more FTP commands |
2295 | | * - an SSL filter is in place and the server will not start the TLS |
2296 | | * handshake until we send more FTP commands |
2297 | | */ |
2298 | 4.04k | if(conn->cfilter[SECONDARYSOCKET]) { |
2299 | 4.04k | bool is_eptr = Curl_conn_is_tcp_listen(data, SECONDARYSOCKET); |
2300 | 4.04k | result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &connected); |
2301 | 4.04k | if(result == CURLE_OUT_OF_MEMORY) |
2302 | 0 | return result; |
2303 | 4.04k | if(result || (!connected && !is_eptr && |
2304 | 10 | !Curl_conn_is_ip_connected(data, SECONDARYSOCKET))) { |
2305 | 10 | if(result && !is_eptr && (ftpc->count1 == 0)) { |
2306 | 8 | *completep = -1; /* go back to DOING please */ |
2307 | | /* this is a EPSV connect failing, try PASV instead */ |
2308 | 8 | return ftp_epsv_disable(data, ftpc, conn); |
2309 | 8 | } |
2310 | 2 | return result; |
2311 | 10 | } |
2312 | 4.04k | } |
2313 | | |
2314 | 4.03k | if(ftpc->state) { |
2315 | | /* already in a state so skip the initial commands. |
2316 | | They are only done to kickstart the do_more state */ |
2317 | 2.54k | result = ftp_statemach(data, ftpc, &complete); |
2318 | | |
2319 | 2.54k | *completep = (int)complete; |
2320 | | |
2321 | | /* if we got an error or if we do not wait for a data connection return |
2322 | | immediately */ |
2323 | 2.54k | if(result || !ftpc->wait_data_conn) |
2324 | 2.54k | return result; |
2325 | | |
2326 | | /* if we reach the end of the FTP state machine here, *complete will be |
2327 | | TRUE but so is ftpc->wait_data_conn, which says we need to wait for the |
2328 | | data connection and therefore we are not actually complete */ |
2329 | 0 | *completep = 0; |
2330 | 0 | } |
2331 | | |
2332 | 1.49k | if(ftp->transfer <= PPTRANSFER_INFO) { |
2333 | | /* a transfer is about to take place, or if not a filename was given so we |
2334 | | will do a SIZE on it later and then we need the right TYPE first */ |
2335 | | |
2336 | 1.49k | if(ftpc->wait_data_conn) { |
2337 | 0 | bool serv_conned; |
2338 | |
|
2339 | 0 | result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &serv_conned); |
2340 | 0 | if(result) |
2341 | 0 | return result; /* Failed to accept data connection */ |
2342 | | |
2343 | 0 | if(serv_conned) { |
2344 | | /* It looks data connection is established */ |
2345 | 0 | ftpc->wait_data_conn = FALSE; |
2346 | 0 | result = ftp_initiate_transfer(data, ftpc); |
2347 | |
|
2348 | 0 | if(result) |
2349 | 0 | return result; |
2350 | | |
2351 | 0 | *completep = 1; /* this state is now complete when the server has |
2352 | | connected back to us */ |
2353 | 0 | } |
2354 | 0 | else { |
2355 | 0 | result = ftp_check_ctrl_on_data_wait(data, ftpc); |
2356 | 0 | if(result) |
2357 | 0 | return result; |
2358 | 0 | } |
2359 | 0 | } |
2360 | 1.49k | else if(data->state.upload) { |
2361 | 86 | result = ftp_nb_type(data, ftpc, ftp, (bool)data->state.prefer_ascii, |
2362 | 86 | FTP_STOR_TYPE); |
2363 | 86 | if(result) |
2364 | 0 | return result; |
2365 | | |
2366 | 86 | result = ftp_statemach(data, ftpc, &complete); |
2367 | | /* ftp_nb_type() might have skipped sending `TYPE A|I` when not |
2368 | | * deemed necessary and directly sent `STORE name`. If this was |
2369 | | * then complete, but we are still waiting on the data connection, |
2370 | | * the transfer has not been initiated yet. */ |
2371 | 86 | *completep = (int)(ftpc->wait_data_conn ? 0 : complete); |
2372 | 86 | } |
2373 | 1.40k | else { |
2374 | | /* download */ |
2375 | 1.40k | ftp->downloadsize = -1; /* unknown as of yet */ |
2376 | | |
2377 | 1.40k | result = Curl_range(data); |
2378 | | |
2379 | 1.40k | if(result == CURLE_OK && data->req.maxdownload >= 0) { |
2380 | | /* Do not check for successful transfer */ |
2381 | 345 | ftpc->dont_check = TRUE; |
2382 | 345 | } |
2383 | | |
2384 | 1.40k | if(result) |
2385 | 59 | ; |
2386 | 1.34k | else if((data->state.list_only || !ftpc->file) && |
2387 | 646 | !(data->set.prequote)) { |
2388 | | /* The specified path ends with a slash, and therefore we think this |
2389 | | is a directory that is requested, use LIST. Before that, we also |
2390 | | need to set ASCII transfer mode. */ |
2391 | | |
2392 | | /* Only if a body transfer was requested. */ |
2393 | 646 | if(ftp->transfer == PPTRANSFER_BODY) { |
2394 | 646 | result = ftp_nb_type(data, ftpc, ftp, TRUE, FTP_LIST_TYPE); |
2395 | 646 | if(result) |
2396 | 0 | return result; |
2397 | 646 | } |
2398 | | /* otherwise fall through */ |
2399 | 646 | } |
2400 | 700 | else { |
2401 | 700 | if(data->set.prequote && !ftpc->file) { |
2402 | 0 | result = ftp_nb_type(data, ftpc, ftp, TRUE, |
2403 | 0 | FTP_RETR_LIST_TYPE); |
2404 | 0 | } |
2405 | 700 | else { |
2406 | 700 | result = ftp_nb_type(data, ftpc, ftp, (bool)data->state.prefer_ascii, |
2407 | 700 | FTP_RETR_TYPE); |
2408 | 700 | } |
2409 | 700 | if(result) |
2410 | 0 | return result; |
2411 | 700 | } |
2412 | | |
2413 | 1.40k | result = ftp_statemach(data, ftpc, &complete); |
2414 | 1.40k | *completep = (int)complete; |
2415 | 1.40k | } |
2416 | 1.49k | return result; |
2417 | 1.49k | } |
2418 | | |
2419 | | /* no data to transfer */ |
2420 | 0 | Curl_xfer_setup_nop(data); |
2421 | |
|
2422 | 0 | if(!ftpc->wait_data_conn) { |
2423 | | /* no waiting for the data connection so this is now complete */ |
2424 | 0 | *completep = 1; |
2425 | 0 | CURL_TRC_FTP(data, "[%s] DO-MORE phase ends with %d", FTP_CSTATE(ftpc), |
2426 | 0 | (int)result); |
2427 | 0 | } |
2428 | |
|
2429 | 0 | return result; |
2430 | 1.49k | } |
2431 | | |
2432 | | /* call this when the DO phase has completed */ |
2433 | | static CURLcode ftp_dophase_done(struct Curl_easy *data, |
2434 | | struct ftp_conn *ftpc, |
2435 | | struct FTP *ftp, |
2436 | | bool connected) |
2437 | 1.60k | { |
2438 | 1.60k | if(connected) { |
2439 | 0 | int completed; |
2440 | 0 | CURLcode result = ftp_do_more(data, &completed); |
2441 | |
|
2442 | 0 | if(result) { |
2443 | 0 | close_secondarysocket(data, ftpc); |
2444 | 0 | return result; |
2445 | 0 | } |
2446 | 0 | } |
2447 | | |
2448 | 1.60k | if(ftp->transfer != PPTRANSFER_BODY) |
2449 | | /* no data to transfer */ |
2450 | 107 | Curl_xfer_setup_nop(data); |
2451 | 1.50k | else if(!connected) |
2452 | | /* since we did not connect now, we want do_more to get called */ |
2453 | 1.50k | data->conn->bits.do_more = TRUE; |
2454 | | |
2455 | 1.60k | ftpc->ctl_valid = TRUE; /* seems good */ |
2456 | | |
2457 | 1.60k | return CURLE_OK; |
2458 | 1.60k | } |
2459 | | |
2460 | | static CURLcode ftp_state_port_resp(struct Curl_easy *data, |
2461 | | struct ftp_conn *ftpc, |
2462 | | struct FTP *ftp, |
2463 | | int ftpcode) |
2464 | 0 | { |
2465 | 0 | struct connectdata *conn = data->conn; |
2466 | 0 | ftpport fcmd = (ftpport)ftpc->count1; |
2467 | 0 | CURLcode result = CURLE_OK; |
2468 | | |
2469 | | /* The FTP spec tells a positive response should have code 200. |
2470 | | Be more permissive here to tolerate deviant servers. */ |
2471 | 0 | if(ftpcode / 100 != 2) { |
2472 | | /* the command failed */ |
2473 | |
|
2474 | 0 | if(EPRT == fcmd) { |
2475 | 0 | infof(data, "disabling EPRT usage"); |
2476 | 0 | conn->bits.ftp_use_eprt = FALSE; |
2477 | 0 | } |
2478 | 0 | fcmd++; |
2479 | |
|
2480 | 0 | if(fcmd == DONE) { |
2481 | 0 | failf(data, "Failed to do PORT"); |
2482 | 0 | result = CURLE_FTP_PORT_FAILED; |
2483 | 0 | } |
2484 | 0 | else |
2485 | | /* try next */ |
2486 | 0 | result = ftp_state_use_port(data, ftpc, fcmd); |
2487 | 0 | } |
2488 | 0 | else { |
2489 | 0 | infof(data, "Connect data stream actively"); |
2490 | 0 | ftp_state(data, ftpc, FTP_STOP); /* end of DO phase */ |
2491 | 0 | result = ftp_dophase_done(data, ftpc, ftp, FALSE); |
2492 | 0 | } |
2493 | |
|
2494 | 0 | return result; |
2495 | 0 | } |
2496 | | |
2497 | | /* return TRUE on error, FALSE on success */ |
2498 | | static bool twodigit(const char *p, int *val) |
2499 | 187 | { |
2500 | 187 | if(!ISDIGIT(p[0]) || !ISDIGIT(p[1])) |
2501 | 27 | return TRUE; |
2502 | | /* curlx_hexval() works fine here since we make sure it is decimal above */ |
2503 | 160 | *val = (curlx_hexval(p[0]) * 10) + curlx_hexval(p[1]); |
2504 | 160 | return FALSE; |
2505 | 187 | } |
2506 | | |
2507 | | /* |
2508 | | * @unittest 1668 |
2509 | | */ |
2510 | | UNITTEST bool ftp_213_date(const char *p, int *year, int *month, int *day, |
2511 | | int *hour, int *minute, int *second); |
2512 | | UNITTEST bool ftp_213_date(const char *p, int *year, int *month, int *day, |
2513 | | int *hour, int *minute, int *second) |
2514 | 44 | { |
2515 | 44 | int century; |
2516 | 44 | if((strlen(p) < 14) || twodigit(&p[0], ¢ury) || twodigit(&p[2], year) || |
2517 | 30 | twodigit(&p[4], month) || twodigit(&p[6], day) || |
2518 | 22 | twodigit(&p[8], hour) || twodigit(&p[10], minute) || |
2519 | 15 | twodigit(&p[12], second)) |
2520 | 30 | return FALSE; |
2521 | | |
2522 | 14 | *year += century * 100; |
2523 | 14 | if((*month > 12) || (*day > 31) || (*hour > 23) || (*minute > 59) || |
2524 | 10 | (*second > 60)) |
2525 | 5 | return FALSE; |
2526 | 9 | return TRUE; |
2527 | 14 | } |
2528 | | |
2529 | | static CURLcode client_write_header(struct Curl_easy *data, |
2530 | | char *buf, size_t blen) |
2531 | 29 | { |
2532 | | /* Some replies from an FTP server are written to the client |
2533 | | * as CLIENTWRITE_HEADER, formatted as if they came from a |
2534 | | * HTTP conversation. |
2535 | | * In all protocols, CLIENTWRITE_HEADER data is only passed to |
2536 | | * the body write callback when data->set.include_header is set |
2537 | | * via CURLOPT_HEADER. |
2538 | | * For historic reasons, FTP never played this game and expects |
2539 | | * all its headers to do that always. Set that flag during the |
2540 | | * call to Curl_client_write() so it does the right thing. |
2541 | | * |
2542 | | * Notice that we cannot enable this flag for FTP in general, |
2543 | | * as an FTP transfer might involve an HTTP proxy connection and |
2544 | | * headers from CONNECT should not automatically be part of the |
2545 | | * output. */ |
2546 | 29 | CURLcode result; |
2547 | 29 | bool save = (bool)data->set.include_header; |
2548 | 29 | data->set.include_header = TRUE; |
2549 | 29 | result = Curl_client_write(data, CLIENTWRITE_HEADER, buf, blen); |
2550 | 29 | data->set.include_header = save; |
2551 | 29 | return result; |
2552 | 29 | } |
2553 | | |
2554 | | static CURLcode ftp_state_mdtm_resp(struct Curl_easy *data, |
2555 | | struct ftp_conn *ftpc, |
2556 | | struct FTP *ftp, |
2557 | | int ftpcode) |
2558 | 47 | { |
2559 | 47 | CURLcode result = CURLE_OK; |
2560 | | |
2561 | 47 | switch(ftpcode) { |
2562 | 44 | case 213: { |
2563 | | /* we got a time. Format should be: "YYYYMMDDHHMMSS[.sss]" where the |
2564 | | last .sss part is optional and means fractions of a second */ |
2565 | 44 | int year, month, day, hour, minute, second; |
2566 | 44 | struct pingpong *pp = &ftpc->pp; |
2567 | 44 | const char *resp = curlx_dyn_ptr(&pp->recvbuf) + 4; |
2568 | 44 | bool showtime = FALSE; |
2569 | 44 | if(ftp_213_date(resp, &year, &month, &day, &hour, &minute, &second)) { |
2570 | | /* we have a time, reformat it */ |
2571 | 9 | char timebuf[24]; |
2572 | 9 | curl_msnprintf(timebuf, sizeof(timebuf), |
2573 | 9 | "%04d%02d%02d %02d:%02d:%02d GMT", |
2574 | 9 | year, month, day, hour, minute, second); |
2575 | | /* now, convert this into a time() value: */ |
2576 | 9 | if(!Curl_getdate_capped(timebuf, &data->info.filetime)) |
2577 | 7 | showtime = TRUE; |
2578 | 9 | } |
2579 | | |
2580 | | /* If we asked for a time of the file and we actually got one as well, |
2581 | | we "emulate" an HTTP-style header in our output. */ |
2582 | | |
2583 | | #if defined(CURL_HAVE_DIAG) && (defined(__DJGPP__) || defined(__AMIGA__)) |
2584 | | #pragma GCC diagnostic push |
2585 | | /* 'time_t' is unsigned in MSDOS and AmigaOS. Silence: |
2586 | | warning: comparison of unsigned expression in '>= 0' is always true */ |
2587 | | #pragma GCC diagnostic ignored "-Wtype-limits" |
2588 | | #endif |
2589 | 44 | if(data->req.no_body && ftpc->file && |
2590 | 2 | data->set.get_filetime && showtime) { |
2591 | | #if defined(CURL_HAVE_DIAG) && (defined(__DJGPP__) || defined(__AMIGA__)) |
2592 | | #pragma GCC diagnostic pop |
2593 | | #endif |
2594 | 0 | char headerbuf[128]; |
2595 | 0 | int headerbuflen; |
2596 | 0 | time_t filetime = data->info.filetime; |
2597 | 0 | struct tm buffer; |
2598 | 0 | const struct tm *tm = &buffer; |
2599 | |
|
2600 | 0 | result = curlx_gmtime(filetime, &buffer); |
2601 | 0 | if(result) |
2602 | 0 | return result; |
2603 | | |
2604 | | /* format: "Tue, 15 Nov 1994 12:45:26" */ |
2605 | 0 | headerbuflen = |
2606 | 0 | curl_msnprintf(headerbuf, sizeof(headerbuf), |
2607 | 0 | "Last-Modified: %s, %02d %s %4d %02d:%02d:%02d " |
2608 | 0 | "GMT\r\n", |
2609 | 0 | Curl_wkday[tm->tm_wday ? tm->tm_wday-1 : 6], |
2610 | 0 | tm->tm_mday, |
2611 | 0 | Curl_month[tm->tm_mon], |
2612 | 0 | tm->tm_year + 1900, |
2613 | 0 | tm->tm_hour, |
2614 | 0 | tm->tm_min, |
2615 | 0 | tm->tm_sec); |
2616 | 0 | result = client_write_header(data, headerbuf, headerbuflen); |
2617 | 0 | if(result) |
2618 | 0 | return result; |
2619 | 0 | } /* end of a ridiculous amount of conditionals */ |
2620 | 44 | } |
2621 | 44 | break; |
2622 | 44 | default: |
2623 | 2 | infof(data, "unsupported MDTM reply format"); |
2624 | 2 | break; |
2625 | 1 | case 550: /* 550 is used for several different problems, e.g. |
2626 | | "No such file or directory" or "Permission denied". |
2627 | | It does not mean that the file does not exist at all. */ |
2628 | 1 | infof(data, "MDTM failed: file does not exist or permission problem," |
2629 | 1 | " continuing"); |
2630 | 1 | break; |
2631 | 47 | } |
2632 | | |
2633 | 47 | if(data->set.timecondition) { |
2634 | 45 | if((data->info.filetime > 0) && (data->set.timevalue > 0)) { |
2635 | 0 | switch(data->set.timecondition) { |
2636 | 0 | case CURL_TIMECOND_IFMODSINCE: |
2637 | 0 | default: |
2638 | 0 | if(data->info.filetime <= data->set.timevalue) { |
2639 | 0 | infof(data, "The requested document is not new enough"); |
2640 | 0 | ftp->transfer = PPTRANSFER_NONE; /* mark to not transfer data */ |
2641 | 0 | data->info.timecond = TRUE; |
2642 | 0 | ftp_state(data, ftpc, FTP_STOP); |
2643 | 0 | return CURLE_OK; |
2644 | 0 | } |
2645 | 0 | break; |
2646 | 0 | case CURL_TIMECOND_IFUNMODSINCE: |
2647 | 0 | if(data->info.filetime > data->set.timevalue) { |
2648 | 0 | infof(data, "The requested document is not old enough"); |
2649 | 0 | ftp->transfer = PPTRANSFER_NONE; /* mark to not transfer data */ |
2650 | 0 | data->info.timecond = TRUE; |
2651 | 0 | ftp_state(data, ftpc, FTP_STOP); |
2652 | 0 | return CURLE_OK; |
2653 | 0 | } |
2654 | 0 | break; |
2655 | 0 | } /* switch */ |
2656 | 0 | } |
2657 | 45 | else { |
2658 | 45 | infof(data, "Skipping time comparison"); |
2659 | 45 | } |
2660 | 45 | } |
2661 | | |
2662 | 47 | if(!result) |
2663 | 47 | result = ftp_state_type(data, ftpc, ftp); |
2664 | | |
2665 | 47 | return result; |
2666 | 47 | } |
2667 | | |
2668 | | static CURLcode ftp_state_type_resp(struct Curl_easy *data, |
2669 | | struct ftp_conn *ftpc, |
2670 | | struct FTP *ftp, |
2671 | | int ftpcode, |
2672 | | ftpstate instate) |
2673 | 1.43k | { |
2674 | 1.43k | CURLcode result = CURLE_OK; |
2675 | | |
2676 | 1.43k | if(ftpcode / 100 != 2) { |
2677 | | /* "sasserftpd" and "(u)r(x)bot ftpd" both responds with 226 after a |
2678 | | successful 'TYPE I'. While that is not as RFC959 says, it is still a |
2679 | | positive response code and we allow that. */ |
2680 | 76 | failf(data, "Could not set desired mode"); |
2681 | 76 | return CURLE_FTP_COULDNT_SET_TYPE; |
2682 | 76 | } |
2683 | 1.35k | if(ftpcode != 200) |
2684 | 1.29k | infof(data, "Got a %03d response code instead of the assumed 200", |
2685 | 1.35k | ftpcode); |
2686 | | |
2687 | 1.35k | if(instate == FTP_TYPE) |
2688 | 34 | result = ftp_state_size(data, ftpc, ftp); |
2689 | 1.32k | else if(instate == FTP_LIST_TYPE) |
2690 | 567 | result = ftp_state_list(data, ftpc, ftp); |
2691 | 757 | else if(instate == FTP_RETR_TYPE) |
2692 | 674 | result = ftp_state_retr_prequote(data, ftpc, ftp); |
2693 | 83 | else if(instate == FTP_STOR_TYPE) |
2694 | 83 | result = ftp_state_stor_prequote(data, ftpc, ftp); |
2695 | 0 | else if(instate == FTP_RETR_LIST_TYPE) |
2696 | 0 | result = ftp_state_list_prequote(data, ftpc, ftp); |
2697 | | |
2698 | 1.35k | return result; |
2699 | 1.43k | } |
2700 | | |
2701 | | static CURLcode ftp_state_size_resp(struct Curl_easy *data, |
2702 | | struct ftp_conn *ftpc, |
2703 | | struct FTP *ftp, |
2704 | | int ftpcode, |
2705 | | ftpstate instate) |
2706 | 699 | { |
2707 | 699 | CURLcode result = CURLE_OK; |
2708 | 699 | curl_off_t filesize = -1; |
2709 | 699 | const char *buf = curlx_dyn_ptr(&ftpc->pp.recvbuf); |
2710 | 699 | size_t len = ftpc->pp.nfinal; |
2711 | | |
2712 | | /* get the size from the ascii string: */ |
2713 | 699 | if(ftpcode == 213) { |
2714 | | /* To allow servers to prepend "rubbish" in the response string, we scan |
2715 | | for all the digits at the end of the response and parse only those as a |
2716 | | number. */ |
2717 | 568 | const char *start = &buf[4]; |
2718 | 568 | const char *fdigit = memchr(start, '\r', len - 4); |
2719 | 568 | if(fdigit) { |
2720 | 27 | fdigit--; |
2721 | 27 | if(*fdigit == '\n') |
2722 | 0 | fdigit--; |
2723 | 103 | while(ISDIGIT(fdigit[-1]) && (fdigit > start)) |
2724 | 76 | fdigit--; |
2725 | 27 | } |
2726 | 541 | else |
2727 | 541 | fdigit = start; |
2728 | 568 | if(curlx_str_number(&fdigit, &filesize, CURL_OFF_T_MAX)) |
2729 | 27 | filesize = -1; /* size remain unknown */ |
2730 | 568 | } |
2731 | 131 | else if(ftpcode == 550) { /* "No such file or directory" */ |
2732 | | /* allow a SIZE failure for (resumed) uploads, when probing what command |
2733 | | to use */ |
2734 | 1 | if(instate != FTP_STOR_SIZE) { |
2735 | 1 | failf(data, "The file does not exist"); |
2736 | 1 | return CURLE_REMOTE_FILE_NOT_FOUND; |
2737 | 1 | } |
2738 | 1 | } |
2739 | | |
2740 | 698 | if(instate == FTP_SIZE) { |
2741 | 32 | if(filesize != -1) { |
2742 | 29 | char clbuf[128]; |
2743 | 29 | int clbuflen = curl_msnprintf(clbuf, sizeof(clbuf), |
2744 | 29 | "Content-Length: %" FMT_OFF_T "\r\n", |
2745 | 29 | filesize); |
2746 | 29 | result = client_write_header(data, clbuf, clbuflen); |
2747 | 29 | if(result) |
2748 | 0 | return result; |
2749 | 29 | } |
2750 | 32 | Curl_pgrsSetDownloadSize(data, filesize); |
2751 | 32 | result = ftp_state_rest(data, ftpc, ftp); |
2752 | 32 | } |
2753 | 666 | else if(instate == FTP_RETR_SIZE) { |
2754 | 666 | Curl_pgrsSetDownloadSize(data, filesize); |
2755 | 666 | result = ftp_state_retr(data, ftpc, ftp, filesize); |
2756 | 666 | } |
2757 | 0 | else if(instate == FTP_STOR_SIZE) { |
2758 | 0 | data->state.resume_from = filesize; |
2759 | 0 | result = ftp_state_ul_setup(data, ftpc, ftp, TRUE); |
2760 | 0 | } |
2761 | | |
2762 | 698 | return result; |
2763 | 698 | } |
2764 | | |
2765 | | static CURLcode ftp_state_rest_resp(struct Curl_easy *data, |
2766 | | struct ftp_conn *ftpc, |
2767 | | struct FTP *ftp, |
2768 | | int ftpcode, |
2769 | | ftpstate instate) |
2770 | 19 | { |
2771 | 19 | CURLcode result = CURLE_OK; |
2772 | | |
2773 | 19 | switch(instate) { |
2774 | 4 | case FTP_REST: |
2775 | 4 | default: |
2776 | 4 | if(ftpcode == 350) { |
2777 | 0 | char buffer[24] = { "Accept-ranges: bytes\r\n" }; |
2778 | 0 | result = client_write_header(data, buffer, strlen(buffer)); |
2779 | 0 | if(result) |
2780 | 0 | return result; |
2781 | 0 | } |
2782 | 4 | result = ftp_state_prepare_transfer(data, ftpc, ftp); |
2783 | 4 | break; |
2784 | | |
2785 | 15 | case FTP_RETR_REST: |
2786 | 15 | if(ftpcode != 350) { |
2787 | 15 | failf(data, "Could not use REST"); |
2788 | 15 | result = CURLE_FTP_COULDNT_USE_REST; |
2789 | 15 | } |
2790 | 0 | else { |
2791 | 0 | result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file); |
2792 | 0 | if(!result) |
2793 | 0 | ftp_state(data, ftpc, FTP_RETR); |
2794 | 0 | } |
2795 | 15 | break; |
2796 | 19 | } |
2797 | | |
2798 | 19 | return result; |
2799 | 19 | } |
2800 | | |
2801 | | static CURLcode ftp_state_stor_resp(struct Curl_easy *data, |
2802 | | struct ftp_conn *ftpc, |
2803 | | int ftpcode) |
2804 | 46 | { |
2805 | 46 | CURLcode result = CURLE_OK; |
2806 | | |
2807 | 46 | if(ftpcode >= 400) { |
2808 | 1 | failf(data, "Failed FTP upload: %0d", ftpcode); |
2809 | 1 | ftp_state(data, ftpc, FTP_STOP); |
2810 | 1 | return CURLE_UPLOAD_FAILED; |
2811 | 1 | } |
2812 | | |
2813 | | /* PORT means we are now awaiting the server to connect to us. */ |
2814 | 45 | if(data->set.ftp_use_port) { |
2815 | 0 | bool connected; |
2816 | |
|
2817 | 0 | ftp_state(data, ftpc, FTP_STOP); /* no longer in STOR state */ |
2818 | |
|
2819 | 0 | result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &connected); |
2820 | 0 | if(result) |
2821 | 0 | return result; |
2822 | | |
2823 | 0 | if(!connected) { |
2824 | 0 | infof(data, "Data conn was not available immediately"); |
2825 | 0 | ftpc->wait_data_conn = TRUE; |
2826 | 0 | return ftp_check_ctrl_on_data_wait(data, ftpc); |
2827 | 0 | } |
2828 | 0 | ftpc->wait_data_conn = FALSE; |
2829 | 0 | } |
2830 | 45 | return ftp_initiate_transfer(data, ftpc); |
2831 | 45 | } |
2832 | | |
2833 | | /* for LIST and RETR responses */ |
2834 | | static CURLcode ftp_state_get_resp(struct Curl_easy *data, |
2835 | | struct ftp_conn *ftpc, |
2836 | | struct FTP *ftp, |
2837 | | int ftpcode, |
2838 | | ftpstate instate) |
2839 | 922 | { |
2840 | 922 | CURLcode result = CURLE_OK; |
2841 | | |
2842 | 922 | if((ftpcode == 150) || (ftpcode == 125)) { |
2843 | | |
2844 | | /* |
2845 | | A; |
2846 | | 150 Opening BINARY mode data connection for /etc/passwd (2241 |
2847 | | bytes). (ok, the file is being transferred) |
2848 | | |
2849 | | B: |
2850 | | 150 Opening ASCII mode data connection for /bin/ls |
2851 | | |
2852 | | C: |
2853 | | 150 ASCII data connection for /bin/ls (137.167.104.91,37445) (0 bytes). |
2854 | | |
2855 | | D: |
2856 | | 150 Opening ASCII mode data connection for [file] (0.0.0.0,0) (545 bytes) |
2857 | | |
2858 | | E: |
2859 | | 125 Data connection already open; Transfer starting. */ |
2860 | | |
2861 | 909 | data->req.size = -1; /* default unknown size */ |
2862 | | |
2863 | | /* |
2864 | | * It appears that there are FTP-servers that return size 0 for files when |
2865 | | * SIZE is used on the file while being in BINARY mode. To work around |
2866 | | * that (stupid) behavior, we attempt to parse the RETR response even if |
2867 | | * the SIZE returned size zero. |
2868 | | * |
2869 | | * Debugging help from Salvatore Sorrentino on February 26, 2003. |
2870 | | */ |
2871 | | |
2872 | 909 | if((instate != FTP_LIST) && |
2873 | 353 | !data->state.prefer_ascii && |
2874 | 352 | !data->set.ignorecl && |
2875 | 351 | (ftp->downloadsize < 1)) { |
2876 | | /* |
2877 | | * It seems directory listings either do not show the size or often uses |
2878 | | * size 0 anyway. ASCII transfers may cause that the transferred amount |
2879 | | * of data is not the same as this line tells, why using this number in |
2880 | | * those cases only confuses us. |
2881 | | * |
2882 | | * Example D above makes this parsing a little tricky */ |
2883 | 81 | size_t len = curlx_dyn_len(&ftpc->pp.recvbuf); |
2884 | 81 | if(len >= 7) { /* "1 bytes" is 7 characters */ |
2885 | 80 | size_t i; |
2886 | 1.60k | for(i = 0; i < len - 7; i++) { |
2887 | 1.53k | curl_off_t what; |
2888 | 1.53k | const char *buf = curlx_dyn_ptr(&ftpc->pp.recvbuf); |
2889 | 1.53k | const char *c = &buf[i]; |
2890 | 1.53k | if(!curlx_str_number(&c, &what, CURL_OFF_T_MAX) && |
2891 | 667 | !curlx_str_single(&c, ' ') && |
2892 | 312 | !strncmp(c, "bytes", 5)) { |
2893 | 1 | data->req.size = what; |
2894 | 1 | break; |
2895 | 1 | } |
2896 | 1.53k | } |
2897 | 80 | } |
2898 | 81 | } |
2899 | 828 | else if(ftp->downloadsize > -1) |
2900 | 270 | data->req.size = ftp->downloadsize; |
2901 | | |
2902 | 909 | if(data->req.size > data->req.maxdownload && data->req.maxdownload > 0) |
2903 | 21 | data->req.size = data->req.maxdownload; |
2904 | 888 | else if((instate != FTP_LIST) && (data->state.prefer_ascii)) |
2905 | 1 | data->req.size = -1; /* for servers that understate ASCII mode file |
2906 | | size */ |
2907 | | |
2908 | 909 | infof(data, "Maxdownload = %" FMT_OFF_T, data->req.maxdownload); |
2909 | | |
2910 | 909 | if(instate != FTP_LIST) |
2911 | 353 | infof(data, "Getting file with size: %" FMT_OFF_T, data->req.size); |
2912 | | |
2913 | 909 | if(data->set.ftp_use_port) { |
2914 | 0 | bool connected; |
2915 | |
|
2916 | 0 | result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &connected); |
2917 | 0 | if(result) |
2918 | 0 | return result; |
2919 | | |
2920 | 0 | if(!connected) { |
2921 | 0 | infof(data, "Data conn was not available immediately"); |
2922 | 0 | ftp_state(data, ftpc, FTP_STOP); |
2923 | 0 | ftpc->wait_data_conn = TRUE; |
2924 | 0 | return ftp_check_ctrl_on_data_wait(data, ftpc); |
2925 | 0 | } |
2926 | 0 | ftpc->wait_data_conn = FALSE; |
2927 | 0 | } |
2928 | 909 | return ftp_initiate_transfer(data, ftpc); |
2929 | 909 | } |
2930 | 13 | else { |
2931 | 13 | if((instate == FTP_LIST) && (ftpcode == 450)) { |
2932 | | /* no matching files in the directory listing */ |
2933 | 1 | ftp->transfer = PPTRANSFER_NONE; /* do not download anything */ |
2934 | 1 | ftp_state(data, ftpc, FTP_STOP); /* this phase is over */ |
2935 | 1 | } |
2936 | 12 | else { |
2937 | 12 | failf(data, "RETR response: %03d", ftpcode); |
2938 | 12 | return instate == FTP_RETR && ftpcode == 550 ? |
2939 | 0 | CURLE_REMOTE_FILE_NOT_FOUND : |
2940 | 12 | CURLE_FTP_COULDNT_RETR_FILE; |
2941 | 12 | } |
2942 | 13 | } |
2943 | | |
2944 | 1 | return result; |
2945 | 922 | } |
2946 | | |
2947 | | /* after USER, PASS and ACCT */ |
2948 | | static CURLcode ftp_state_loggedin(struct Curl_easy *data, |
2949 | | struct ftp_conn *ftpc) |
2950 | 1.99k | { |
2951 | 1.99k | CURLcode result = CURLE_OK; |
2952 | | |
2953 | 1.99k | if(data->conn->bits.ftp_use_control_ssl) { |
2954 | | /* PBSZ = PROTECTION BUFFER SIZE. |
2955 | | |
2956 | | The 'draft-murray-auth-ftp-ssl' (draft 12, page 7) says: |
2957 | | |
2958 | | Specifically, the PROT command MUST be preceded by a PBSZ |
2959 | | command and a PBSZ command MUST be preceded by a successful |
2960 | | security data exchange (the TLS negotiation in this case) |
2961 | | |
2962 | | ... (and on page 8): |
2963 | | |
2964 | | Thus the PBSZ command must still be issued, but must have a |
2965 | | parameter of '0' to indicate that no buffering is taking place |
2966 | | and the data connection should not be encapsulated. |
2967 | | */ |
2968 | 0 | result = Curl_pp_sendf(data, &ftpc->pp, "PBSZ %d", 0); |
2969 | 0 | if(!result) |
2970 | 0 | ftp_state(data, ftpc, FTP_PBSZ); |
2971 | 0 | } |
2972 | 1.99k | else { |
2973 | 1.99k | result = ftp_state_pwd(data, ftpc); |
2974 | 1.99k | } |
2975 | 1.99k | return result; |
2976 | 1.99k | } |
2977 | | |
2978 | | /* for USER and PASS responses */ |
2979 | | static CURLcode ftp_state_user_resp(struct Curl_easy *data, |
2980 | | struct ftp_conn *ftpc, |
2981 | | int ftpcode) |
2982 | 2.01k | { |
2983 | 2.01k | CURLcode result = CURLE_OK; |
2984 | | |
2985 | | /* some need password anyway, and others return 2xx ignored */ |
2986 | 2.01k | if((ftpcode == 331) && (ftpc->state == FTP_USER)) { |
2987 | | /* 331 Password required for ... |
2988 | | (the server requires to send the user's password too) */ |
2989 | 7 | result = Curl_pp_sendf(data, &ftpc->pp, "PASS %s", data->conn->passwd); |
2990 | 7 | if(!result) |
2991 | 7 | ftp_state(data, ftpc, FTP_PASS); |
2992 | 7 | } |
2993 | 2.00k | else if(ftpcode / 100 == 2) { |
2994 | | /* 230 User ... logged in. |
2995 | | (the user logged in with or without password) */ |
2996 | 1.99k | result = ftp_state_loggedin(data, ftpc); |
2997 | 1.99k | } |
2998 | 10 | else if(ftpcode == 332) { |
2999 | 1 | if(data->set.str[STRING_FTP_ACCOUNT]) { |
3000 | 0 | result = Curl_pp_sendf(data, &ftpc->pp, "ACCT %s", |
3001 | 0 | data->set.str[STRING_FTP_ACCOUNT]); |
3002 | 0 | if(!result) |
3003 | 0 | ftp_state(data, ftpc, FTP_ACCT); |
3004 | 0 | } |
3005 | 1 | else { |
3006 | 1 | failf(data, "ACCT requested but none available"); |
3007 | 1 | result = CURLE_LOGIN_DENIED; |
3008 | 1 | } |
3009 | 1 | } |
3010 | 9 | else { |
3011 | | /* All other response codes, like: |
3012 | | |
3013 | | 530 User ... access denied |
3014 | | (the server denies to log the specified user) */ |
3015 | | |
3016 | 9 | if(data->set.str[STRING_FTP_ALTERNATIVE_TO_USER] && |
3017 | 5 | !ftpc->ftp_trying_alternative) { |
3018 | | /* Ok, USER failed. Let's try the supplied command. */ |
3019 | 3 | result = Curl_pp_sendf(data, &ftpc->pp, "%s", |
3020 | 3 | data->set.str[STRING_FTP_ALTERNATIVE_TO_USER]); |
3021 | 3 | if(!result) { |
3022 | 3 | ftpc->ftp_trying_alternative = TRUE; |
3023 | 3 | ftp_state(data, ftpc, FTP_USER); |
3024 | 3 | } |
3025 | 3 | } |
3026 | 6 | else { |
3027 | 6 | failf(data, "Access denied: %03d", ftpcode); |
3028 | 6 | result = CURLE_LOGIN_DENIED; |
3029 | 6 | } |
3030 | 9 | } |
3031 | 2.01k | return result; |
3032 | 2.01k | } |
3033 | | |
3034 | | /* for ACCT response */ |
3035 | | static CURLcode ftp_state_acct_resp(struct Curl_easy *data, |
3036 | | struct ftp_conn *ftpc, |
3037 | | int ftpcode) |
3038 | 0 | { |
3039 | 0 | CURLcode result = CURLE_OK; |
3040 | 0 | if(ftpcode != 230) { |
3041 | 0 | failf(data, "ACCT rejected by server: %03d", ftpcode); |
3042 | 0 | result = CURLE_FTP_WEIRD_PASS_REPLY; /* FIX */ |
3043 | 0 | } |
3044 | 0 | else |
3045 | 0 | result = ftp_state_loggedin(data, ftpc); |
3046 | |
|
3047 | 0 | return result; |
3048 | 0 | } |
3049 | | |
3050 | | static CURLcode ftp_pwd_resp(struct Curl_easy *data, |
3051 | | struct ftp_conn *ftpc, |
3052 | | int ftpcode) |
3053 | 1.97k | { |
3054 | 1.97k | struct pingpong *pp = &ftpc->pp; |
3055 | 1.97k | CURLcode result; |
3056 | | |
3057 | 1.97k | if(ftpcode == 257) { |
3058 | 123 | const char *ptr = curlx_dyn_ptr(&pp->recvbuf) + 4; /* start on the first |
3059 | | letter */ |
3060 | 123 | bool entry_extracted = FALSE; |
3061 | 123 | struct dynbuf out; |
3062 | 123 | curlx_dyn_init(&out, 1000); |
3063 | | |
3064 | | /* Reply format is like |
3065 | | 257<space>[rubbish]"<directory-name>"<space><commentary> and the |
3066 | | RFC959 says |
3067 | | |
3068 | | The directory name can contain any character; embedded |
3069 | | double-quotes should be escaped by double-quotes (the |
3070 | | "quote-doubling" convention). |
3071 | | */ |
3072 | | |
3073 | | /* scan for the first double-quote for non-standard responses */ |
3074 | 665 | while(*ptr != '\n' && *ptr != '\0' && *ptr != '"') |
3075 | 542 | ptr++; |
3076 | | |
3077 | 123 | if('\"' == *ptr) { |
3078 | | /* it started good */ |
3079 | 3.97k | for(ptr++; *ptr; ptr++) { |
3080 | 3.96k | if('\"' == *ptr) { |
3081 | 1.43k | if('\"' == ptr[1]) { |
3082 | | /* "quote-doubling" */ |
3083 | 1.41k | result = curlx_dyn_addn(&out, &ptr[1], 1); |
3084 | 1.41k | ptr++; |
3085 | 1.41k | } |
3086 | 27 | else { |
3087 | | /* end of path */ |
3088 | 27 | if(curlx_dyn_len(&out)) |
3089 | 26 | entry_extracted = TRUE; |
3090 | 27 | break; /* get out of this loop */ |
3091 | 27 | } |
3092 | 1.43k | } |
3093 | 2.53k | else { |
3094 | 2.53k | if(ISCNTRL(*ptr)) { |
3095 | | /* control characters have no business in a path */ |
3096 | 8 | curlx_dyn_free(&out); |
3097 | 8 | return CURLE_WEIRD_SERVER_REPLY; |
3098 | 8 | } |
3099 | 2.52k | result = curlx_dyn_addn(&out, ptr, 1); |
3100 | 2.52k | } |
3101 | 3.93k | if(result) { |
3102 | 1 | curlx_dyn_free(&out); |
3103 | 1 | return result; |
3104 | 1 | } |
3105 | 3.93k | } |
3106 | 41 | } |
3107 | 114 | if(entry_extracted) { |
3108 | | /* If the path name does not look like an absolute path (i.e.: it |
3109 | | does not start with a '/'), we probably need some server-dependent |
3110 | | adjustments. For example, this is the case when connecting to |
3111 | | an OS400 FTP server: this server supports two name syntaxes, |
3112 | | the default one being incompatible with standard paths. In |
3113 | | addition, this server switches automatically to the regular path |
3114 | | syntax when one is encountered in a command: this results in |
3115 | | having an entrypath in the wrong syntax when later used in CWD. |
3116 | | The method used here is to check the server OS: we do it only |
3117 | | if the path name looks strange to minimize overhead on other |
3118 | | systems. */ |
3119 | 26 | char *dir = curlx_dyn_ptr(&out); |
3120 | | |
3121 | 26 | if(!ftpc->server_os && dir[0] != '/') { |
3122 | 22 | result = Curl_pp_sendf(data, &ftpc->pp, "%s", "SYST"); |
3123 | 22 | if(result) { |
3124 | 0 | curlx_dyn_free(&out); |
3125 | 0 | return result; |
3126 | 0 | } |
3127 | 22 | } |
3128 | | |
3129 | 26 | curlx_free(ftpc->entrypath); |
3130 | 26 | ftpc->entrypath = dir; /* remember this */ |
3131 | 26 | infof(data, "Entry path is '%s'", ftpc->entrypath); |
3132 | | /* also save it where getinfo can access it: */ |
3133 | 26 | curlx_free(data->state.most_recent_ftp_entrypath); |
3134 | 26 | data->state.most_recent_ftp_entrypath = curlx_strdup(ftpc->entrypath); |
3135 | 26 | if(!data->state.most_recent_ftp_entrypath) |
3136 | 0 | return CURLE_OUT_OF_MEMORY; |
3137 | | |
3138 | 26 | if(!ftpc->server_os && dir[0] != '/') { |
3139 | 22 | ftp_state(data, ftpc, FTP_SYST); |
3140 | 22 | return CURLE_OK; |
3141 | 22 | } |
3142 | 26 | } |
3143 | 88 | else { |
3144 | | /* could not get the path */ |
3145 | 88 | curlx_dyn_free(&out); |
3146 | 88 | infof(data, "Failed to figure out path"); |
3147 | 88 | } |
3148 | 114 | } |
3149 | 1.94k | ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */ |
3150 | 1.94k | CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc)); |
3151 | 1.94k | return CURLE_OK; |
3152 | 1.97k | } |
3153 | | |
3154 | | static const char * const ftpauth[] = { "SSL", "TLS" }; |
3155 | | |
3156 | | static CURLcode ftp_wait_resp(struct Curl_easy *data, |
3157 | | struct connectdata *conn, |
3158 | | struct ftp_conn *ftpc, |
3159 | | int ftpcode) |
3160 | 2.03k | { |
3161 | 2.03k | CURLcode result = CURLE_OK; |
3162 | 2.03k | if(ftpcode == 230) { |
3163 | | /* 230 User logged in - already! Take as 220 if TLS required. */ |
3164 | 1.82k | if(data->set.use_ssl <= CURLUSESSL_TRY || |
3165 | 4 | conn->bits.ftp_use_control_ssl) |
3166 | 1.82k | return ftp_state_user_resp(data, ftpc, ftpcode); |
3167 | 1.82k | } |
3168 | 202 | else if(ftpcode != 220) { |
3169 | 17 | failf(data, "Got a %03d ftp-server response when 220 was expected", |
3170 | 17 | ftpcode); |
3171 | 17 | return CURLE_WEIRD_SERVER_REPLY; |
3172 | 17 | } |
3173 | | |
3174 | 189 | if(data->set.use_ssl && !conn->bits.ftp_use_control_ssl) { |
3175 | | /* We do not have an SSL/TLS control connection yet, but FTPS is |
3176 | | requested. Try an FTPS connection now */ |
3177 | | |
3178 | 6 | ftpc->count3 = 0; |
3179 | 6 | switch((long)data->set.ftpsslauth) { |
3180 | 6 | case CURLFTPAUTH_DEFAULT: |
3181 | 6 | case CURLFTPAUTH_SSL: |
3182 | 6 | ftpc->count2 = 1; /* add one to get next */ |
3183 | 6 | ftpc->count1 = 0; |
3184 | 6 | break; |
3185 | 0 | case CURLFTPAUTH_TLS: |
3186 | 0 | ftpc->count2 = -1; /* subtract one to get next */ |
3187 | 0 | ftpc->count1 = 1; |
3188 | 0 | break; |
3189 | 0 | default: |
3190 | 0 | failf(data, "unsupported parameter to CURLOPT_FTPSSLAUTH: %d", |
3191 | 0 | (int)data->set.ftpsslauth); |
3192 | 0 | return CURLE_UNKNOWN_OPTION; /* we do not know what to do */ |
3193 | 6 | } |
3194 | 6 | result = Curl_pp_sendf(data, &ftpc->pp, "AUTH %s", ftpauth[ftpc->count1]); |
3195 | 6 | if(!result) |
3196 | 6 | ftp_state(data, ftpc, FTP_AUTH); |
3197 | 6 | } |
3198 | 183 | else |
3199 | 183 | result = ftp_state_user(data, ftpc, conn); |
3200 | 189 | return result; |
3201 | 189 | } |
3202 | | |
3203 | | static CURLcode ftp_pp_statemachine(struct Curl_easy *data, |
3204 | | struct connectdata *conn) |
3205 | 18.7k | { |
3206 | 18.7k | CURLcode result; |
3207 | 18.7k | int ftpcode; |
3208 | 18.7k | struct ftp_conn *ftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN); |
3209 | 18.7k | struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY); |
3210 | 18.7k | struct pingpong *pp; |
3211 | 18.7k | size_t nread = 0; |
3212 | | |
3213 | 18.7k | if(!ftpc || !ftp) |
3214 | 1.10k | return CURLE_FAILED_INIT; |
3215 | 17.6k | pp = &ftpc->pp; |
3216 | 17.6k | if(pp->sendleft) |
3217 | 0 | return Curl_pp_flushsend(data, pp); |
3218 | | |
3219 | 17.6k | result = ftp_readresp(data, ftpc, FIRSTSOCKET, pp, &ftpcode, &nread); |
3220 | 17.6k | if(result || !ftpcode) |
3221 | 7.72k | return result; |
3222 | | |
3223 | | /* we have now received a full FTP server response */ |
3224 | 9.93k | switch(ftpc->state) { |
3225 | 2.03k | case FTP_WAIT220: |
3226 | 2.03k | result = ftp_wait_resp(data, conn, ftpc, ftpcode); |
3227 | 2.03k | break; |
3228 | | |
3229 | 8 | case FTP_AUTH: |
3230 | | /* we have gotten the response to a previous AUTH command */ |
3231 | | |
3232 | 8 | if(pp->overflow) |
3233 | 1 | return CURLE_WEIRD_SERVER_REPLY; /* Forbid pipelining in response. */ |
3234 | | |
3235 | | /* RFC2228 (page 5) says: |
3236 | | * |
3237 | | * If the server is willing to accept the named security mechanism, |
3238 | | * and does not require any security data, it must respond with |
3239 | | * reply code 234/334. |
3240 | | */ |
3241 | | |
3242 | 7 | if((ftpcode == 234) || (ftpcode == 334)) { |
3243 | | /* this was BLOCKING, keep it so for now */ |
3244 | 2 | bool done; |
3245 | 2 | if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) { |
3246 | 2 | result = Curl_ssl_cfilter_add(data, conn, FIRSTSOCKET); |
3247 | 2 | if(result) { |
3248 | | /* we failed and bail out */ |
3249 | 0 | return CURLE_USE_SSL_FAILED; |
3250 | 0 | } |
3251 | 2 | } |
3252 | 2 | result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, &done); |
3253 | 2 | if(!result) { |
3254 | 0 | conn->bits.ftp_use_data_ssl = FALSE; /* clear-text data */ |
3255 | 0 | conn->bits.ftp_use_control_ssl = TRUE; /* SSL on control */ |
3256 | 0 | result = ftp_state_user(data, ftpc, conn); |
3257 | 0 | } |
3258 | 2 | } |
3259 | 5 | else if(ftpc->count3 < 1) { |
3260 | 3 | ftpc->count3++; |
3261 | 3 | ftpc->count1 += ftpc->count2; /* get next attempt */ |
3262 | 3 | result = Curl_pp_sendf(data, &ftpc->pp, "AUTH %s", |
3263 | 3 | ftpauth[ftpc->count1]); |
3264 | | /* remain in this same state */ |
3265 | 3 | } |
3266 | 2 | else { |
3267 | 2 | if(data->set.use_ssl > CURLUSESSL_TRY) |
3268 | | /* we failed and CURLUSESSL_CONTROL or CURLUSESSL_ALL is set */ |
3269 | 1 | result = CURLE_USE_SSL_FAILED; |
3270 | 1 | else |
3271 | | /* ignore the failure and continue */ |
3272 | 1 | result = ftp_state_user(data, ftpc, conn); |
3273 | 2 | } |
3274 | 7 | break; |
3275 | | |
3276 | 182 | case FTP_USER: |
3277 | 188 | case FTP_PASS: |
3278 | 188 | result = ftp_state_user_resp(data, ftpc, ftpcode); |
3279 | 188 | break; |
3280 | | |
3281 | 0 | case FTP_ACCT: |
3282 | 0 | result = ftp_state_acct_resp(data, ftpc, ftpcode); |
3283 | 0 | break; |
3284 | | |
3285 | 0 | case FTP_PBSZ: |
3286 | 0 | result = |
3287 | 0 | Curl_pp_sendf(data, &ftpc->pp, "PROT %c", |
3288 | 0 | data->set.use_ssl == CURLUSESSL_CONTROL ? 'C' : 'P'); |
3289 | 0 | if(!result) |
3290 | 0 | ftp_state(data, ftpc, FTP_PROT); |
3291 | 0 | break; |
3292 | | |
3293 | 0 | case FTP_PROT: |
3294 | 0 | if(ftpcode / 100 == 2) |
3295 | | /* We have enabled SSL for the data connection! */ |
3296 | 0 | conn->bits.ftp_use_data_ssl = (data->set.use_ssl != CURLUSESSL_CONTROL); |
3297 | | /* FTP servers typically responds with 500 if they decide to reject |
3298 | | our 'P' request */ |
3299 | 0 | else if(data->set.use_ssl > CURLUSESSL_CONTROL) |
3300 | | /* we failed and bails out */ |
3301 | 0 | return CURLE_USE_SSL_FAILED; |
3302 | | |
3303 | 0 | if(data->set.ftp_ccc) { |
3304 | | /* CCC - Clear Command Channel |
3305 | | */ |
3306 | 0 | result = Curl_pp_sendf(data, &ftpc->pp, "%s", "CCC"); |
3307 | 0 | if(!result) |
3308 | 0 | ftp_state(data, ftpc, FTP_CCC); |
3309 | 0 | } |
3310 | 0 | else |
3311 | 0 | result = ftp_state_pwd(data, ftpc); |
3312 | 0 | break; |
3313 | | |
3314 | 0 | case FTP_CCC: |
3315 | 0 | if(ftpcode < 500) { |
3316 | | /* First shut down the SSL layer (note: this call will block) */ |
3317 | | /* This has only been tested on the proftpd server, and the mod_tls |
3318 | | * code sends a close notify alert without waiting for a close notify |
3319 | | * alert in response. Thus we wait for a close notify alert from the |
3320 | | * server, but we do not send one. Let's hope other servers do |
3321 | | * the same... */ |
3322 | 0 | result = Curl_ssl_cfilter_remove(data, FIRSTSOCKET, |
3323 | 0 | (data->set.ftp_ccc == |
3324 | 0 | (unsigned char)CURLFTPSSL_CCC_ACTIVE)); |
3325 | 0 | if(result) |
3326 | 0 | failf(data, "Failed to clear the command channel (CCC)"); |
3327 | 0 | } |
3328 | 0 | if(!result) |
3329 | | /* Then continue as normal */ |
3330 | 0 | result = ftp_state_pwd(data, ftpc); |
3331 | 0 | break; |
3332 | | |
3333 | 1.97k | case FTP_PWD: |
3334 | 1.97k | result = ftp_pwd_resp(data, ftpc, ftpcode); |
3335 | 1.97k | break; |
3336 | | |
3337 | 20 | case FTP_SYST: |
3338 | 20 | if(ftpcode == 215) { |
3339 | 16 | const char *ptr = curlx_dyn_ptr(&pp->recvbuf) + 4; /* start on the first |
3340 | | letter */ |
3341 | 16 | const char *start; |
3342 | 16 | char *os; |
3343 | | |
3344 | | /* Reply format is like |
3345 | | 215<space><OS-name><space><commentary> |
3346 | | */ |
3347 | 89 | while(*ptr == ' ') |
3348 | 73 | ptr++; |
3349 | 1.21k | for(start = ptr; *ptr && *ptr != ' '; ptr++) |
3350 | 1.20k | ; |
3351 | 16 | os = curlx_memdup0(start, ptr - start); |
3352 | 16 | if(!os) |
3353 | 0 | return CURLE_OUT_OF_MEMORY; |
3354 | | |
3355 | | /* Check for special servers here. */ |
3356 | 16 | if(curl_strequal(os, "OS/400")) { |
3357 | | /* Force OS400 name format 1. */ |
3358 | 0 | result = Curl_pp_sendf(data, &ftpc->pp, "%s", "SITE NAMEFMT 1"); |
3359 | 0 | if(result) { |
3360 | 0 | curlx_free(os); |
3361 | 0 | return result; |
3362 | 0 | } |
3363 | | /* remember target server OS */ |
3364 | 0 | curlx_free(ftpc->server_os); |
3365 | 0 | ftpc->server_os = os; |
3366 | 0 | ftp_state(data, ftpc, FTP_NAMEFMT); |
3367 | 0 | break; |
3368 | 0 | } |
3369 | | /* Nothing special for the target server. */ |
3370 | | /* remember target server OS */ |
3371 | 16 | curlx_free(ftpc->server_os); |
3372 | 16 | ftpc->server_os = os; |
3373 | 16 | } |
3374 | 4 | else { |
3375 | | /* Cannot identify server OS. Continue anyway and cross fingers. */ |
3376 | 4 | } |
3377 | | |
3378 | 20 | ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */ |
3379 | 20 | CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc)); |
3380 | 20 | break; |
3381 | | |
3382 | 0 | case FTP_NAMEFMT: |
3383 | 0 | if(ftpcode == 250) { |
3384 | | /* Name format change successful: reload initial path. */ |
3385 | 0 | ftp_state_pwd(data, ftpc); |
3386 | 0 | break; |
3387 | 0 | } |
3388 | | |
3389 | 0 | ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */ |
3390 | 0 | CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc)); |
3391 | 0 | break; |
3392 | | |
3393 | 0 | case FTP_QUOTE: |
3394 | 0 | case FTP_POSTQUOTE: |
3395 | 0 | case FTP_RETR_PREQUOTE: |
3396 | 0 | case FTP_STOR_PREQUOTE: |
3397 | 0 | case FTP_LIST_PREQUOTE: |
3398 | 0 | if((ftpcode >= 400) && !ftpc->count2) { |
3399 | | /* failure response code, and not allowed to fail */ |
3400 | 0 | failf(data, "QUOT command failed with %03d", ftpcode); |
3401 | 0 | result = CURLE_QUOTE_ERROR; |
3402 | 0 | } |
3403 | 0 | else |
3404 | 0 | result = ftp_state_quote(data, ftpc, ftp, FALSE, ftpc->state); |
3405 | 0 | break; |
3406 | | |
3407 | 196 | case FTP_CWD: |
3408 | 196 | if(ftpcode / 100 != 2) { |
3409 | | /* failure to CWD there */ |
3410 | 30 | if(data->set.ftp_create_missing_dirs && |
3411 | 21 | ftpc->cwdcount && !ftpc->count2) { |
3412 | | /* try making it */ |
3413 | 20 | ftpc->count2++; /* counter to prevent CWD-MKD loops */ |
3414 | | |
3415 | | /* count3 is set to allow MKD to fail once per dir. In the case when |
3416 | | CWD fails and then MKD fails (due to another session raced it to |
3417 | | create the dir) this then allows for a second try to CWD to it. */ |
3418 | 20 | ftpc->count3 = (data->set.ftp_create_missing_dirs == 2) ? 1 : 0; |
3419 | | |
3420 | 20 | result = Curl_pp_sendf(data, &ftpc->pp, "MKD %.*s", |
3421 | 20 | pathlen(ftpc, ftpc->cwdcount - 1), |
3422 | 20 | pathpiece(ftpc, ftpc->cwdcount - 1)); |
3423 | 20 | if(!result) |
3424 | 20 | ftp_state(data, ftpc, FTP_MKD); |
3425 | 20 | } |
3426 | 10 | else { |
3427 | | /* return failure */ |
3428 | 10 | failf(data, "Server denied you to change to the given directory"); |
3429 | 10 | ftpc->cwdfail = TRUE; /* do not remember this path as we failed |
3430 | | to enter it */ |
3431 | 10 | result = CURLE_REMOTE_ACCESS_DENIED; |
3432 | 10 | } |
3433 | 30 | } |
3434 | 166 | else { |
3435 | | /* success */ |
3436 | 166 | ftpc->count2 = 0; |
3437 | 166 | if(ftpc->cwdcount >= ftpc->dirdepth) |
3438 | 144 | result = ftp_state_mdtm(data, ftpc, ftp); |
3439 | 22 | else { |
3440 | 22 | ftpc->cwdcount++; |
3441 | | /* send next CWD */ |
3442 | 22 | result = Curl_pp_sendf(data, &ftpc->pp, "CWD %.*s", |
3443 | 22 | pathlen(ftpc, ftpc->cwdcount - 1), |
3444 | 22 | pathpiece(ftpc, ftpc->cwdcount - 1)); |
3445 | 22 | } |
3446 | 166 | } |
3447 | 196 | break; |
3448 | | |
3449 | 18 | case FTP_MKD: |
3450 | 18 | if((ftpcode / 100 != 2) && !ftpc->count3--) { |
3451 | | /* failure to MKD the directory */ |
3452 | 2 | failf(data, "Failed to MKD dir: %03d", ftpcode); |
3453 | 2 | result = CURLE_REMOTE_ACCESS_DENIED; |
3454 | 2 | } |
3455 | 16 | else { |
3456 | 16 | ftp_state(data, ftpc, FTP_CWD); |
3457 | | /* send CWD */ |
3458 | 16 | result = Curl_pp_sendf(data, &ftpc->pp, "CWD %.*s", |
3459 | 16 | pathlen(ftpc, ftpc->cwdcount - 1), |
3460 | 16 | pathpiece(ftpc, ftpc->cwdcount - 1)); |
3461 | 16 | } |
3462 | 18 | break; |
3463 | | |
3464 | 47 | case FTP_MDTM: |
3465 | 47 | result = ftp_state_mdtm_resp(data, ftpc, ftp, ftpcode); |
3466 | 47 | break; |
3467 | | |
3468 | 35 | case FTP_TYPE: |
3469 | 658 | case FTP_LIST_TYPE: |
3470 | 1.34k | case FTP_RETR_TYPE: |
3471 | 1.43k | case FTP_STOR_TYPE: |
3472 | 1.43k | case FTP_RETR_LIST_TYPE: |
3473 | 1.43k | result = ftp_state_type_resp(data, ftpc, ftp, ftpcode, ftpc->state); |
3474 | 1.43k | break; |
3475 | | |
3476 | 32 | case FTP_SIZE: |
3477 | 699 | case FTP_RETR_SIZE: |
3478 | 699 | case FTP_STOR_SIZE: |
3479 | 699 | result = ftp_state_size_resp(data, ftpc, ftp, ftpcode, ftpc->state); |
3480 | 699 | break; |
3481 | | |
3482 | 4 | case FTP_REST: |
3483 | 19 | case FTP_RETR_REST: |
3484 | 19 | result = ftp_state_rest_resp(data, ftpc, ftp, ftpcode, ftpc->state); |
3485 | 19 | break; |
3486 | | |
3487 | 2 | case FTP_PRET: |
3488 | 2 | if(ftpcode != 200) { |
3489 | | /* there only is this one standard OK return code. */ |
3490 | 1 | failf(data, "PRET command not accepted: %03d", ftpcode); |
3491 | 1 | return CURLE_FTP_PRET_FAILED; |
3492 | 1 | } |
3493 | 1 | result = ftp_state_use_pasv(data, ftpc, conn); |
3494 | 1 | break; |
3495 | | |
3496 | 2.29k | case FTP_PASV: |
3497 | 2.29k | result = ftp_state_pasv_resp(data, ftpc, ftpcode); |
3498 | 2.29k | break; |
3499 | | |
3500 | 0 | case FTP_PORT: |
3501 | 0 | result = ftp_state_port_resp(data, ftpc, ftp, ftpcode); |
3502 | 0 | break; |
3503 | | |
3504 | 563 | case FTP_LIST: |
3505 | 922 | case FTP_RETR: |
3506 | 922 | result = ftp_state_get_resp(data, ftpc, ftp, ftpcode, ftpc->state); |
3507 | 922 | break; |
3508 | | |
3509 | 46 | case FTP_STOR: |
3510 | 46 | result = ftp_state_stor_resp(data, ftpc, ftpcode); |
3511 | 46 | break; |
3512 | | |
3513 | 0 | case FTP_QUIT: |
3514 | 33 | default: |
3515 | | /* internal error */ |
3516 | 33 | ftp_state(data, ftpc, FTP_STOP); |
3517 | 33 | break; |
3518 | 9.93k | } |
3519 | | |
3520 | 9.93k | return result; |
3521 | 9.93k | } |
3522 | | |
3523 | | /* called repeatedly until done from multi.c */ |
3524 | | static CURLcode ftp_multi_statemach(struct Curl_easy *data, |
3525 | | bool *done) |
3526 | 7.98k | { |
3527 | 7.98k | struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN); |
3528 | 7.98k | return ftpc ? ftp_statemach(data, ftpc, done) : CURLE_FAILED_INIT; |
3529 | 7.98k | } |
3530 | | |
3531 | | static CURLcode ftp_block_statemach(struct Curl_easy *data, |
3532 | | struct ftp_conn *ftpc) |
3533 | 1.12k | { |
3534 | 1.12k | struct pingpong *pp = &ftpc->pp; |
3535 | 1.12k | CURLcode result = CURLE_OK; |
3536 | | |
3537 | 1.12k | while(ftpc->state != FTP_STOP) { |
3538 | 1.12k | if(ftpc->shutdown) |
3539 | 1.12k | CURL_TRC_FTP(data, "in shutdown, waiting for server response"); |
3540 | 1.12k | result = Curl_pp_statemach(data, pp, TRUE, TRUE /* disconnecting */); |
3541 | 1.12k | if(result) |
3542 | 1.12k | break; |
3543 | 1.12k | } |
3544 | | |
3545 | 1.12k | return result; |
3546 | 1.12k | } |
3547 | | |
3548 | | /* |
3549 | | * ftp_connect() should do everything that is to be considered a part of |
3550 | | * the connection phase. |
3551 | | * |
3552 | | * The variable 'done' points to will be TRUE if the protocol-layer connect |
3553 | | * phase is done when this function returns, or FALSE if not. |
3554 | | * |
3555 | | */ |
3556 | | static CURLcode ftp_connect(struct Curl_easy *data, |
3557 | | bool *done) /* see description above */ |
3558 | 2.63k | { |
3559 | 2.63k | CURLcode result; |
3560 | 2.63k | struct connectdata *conn = data->conn; |
3561 | 2.63k | struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN); |
3562 | 2.63k | struct pingpong *pp; |
3563 | | |
3564 | 2.63k | *done = FALSE; /* default to not done yet */ |
3565 | 2.63k | if(!ftpc) |
3566 | 0 | return CURLE_FAILED_INIT; |
3567 | 2.63k | pp = &ftpc->pp; |
3568 | 2.63k | PINGPONG_SETUP(pp, ftp_pp_statemachine, ftp_endofresp); |
3569 | | |
3570 | 2.63k | if(Curl_conn_is_ssl(conn, FIRSTSOCKET)) { |
3571 | | /* BLOCKING */ |
3572 | 0 | result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, done); |
3573 | 0 | if(result) |
3574 | 0 | return result; |
3575 | 0 | conn->bits.ftp_use_control_ssl = TRUE; |
3576 | 0 | } |
3577 | | |
3578 | 2.63k | Curl_pp_init(pp, Curl_pgrs_now(data)); /* once per transfer */ |
3579 | | |
3580 | | /* When we connect, we start in the state where we await the 220 |
3581 | | response */ |
3582 | 2.63k | ftp_state(data, ftpc, FTP_WAIT220); |
3583 | | |
3584 | 2.63k | result = ftp_statemach(data, ftpc, done); |
3585 | | |
3586 | 2.63k | return result; |
3587 | 2.63k | } |
3588 | | |
3589 | | /*********************************************************************** |
3590 | | * |
3591 | | * ftp_sendquote() |
3592 | | * |
3593 | | * Where a 'quote' means a list of custom commands to send to the server. |
3594 | | * The quote list is passed as an argument. |
3595 | | * |
3596 | | * BLOCKING |
3597 | | */ |
3598 | | static CURLcode ftp_sendquote(struct Curl_easy *data, |
3599 | | struct ftp_conn *ftpc, |
3600 | | struct curl_slist *quote) |
3601 | 0 | { |
3602 | 0 | struct curl_slist *item; |
3603 | 0 | struct pingpong *pp = &ftpc->pp; |
3604 | |
|
3605 | 0 | item = quote; |
3606 | 0 | while(item) { |
3607 | 0 | if(item->data) { |
3608 | 0 | size_t nread; |
3609 | 0 | const char *cmd = item->data; |
3610 | 0 | bool acceptfail = FALSE; |
3611 | 0 | CURLcode result; |
3612 | 0 | int ftpcode = 0; |
3613 | | |
3614 | | /* if a command starts with an asterisk, which a legal FTP command never |
3615 | | can, the command will be allowed to fail without it causing any |
3616 | | aborts or cancels etc. It will cause libcurl to act as if the command |
3617 | | is successful, whatever the server responds. */ |
3618 | |
|
3619 | 0 | if(cmd[0] == '*') { |
3620 | 0 | cmd++; |
3621 | 0 | acceptfail = TRUE; |
3622 | 0 | } |
3623 | |
|
3624 | 0 | result = Curl_pp_sendf(data, &ftpc->pp, "%s", cmd); |
3625 | 0 | if(!result) { |
3626 | 0 | pp->response = *Curl_pgrs_now(data); /* timeout relative now */ |
3627 | 0 | result = getftpresponse(data, &nread, &ftpcode); |
3628 | 0 | } |
3629 | 0 | if(result) |
3630 | 0 | return result; |
3631 | | |
3632 | 0 | if(!acceptfail && (ftpcode >= 400)) { |
3633 | 0 | failf(data, "QUOT string not accepted: %s", cmd); |
3634 | 0 | return CURLE_QUOTE_ERROR; |
3635 | 0 | } |
3636 | 0 | } |
3637 | | |
3638 | 0 | item = item->next; |
3639 | 0 | } |
3640 | | |
3641 | 0 | return CURLE_OK; |
3642 | 0 | } |
3643 | | |
3644 | | /*********************************************************************** |
3645 | | * |
3646 | | * ftp_done() |
3647 | | * |
3648 | | * The DONE function. This does what needs to be done after a single DO has |
3649 | | * performed. |
3650 | | * |
3651 | | * Input argument is already checked for validity. |
3652 | | */ |
3653 | | static CURLcode ftp_done(struct Curl_easy *data, CURLcode status, |
3654 | | bool premature) |
3655 | 2.65k | { |
3656 | 2.65k | struct connectdata *conn = data->conn; |
3657 | 2.65k | struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY); |
3658 | 2.65k | struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN); |
3659 | 2.65k | struct pingpong *pp; |
3660 | 2.65k | size_t nread; |
3661 | 2.65k | int ftpcode; |
3662 | 2.65k | CURLcode result = CURLE_OK; |
3663 | | |
3664 | 2.65k | if(!ftp || !ftpc) |
3665 | 0 | return CURLE_OK; |
3666 | | |
3667 | 2.65k | pp = &ftpc->pp; |
3668 | 2.65k | switch(status) { |
3669 | 88 | case CURLE_BAD_DOWNLOAD_RESUME: |
3670 | 120 | case CURLE_FTP_WEIRD_PASV_REPLY: |
3671 | 136 | case CURLE_FTP_PORT_FAILED: |
3672 | 136 | case CURLE_FTP_ACCEPT_FAILED: |
3673 | 136 | case CURLE_FTP_ACCEPT_TIMEOUT: |
3674 | 212 | case CURLE_FTP_COULDNT_SET_TYPE: |
3675 | 224 | case CURLE_FTP_COULDNT_RETR_FILE: |
3676 | 494 | case CURLE_PARTIAL_FILE: |
3677 | 495 | case CURLE_UPLOAD_FAILED: |
3678 | 507 | case CURLE_REMOTE_ACCESS_DENIED: |
3679 | 542 | case CURLE_FILESIZE_EXCEEDED: |
3680 | 546 | case CURLE_REMOTE_FILE_NOT_FOUND: |
3681 | 546 | case CURLE_WRITE_ERROR: |
3682 | | /* the connection stays alive fine even though this happened */ |
3683 | 1.55k | case CURLE_OK: /* does not affect the control connection's status */ |
3684 | 1.55k | if(!premature) |
3685 | 1.22k | break; |
3686 | | |
3687 | | /* until we cope better with prematurely ended requests, let them |
3688 | | * fallback as if in complete failure */ |
3689 | 328 | FALLTHROUGH(); |
3690 | 1.43k | default: /* by default, an error means the control connection is |
3691 | | wedged and should not be used anymore */ |
3692 | 1.43k | ftpc->ctl_valid = FALSE; |
3693 | 1.43k | ftpc->cwdfail = TRUE; /* set this TRUE to prevent us to remember the |
3694 | | current path, as this connection is going */ |
3695 | 1.43k | connclose(conn, "FTP ended with bad error code"); |
3696 | 1.43k | result = status; /* use the already set error code */ |
3697 | 1.43k | break; |
3698 | 2.65k | } |
3699 | | |
3700 | 2.65k | if(data->state.wildcardmatch) { |
3701 | 712 | if(data->set.chunk_end && ftpc->file) { |
3702 | 0 | Curl_set_in_callback(data, TRUE); |
3703 | 0 | data->set.chunk_end(data->set.wildcardptr); |
3704 | 0 | Curl_set_in_callback(data, FALSE); |
3705 | 0 | freedirs(ftpc); |
3706 | 0 | } |
3707 | 712 | ftpc->known_filesize = -1; |
3708 | 712 | } |
3709 | | |
3710 | 2.65k | if(result) { |
3711 | | /* We can limp along anyway (and should try to since we may already be in |
3712 | | * the error path) */ |
3713 | 1.37k | ftpc->ctl_valid = FALSE; /* mark control connection as bad */ |
3714 | 1.37k | connclose(conn, "FTP: out of memory!"); /* mark for connection closure */ |
3715 | 1.37k | curlx_free(ftpc->prevpath); |
3716 | 1.37k | ftpc->prevpath = NULL; /* no path remembering */ |
3717 | 1.37k | } |
3718 | 1.28k | else { /* remember working directory for connection reuse */ |
3719 | 1.28k | const char *rawPath = ftpc->rawpath; |
3720 | 1.28k | if(rawPath) { |
3721 | 1.14k | if((data->set.ftp_filemethod == FTPFILE_NOCWD) && (rawPath[0] == '/')) |
3722 | 1 | ; /* full path => no CWDs happened => keep ftpc->prevpath */ |
3723 | 1.14k | else { |
3724 | 1.14k | size_t pathLen = strlen(ftpc->rawpath); |
3725 | | |
3726 | 1.14k | curlx_free(ftpc->prevpath); |
3727 | | |
3728 | 1.14k | if(!ftpc->cwdfail) { |
3729 | 1.10k | if(data->set.ftp_filemethod == FTPFILE_NOCWD) |
3730 | 1 | pathLen = 0; /* relative path => working directory is FTP home */ |
3731 | 1.10k | else |
3732 | | /* file is URL-decoded */ |
3733 | 1.10k | pathLen -= ftpc->file ? strlen(ftpc->file) : 0; |
3734 | 1.10k | ftpc->prevpath = curlx_memdup0(rawPath, pathLen); |
3735 | 1.10k | } |
3736 | 34 | else |
3737 | 34 | ftpc->prevpath = NULL; /* no path */ |
3738 | 1.14k | } |
3739 | 1.14k | } |
3740 | 1.28k | if(ftpc->prevpath) |
3741 | 1.10k | infof(data, "Remembering we are in directory \"%s\"", ftpc->prevpath); |
3742 | 1.28k | } |
3743 | | |
3744 | | /* shut down the socket to inform the server we are done */ |
3745 | | |
3746 | 2.65k | if(Curl_conn_is_setup(conn, SECONDARYSOCKET)) { |
3747 | 1.49k | if(!result && ftpc->dont_check && data->req.maxdownload > 0) { |
3748 | | /* partial download completed */ |
3749 | 213 | result = Curl_pp_sendf(data, pp, "%s", "ABOR"); |
3750 | 213 | if(result) { |
3751 | 0 | failf(data, "Failure sending ABOR command: %s", |
3752 | 0 | curl_easy_strerror(result)); |
3753 | 0 | ftpc->ctl_valid = FALSE; /* mark control connection as bad */ |
3754 | 0 | connclose(conn, "ABOR command failed"); /* connection closure */ |
3755 | 0 | } |
3756 | 213 | } |
3757 | | |
3758 | 1.49k | close_secondarysocket(data, ftpc); |
3759 | 1.49k | } |
3760 | | |
3761 | 2.65k | if(!result && (ftp->transfer == PPTRANSFER_BODY) && ftpc->ctl_valid && |
3762 | 1.00k | pp->pending_resp && !premature) { |
3763 | | /* |
3764 | | * Let's see what the server says about the transfer we performed, but |
3765 | | * lower the timeout as sometimes this connection has died while the data |
3766 | | * has been transferred. This happens when doing through NATs etc that |
3767 | | * abandon old silent connections. |
3768 | | */ |
3769 | 819 | pp->response = *Curl_pgrs_now(data); /* timeout relative now */ |
3770 | 819 | result = getftpresponse(data, &nread, &ftpcode); |
3771 | | |
3772 | 819 | if(!nread && (result == CURLE_OPERATION_TIMEDOUT)) { |
3773 | 11 | failf(data, "control connection looks dead"); |
3774 | 11 | ftpc->ctl_valid = FALSE; /* mark control connection as bad */ |
3775 | 11 | connclose(conn, "Timeout or similar in FTP DONE operation"); /* close */ |
3776 | 11 | } |
3777 | | |
3778 | 819 | if(result) |
3779 | 709 | return result; |
3780 | | |
3781 | 110 | if(ftpc->dont_check && data->req.maxdownload > 0) { |
3782 | | /* we have sent ABOR and there is no reliable way to check if it was |
3783 | | * successful or not; we have to close the connection now */ |
3784 | 62 | infof(data, "partial download completed, closing connection"); |
3785 | 62 | connclose(conn, "Partial download with no ability to check"); |
3786 | 62 | return result; |
3787 | 62 | } |
3788 | | |
3789 | 48 | if(!ftpc->dont_check) { |
3790 | | /* 226 Transfer complete, 250 Requested file action okay, completed. */ |
3791 | 48 | switch(ftpcode) { |
3792 | 17 | case 226: |
3793 | 20 | case 250: |
3794 | 20 | break; |
3795 | 1 | case 552: |
3796 | 1 | failf(data, "Exceeded storage allocation"); |
3797 | 1 | result = CURLE_REMOTE_DISK_FULL; |
3798 | 1 | break; |
3799 | 27 | default: |
3800 | 27 | failf(data, "server did not report OK, got %d", ftpcode); |
3801 | 27 | result = CURLE_PARTIAL_FILE; |
3802 | 27 | break; |
3803 | 48 | } |
3804 | 48 | } |
3805 | 48 | } |
3806 | | |
3807 | 1.88k | if(result || premature) |
3808 | | /* the response code from the transfer showed an error already so no |
3809 | | use checking further */ |
3810 | 1.46k | ; |
3811 | 423 | else if(data->state.upload) { |
3812 | 52 | if((ftp->transfer == PPTRANSFER_BODY) && |
3813 | 18 | (data->state.infilesize != -1) && /* upload with known size */ |
3814 | 18 | ((!data->set.crlf && !data->state.prefer_ascii && /* no conversion */ |
3815 | 13 | (data->state.infilesize != data->req.writebytecount)) || |
3816 | 12 | ((data->set.crlf || data->state.prefer_ascii) && /* maybe crlf conv */ |
3817 | 5 | (data->state.infilesize > data->req.writebytecount)) |
3818 | 18 | )) { |
3819 | 10 | failf(data, "Uploaded unaligned file size (%" FMT_OFF_T |
3820 | 10 | " out of %" FMT_OFF_T " bytes)", |
3821 | 10 | data->req.writebytecount, data->state.infilesize); |
3822 | 10 | result = CURLE_PARTIAL_FILE; |
3823 | 10 | } |
3824 | 52 | } |
3825 | 371 | else { |
3826 | 371 | if((data->req.size != -1) && |
3827 | 0 | (data->req.size != data->req.bytecount) && |
3828 | 0 | (data->req.maxdownload != data->req.bytecount)) { |
3829 | 0 | failf(data, "Received only partial file: %" FMT_OFF_T " bytes", |
3830 | 0 | data->req.bytecount); |
3831 | 0 | result = CURLE_PARTIAL_FILE; |
3832 | 0 | } |
3833 | 371 | else if(!ftpc->dont_check && |
3834 | 369 | !data->req.bytecount && |
3835 | 353 | (data->req.size > 0)) { |
3836 | 0 | failf(data, "No data was received"); |
3837 | 0 | result = CURLE_FTP_COULDNT_RETR_FILE; |
3838 | 0 | } |
3839 | 371 | } |
3840 | | |
3841 | | /* clear these for next connection */ |
3842 | 1.88k | ftp->transfer = PPTRANSFER_BODY; |
3843 | 1.88k | ftpc->dont_check = FALSE; |
3844 | | |
3845 | | /* Send any post-transfer QUOTE strings? */ |
3846 | 1.88k | if(!status && !result && !premature && data->set.postquote) |
3847 | 0 | result = ftp_sendquote(data, ftpc, data->set.postquote); |
3848 | 1.88k | CURL_TRC_FTP(data, "[%s] done, result=%d", FTP_CSTATE(ftpc), result); |
3849 | 1.88k | return result; |
3850 | 2.65k | } |
3851 | | |
3852 | | /*********************************************************************** |
3853 | | * |
3854 | | * ftp_nb_type() |
3855 | | * |
3856 | | * Set TYPE. We only deal with ASCII or BINARY so this function |
3857 | | * sets one of them. |
3858 | | * If the transfer type is not sent, simulate on OK response in newstate |
3859 | | */ |
3860 | | static CURLcode ftp_nb_type(struct Curl_easy *data, |
3861 | | struct ftp_conn *ftpc, |
3862 | | struct FTP *ftp, |
3863 | | bool ascii, ftpstate newstate) |
3864 | 1.46k | { |
3865 | 1.46k | CURLcode result; |
3866 | 1.46k | char want = (char)(ascii ? 'A' : 'I'); |
3867 | | |
3868 | 1.46k | if(ftpc->transfertype == want) { |
3869 | 0 | ftp_state(data, ftpc, newstate); |
3870 | 0 | return ftp_state_type_resp(data, ftpc, ftp, 200, newstate); |
3871 | 0 | } |
3872 | | |
3873 | 1.46k | result = Curl_pp_sendf(data, &ftpc->pp, "TYPE %c", want); |
3874 | 1.46k | if(!result) { |
3875 | 1.46k | ftp_state(data, ftpc, newstate); |
3876 | | |
3877 | | /* keep track of our current transfer type */ |
3878 | 1.46k | ftpc->transfertype = want; |
3879 | 1.46k | } |
3880 | 1.46k | return result; |
3881 | 1.46k | } |
3882 | | |
3883 | | /*********************************************************************** |
3884 | | * |
3885 | | * ftp_perform() |
3886 | | * |
3887 | | * This is the actual DO function for FTP. Get a file/directory according to |
3888 | | * the options previously setup. |
3889 | | */ |
3890 | | static CURLcode ftp_perform( |
3891 | | struct Curl_easy *data, |
3892 | | struct ftp_conn *ftpc, |
3893 | | struct FTP *ftp, |
3894 | | bool *connected, /* connect status after PASV / PORT */ |
3895 | | bool *dophase_done) |
3896 | 1.90k | { |
3897 | | /* this is FTP and no proxy */ |
3898 | 1.90k | CURLcode result = CURLE_OK; |
3899 | | |
3900 | 1.90k | CURL_TRC_FTP(data, "[%s] DO phase starts", FTP_CSTATE(ftpc)); |
3901 | | |
3902 | 1.90k | if(data->req.no_body) { |
3903 | | /* requested no body means no transfer... */ |
3904 | 141 | ftp->transfer = PPTRANSFER_INFO; |
3905 | 141 | } |
3906 | | |
3907 | 1.90k | *dophase_done = FALSE; /* not done yet */ |
3908 | | |
3909 | | /* start the first command in the DO phase */ |
3910 | 1.90k | result = ftp_state_quote(data, ftpc, ftp, TRUE, FTP_QUOTE); |
3911 | 1.90k | if(result) |
3912 | 17 | return result; |
3913 | | |
3914 | | /* run the state-machine */ |
3915 | 1.88k | result = ftp_statemach(data, ftpc, dophase_done); |
3916 | | |
3917 | 1.88k | *connected = Curl_conn_is_connected(data->conn, SECONDARYSOCKET); |
3918 | | |
3919 | 1.88k | if(*connected) |
3920 | 0 | infof(data, "[FTP] [%s] perform, DATA connection established", |
3921 | 1.88k | FTP_CSTATE(ftpc)); |
3922 | 1.88k | else |
3923 | 1.88k | CURL_TRC_FTP(data, "[%s] perform, awaiting DATA connect", |
3924 | 1.88k | FTP_CSTATE(ftpc)); |
3925 | | |
3926 | 1.88k | if(*dophase_done) |
3927 | 924 | CURL_TRC_FTP(data, "[%s] DO phase is complete1", FTP_CSTATE(ftpc)); |
3928 | | |
3929 | 1.88k | return result; |
3930 | 1.90k | } |
3931 | | |
3932 | | static void wc_data_dtor(void *ptr) |
3933 | 427 | { |
3934 | 427 | struct ftp_wc *ftpwc = ptr; |
3935 | 427 | if(ftpwc && ftpwc->parser) |
3936 | 427 | Curl_ftp_parselist_data_free(&ftpwc->parser); |
3937 | 427 | curlx_free(ftpwc); |
3938 | 427 | } |
3939 | | |
3940 | | static CURLcode init_wc_data(struct Curl_easy *data, |
3941 | | struct ftp_conn *ftpc, |
3942 | | struct FTP *ftp) |
3943 | 521 | { |
3944 | 521 | char *last_slash; |
3945 | 521 | char *path = ftp->path; |
3946 | 521 | struct WildcardData *wildcard = data->wildcard; |
3947 | 521 | CURLcode result = CURLE_OK; |
3948 | 521 | struct ftp_wc *ftpwc = NULL; |
3949 | | |
3950 | 521 | last_slash = strrchr(ftp->path, '/'); |
3951 | 521 | if(last_slash) { |
3952 | 87 | last_slash++; |
3953 | 87 | if(last_slash[0] == '\0') { |
3954 | 3 | wildcard->state = CURLWC_CLEAN; |
3955 | 3 | return ftp_parse_url_path(data, ftpc, ftp); |
3956 | 3 | } |
3957 | 84 | wildcard->pattern = curlx_strdup(last_slash); |
3958 | 84 | if(!wildcard->pattern) |
3959 | 0 | return CURLE_OUT_OF_MEMORY; |
3960 | 84 | last_slash[0] = '\0'; /* cut file from path */ |
3961 | 84 | } |
3962 | 434 | else { /* there is only 'wildcard pattern' or nothing */ |
3963 | 434 | if(path[0]) { |
3964 | 344 | wildcard->pattern = curlx_strdup(path); |
3965 | 344 | if(!wildcard->pattern) |
3966 | 0 | return CURLE_OUT_OF_MEMORY; |
3967 | 344 | path[0] = '\0'; |
3968 | 344 | } |
3969 | 90 | else { /* only list */ |
3970 | 90 | wildcard->state = CURLWC_CLEAN; |
3971 | 90 | return ftp_parse_url_path(data, ftpc, ftp); |
3972 | 90 | } |
3973 | 434 | } |
3974 | | |
3975 | | /* program continues only if URL is not ending with slash, allocate needed |
3976 | | resources for wildcard transfer */ |
3977 | | |
3978 | | /* allocate ftp protocol specific wildcard data */ |
3979 | 428 | ftpwc = curlx_calloc(1, sizeof(struct ftp_wc)); |
3980 | 428 | if(!ftpwc) { |
3981 | 0 | result = CURLE_OUT_OF_MEMORY; |
3982 | 0 | goto fail; |
3983 | 0 | } |
3984 | | |
3985 | | /* INITIALIZE parselist structure */ |
3986 | 428 | ftpwc->parser = Curl_ftp_parselist_data_alloc(); |
3987 | 428 | if(!ftpwc->parser) { |
3988 | 0 | result = CURLE_OUT_OF_MEMORY; |
3989 | 0 | goto fail; |
3990 | 0 | } |
3991 | | |
3992 | 428 | wildcard->ftpwc = ftpwc; /* put it to the WildcardData tmp pointer */ |
3993 | 428 | wildcard->dtor = wc_data_dtor; |
3994 | | |
3995 | | /* wildcard does not support NOCWD option (assert it?) */ |
3996 | 428 | if(data->set.ftp_filemethod == FTPFILE_NOCWD) |
3997 | 1 | data->set.ftp_filemethod = FTPFILE_MULTICWD; |
3998 | | |
3999 | | /* try to parse ftp URL */ |
4000 | 428 | result = ftp_parse_url_path(data, ftpc, ftp); |
4001 | 428 | if(result) { |
4002 | 1 | goto fail; |
4003 | 1 | } |
4004 | | |
4005 | 427 | wildcard->path = curlx_strdup(ftp->path); |
4006 | 427 | if(!wildcard->path) { |
4007 | 0 | result = CURLE_OUT_OF_MEMORY; |
4008 | 0 | goto fail; |
4009 | 0 | } |
4010 | | |
4011 | | /* backup old write_function */ |
4012 | 427 | ftpwc->backup.write_function = data->set.fwrite_func; |
4013 | | /* parsing write function */ |
4014 | 427 | data->set.fwrite_func = Curl_ftp_parselist; |
4015 | | /* backup old file descriptor */ |
4016 | 427 | ftpwc->backup.file_descriptor = data->set.out; |
4017 | | /* let the writefunc callback know the transfer */ |
4018 | 427 | data->set.out = data; |
4019 | | |
4020 | 427 | infof(data, "Wildcard - Parsing started"); |
4021 | 427 | return CURLE_OK; |
4022 | | |
4023 | 1 | fail: |
4024 | 1 | if(ftpwc) { |
4025 | 1 | Curl_ftp_parselist_data_free(&ftpwc->parser); |
4026 | 1 | curlx_free(ftpwc); |
4027 | 1 | } |
4028 | 1 | curlx_safefree(wildcard->pattern); |
4029 | 1 | wildcard->dtor = ZERO_NULL; |
4030 | 1 | wildcard->ftpwc = NULL; |
4031 | 1 | return result; |
4032 | 427 | } |
4033 | | |
4034 | | static CURLcode wc_statemach(struct Curl_easy *data, |
4035 | | struct ftp_conn *ftpc, |
4036 | | struct FTP *ftp) |
4037 | 540 | { |
4038 | 540 | struct WildcardData * const wildcard = data->wildcard; |
4039 | 540 | CURLcode result = CURLE_OK; |
4040 | | |
4041 | 588 | for(;;) { |
4042 | 588 | switch(wildcard->state) { |
4043 | 521 | case CURLWC_INIT: |
4044 | 521 | result = init_wc_data(data, ftpc, ftp); |
4045 | 521 | if(wildcard->state == CURLWC_CLEAN) |
4046 | | /* only listing! */ |
4047 | 93 | return result; |
4048 | 428 | wildcard->state = result ? CURLWC_ERROR : CURLWC_MATCHING; |
4049 | 428 | return result; |
4050 | | |
4051 | 15 | case CURLWC_MATCHING: { |
4052 | | /* In this state is LIST response successfully parsed, so lets restore |
4053 | | previous WRITEFUNCTION callback and WRITEDATA pointer */ |
4054 | 15 | struct ftp_wc *ftpwc = wildcard->ftpwc; |
4055 | 15 | data->set.fwrite_func = ftpwc->backup.write_function; |
4056 | 15 | data->set.out = ftpwc->backup.file_descriptor; |
4057 | 15 | ftpwc->backup.write_function = ZERO_NULL; |
4058 | 15 | ftpwc->backup.file_descriptor = NULL; |
4059 | 15 | wildcard->state = CURLWC_DOWNLOADING; |
4060 | | |
4061 | 15 | if(Curl_ftp_parselist_geterror(ftpwc->parser)) { |
4062 | | /* error found in LIST parsing */ |
4063 | 3 | wildcard->state = CURLWC_CLEAN; |
4064 | 3 | continue; |
4065 | 3 | } |
4066 | 12 | if(Curl_llist_count(&wildcard->filelist) == 0) { |
4067 | | /* no corresponding file */ |
4068 | 3 | wildcard->state = CURLWC_CLEAN; |
4069 | 3 | return CURLE_REMOTE_FILE_NOT_FOUND; |
4070 | 3 | } |
4071 | 9 | continue; |
4072 | 12 | } |
4073 | | |
4074 | 25 | case CURLWC_DOWNLOADING: { |
4075 | | /* filelist has at least one file, lets get first one */ |
4076 | 25 | struct Curl_llist_node *head = Curl_llist_head(&wildcard->filelist); |
4077 | 25 | struct curl_fileinfo *finfo = Curl_node_elem(head); |
4078 | | |
4079 | 25 | char *tmp_path = curl_maprintf("%s%s", wildcard->path, finfo->filename); |
4080 | 25 | if(!tmp_path) |
4081 | 0 | return CURLE_OUT_OF_MEMORY; |
4082 | | |
4083 | | /* switch default ftp->path and tmp_path */ |
4084 | 25 | curlx_free(ftp->pathalloc); |
4085 | 25 | ftp->pathalloc = ftp->path = tmp_path; |
4086 | | |
4087 | 25 | infof(data, "Wildcard - START of \"%s\"", finfo->filename); |
4088 | 25 | if(data->set.chunk_bgn) { |
4089 | 0 | long userresponse; |
4090 | 0 | Curl_set_in_callback(data, TRUE); |
4091 | 0 | userresponse = data->set.chunk_bgn( |
4092 | 0 | finfo, data->set.wildcardptr, |
4093 | 0 | (int)Curl_llist_count(&wildcard->filelist)); |
4094 | 0 | Curl_set_in_callback(data, FALSE); |
4095 | 0 | switch(userresponse) { |
4096 | 0 | case CURL_CHUNK_BGN_FUNC_SKIP: |
4097 | 0 | infof(data, "Wildcard - \"%s\" skipped by user", finfo->filename); |
4098 | 0 | wildcard->state = CURLWC_SKIP; |
4099 | 0 | continue; |
4100 | 0 | case CURL_CHUNK_BGN_FUNC_FAIL: |
4101 | 0 | return CURLE_CHUNK_FAILED; |
4102 | 0 | } |
4103 | 0 | } |
4104 | | |
4105 | 25 | if(finfo->filetype != CURLFILETYPE_FILE) { |
4106 | 18 | wildcard->state = CURLWC_SKIP; |
4107 | 18 | continue; |
4108 | 18 | } |
4109 | | |
4110 | 7 | if(finfo->flags & CURLFINFOFLAG_KNOWN_SIZE) |
4111 | 7 | ftpc->known_filesize = finfo->size; |
4112 | | |
4113 | 7 | result = ftp_parse_url_path(data, ftpc, ftp); |
4114 | 7 | if(result) |
4115 | 1 | return result; |
4116 | | |
4117 | | /* we do not need the Curl_fileinfo of first file anymore */ |
4118 | 6 | Curl_node_remove(Curl_llist_head(&wildcard->filelist)); |
4119 | | |
4120 | 6 | if(Curl_llist_count(&wildcard->filelist) == 0) { |
4121 | | /* remains only one file to down. */ |
4122 | 3 | wildcard->state = CURLWC_CLEAN; |
4123 | | /* after that will be ftp_do called once again and no transfer |
4124 | | will be done because of CURLWC_CLEAN state */ |
4125 | 3 | return CURLE_OK; |
4126 | 3 | } |
4127 | 3 | return result; |
4128 | 6 | } |
4129 | | |
4130 | 18 | case CURLWC_SKIP: { |
4131 | 18 | if(data->set.chunk_end) { |
4132 | 0 | Curl_set_in_callback(data, TRUE); |
4133 | 0 | data->set.chunk_end(data->set.wildcardptr); |
4134 | 0 | Curl_set_in_callback(data, FALSE); |
4135 | 0 | } |
4136 | 18 | Curl_node_remove(Curl_llist_head(&wildcard->filelist)); |
4137 | 18 | wildcard->state = (Curl_llist_count(&wildcard->filelist) == 0) ? |
4138 | 16 | CURLWC_CLEAN : CURLWC_DOWNLOADING; |
4139 | 18 | continue; |
4140 | 6 | } |
4141 | | |
4142 | 9 | case CURLWC_CLEAN: { |
4143 | 9 | struct ftp_wc *ftpwc = wildcard->ftpwc; |
4144 | 9 | result = CURLE_OK; |
4145 | 9 | if(ftpwc) |
4146 | 5 | result = Curl_ftp_parselist_geterror(ftpwc->parser); |
4147 | | |
4148 | 9 | wildcard->state = result ? CURLWC_ERROR : CURLWC_DONE; |
4149 | 9 | return result; |
4150 | 6 | } |
4151 | | |
4152 | 0 | case CURLWC_DONE: |
4153 | 0 | case CURLWC_ERROR: |
4154 | 0 | case CURLWC_CLEAR: |
4155 | 0 | if(wildcard->dtor) { |
4156 | 0 | wildcard->dtor(wildcard->ftpwc); |
4157 | 0 | wildcard->ftpwc = NULL; |
4158 | 0 | } |
4159 | 0 | return result; |
4160 | 588 | } |
4161 | 588 | } |
4162 | | /* UNREACHABLE */ |
4163 | 540 | } |
4164 | | |
4165 | | /*********************************************************************** |
4166 | | * |
4167 | | * ftp_regular_transfer() |
4168 | | * |
4169 | | * The input argument is already checked for validity. |
4170 | | * |
4171 | | * Performs all commands done before a regular transfer between a local and a |
4172 | | * remote host. |
4173 | | * |
4174 | | * ftp->ctl_valid starts out as FALSE, and gets set to TRUE if we reach the |
4175 | | * ftp_done() function without finding any major problem. |
4176 | | */ |
4177 | | static CURLcode ftp_regular_transfer(struct Curl_easy *data, |
4178 | | struct ftp_conn *ftpc, |
4179 | | struct FTP *ftp, |
4180 | | bool *dophase_done) |
4181 | 1.90k | { |
4182 | 1.90k | CURLcode result = CURLE_OK; |
4183 | 1.90k | bool connected = FALSE; |
4184 | 1.90k | data->req.size = -1; /* make sure this is unknown at this point */ |
4185 | | |
4186 | 1.90k | Curl_pgrsReset(data); |
4187 | | |
4188 | 1.90k | ftpc->ctl_valid = TRUE; /* starts good */ |
4189 | | |
4190 | 1.90k | result = ftp_perform(data, ftpc, ftp, |
4191 | 1.90k | &connected, /* have we connected after PASV/PORT */ |
4192 | 1.90k | dophase_done); /* all commands in the DO-phase done? */ |
4193 | | |
4194 | 1.90k | if(!result) { |
4195 | | |
4196 | 1.81k | if(!*dophase_done) |
4197 | | /* the DO phase has not completed yet */ |
4198 | 893 | return CURLE_OK; |
4199 | | |
4200 | 922 | result = ftp_dophase_done(data, ftpc, ftp, connected); |
4201 | | |
4202 | 922 | if(result) |
4203 | 0 | return result; |
4204 | 922 | } |
4205 | 86 | else |
4206 | 86 | freedirs(ftpc); |
4207 | | |
4208 | 1.00k | return result; |
4209 | 1.90k | } |
4210 | | |
4211 | | /*********************************************************************** |
4212 | | * |
4213 | | * ftp_do() |
4214 | | * |
4215 | | * This function is registered as 'curl_do' function. It decodes the path |
4216 | | * parts etc as a wrapper to the actual DO function (ftp_perform). |
4217 | | * |
4218 | | * The input argument is already checked for validity. |
4219 | | */ |
4220 | | static CURLcode ftp_do(struct Curl_easy *data, bool *done) |
4221 | 1.91k | { |
4222 | 1.91k | CURLcode result = CURLE_OK; |
4223 | 1.91k | struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN); |
4224 | 1.91k | struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY); |
4225 | | |
4226 | 1.91k | *done = FALSE; /* default to false */ |
4227 | 1.91k | if(!ftpc || !ftp) |
4228 | 0 | return CURLE_FAILED_INIT; |
4229 | 1.91k | ftpc->wait_data_conn = FALSE; /* default to no such wait */ |
4230 | | |
4231 | 1.91k | #ifdef CURL_PREFER_LF_LINEENDS |
4232 | 1.91k | { |
4233 | | /* FTP data may need conversion. */ |
4234 | 1.91k | struct Curl_cwriter *ftp_lc_writer; |
4235 | | |
4236 | 1.91k | result = Curl_cwriter_create(&ftp_lc_writer, data, &ftp_cw_lc, |
4237 | 1.91k | CURL_CW_CONTENT_DECODE); |
4238 | 1.91k | if(result) |
4239 | 0 | return result; |
4240 | | |
4241 | 1.91k | result = Curl_cwriter_add(data, ftp_lc_writer); |
4242 | 1.91k | if(result) { |
4243 | 0 | Curl_cwriter_free(data, ftp_lc_writer); |
4244 | 0 | return result; |
4245 | 0 | } |
4246 | 1.91k | } |
4247 | 1.91k | #endif /* CURL_PREFER_LF_LINEENDS */ |
4248 | | |
4249 | 1.91k | if(data->state.wildcardmatch) { |
4250 | 540 | result = wc_statemach(data, ftpc, ftp); |
4251 | 540 | if(data->wildcard->state == CURLWC_SKIP || |
4252 | 540 | data->wildcard->state == CURLWC_DONE) { |
4253 | | /* do not call ftp_regular_transfer */ |
4254 | 6 | return CURLE_OK; |
4255 | 6 | } |
4256 | 534 | if(result) /* error, loop or skipping the file */ |
4257 | 8 | return result; |
4258 | 534 | } |
4259 | 1.37k | else { /* no wildcard FSM needed */ |
4260 | 1.37k | result = ftp_parse_url_path(data, ftpc, ftp); |
4261 | 1.37k | if(result) |
4262 | 2 | return result; |
4263 | 1.37k | } |
4264 | | |
4265 | 1.90k | result = ftp_regular_transfer(data, ftpc, ftp, done); |
4266 | | |
4267 | 1.90k | return result; |
4268 | 1.91k | } |
4269 | | |
4270 | | /*********************************************************************** |
4271 | | * |
4272 | | * ftp_quit() |
4273 | | * |
4274 | | * This should be called before calling sclose() on an ftp control connection |
4275 | | * (not data connections). We should then wait for the response from the |
4276 | | * server before returning. The calling code should then try to close the |
4277 | | * connection. |
4278 | | * |
4279 | | */ |
4280 | | static CURLcode ftp_quit(struct Curl_easy *data, |
4281 | | struct ftp_conn *ftpc) |
4282 | 5.71k | { |
4283 | 5.71k | CURLcode result = CURLE_OK; |
4284 | | |
4285 | 5.71k | if(ftpc->ctl_valid) { |
4286 | 1.12k | CURL_TRC_FTP(data, "sending QUIT to close session"); |
4287 | 1.12k | result = Curl_pp_sendf(data, &ftpc->pp, "%s", "QUIT"); |
4288 | 1.12k | if(result) { |
4289 | 0 | failf(data, "Failure sending QUIT command: %s", |
4290 | 0 | curl_easy_strerror(result)); |
4291 | 0 | ftpc->ctl_valid = FALSE; /* mark control connection as bad */ |
4292 | 0 | connclose(data->conn, "QUIT command failed"); /* mark for closure */ |
4293 | 0 | ftp_state(data, ftpc, FTP_STOP); |
4294 | 0 | return result; |
4295 | 0 | } |
4296 | | |
4297 | 1.12k | ftp_state(data, ftpc, FTP_QUIT); |
4298 | | |
4299 | 1.12k | result = ftp_block_statemach(data, ftpc); |
4300 | 1.12k | } |
4301 | | |
4302 | 5.71k | return result; |
4303 | 5.71k | } |
4304 | | |
4305 | | /*********************************************************************** |
4306 | | * |
4307 | | * ftp_disconnect() |
4308 | | * |
4309 | | * Disconnect from an FTP server. Cleanup protocol-specific per-connection |
4310 | | * resources. BLOCKING. |
4311 | | */ |
4312 | | static CURLcode ftp_disconnect(struct Curl_easy *data, |
4313 | | struct connectdata *conn, |
4314 | | bool dead_connection) |
4315 | 5.72k | { |
4316 | 5.72k | struct ftp_conn *ftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN); |
4317 | | |
4318 | 5.72k | if(!ftpc) |
4319 | 17 | return CURLE_FAILED_INIT; |
4320 | | /* We cannot send quit unconditionally. If this connection is stale or |
4321 | | bad in any way, sending quit and waiting around here will make the |
4322 | | disconnect wait in vain and cause more problems than we need to. |
4323 | | |
4324 | | ftp_quit() will check the state of ftp->ctl_valid. If it is ok it |
4325 | | will try to send the QUIT command, otherwise it will return. |
4326 | | */ |
4327 | 5.71k | ftpc->shutdown = TRUE; |
4328 | 5.71k | if(dead_connection || Curl_pp_needs_flush(data, &ftpc->pp)) |
4329 | 4.11k | ftpc->ctl_valid = FALSE; |
4330 | | |
4331 | | /* The FTP session may or may not have been allocated/setup at this point! */ |
4332 | 5.71k | (void)ftp_quit(data, ftpc); /* ignore errors on the QUIT */ |
4333 | 5.71k | return CURLE_OK; |
4334 | 5.72k | } |
4335 | | |
4336 | | /* called from multi.c while DOing */ |
4337 | | static CURLcode ftp_doing(struct Curl_easy *data, |
4338 | | bool *dophase_done) |
4339 | 1.80k | { |
4340 | 1.80k | struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN); |
4341 | 1.80k | struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY); |
4342 | 1.80k | CURLcode result; |
4343 | | |
4344 | 1.80k | if(!ftpc || !ftp) |
4345 | 0 | return CURLE_FAILED_INIT; |
4346 | 1.80k | result = ftp_statemach(data, ftpc, dophase_done); |
4347 | | |
4348 | 1.80k | if(result) |
4349 | 204 | CURL_TRC_FTP(data, "[%s] DO phase failed", FTP_CSTATE(ftpc)); |
4350 | 1.60k | else if(*dophase_done) { |
4351 | 685 | result = ftp_dophase_done(data, ftpc, ftp, FALSE /* not connected */); |
4352 | | |
4353 | 685 | CURL_TRC_FTP(data, "[%s] DO phase is complete2", FTP_CSTATE(ftpc)); |
4354 | 685 | } |
4355 | 1.80k | return result; |
4356 | 1.80k | } |
4357 | | |
4358 | | static void ftp_easy_dtor(void *key, size_t klen, void *entry) |
4359 | 5.71k | { |
4360 | 5.71k | struct FTP *ftp = entry; |
4361 | 5.71k | (void)key; |
4362 | 5.71k | (void)klen; |
4363 | 5.71k | curlx_safefree(ftp->pathalloc); |
4364 | 5.71k | curlx_free(ftp); |
4365 | 5.71k | } |
4366 | | |
4367 | | static void ftp_conn_dtor(void *key, size_t klen, void *entry) |
4368 | 5.71k | { |
4369 | 5.71k | struct ftp_conn *ftpc = entry; |
4370 | 5.71k | (void)key; |
4371 | 5.71k | (void)klen; |
4372 | 5.71k | freedirs(ftpc); |
4373 | 5.71k | curlx_safefree(ftpc->account); |
4374 | 5.71k | curlx_safefree(ftpc->alternative_to_user); |
4375 | 5.71k | curlx_safefree(ftpc->entrypath); |
4376 | 5.71k | curlx_safefree(ftpc->prevpath); |
4377 | 5.71k | curlx_safefree(ftpc->server_os); |
4378 | 5.71k | Curl_pp_disconnect(&ftpc->pp); |
4379 | 5.71k | curlx_free(ftpc); |
4380 | 5.71k | } |
4381 | | |
4382 | | static void type_url_check(struct Curl_easy *data, struct FTP *ftp) |
4383 | 5.71k | { |
4384 | 5.71k | size_t len = strlen(ftp->path); |
4385 | | /* FTP URLs support an extension like ";type=<typecode>" that |
4386 | | * we will try to get now! */ |
4387 | 5.71k | if((len >= 7) && !memcmp(&ftp->path[len - 7], ";type=", 6)) { |
4388 | 61 | char *type = &ftp->path[len - 7]; |
4389 | 61 | char command = Curl_raw_toupper(type[6]); |
4390 | | |
4391 | 61 | *type = 0; /* cut it off */ |
4392 | | |
4393 | 61 | switch(command) { |
4394 | 41 | case 'A': /* ASCII mode */ |
4395 | 41 | data->state.prefer_ascii = TRUE; |
4396 | 41 | break; |
4397 | | |
4398 | 7 | case 'D': /* directory mode */ |
4399 | 7 | data->state.list_only = TRUE; |
4400 | 7 | break; |
4401 | | |
4402 | 0 | case 'I': /* binary mode */ |
4403 | 13 | default: |
4404 | | /* switch off ASCII */ |
4405 | 13 | data->state.prefer_ascii = FALSE; |
4406 | 13 | break; |
4407 | 61 | } |
4408 | 61 | } |
4409 | 5.71k | } |
4410 | | |
4411 | | static CURLcode ftp_setup_connection(struct Curl_easy *data, |
4412 | | struct connectdata *conn) |
4413 | 5.71k | { |
4414 | 5.71k | struct FTP *ftp; |
4415 | 5.71k | CURLcode result = CURLE_OK; |
4416 | 5.71k | struct ftp_conn *ftpc; |
4417 | | |
4418 | 5.71k | ftp = curlx_calloc(1, sizeof(*ftp)); |
4419 | 5.71k | if(!ftp || |
4420 | 5.71k | Curl_meta_set(data, CURL_META_FTP_EASY, ftp, ftp_easy_dtor)) |
4421 | 0 | return CURLE_OUT_OF_MEMORY; |
4422 | | |
4423 | 5.71k | ftpc = curlx_calloc(1, sizeof(*ftpc)); |
4424 | 5.71k | if(!ftpc || |
4425 | 5.71k | Curl_conn_meta_set(conn, CURL_META_FTP_CONN, ftpc, ftp_conn_dtor)) |
4426 | 0 | return CURLE_OUT_OF_MEMORY; |
4427 | | |
4428 | | /* clone connection related data that is FTP specific */ |
4429 | 5.71k | if(data->set.str[STRING_FTP_ACCOUNT]) { |
4430 | 12 | ftpc->account = curlx_strdup(data->set.str[STRING_FTP_ACCOUNT]); |
4431 | 12 | if(!ftpc->account) { |
4432 | 0 | Curl_conn_meta_remove(conn, CURL_META_FTP_CONN); |
4433 | 0 | return CURLE_OUT_OF_MEMORY; |
4434 | 0 | } |
4435 | 12 | } |
4436 | 5.71k | if(data->set.str[STRING_FTP_ALTERNATIVE_TO_USER]) { |
4437 | 12 | ftpc->alternative_to_user = |
4438 | 12 | curlx_strdup(data->set.str[STRING_FTP_ALTERNATIVE_TO_USER]); |
4439 | 12 | if(!ftpc->alternative_to_user) { |
4440 | 0 | curlx_safefree(ftpc->account); |
4441 | 0 | Curl_conn_meta_remove(conn, CURL_META_FTP_CONN); |
4442 | 0 | return CURLE_OUT_OF_MEMORY; |
4443 | 0 | } |
4444 | 12 | } |
4445 | | |
4446 | 5.71k | ftp->path = &data->state.up.path[1]; /* do not include the initial slash */ |
4447 | | |
4448 | 5.71k | type_url_check(data, ftp); |
4449 | | |
4450 | | /* get some initial data into the ftp struct */ |
4451 | 5.71k | ftp->transfer = PPTRANSFER_BODY; |
4452 | 5.71k | ftp->downloadsize = 0; |
4453 | 5.71k | ftpc->known_filesize = -1; /* unknown size for now */ |
4454 | 5.71k | ftpc->use_ssl = data->set.use_ssl; |
4455 | 5.71k | ftpc->ccc = data->set.ftp_ccc; |
4456 | | |
4457 | 5.71k | CURL_TRC_FTP(data, "[%s] setup connection -> %d", FTP_CSTATE(ftpc), result); |
4458 | 5.71k | return result; |
4459 | 5.71k | } |
4460 | | |
4461 | | bool ftp_conns_match(struct connectdata *needle, struct connectdata *conn) |
4462 | 114 | { |
4463 | 114 | struct ftp_conn *nftpc = Curl_conn_meta_get(needle, CURL_META_FTP_CONN); |
4464 | 114 | struct ftp_conn *cftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN); |
4465 | | /* Also match ACCOUNT, ALTERNATIVE-TO-USER, USE_SSL and CCC options */ |
4466 | 114 | if(!nftpc || !cftpc || |
4467 | 114 | Curl_timestrcmp(nftpc->account, cftpc->account) || |
4468 | 114 | Curl_timestrcmp(nftpc->alternative_to_user, |
4469 | 114 | cftpc->alternative_to_user) || |
4470 | 114 | (nftpc->use_ssl != cftpc->use_ssl) || |
4471 | 114 | (nftpc->ccc != cftpc->ccc)) |
4472 | 0 | return FALSE; |
4473 | 114 | return TRUE; |
4474 | 114 | } |
4475 | | |
4476 | | /* |
4477 | | * FTP protocol. |
4478 | | */ |
4479 | | const struct Curl_protocol Curl_protocol_ftp = { |
4480 | | ftp_setup_connection, /* setup_connection */ |
4481 | | ftp_do, /* do_it */ |
4482 | | ftp_done, /* done */ |
4483 | | ftp_do_more, /* do_more */ |
4484 | | ftp_connect, /* connect_it */ |
4485 | | ftp_multi_statemach, /* connecting */ |
4486 | | ftp_doing, /* doing */ |
4487 | | ftp_pollset, /* proto_pollset */ |
4488 | | ftp_pollset, /* doing_pollset */ |
4489 | | ftp_domore_pollset, /* domore_pollset */ |
4490 | | ZERO_NULL, /* perform_pollset */ |
4491 | | ftp_disconnect, /* disconnect */ |
4492 | | ZERO_NULL, /* write_resp */ |
4493 | | ZERO_NULL, /* write_resp_hd */ |
4494 | | ZERO_NULL, /* connection_is_dead */ |
4495 | | ZERO_NULL, /* attach connection */ |
4496 | | ZERO_NULL, /* follow */ |
4497 | | }; |
4498 | | |
4499 | | #endif /* CURL_DISABLE_FTP */ |