PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/Externals/curl/lib/ftp.c

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