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