PageRenderTime 70ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/ftp.c

http://github.com/bagder/curl
C | 4367 lines | 3409 code | 395 blank | 563 comment | 465 complexity | fec878c8bbbc9e0371dd7a71a897472d MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifndef CURL_DISABLE_FTP
  24. #ifdef HAVE_NETINET_IN_H
  25. #include <netinet/in.h>
  26. #endif
  27. #ifdef HAVE_ARPA_INET_H
  28. #include <arpa/inet.h>
  29. #endif
  30. #ifdef HAVE_UTSNAME_H
  31. #include <sys/utsname.h>
  32. #endif
  33. #ifdef HAVE_NETDB_H
  34. #include <netdb.h>
  35. #endif
  36. #ifdef __VMS
  37. #include <in.h>
  38. #include <inet.h>
  39. #endif
  40. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  41. #undef in_addr_t
  42. #define in_addr_t unsigned long
  43. #endif
  44. #include <curl/curl.h>
  45. #include "urldata.h"
  46. #include "sendf.h"
  47. #include "if2ip.h"
  48. #include "hostip.h"
  49. #include "progress.h"
  50. #include "transfer.h"
  51. #include "escape.h"
  52. #include "http.h" /* for HTTP proxy tunnel stuff */
  53. #include "ftp.h"
  54. #include "fileinfo.h"
  55. #include "ftplistparser.h"
  56. #include "curl_range.h"
  57. #include "curl_sec.h"
  58. #include "strtoofft.h"
  59. #include "strcase.h"
  60. #include "vtls/vtls.h"
  61. #include "connect.h"
  62. #include "strerror.h"
  63. #include "inet_ntop.h"
  64. #include "inet_pton.h"
  65. #include "select.h"
  66. #include "parsedate.h" /* for the week day and month names */
  67. #include "sockaddr.h" /* required for Curl_sockaddr_storage */
  68. #include "multiif.h"
  69. #include "url.h"
  70. #include "strcase.h"
  71. #include "speedcheck.h"
  72. #include "warnless.h"
  73. #include "http_proxy.h"
  74. #include "non-ascii.h"
  75. #include "socks.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. #ifndef NI_MAXHOST
  81. #define NI_MAXHOST 1025
  82. #endif
  83. #ifndef INET_ADDRSTRLEN
  84. #define INET_ADDRSTRLEN 16
  85. #endif
  86. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  87. #define ftp_pasv_verbose(a,b,c,d) Curl_nop_stmt
  88. #endif
  89. /* Local API functions */
  90. #ifndef DEBUGBUILD
  91. static void _state(struct connectdata *conn,
  92. ftpstate newstate);
  93. #define state(x,y) _state(x,y)
  94. #else
  95. static void _state(struct connectdata *conn,
  96. ftpstate newstate,
  97. int lineno);
  98. #define state(x,y) _state(x,y,__LINE__)
  99. #endif
  100. static CURLcode ftp_sendquote(struct connectdata *conn,
  101. struct curl_slist *quote);
  102. static CURLcode ftp_quit(struct connectdata *conn);
  103. static CURLcode ftp_parse_url_path(struct connectdata *conn);
  104. static CURLcode ftp_regular_transfer(struct connectdata *conn, bool *done);
  105. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  106. static void ftp_pasv_verbose(struct connectdata *conn,
  107. Curl_addrinfo *ai,
  108. char *newhost, /* ascii version */
  109. int port);
  110. #endif
  111. static CURLcode ftp_state_prepare_transfer(struct connectdata *conn);
  112. static CURLcode ftp_state_mdtm(struct connectdata *conn);
  113. static CURLcode ftp_state_quote(struct connectdata *conn,
  114. bool init, ftpstate instate);
  115. static CURLcode ftp_nb_type(struct connectdata *conn,
  116. bool ascii, ftpstate newstate);
  117. static int ftp_need_type(struct connectdata *conn,
  118. bool ascii);
  119. static CURLcode ftp_do(struct connectdata *conn, bool *done);
  120. static CURLcode ftp_done(struct connectdata *conn,
  121. CURLcode, bool premature);
  122. static CURLcode ftp_connect(struct connectdata *conn, bool *done);
  123. static CURLcode ftp_disconnect(struct connectdata *conn, bool dead_connection);
  124. static CURLcode ftp_do_more(struct connectdata *conn, int *completed);
  125. static CURLcode ftp_multi_statemach(struct connectdata *conn, bool *done);
  126. static int ftp_getsock(struct connectdata *conn, curl_socket_t *socks);
  127. static int ftp_domore_getsock(struct connectdata *conn, curl_socket_t *socks);
  128. static CURLcode ftp_doing(struct connectdata *conn,
  129. bool *dophase_done);
  130. static CURLcode ftp_setup_connection(struct connectdata * conn);
  131. static CURLcode init_wc_data(struct connectdata *conn);
  132. static CURLcode wc_statemach(struct connectdata *conn);
  133. static void wc_data_dtor(void *ptr);
  134. static CURLcode ftp_state_retr(struct connectdata *conn, curl_off_t filesize);
  135. static CURLcode ftp_readresp(curl_socket_t sockfd,
  136. struct pingpong *pp,
  137. int *ftpcode,
  138. size_t *size);
  139. static CURLcode ftp_dophase_done(struct connectdata *conn,
  140. bool connected);
  141. /* easy-to-use macro: */
  142. #define PPSENDF(x,y,z) result = Curl_pp_sendf(x,y,z); \
  143. if(result) \
  144. return result
  145. /*
  146. * FTP protocol handler.
  147. */
  148. const struct Curl_handler Curl_handler_ftp = {
  149. "FTP", /* scheme */
  150. ftp_setup_connection, /* setup_connection */
  151. ftp_do, /* do_it */
  152. ftp_done, /* done */
  153. ftp_do_more, /* do_more */
  154. ftp_connect, /* connect_it */
  155. ftp_multi_statemach, /* connecting */
  156. ftp_doing, /* doing */
  157. ftp_getsock, /* proto_getsock */
  158. ftp_getsock, /* doing_getsock */
  159. ftp_domore_getsock, /* domore_getsock */
  160. ZERO_NULL, /* perform_getsock */
  161. ftp_disconnect, /* disconnect */
  162. ZERO_NULL, /* readwrite */
  163. ZERO_NULL, /* connection_check */
  164. PORT_FTP, /* defport */
  165. CURLPROTO_FTP, /* protocol */
  166. PROTOPT_DUAL | PROTOPT_CLOSEACTION | PROTOPT_NEEDSPWD |
  167. PROTOPT_NOURLQUERY | PROTOPT_PROXY_AS_HTTP |
  168. PROTOPT_WILDCARD /* flags */
  169. };
  170. #ifdef USE_SSL
  171. /*
  172. * FTPS protocol handler.
  173. */
  174. const struct Curl_handler Curl_handler_ftps = {
  175. "FTPS", /* scheme */
  176. ftp_setup_connection, /* setup_connection */
  177. ftp_do, /* do_it */
  178. ftp_done, /* done */
  179. ftp_do_more, /* do_more */
  180. ftp_connect, /* connect_it */
  181. ftp_multi_statemach, /* connecting */
  182. ftp_doing, /* doing */
  183. ftp_getsock, /* proto_getsock */
  184. ftp_getsock, /* doing_getsock */
  185. ftp_domore_getsock, /* domore_getsock */
  186. ZERO_NULL, /* perform_getsock */
  187. ftp_disconnect, /* disconnect */
  188. ZERO_NULL, /* readwrite */
  189. ZERO_NULL, /* connection_check */
  190. PORT_FTPS, /* defport */
  191. CURLPROTO_FTPS, /* protocol */
  192. PROTOPT_SSL | PROTOPT_DUAL | PROTOPT_CLOSEACTION |
  193. PROTOPT_NEEDSPWD | PROTOPT_NOURLQUERY | PROTOPT_WILDCARD /* flags */
  194. };
  195. #endif
  196. static void close_secondarysocket(struct connectdata *conn)
  197. {
  198. if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET]) {
  199. Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
  200. conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
  201. }
  202. conn->bits.tcpconnect[SECONDARYSOCKET] = FALSE;
  203. }
  204. /*
  205. * NOTE: back in the old days, we added code in the FTP code that made NOBODY
  206. * requests on files respond with headers passed to the client/stdout that
  207. * looked like HTTP ones.
  208. *
  209. * This approach is not very elegant, it causes confusion and is error-prone.
  210. * It is subject for removal at the next (or at least a future) soname bump.
  211. * Until then you can test the effects of the removal by undefining the
  212. * following define named CURL_FTP_HTTPSTYLE_HEAD.
  213. */
  214. #define CURL_FTP_HTTPSTYLE_HEAD 1
  215. static void freedirs(struct ftp_conn *ftpc)
  216. {
  217. if(ftpc->dirs) {
  218. int i;
  219. for(i = 0; i < ftpc->dirdepth; i++) {
  220. free(ftpc->dirs[i]);
  221. ftpc->dirs[i] = NULL;
  222. }
  223. free(ftpc->dirs);
  224. ftpc->dirs = NULL;
  225. ftpc->dirdepth = 0;
  226. }
  227. Curl_safefree(ftpc->file);
  228. /* no longer of any use */
  229. Curl_safefree(ftpc->newhost);
  230. }
  231. /***********************************************************************
  232. *
  233. * AcceptServerConnect()
  234. *
  235. * After connection request is received from the server this function is
  236. * called to accept the connection and close the listening socket
  237. *
  238. */
  239. static CURLcode AcceptServerConnect(struct connectdata *conn)
  240. {
  241. struct Curl_easy *data = conn->data;
  242. curl_socket_t sock = conn->sock[SECONDARYSOCKET];
  243. curl_socket_t s = CURL_SOCKET_BAD;
  244. #ifdef ENABLE_IPV6
  245. struct Curl_sockaddr_storage add;
  246. #else
  247. struct sockaddr_in add;
  248. #endif
  249. curl_socklen_t size = (curl_socklen_t) sizeof(add);
  250. if(0 == getsockname(sock, (struct sockaddr *) &add, &size)) {
  251. size = sizeof(add);
  252. s = accept(sock, (struct sockaddr *) &add, &size);
  253. }
  254. Curl_closesocket(conn, sock); /* close the first socket */
  255. if(CURL_SOCKET_BAD == s) {
  256. failf(data, "Error accept()ing server connect");
  257. return CURLE_FTP_PORT_FAILED;
  258. }
  259. infof(data, "Connection accepted from server\n");
  260. /* when this happens within the DO state it is important that we mark us as
  261. not needing DO_MORE anymore */
  262. conn->bits.do_more = FALSE;
  263. conn->sock[SECONDARYSOCKET] = s;
  264. (void)curlx_nonblock(s, TRUE); /* enable non-blocking */
  265. conn->sock_accepted = TRUE;
  266. if(data->set.fsockopt) {
  267. int error = 0;
  268. /* activate callback for setting socket options */
  269. Curl_set_in_callback(data, true);
  270. error = data->set.fsockopt(data->set.sockopt_client,
  271. s,
  272. CURLSOCKTYPE_ACCEPT);
  273. Curl_set_in_callback(data, false);
  274. if(error) {
  275. close_secondarysocket(conn);
  276. return CURLE_ABORTED_BY_CALLBACK;
  277. }
  278. }
  279. return CURLE_OK;
  280. }
  281. /*
  282. * ftp_timeleft_accept() returns the amount of milliseconds left allowed for
  283. * waiting server to connect. If the value is negative, the timeout time has
  284. * already elapsed.
  285. *
  286. * The start time is stored in progress.t_acceptdata - as set with
  287. * Curl_pgrsTime(..., TIMER_STARTACCEPT);
  288. *
  289. */
  290. static timediff_t ftp_timeleft_accept(struct Curl_easy *data)
  291. {
  292. timediff_t timeout_ms = DEFAULT_ACCEPT_TIMEOUT;
  293. timediff_t other;
  294. struct curltime now;
  295. if(data->set.accepttimeout > 0)
  296. timeout_ms = data->set.accepttimeout;
  297. now = Curl_now();
  298. /* check if the generic timeout possibly is set shorter */
  299. other = Curl_timeleft(data, &now, FALSE);
  300. if(other && (other < timeout_ms))
  301. /* note that this also works fine for when other happens to be negative
  302. due to it already having elapsed */
  303. timeout_ms = other;
  304. else {
  305. /* subtract elapsed time */
  306. timeout_ms -= Curl_timediff(now, data->progress.t_acceptdata);
  307. if(!timeout_ms)
  308. /* avoid returning 0 as that means no timeout! */
  309. return -1;
  310. }
  311. return timeout_ms;
  312. }
  313. /***********************************************************************
  314. *
  315. * ReceivedServerConnect()
  316. *
  317. * After allowing server to connect to us from data port, this function
  318. * checks both data connection for connection establishment and ctrl
  319. * connection for a negative response regarding a failure in connecting
  320. *
  321. */
  322. static CURLcode ReceivedServerConnect(struct connectdata *conn, bool *received)
  323. {
  324. struct Curl_easy *data = conn->data;
  325. curl_socket_t ctrl_sock = conn->sock[FIRSTSOCKET];
  326. curl_socket_t data_sock = conn->sock[SECONDARYSOCKET];
  327. struct ftp_conn *ftpc = &conn->proto.ftpc;
  328. struct pingpong *pp = &ftpc->pp;
  329. int result;
  330. timediff_t timeout_ms;
  331. ssize_t nread;
  332. int ftpcode;
  333. *received = FALSE;
  334. timeout_ms = ftp_timeleft_accept(data);
  335. infof(data, "Checking for server connect\n");
  336. if(timeout_ms < 0) {
  337. /* if a timeout was already reached, bail out */
  338. failf(data, "Accept timeout occurred while waiting server connect");
  339. return CURLE_FTP_ACCEPT_TIMEOUT;
  340. }
  341. /* First check whether there is a cached response from server */
  342. if(pp->cache_size && pp->cache && pp->cache[0] > '3') {
  343. /* Data connection could not be established, let's return */
  344. infof(data, "There is negative response in cache while serv connect\n");
  345. Curl_GetFTPResponse(&nread, conn, &ftpcode);
  346. return CURLE_FTP_ACCEPT_FAILED;
  347. }
  348. result = Curl_socket_check(ctrl_sock, data_sock, CURL_SOCKET_BAD, 0);
  349. /* see if the connection request is already here */
  350. switch(result) {
  351. case -1: /* error */
  352. /* let's die here */
  353. failf(data, "Error while waiting for server connect");
  354. return CURLE_FTP_ACCEPT_FAILED;
  355. case 0: /* Server connect is not received yet */
  356. break; /* loop */
  357. default:
  358. if(result & CURL_CSELECT_IN2) {
  359. infof(data, "Ready to accept data connection from server\n");
  360. *received = TRUE;
  361. }
  362. else if(result & CURL_CSELECT_IN) {
  363. infof(data, "Ctrl conn has data while waiting for data conn\n");
  364. Curl_GetFTPResponse(&nread, conn, &ftpcode);
  365. if(ftpcode/100 > 3)
  366. return CURLE_FTP_ACCEPT_FAILED;
  367. return CURLE_WEIRD_SERVER_REPLY;
  368. }
  369. break;
  370. } /* switch() */
  371. return CURLE_OK;
  372. }
  373. /***********************************************************************
  374. *
  375. * InitiateTransfer()
  376. *
  377. * After connection from server is accepted this function is called to
  378. * setup transfer parameters and initiate the data transfer.
  379. *
  380. */
  381. static CURLcode InitiateTransfer(struct connectdata *conn)
  382. {
  383. struct Curl_easy *data = conn->data;
  384. CURLcode result = CURLE_OK;
  385. if(conn->bits.ftp_use_data_ssl) {
  386. /* since we only have a plaintext TCP connection here, we must now
  387. * do the TLS stuff */
  388. infof(data, "Doing the SSL/TLS handshake on the data stream\n");
  389. result = Curl_ssl_connect(conn, SECONDARYSOCKET);
  390. if(result)
  391. return result;
  392. }
  393. if(conn->proto.ftpc.state_saved == FTP_STOR) {
  394. /* When we know we're uploading a specified file, we can get the file
  395. size prior to the actual upload. */
  396. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  397. /* set the SO_SNDBUF for the secondary socket for those who need it */
  398. Curl_sndbufset(conn->sock[SECONDARYSOCKET]);
  399. Curl_setup_transfer(data, -1, -1, FALSE, SECONDARYSOCKET);
  400. }
  401. else {
  402. /* FTP download: */
  403. Curl_setup_transfer(data, SECONDARYSOCKET,
  404. conn->proto.ftpc.retr_size_saved, FALSE, -1);
  405. }
  406. conn->proto.ftpc.pp.pending_resp = TRUE; /* expect server response */
  407. state(conn, FTP_STOP);
  408. return CURLE_OK;
  409. }
  410. /***********************************************************************
  411. *
  412. * AllowServerConnect()
  413. *
  414. * When we've issue the PORT command, we have told the server to connect to
  415. * us. This function checks whether data connection is established if so it is
  416. * accepted.
  417. *
  418. */
  419. static CURLcode AllowServerConnect(struct connectdata *conn, bool *connected)
  420. {
  421. struct Curl_easy *data = conn->data;
  422. timediff_t timeout_ms;
  423. CURLcode result = CURLE_OK;
  424. *connected = FALSE;
  425. infof(data, "Preparing for accepting server on data port\n");
  426. /* Save the time we start accepting server connect */
  427. Curl_pgrsTime(data, TIMER_STARTACCEPT);
  428. timeout_ms = ftp_timeleft_accept(data);
  429. if(timeout_ms < 0) {
  430. /* if a timeout was already reached, bail out */
  431. failf(data, "Accept timeout occurred while waiting server connect");
  432. return CURLE_FTP_ACCEPT_TIMEOUT;
  433. }
  434. /* see if the connection request is already here */
  435. result = ReceivedServerConnect(conn, connected);
  436. if(result)
  437. return result;
  438. if(*connected) {
  439. result = AcceptServerConnect(conn);
  440. if(result)
  441. return result;
  442. result = InitiateTransfer(conn);
  443. if(result)
  444. return result;
  445. }
  446. else {
  447. /* Add timeout to multi handle and break out of the loop */
  448. if(*connected == FALSE) {
  449. Curl_expire(data, data->set.accepttimeout > 0 ?
  450. data->set.accepttimeout: DEFAULT_ACCEPT_TIMEOUT, 0);
  451. }
  452. }
  453. return result;
  454. }
  455. /* macro to check for a three-digit ftp status code at the start of the
  456. given string */
  457. #define STATUSCODE(line) (ISDIGIT(line[0]) && ISDIGIT(line[1]) && \
  458. ISDIGIT(line[2]))
  459. /* macro to check for the last line in an FTP server response */
  460. #define LASTLINE(line) (STATUSCODE(line) && (' ' == line[3]))
  461. static bool ftp_endofresp(struct connectdata *conn, char *line, size_t len,
  462. int *code)
  463. {
  464. (void)conn;
  465. if((len > 3) && LASTLINE(line)) {
  466. *code = curlx_sltosi(strtol(line, NULL, 10));
  467. return TRUE;
  468. }
  469. return FALSE;
  470. }
  471. static CURLcode ftp_readresp(curl_socket_t sockfd,
  472. struct pingpong *pp,
  473. int *ftpcode, /* return the ftp-code if done */
  474. size_t *size) /* size of the response */
  475. {
  476. struct connectdata *conn = pp->conn;
  477. struct Curl_easy *data = conn->data;
  478. #ifdef HAVE_GSSAPI
  479. char * const buf = data->state.buffer;
  480. #endif
  481. int code;
  482. CURLcode result = Curl_pp_readresp(sockfd, pp, &code, size);
  483. #if defined(HAVE_GSSAPI)
  484. /* handle the security-oriented responses 6xx ***/
  485. switch(code) {
  486. case 631:
  487. code = Curl_sec_read_msg(conn, buf, PROT_SAFE);
  488. break;
  489. case 632:
  490. code = Curl_sec_read_msg(conn, buf, PROT_PRIVATE);
  491. break;
  492. case 633:
  493. code = Curl_sec_read_msg(conn, buf, PROT_CONFIDENTIAL);
  494. break;
  495. default:
  496. /* normal ftp stuff we pass through! */
  497. break;
  498. }
  499. #endif
  500. /* store the latest code for later retrieval */
  501. data->info.httpcode = code;
  502. if(ftpcode)
  503. *ftpcode = code;
  504. if(421 == code) {
  505. /* 421 means "Service not available, closing control connection." and FTP
  506. * servers use it to signal that idle session timeout has been exceeded.
  507. * If we ignored the response, it could end up hanging in some cases.
  508. *
  509. * This response code can come at any point so having it treated
  510. * generically is a good idea.
  511. */
  512. infof(data, "We got a 421 - timeout!\n");
  513. state(conn, FTP_STOP);
  514. return CURLE_OPERATION_TIMEDOUT;
  515. }
  516. return result;
  517. }
  518. /* --- parse FTP server responses --- */
  519. /*
  520. * Curl_GetFTPResponse() is a BLOCKING function to read the full response
  521. * from a server after a command.
  522. *
  523. */
  524. CURLcode Curl_GetFTPResponse(ssize_t *nreadp, /* return number of bytes read */
  525. struct connectdata *conn,
  526. int *ftpcode) /* return the ftp-code */
  527. {
  528. /*
  529. * We cannot read just one byte per read() and then go back to select() as
  530. * the OpenSSL read() doesn't grok that properly.
  531. *
  532. * Alas, read as much as possible, split up into lines, use the ending
  533. * line in a response or continue reading. */
  534. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  535. struct Curl_easy *data = conn->data;
  536. CURLcode result = CURLE_OK;
  537. struct ftp_conn *ftpc = &conn->proto.ftpc;
  538. struct pingpong *pp = &ftpc->pp;
  539. size_t nread;
  540. int cache_skip = 0;
  541. int value_to_be_ignored = 0;
  542. if(ftpcode)
  543. *ftpcode = 0; /* 0 for errors */
  544. else
  545. /* make the pointer point to something for the rest of this function */
  546. ftpcode = &value_to_be_ignored;
  547. *nreadp = 0;
  548. while(!*ftpcode && !result) {
  549. /* check and reset timeout value every lap */
  550. time_t timeout = Curl_pp_state_timeout(pp, FALSE);
  551. time_t interval_ms;
  552. if(timeout <= 0) {
  553. failf(data, "FTP response timeout");
  554. return CURLE_OPERATION_TIMEDOUT; /* already too little time */
  555. }
  556. interval_ms = 1000; /* use 1 second timeout intervals */
  557. if(timeout < interval_ms)
  558. interval_ms = timeout;
  559. /*
  560. * Since this function is blocking, we need to wait here for input on the
  561. * connection and only then we call the response reading function. We do
  562. * timeout at least every second to make the timeout check run.
  563. *
  564. * A caution here is that the ftp_readresp() function has a cache that may
  565. * contain pieces of a response from the previous invoke and we need to
  566. * make sure we don't just wait for input while there is unhandled data in
  567. * that cache. But also, if the cache is there, we call ftp_readresp() and
  568. * the cache wasn't good enough to continue we must not just busy-loop
  569. * around this function.
  570. *
  571. */
  572. if(pp->cache && (cache_skip < 2)) {
  573. /*
  574. * There's a cache left since before. We then skipping the wait for
  575. * socket action, unless this is the same cache like the previous round
  576. * as then the cache was deemed not enough to act on and we then need to
  577. * wait for more data anyway.
  578. */
  579. }
  580. else if(!Curl_conn_data_pending(conn, FIRSTSOCKET)) {
  581. switch(SOCKET_READABLE(sockfd, interval_ms)) {
  582. case -1: /* select() error, stop reading */
  583. failf(data, "FTP response aborted due to select/poll error: %d",
  584. SOCKERRNO);
  585. return CURLE_RECV_ERROR;
  586. case 0: /* timeout */
  587. if(Curl_pgrsUpdate(conn))
  588. return CURLE_ABORTED_BY_CALLBACK;
  589. continue; /* just continue in our loop for the timeout duration */
  590. default: /* for clarity */
  591. break;
  592. }
  593. }
  594. result = ftp_readresp(sockfd, pp, ftpcode, &nread);
  595. if(result)
  596. break;
  597. if(!nread && pp->cache)
  598. /* bump cache skip counter as on repeated skips we must wait for more
  599. data */
  600. cache_skip++;
  601. else
  602. /* when we got data or there is no cache left, we reset the cache skip
  603. counter */
  604. cache_skip = 0;
  605. *nreadp += nread;
  606. } /* while there's buffer left and loop is requested */
  607. pp->pending_resp = FALSE;
  608. return result;
  609. }
  610. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  611. /* for debug purposes */
  612. static const char * const ftp_state_names[]={
  613. "STOP",
  614. "WAIT220",
  615. "AUTH",
  616. "USER",
  617. "PASS",
  618. "ACCT",
  619. "PBSZ",
  620. "PROT",
  621. "CCC",
  622. "PWD",
  623. "SYST",
  624. "NAMEFMT",
  625. "QUOTE",
  626. "RETR_PREQUOTE",
  627. "STOR_PREQUOTE",
  628. "POSTQUOTE",
  629. "CWD",
  630. "MKD",
  631. "MDTM",
  632. "TYPE",
  633. "LIST_TYPE",
  634. "RETR_TYPE",
  635. "STOR_TYPE",
  636. "SIZE",
  637. "RETR_SIZE",
  638. "STOR_SIZE",
  639. "REST",
  640. "RETR_REST",
  641. "PORT",
  642. "PRET",
  643. "PASV",
  644. "LIST",
  645. "RETR",
  646. "STOR",
  647. "QUIT"
  648. };
  649. #endif
  650. /* This is the ONLY way to change FTP state! */
  651. static void _state(struct connectdata *conn,
  652. ftpstate newstate
  653. #ifdef DEBUGBUILD
  654. , int lineno
  655. #endif
  656. )
  657. {
  658. struct ftp_conn *ftpc = &conn->proto.ftpc;
  659. #if defined(DEBUGBUILD)
  660. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  661. (void) lineno;
  662. #else
  663. if(ftpc->state != newstate)
  664. infof(conn->data, "FTP %p (line %d) state change from %s to %s\n",
  665. (void *)ftpc, lineno, ftp_state_names[ftpc->state],
  666. ftp_state_names[newstate]);
  667. #endif
  668. #endif
  669. ftpc->state = newstate;
  670. }
  671. static CURLcode ftp_state_user(struct connectdata *conn)
  672. {
  673. CURLcode result;
  674. /* send USER */
  675. PPSENDF(&conn->proto.ftpc.pp, "USER %s", conn->user?conn->user:"");
  676. state(conn, FTP_USER);
  677. conn->data->state.ftp_trying_alternative = FALSE;
  678. return CURLE_OK;
  679. }
  680. static CURLcode ftp_state_pwd(struct connectdata *conn)
  681. {
  682. CURLcode result;
  683. /* send PWD to discover our entry point */
  684. PPSENDF(&conn->proto.ftpc.pp, "%s", "PWD");
  685. state(conn, FTP_PWD);
  686. return CURLE_OK;
  687. }
  688. /* For the FTP "protocol connect" and "doing" phases only */
  689. static int ftp_getsock(struct connectdata *conn,
  690. curl_socket_t *socks)
  691. {
  692. return Curl_pp_getsock(&conn->proto.ftpc.pp, socks);
  693. }
  694. /* For the FTP "DO_MORE" phase only */
  695. static int ftp_domore_getsock(struct connectdata *conn, curl_socket_t *socks)
  696. {
  697. struct ftp_conn *ftpc = &conn->proto.ftpc;
  698. /* When in DO_MORE state, we could be either waiting for us to connect to a
  699. * remote site, or we could wait for that site to connect to us. Or just
  700. * handle ordinary commands.
  701. */
  702. if(SOCKS_STATE(conn->cnnct.state))
  703. return Curl_SOCKS_getsock(conn, socks, SECONDARYSOCKET);
  704. if(FTP_STOP == ftpc->state) {
  705. int bits = GETSOCK_READSOCK(0);
  706. /* if stopped and still in this state, then we're also waiting for a
  707. connect on the secondary connection */
  708. socks[0] = conn->sock[FIRSTSOCKET];
  709. if(!conn->data->set.ftp_use_port) {
  710. int s;
  711. int i;
  712. /* PORT is used to tell the server to connect to us, and during that we
  713. don't do happy eyeballs, but we do if we connect to the server */
  714. for(s = 1, i = 0; i<2; i++) {
  715. if(conn->tempsock[i] != CURL_SOCKET_BAD) {
  716. socks[s] = conn->tempsock[i];
  717. bits |= GETSOCK_WRITESOCK(s++);
  718. }
  719. }
  720. }
  721. else {
  722. socks[1] = conn->sock[SECONDARYSOCKET];
  723. bits |= GETSOCK_WRITESOCK(1) | GETSOCK_READSOCK(1);
  724. }
  725. return bits;
  726. }
  727. return Curl_pp_getsock(&conn->proto.ftpc.pp, socks);
  728. }
  729. /* This is called after the FTP_QUOTE state is passed.
  730. ftp_state_cwd() sends the range of CWD commands to the server to change to
  731. the correct directory. It may also need to send MKD commands to create
  732. missing ones, if that option is enabled.
  733. */
  734. static CURLcode ftp_state_cwd(struct connectdata *conn)
  735. {
  736. CURLcode result = CURLE_OK;
  737. struct ftp_conn *ftpc = &conn->proto.ftpc;
  738. if(ftpc->cwddone)
  739. /* already done and fine */
  740. result = ftp_state_mdtm(conn);
  741. else {
  742. /* FTPFILE_NOCWD with full path: expect ftpc->cwddone! */
  743. DEBUGASSERT((conn->data->set.ftp_filemethod != FTPFILE_NOCWD) ||
  744. !(ftpc->dirdepth && ftpc->dirs[0][0] == '/'));
  745. ftpc->count2 = 0; /* count2 counts failed CWDs */
  746. /* count3 is set to allow a MKD to fail once. In the case when first CWD
  747. fails and then MKD fails (due to another session raced it to create the
  748. dir) this then allows for a second try to CWD to it */
  749. ftpc->count3 = (conn->data->set.ftp_create_missing_dirs == 2)?1:0;
  750. if(conn->bits.reuse && ftpc->entrypath &&
  751. /* no need to go to entrypath when we have an absolute path */
  752. !(ftpc->dirdepth && ftpc->dirs[0][0] == '/')) {
  753. /* This is a re-used connection. Since we change directory to where the
  754. transfer is taking place, we must first get back to the original dir
  755. where we ended up after login: */
  756. ftpc->cwdcount = 0; /* we count this as the first path, then we add one
  757. for all upcoming ones in the ftp->dirs[] array */
  758. PPSENDF(&conn->proto.ftpc.pp, "CWD %s", ftpc->entrypath);
  759. state(conn, FTP_CWD);
  760. }
  761. else {
  762. if(ftpc->dirdepth) {
  763. ftpc->cwdcount = 1;
  764. /* issue the first CWD, the rest is sent when the CWD responses are
  765. received... */
  766. PPSENDF(&conn->proto.ftpc.pp, "CWD %s", ftpc->dirs[ftpc->cwdcount -1]);
  767. state(conn, FTP_CWD);
  768. }
  769. else {
  770. /* No CWD necessary */
  771. result = ftp_state_mdtm(conn);
  772. }
  773. }
  774. }
  775. return result;
  776. }
  777. typedef enum {
  778. EPRT,
  779. PORT,
  780. DONE
  781. } ftpport;
  782. static CURLcode ftp_state_use_port(struct connectdata *conn,
  783. ftpport fcmd) /* start with this */
  784. {
  785. CURLcode result = CURLE_OK;
  786. struct ftp_conn *ftpc = &conn->proto.ftpc;
  787. struct Curl_easy *data = conn->data;
  788. curl_socket_t portsock = CURL_SOCKET_BAD;
  789. char myhost[MAX_IPADR_LEN + 1] = "";
  790. struct Curl_sockaddr_storage ss;
  791. Curl_addrinfo *res, *ai;
  792. curl_socklen_t sslen;
  793. char hbuf[NI_MAXHOST];
  794. struct sockaddr *sa = (struct sockaddr *)&ss;
  795. struct sockaddr_in * const sa4 = (void *)sa;
  796. #ifdef ENABLE_IPV6
  797. struct sockaddr_in6 * const sa6 = (void *)sa;
  798. #endif
  799. static const char mode[][5] = { "EPRT", "PORT" };
  800. enum resolve_t rc;
  801. int error;
  802. char *host = NULL;
  803. char *string_ftpport = data->set.str[STRING_FTPPORT];
  804. struct Curl_dns_entry *h = NULL;
  805. unsigned short port_min = 0;
  806. unsigned short port_max = 0;
  807. unsigned short port;
  808. bool possibly_non_local = TRUE;
  809. char buffer[STRERROR_LEN];
  810. char *addr = NULL;
  811. /* Step 1, figure out what is requested,
  812. * accepted format :
  813. * (ipv4|ipv6|domain|interface)?(:port(-range)?)?
  814. */
  815. if(data->set.str[STRING_FTPPORT] &&
  816. (strlen(data->set.str[STRING_FTPPORT]) > 1)) {
  817. #ifdef ENABLE_IPV6
  818. size_t addrlen = INET6_ADDRSTRLEN > strlen(string_ftpport) ?
  819. INET6_ADDRSTRLEN : strlen(string_ftpport);
  820. #else
  821. size_t addrlen = INET_ADDRSTRLEN > strlen(string_ftpport) ?
  822. INET_ADDRSTRLEN : strlen(string_ftpport);
  823. #endif
  824. char *ip_start = string_ftpport;
  825. char *ip_end = NULL;
  826. char *port_start = NULL;
  827. char *port_sep = NULL;
  828. addr = calloc(addrlen + 1, 1);
  829. if(!addr)
  830. return CURLE_OUT_OF_MEMORY;
  831. #ifdef ENABLE_IPV6
  832. if(*string_ftpport == '[') {
  833. /* [ipv6]:port(-range) */
  834. ip_start = string_ftpport + 1;
  835. ip_end = strchr(string_ftpport, ']');
  836. if(ip_end)
  837. strncpy(addr, ip_start, ip_end - ip_start);
  838. }
  839. else
  840. #endif
  841. if(*string_ftpport == ':') {
  842. /* :port */
  843. ip_end = string_ftpport;
  844. }
  845. else {
  846. ip_end = strchr(string_ftpport, ':');
  847. if(ip_end) {
  848. /* either ipv6 or (ipv4|domain|interface):port(-range) */
  849. #ifdef ENABLE_IPV6
  850. if(Curl_inet_pton(AF_INET6, string_ftpport, sa6) == 1) {
  851. /* ipv6 */
  852. port_min = port_max = 0;
  853. strcpy(addr, string_ftpport);
  854. ip_end = NULL; /* this got no port ! */
  855. }
  856. else
  857. #endif
  858. /* (ipv4|domain|interface):port(-range) */
  859. strncpy(addr, string_ftpport, ip_end - ip_start);
  860. }
  861. else
  862. /* ipv4|interface */
  863. strcpy(addr, string_ftpport);
  864. }
  865. /* parse the port */
  866. if(ip_end != NULL) {
  867. port_start = strchr(ip_end, ':');
  868. if(port_start) {
  869. port_min = curlx_ultous(strtoul(port_start + 1, NULL, 10));
  870. port_sep = strchr(port_start, '-');
  871. if(port_sep) {
  872. port_max = curlx_ultous(strtoul(port_sep + 1, NULL, 10));
  873. }
  874. else
  875. port_max = port_min;
  876. }
  877. }
  878. /* correct errors like:
  879. * :1234-1230
  880. * :-4711, in this case port_min is (unsigned)-1,
  881. * therefore port_min > port_max for all cases
  882. * but port_max = (unsigned)-1
  883. */
  884. if(port_min > port_max)
  885. port_min = port_max = 0;
  886. if(*addr != '\0') {
  887. /* attempt to get the address of the given interface name */
  888. switch(Curl_if2ip(conn->ip_addr->ai_family,
  889. Curl_ipv6_scope(conn->ip_addr->ai_addr),
  890. conn->scope_id, addr, hbuf, sizeof(hbuf))) {
  891. case IF2IP_NOT_FOUND:
  892. /* not an interface, use the given string as host name instead */
  893. host = addr;
  894. break;
  895. case IF2IP_AF_NOT_SUPPORTED:
  896. return CURLE_FTP_PORT_FAILED;
  897. case IF2IP_FOUND:
  898. host = hbuf; /* use the hbuf for host name */
  899. }
  900. }
  901. else
  902. /* there was only a port(-range) given, default the host */
  903. host = NULL;
  904. } /* data->set.ftpport */
  905. if(!host) {
  906. /* not an interface and not a host name, get default by extracting
  907. the IP from the control connection */
  908. sslen = sizeof(ss);
  909. if(getsockname(conn->sock[FIRSTSOCKET], sa, &sslen)) {
  910. failf(data, "getsockname() failed: %s",
  911. Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  912. free(addr);
  913. return CURLE_FTP_PORT_FAILED;
  914. }
  915. switch(sa->sa_family) {
  916. #ifdef ENABLE_IPV6
  917. case AF_INET6:
  918. Curl_inet_ntop(sa->sa_family, &sa6->sin6_addr, hbuf, sizeof(hbuf));
  919. break;
  920. #endif
  921. default:
  922. Curl_inet_ntop(sa->sa_family, &sa4->sin_addr, hbuf, sizeof(hbuf));
  923. break;
  924. }
  925. host = hbuf; /* use this host name */
  926. possibly_non_local = FALSE; /* we know it is local now */
  927. }
  928. /* resolv ip/host to ip */
  929. rc = Curl_resolv(conn, host, 0, FALSE, &h);
  930. if(rc == CURLRESOLV_PENDING)
  931. (void)Curl_resolver_wait_resolv(conn, &h);
  932. if(h) {
  933. res = h->addr;
  934. /* when we return from this function, we can forget about this entry
  935. to we can unlock it now already */
  936. Curl_resolv_unlock(data, h);
  937. } /* (h) */
  938. else
  939. res = NULL; /* failure! */
  940. if(res == NULL) {
  941. failf(data, "failed to resolve the address provided to PORT: %s", host);
  942. free(addr);
  943. return CURLE_FTP_PORT_FAILED;
  944. }
  945. free(addr);
  946. host = NULL;
  947. /* step 2, create a socket for the requested address */
  948. portsock = CURL_SOCKET_BAD;
  949. error = 0;
  950. for(ai = res; ai; ai = ai->ai_next) {
  951. result = Curl_socket(conn, ai, NULL, &portsock);
  952. if(result) {
  953. error = SOCKERRNO;
  954. continue;
  955. }
  956. break;
  957. }
  958. if(!ai) {
  959. failf(data, "socket failure: %s",
  960. Curl_strerror(error, buffer, sizeof(buffer)));
  961. return CURLE_FTP_PORT_FAILED;
  962. }
  963. /* step 3, bind to a suitable local address */
  964. memcpy(sa, ai->ai_addr, ai->ai_addrlen);
  965. sslen = ai->ai_addrlen;
  966. for(port = port_min; port <= port_max;) {
  967. if(sa->sa_family == AF_INET)
  968. sa4->sin_port = htons(port);
  969. #ifdef ENABLE_IPV6
  970. else
  971. sa6->sin6_port = htons(port);
  972. #endif
  973. /* Try binding the given address. */
  974. if(bind(portsock, sa, sslen) ) {
  975. /* It failed. */
  976. error = SOCKERRNO;
  977. if(possibly_non_local && (error == EADDRNOTAVAIL)) {
  978. /* The requested bind address is not local. Use the address used for
  979. * the control connection instead and restart the port loop
  980. */
  981. infof(data, "bind(port=%hu) on non-local address failed: %s\n", port,
  982. Curl_strerror(error, buffer, sizeof(buffer)));
  983. sslen = sizeof(ss);
  984. if(getsockname(conn->sock[FIRSTSOCKET], sa, &sslen)) {
  985. failf(data, "getsockname() failed: %s",
  986. Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  987. Curl_closesocket(conn, portsock);
  988. return CURLE_FTP_PORT_FAILED;
  989. }
  990. port = port_min;
  991. possibly_non_local = FALSE; /* don't try this again */
  992. continue;
  993. }
  994. if(error != EADDRINUSE && error != EACCES) {
  995. failf(data, "bind(port=%hu) failed: %s", port,
  996. Curl_strerror(error, buffer, sizeof(buffer)));
  997. Curl_closesocket(conn, portsock);
  998. return CURLE_FTP_PORT_FAILED;
  999. }
  1000. }
  1001. else
  1002. break;
  1003. port++;
  1004. }
  1005. /* maybe all ports were in use already*/
  1006. if(port > port_max) {
  1007. failf(data, "bind() failed, we ran out of ports!");
  1008. Curl_closesocket(conn, portsock);
  1009. return CURLE_FTP_PORT_FAILED;
  1010. }
  1011. /* get the name again after the bind() so that we can extract the
  1012. port number it uses now */
  1013. sslen = sizeof(ss);
  1014. if(getsockname(portsock, (struct sockaddr *)sa, &sslen)) {
  1015. failf(data, "getsockname() failed: %s",
  1016. Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  1017. Curl_closesocket(conn, portsock);
  1018. return CURLE_FTP_PORT_FAILED;
  1019. }
  1020. /* step 4, listen on the socket */
  1021. if(listen(portsock, 1)) {
  1022. failf(data, "socket failure: %s",
  1023. Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  1024. Curl_closesocket(conn, portsock);
  1025. return CURLE_FTP_PORT_FAILED;
  1026. }
  1027. /* step 5, send the proper FTP command */
  1028. /* get a plain printable version of the numerical address to work with
  1029. below */
  1030. Curl_printable_address(ai, myhost, sizeof(myhost));
  1031. #ifdef ENABLE_IPV6
  1032. if(!conn->bits.ftp_use_eprt && conn->bits.ipv6)
  1033. /* EPRT is disabled but we are connected to a IPv6 host, so we ignore the
  1034. request and enable EPRT again! */
  1035. conn->bits.ftp_use_eprt = TRUE;
  1036. #endif
  1037. for(; fcmd != DONE; fcmd++) {
  1038. if(!conn->bits.ftp_use_eprt && (EPRT == fcmd))
  1039. /* if disabled, goto next */
  1040. continue;
  1041. if((PORT == fcmd) && sa->sa_family != AF_INET)
  1042. /* PORT is IPv4 only */
  1043. continue;
  1044. switch(sa->sa_family) {
  1045. case AF_INET:
  1046. port = ntohs(sa4->sin_port);
  1047. break;
  1048. #ifdef ENABLE_IPV6
  1049. case AF_INET6:
  1050. port = ntohs(sa6->sin6_port);
  1051. break;
  1052. #endif
  1053. default:
  1054. continue; /* might as well skip this */
  1055. }
  1056. if(EPRT == fcmd) {
  1057. /*
  1058. * Two fine examples from RFC2428;
  1059. *
  1060. * EPRT |1|132.235.1.2|6275|
  1061. *
  1062. * EPRT |2|1080::8:800:200C:417A|5282|
  1063. */
  1064. result = Curl_pp_sendf(&ftpc->pp, "%s |%d|%s|%hu|", mode[fcmd],
  1065. sa->sa_family == AF_INET?1:2,
  1066. myhost, port);
  1067. if(result) {
  1068. failf(data, "Failure sending EPRT command: %s",
  1069. curl_easy_strerror(result));
  1070. Curl_closesocket(conn, portsock);
  1071. /* don't retry using PORT */
  1072. ftpc->count1 = PORT;
  1073. /* bail out */
  1074. state(conn, FTP_STOP);
  1075. return result;
  1076. }
  1077. break;
  1078. }
  1079. if(PORT == fcmd) {
  1080. /* large enough for [IP address],[num],[num] */
  1081. char target[sizeof(myhost) + 20];
  1082. char *source = myhost;
  1083. char *dest = target;
  1084. /* translate x.x.x.x to x,x,x,x */
  1085. while(source && *source) {
  1086. if(*source == '.')
  1087. *dest = ',';
  1088. else
  1089. *dest = *source;
  1090. dest++;
  1091. source++;
  1092. }
  1093. *dest = 0;
  1094. msnprintf(dest, 20, ",%d,%d", (int)(port>>8), (int)(port&0xff));
  1095. result = Curl_pp_sendf(&ftpc->pp, "%s %s", mode[fcmd], target);
  1096. if(result) {
  1097. failf(data, "Failure sending PORT command: %s",
  1098. curl_easy_strerror(result));
  1099. Curl_closesocket(conn, portsock);
  1100. /* bail out */
  1101. state(conn, FTP_STOP);
  1102. return result;
  1103. }
  1104. break;
  1105. }
  1106. }
  1107. /* store which command was sent */
  1108. ftpc->count1 = fcmd;
  1109. close_secondarysocket(conn);
  1110. /* we set the secondary socket variable to this for now, it is only so that
  1111. the cleanup function will close it in case we fail before the true
  1112. secondary stuff is made */
  1113. conn->sock[SECONDARYSOCKET] = portsock;
  1114. /* this tcpconnect assignment below is a hackish work-around to make the
  1115. multi interface with active FTP work - as it will not wait for a
  1116. (passive) connect in Curl_is_connected().
  1117. The *proper* fix is to make sure that the active connection from the
  1118. server is done in a non-blocking way. Currently, it is still BLOCKING.
  1119. */
  1120. conn->bits.tcpconnect[SECONDARYSOCKET] = TRUE;
  1121. state(conn, FTP_PORT);
  1122. return result;
  1123. }
  1124. static CURLcode ftp_state_use_pasv(struct connectdata *conn)
  1125. {
  1126. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1127. CURLcode result = CURLE_OK;
  1128. /*
  1129. Here's the excecutive summary on what to do:
  1130. PASV is RFC959, expect:
  1131. 227 Entering Passive Mode (a1,a2,a3,a4,p1,p2)
  1132. LPSV is RFC1639, expect:
  1133. 228 Entering Long Passive Mode (4,4,a1,a2,a3,a4,2,p1,p2)
  1134. EPSV is RFC2428, expect:
  1135. 229 Entering Extended Passive Mode (|||port|)
  1136. */
  1137. static const char mode[][5] = { "EPSV", "PASV" };
  1138. int modeoff;
  1139. #ifdef PF_INET6
  1140. if(!conn->bits.ftp_use_epsv && conn->bits.ipv6)
  1141. /* EPSV is disabled but we are connected to a IPv6 host, so we ignore the
  1142. request and enable EPSV again! */
  1143. conn->bits.ftp_use_epsv = TRUE;
  1144. #endif
  1145. modeoff = conn->bits.ftp_use_epsv?0:1;
  1146. PPSENDF(&ftpc->pp, "%s", mode[modeoff]);
  1147. ftpc->count1 = modeoff;
  1148. state(conn, FTP_PASV);
  1149. infof(conn->data, "Connect data stream passively\n");
  1150. return result;
  1151. }
  1152. /*
  1153. * ftp_state_prepare_transfer() starts PORT, PASV or PRET etc.
  1154. *
  1155. * REST is the last command in the chain of commands when a "head"-like
  1156. * request is made. Thus, if an actual transfer is to be made this is where we
  1157. * take off for real.
  1158. */
  1159. static CURLcode ftp_state_prepare_transfer(struct connectdata *conn)
  1160. {
  1161. CURLcode result = CURLE_OK;
  1162. struct FTP *ftp = conn->data->req.protop;
  1163. struct Curl_easy *data = conn->data;
  1164. if(ftp->transfer != FTPTRANSFER_BODY) {
  1165. /* doesn't transfer any data */
  1166. /* still possibly do PRE QUOTE jobs */
  1167. state(conn, FTP_RETR_PREQUOTE);
  1168. result = ftp_state_quote(conn, TRUE, FTP_RETR_PREQUOTE);
  1169. }
  1170. else if(data->set.ftp_use_port) {
  1171. /* We have chosen to use the PORT (or similar) command */
  1172. result = ftp_state_use_port(conn, EPRT);
  1173. }
  1174. else {
  1175. /* We have chosen (this is default) to use the PASV (or similar) command */
  1176. if(data->set.ftp_use_pret) {
  1177. /* The user has requested that we send a PRET command
  1178. to prepare the server for the upcoming PASV */
  1179. if(!conn->proto.ftpc.file) {
  1180. PPSENDF(&conn->proto.ftpc.pp, "PRET %s",
  1181. data->set.str[STRING_CUSTOMREQUEST]?
  1182. data->set.str[STRING_CUSTOMREQUEST]:
  1183. (data->set.ftp_list_only?"NLST":"LIST"));
  1184. }
  1185. else if(data->set.upload) {
  1186. PPSENDF(&conn->proto.ftpc.pp, "PRET STOR %s", conn->proto.ftpc.file);
  1187. }
  1188. else {
  1189. PPSENDF(&conn->proto.ftpc.pp, "PRET RETR %s", conn->proto.ftpc.file);
  1190. }
  1191. state(conn, FTP_PRET);
  1192. }
  1193. else {
  1194. result = ftp_state_use_pasv(conn);
  1195. }
  1196. }
  1197. return result;
  1198. }
  1199. static CURLcode ftp_state_rest(struct connectdata *conn)
  1200. {
  1201. CURLcode result = CURLE_OK;
  1202. struct FTP *ftp = conn->data->req.protop;
  1203. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1204. if((ftp->transfer != FTPTRANSFER_BODY) && ftpc->file) {
  1205. /* if a "head"-like request is being made (on a file) */
  1206. /* Determine if server can respond to REST command and therefore
  1207. whether it supports range */
  1208. PPSENDF(&conn->proto.ftpc.pp, "REST %d", 0);
  1209. state(conn, FTP_REST);
  1210. }
  1211. else
  1212. result = ftp_state_prepare_transfer(conn);
  1213. return result;
  1214. }
  1215. static CURLcode ftp_state_size(struct connectdata *conn)
  1216. {
  1217. CURLcode result = CURLE_OK;
  1218. struct FTP *ftp = conn->data->req.protop;
  1219. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1220. if((ftp->transfer == FTPTRANSFER_INFO) && ftpc->file) {
  1221. /* if a "head"-like request is being made (on a file) */
  1222. /* we know ftpc->file is a valid pointer to a file name */
  1223. PPSENDF(&ftpc->pp, "SIZE %s", ftpc->file);
  1224. state(conn, FTP_SIZE);
  1225. }
  1226. else
  1227. result = ftp_state_rest(conn);
  1228. return result;
  1229. }
  1230. static CURLcode ftp_state_list(struct connectdata *conn)
  1231. {
  1232. CURLcode result = CURLE_OK;
  1233. struct Curl_easy *data = conn->data;
  1234. struct FTP *ftp = data->req.protop;
  1235. /* If this output is to be machine-parsed, the NLST command might be better
  1236. to use, since the LIST command output is not specified or standard in any
  1237. way. It has turned out that the NLST list output is not the same on all
  1238. servers either... */
  1239. /*
  1240. if FTPFILE_NOCWD was specified, we should add the path
  1241. as argument for the LIST / NLST / or custom command.
  1242. Whether the server will support this, is uncertain.
  1243. The other ftp_filemethods will CWD into dir/dir/ first and
  1244. then just do LIST (in that case: nothing to do here)
  1245. */
  1246. char *lstArg = NULL;
  1247. char *cmd;
  1248. if((data->set.ftp_filemethod == FTPFILE_NOCWD) && ftp->path) {
  1249. /* url-decode before evaluation: e.g. paths starting/ending with %2f */
  1250. const char *slashPos = NULL;
  1251. char *rawPath = NULL;
  1252. result = Curl_urldecode(data, ftp->path, 0, &rawPath, NULL, TRUE);
  1253. if(result)
  1254. return result;
  1255. slashPos = strrchr(rawPath, '/');
  1256. if(slashPos) {
  1257. /* chop off the file part if format is dir/file otherwise remove
  1258. the trailing slash for dir/dir/ except for absolute path / */
  1259. size_t n = slashPos - rawPath;
  1260. if(n == 0)
  1261. ++n;
  1262. lstArg = rawPath;
  1263. lstArg[n] = '\0';
  1264. }
  1265. else
  1266. free(rawPath);
  1267. }
  1268. cmd = aprintf("%s%s%s",
  1269. data->set.str[STRING_CUSTOMREQUEST]?
  1270. data->set.str[STRING_CUSTOMREQUEST]:
  1271. (data->set.ftp_list_only?"NLST":"LIST"),
  1272. lstArg? " ": "",
  1273. lstArg? lstArg: "");
  1274. free(lstArg);
  1275. if(!cmd)
  1276. return CURLE_OUT_OF_MEMORY;
  1277. result = Curl_pp_sendf(&conn->proto.ftpc.pp, "%s", cmd);
  1278. free(cmd);
  1279. if(result)
  1280. return result;
  1281. state(conn, FTP_LIST);
  1282. return result;
  1283. }
  1284. static CURLcode ftp_state_retr_prequote(struct connectdata *conn)
  1285. {
  1286. /* We've sent the TYPE, now we must send the list of prequote strings */
  1287. return ftp_state_quote(conn, TRUE, FTP_RETR_PREQUOTE);
  1288. }
  1289. static CURLcode ftp_state_stor_prequote(struct connectdata *conn)
  1290. {
  1291. /* We've sent the TYPE, now we must send the list of prequote strings */
  1292. return ftp_state_quote(conn, TRUE, FTP_STOR_PREQUOTE);
  1293. }
  1294. static CURLcode ftp_state_type(struct connectdata *conn)
  1295. {
  1296. CURLcode result = CURLE_OK;
  1297. struct FTP *ftp = conn->data->req.protop;
  1298. struct Curl_easy *data = conn->data;
  1299. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1300. /* If we have selected NOBODY and HEADER, it means that we only want file
  1301. information. Which in FTP can't be much more than the file size and
  1302. date. */
  1303. if(data->set.opt_no_body && ftpc->file &&
  1304. ftp_need_type(conn, data->set.prefer_ascii)) {
  1305. /* The SIZE command is _not_ RFC 959 specified, and therefore many servers
  1306. may not support it! It is however the only way we have to get a file's
  1307. size! */
  1308. ftp->transfer = FTPTRANSFER_INFO;
  1309. /* this means no actual transfer will be made */
  1310. /* Some servers return different sizes for different modes, and thus we
  1311. must set the proper type before we check the size */
  1312. result = ftp_nb_type(conn, data->set.prefer_ascii, FTP_TYPE);
  1313. if(result)
  1314. return result;
  1315. }
  1316. else
  1317. result = ftp_state_size(conn);
  1318. return result;
  1319. }
  1320. /* This is called after the CWD commands have been done in the beginning of
  1321. the DO phase */
  1322. static CURLcode ftp_state_mdtm(struct connectdata *conn)
  1323. {
  1324. CURLcode result = CURLE_OK;
  1325. struct Curl_easy *data = conn->data;
  1326. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1327. /* Requested time of file or time-depended transfer? */
  1328. if((data->set.get_filetime || data->set.timecondition) && ftpc->file) {
  1329. /* we have requested to get the modified-time of the file, this is a white
  1330. spot as the MDTM is not mentioned in RFC959 */
  1331. PPSENDF(&ftpc->pp, "MDTM %s", ftpc->file);
  1332. state(conn, FTP_MDTM);
  1333. }
  1334. else
  1335. result = ftp_state_type(conn);
  1336. return result;
  1337. }
  1338. /* This is called after the TYPE and possible quote commands have been sent */
  1339. static CURLcode ftp_state_ul_setup(struct connectdata *conn,
  1340. bool sizechecked)
  1341. {
  1342. CURLcode result = CURLE_OK;
  1343. struct FTP *ftp = conn->data->req.protop;
  1344. struct Curl_easy *data = conn->data;
  1345. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1346. if((data->state.resume_from && !sizechecked) ||
  1347. ((data->state.resume_from > 0) && sizechecked)) {
  1348. /* we're about to continue the uploading of a file */
  1349. /* 1. get already existing file's size. We use the SIZE command for this
  1350. which may not exist in the server! The SIZE command is not in
  1351. RFC959. */
  1352. /* 2. This used to set REST. But since we can do append, we
  1353. don't another ftp command. We just skip the source file
  1354. offset and then we APPEND the rest on the file instead */
  1355. /* 3. pass file-size number of bytes in the source file */
  1356. /* 4. lower the infilesize counter */
  1357. /* => transfer as usual */
  1358. int seekerr = CURL_SEEKFUNC_OK;
  1359. if(data->state.resume_from < 0) {
  1360. /* Got no given size to start from, figure it out */
  1361. PPSENDF(&ftpc->pp, "SIZE %s", ftpc->file);
  1362. state(conn, FTP_STOR_SIZE);
  1363. return result;
  1364. }
  1365. /* enable append */
  1366. data->set.ftp_append = TRUE;
  1367. /* Let's read off the proper amount of bytes from the input. */
  1368. if(conn->seek_func) {
  1369. Curl_set_in_callback(data, true);
  1370. seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
  1371. SEEK_SET);
  1372. Curl_set_in_callback(data, false);
  1373. }
  1374. if(seekerr != CURL_SEEKFUNC_OK) {
  1375. curl_off_t passed = 0;
  1376. if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
  1377. failf(data, "Could not seek stream");
  1378. return CURLE_FTP_COULDNT_USE_REST;
  1379. }
  1380. /* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
  1381. do {
  1382. size_t readthisamountnow =
  1383. (data->state.resume_from - passed > data->set.buffer_size) ?
  1384. (size_t)data->set.buffer_size :
  1385. curlx_sotouz(data->state.resume_from - passed);
  1386. size_t actuallyread =
  1387. data->state.fread_func(data->state.buffer, 1, readthisamountnow,
  1388. data->state.in);
  1389. passed += actuallyread;
  1390. if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
  1391. /* this checks for greater-than only to make sure that the
  1392. CURL_READFUNC_ABORT return code still aborts */
  1393. failf(data, "Failed to read data");
  1394. return CURLE_FTP_COULDNT_USE_REST;
  1395. }
  1396. } while(passed < data->state.resume_from);
  1397. }
  1398. /* now, decrease the size of the read */
  1399. if(data->state.infilesize>0) {
  1400. data->state.infilesize -= data->state.resume_from;
  1401. if(data->state.infilesize <= 0) {
  1402. infof(data, "File already completely uploaded\n");
  1403. /* no data to transfer */
  1404. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  1405. /* Set ->transfer so that we won't get any error in
  1406. * ftp_done() because we didn't transfer anything! */
  1407. ftp->transfer = FTPTRANSFER_NONE;
  1408. state(conn, FTP_STOP);
  1409. return CURLE_OK;
  1410. }
  1411. }
  1412. /* we've passed, proceed as normal */
  1413. } /* resume_from */
  1414. PPSENDF(&ftpc->pp, data->set.ftp_append?"APPE %s":"STOR %s",
  1415. ftpc->file);
  1416. state(conn, FTP_STOR);
  1417. return result;
  1418. }
  1419. static CURLcode ftp_state_quote(struct connectdata *conn,
  1420. bool init,
  1421. ftpstate instate)
  1422. {
  1423. CURLcode result = CURLE_OK;
  1424. struct Curl_easy *data = conn->data;
  1425. struct FTP *ftp = data->req.protop

Large files files are truncated, but you can click here to view the full file