ftp_fopen_wrapper.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Rasmus Lerdorf <rasmus@php.net> |
  16. | Jim Winstead <jimw@php.net> |
  17. | Hartmut Holzgraefe <hholzgra@php.net> |
  18. | Sara Golemon <pollita@php.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. /* $Id$ */
  22. #include "php.h"
  23. #include "php_globals.h"
  24. #include "php_network.h"
  25. #include "php_ini.h"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <errno.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #ifdef PHP_WIN32
  33. #include <winsock2.h>
  34. #define O_RDONLY _O_RDONLY
  35. #include "win32/param.h"
  36. #else
  37. #include <sys/param.h>
  38. #endif
  39. #include "php_standard.h"
  40. #include <sys/types.h>
  41. #if HAVE_SYS_SOCKET_H
  42. #include <sys/socket.h>
  43. #endif
  44. #ifdef PHP_WIN32
  45. #include <winsock2.h>
  46. #elif defined(NETWARE) && defined(USE_WINSOCK)
  47. #include <novsock2.h>
  48. #else
  49. #include <netinet/in.h>
  50. #include <netdb.h>
  51. #if HAVE_ARPA_INET_H
  52. #include <arpa/inet.h>
  53. #endif
  54. #endif
  55. #if defined(PHP_WIN32) || defined(__riscos__) || defined(NETWARE)
  56. #undef AF_UNIX
  57. #endif
  58. #if defined(AF_UNIX)
  59. #include <sys/un.h>
  60. #endif
  61. #include "php_fopen_wrappers.h"
  62. #define FTPS_ENCRYPT_DATA 1
  63. #define GET_FTP_RESULT(stream) get_ftp_result((stream), tmp_line, sizeof(tmp_line) TSRMLS_CC)
  64. typedef struct _php_ftp_dirstream_data {
  65. php_stream *datastream;
  66. php_stream *controlstream;
  67. php_stream *dirstream;
  68. } php_ftp_dirstream_data;
  69. /* {{{ get_ftp_result
  70. */
  71. static inline int get_ftp_result(php_stream *stream, char *buffer, size_t buffer_size TSRMLS_DC)
  72. {
  73. buffer[0] = '\0'; /* in case read fails to read anything */
  74. while (php_stream_gets(stream, buffer, buffer_size-1) &&
  75. !(isdigit((int) buffer[0]) && isdigit((int) buffer[1]) &&
  76. isdigit((int) buffer[2]) && buffer[3] == ' '));
  77. return strtol(buffer, NULL, 10);
  78. }
  79. /* }}} */
  80. /* {{{ php_stream_ftp_stream_stat
  81. */
  82. static int php_stream_ftp_stream_stat(php_stream_wrapper *wrapper, php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
  83. {
  84. /* For now, we return with a failure code to prevent the underlying
  85. * file's details from being used instead. */
  86. return -1;
  87. }
  88. /* }}} */
  89. /* {{{ php_stream_ftp_stream_close
  90. */
  91. static int php_stream_ftp_stream_close(php_stream_wrapper *wrapper, php_stream *stream TSRMLS_DC)
  92. {
  93. php_stream *controlstream = stream->wrapperthis;
  94. int ret = 0;
  95. if (controlstream) {
  96. if (strpbrk(stream->mode, "wa+")) {
  97. char tmp_line[512];
  98. int result;
  99. /* For write modes close data stream first to signal EOF to server */
  100. result = GET_FTP_RESULT(controlstream);
  101. if (result != 226 && result != 250) {
  102. php_error_docref(NULL TSRMLS_CC, E_WARNING, "FTP server error %d:%s", result, tmp_line);
  103. ret = EOF;
  104. }
  105. }
  106. php_stream_write_string(controlstream, "QUIT\r\n");
  107. php_stream_close(controlstream);
  108. stream->wrapperthis = NULL;
  109. }
  110. return ret;
  111. }
  112. /* }}} */
  113. /* {{{ php_ftp_fopen_connect
  114. */
  115. static php_stream *php_ftp_fopen_connect(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
  116. char **opened_path, php_stream_context *context, php_stream **preuseid,
  117. php_url **presource, int *puse_ssl, int *puse_ssl_on_data TSRMLS_DC)
  118. {
  119. php_stream *stream = NULL, *reuseid = NULL;
  120. php_url *resource = NULL;
  121. int result, use_ssl, use_ssl_on_data = 0, tmp_len;
  122. char tmp_line[512];
  123. char *transport;
  124. int transport_len;
  125. resource = php_url_parse(path);
  126. if (resource == NULL || resource->path == NULL) {
  127. if (resource && presource) {
  128. *presource = resource;
  129. }
  130. return NULL;
  131. }
  132. use_ssl = resource->scheme && (strlen(resource->scheme) > 3) && resource->scheme[3] == 's';
  133. /* use port 21 if one wasn't specified */
  134. if (resource->port == 0)
  135. resource->port = 21;
  136. transport_len = spprintf(&transport, 0, "tcp://%s:%d", resource->host, resource->port);
  137. stream = php_stream_xport_create(transport, transport_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, NULL, NULL, context, NULL, NULL);
  138. efree(transport);
  139. if (stream == NULL) {
  140. result = 0; /* silence */
  141. goto connect_errexit;
  142. }
  143. php_stream_context_set(stream, context);
  144. php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0);
  145. /* Start talking to ftp server */
  146. result = GET_FTP_RESULT(stream);
  147. if (result > 299 || result < 200) {
  148. php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE, tmp_line, result);
  149. goto connect_errexit;
  150. }
  151. if (use_ssl) {
  152. /* send the AUTH TLS request name */
  153. php_stream_write_string(stream, "AUTH TLS\r\n");
  154. /* get the response */
  155. result = GET_FTP_RESULT(stream);
  156. if (result != 234) {
  157. /* AUTH TLS not supported try AUTH SSL */
  158. php_stream_write_string(stream, "AUTH SSL\r\n");
  159. /* get the response */
  160. result = GET_FTP_RESULT(stream);
  161. if (result != 334) {
  162. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Server doesn't support FTPS.");
  163. goto connect_errexit;
  164. } else {
  165. /* we must reuse the old SSL session id */
  166. /* if we talk to an old ftpd-ssl */
  167. reuseid = stream;
  168. }
  169. } else {
  170. /* encrypt data etc */
  171. }
  172. }
  173. if (use_ssl) {
  174. if (php_stream_xport_crypto_setup(stream,
  175. STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL TSRMLS_CC) < 0
  176. || php_stream_xport_crypto_enable(stream, 1 TSRMLS_CC) < 0) {
  177. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Unable to activate SSL mode");
  178. php_stream_close(stream);
  179. stream = NULL;
  180. goto connect_errexit;
  181. }
  182. /* set PBSZ to 0 */
  183. php_stream_write_string(stream, "PBSZ 0\r\n");
  184. /* ignore the response */
  185. result = GET_FTP_RESULT(stream);
  186. /* set data connection protection level */
  187. #if FTPS_ENCRYPT_DATA
  188. php_stream_write_string(stream, "PROT P\r\n");
  189. /* get the response */
  190. result = GET_FTP_RESULT(stream);
  191. use_ssl_on_data = (result >= 200 && result<=299) || reuseid;
  192. #else
  193. php_stream_write_string(stream, "PROT C\r\n");
  194. /* get the response */
  195. result = GET_FTP_RESULT(stream);
  196. #endif
  197. }
  198. #define PHP_FTP_CNTRL_CHK(val, val_len, err_msg) { \
  199. unsigned char *s = val, *e = s + val_len; \
  200. while (s < e) { \
  201. if (iscntrl(*s)) { \
  202. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, err_msg, val); \
  203. goto connect_errexit; \
  204. } \
  205. s++; \
  206. } \
  207. }
  208. /* send the user name */
  209. if (resource->user != NULL) {
  210. tmp_len = php_raw_url_decode(resource->user, strlen(resource->user));
  211. PHP_FTP_CNTRL_CHK(resource->user, tmp_len, "Invalid login %s")
  212. php_stream_printf(stream TSRMLS_CC, "USER %s\r\n", resource->user);
  213. } else {
  214. php_stream_write_string(stream, "USER anonymous\r\n");
  215. }
  216. /* get the response */
  217. result = GET_FTP_RESULT(stream);
  218. /* if a password is required, send it */
  219. if (result >= 300 && result <= 399) {
  220. php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_REQUIRED, tmp_line, 0);
  221. if (resource->pass != NULL) {
  222. tmp_len = php_raw_url_decode(resource->pass, strlen(resource->pass));
  223. PHP_FTP_CNTRL_CHK(resource->pass, tmp_len, "Invalid password %s")
  224. php_stream_printf(stream TSRMLS_CC, "PASS %s\r\n", resource->pass);
  225. } else {
  226. /* if the user has configured who they are,
  227. send that as the password */
  228. if (FG(from_address)) {
  229. php_stream_printf(stream TSRMLS_CC, "PASS %s\r\n", FG(from_address));
  230. } else {
  231. php_stream_write_string(stream, "PASS anonymous\r\n");
  232. }
  233. }
  234. /* read the response */
  235. result = GET_FTP_RESULT(stream);
  236. if (result > 299 || result < 200) {
  237. php_stream_notify_error(context, PHP_STREAM_NOTIFY_AUTH_RESULT, tmp_line, result);
  238. } else {
  239. php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_RESULT, tmp_line, result);
  240. }
  241. }
  242. if (result > 299 || result < 200) {
  243. goto connect_errexit;
  244. }
  245. if (puse_ssl) {
  246. *puse_ssl = use_ssl;
  247. }
  248. if (puse_ssl_on_data) {
  249. *puse_ssl_on_data = use_ssl_on_data;
  250. }
  251. if (preuseid) {
  252. *preuseid = reuseid;
  253. }
  254. if (presource) {
  255. *presource = resource;
  256. }
  257. return stream;
  258. connect_errexit:
  259. if (resource) {
  260. php_url_free(resource);
  261. }
  262. if (stream) {
  263. php_stream_close(stream);
  264. }
  265. return NULL;
  266. }
  267. /* }}} */
  268. /* {{{ php_fopen_do_pasv
  269. */
  270. static unsigned short php_fopen_do_pasv(php_stream *stream, char *ip, size_t ip_size, char **phoststart TSRMLS_DC)
  271. {
  272. char tmp_line[512];
  273. int result, i;
  274. unsigned short portno;
  275. char *tpath, *ttpath, *hoststart=NULL;
  276. #ifdef HAVE_IPV6
  277. /* We try EPSV first, needed for IPv6 and works on some IPv4 servers */
  278. php_stream_write_string(stream, "EPSV\r\n");
  279. result = GET_FTP_RESULT(stream);
  280. /* check if we got a 229 response */
  281. if (result != 229) {
  282. #endif
  283. /* EPSV failed, let's try PASV */
  284. php_stream_write_string(stream, "PASV\r\n");
  285. result = GET_FTP_RESULT(stream);
  286. /* make sure we got a 227 response */
  287. if (result != 227) {
  288. return 0;
  289. }
  290. /* parse pasv command (129, 80, 95, 25, 13, 221) */
  291. tpath = tmp_line;
  292. /* skip over the "227 Some message " part */
  293. for (tpath += 4; *tpath && !isdigit((int) *tpath); tpath++);
  294. if (!*tpath) {
  295. return 0;
  296. }
  297. /* skip over the host ip, to get the port */
  298. hoststart = tpath;
  299. for (i = 0; i < 4; i++) {
  300. for (; isdigit((int) *tpath); tpath++);
  301. if (*tpath != ',') {
  302. return 0;
  303. }
  304. *tpath='.';
  305. tpath++;
  306. }
  307. tpath[-1] = '\0';
  308. memcpy(ip, hoststart, ip_size);
  309. ip[ip_size-1] = '\0';
  310. hoststart = ip;
  311. /* pull out the MSB of the port */
  312. portno = (unsigned short) strtoul(tpath, &ttpath, 10) * 256;
  313. if (ttpath == NULL) {
  314. /* didn't get correct response from PASV */
  315. return 0;
  316. }
  317. tpath = ttpath;
  318. if (*tpath != ',') {
  319. return 0;
  320. }
  321. tpath++;
  322. /* pull out the LSB of the port */
  323. portno += (unsigned short) strtoul(tpath, &ttpath, 10);
  324. #ifdef HAVE_IPV6
  325. } else {
  326. /* parse epsv command (|||6446|) */
  327. for (i = 0, tpath = tmp_line + 4; *tpath; tpath++) {
  328. if (*tpath == '|') {
  329. i++;
  330. if (i == 3)
  331. break;
  332. }
  333. }
  334. if (i < 3) {
  335. return 0;
  336. }
  337. /* pull out the port */
  338. portno = (unsigned short) strtoul(tpath + 1, &ttpath, 10);
  339. }
  340. #endif
  341. if (ttpath == NULL) {
  342. /* didn't get correct response from EPSV/PASV */
  343. return 0;
  344. }
  345. if (phoststart) {
  346. *phoststart = hoststart;
  347. }
  348. return portno;
  349. }
  350. /* }}} */
  351. /* {{{ php_fopen_url_wrap_ftp
  352. */
  353. php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, const char *path, const char *mode,
  354. int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
  355. {
  356. php_stream *stream = NULL, *datastream = NULL;
  357. php_url *resource = NULL;
  358. char tmp_line[512];
  359. char ip[sizeof("123.123.123.123")];
  360. unsigned short portno;
  361. char *hoststart = NULL;
  362. int result = 0, use_ssl, use_ssl_on_data=0;
  363. php_stream *reuseid=NULL;
  364. size_t file_size = 0;
  365. zval **tmpzval;
  366. int allow_overwrite = 0;
  367. int read_write = 0;
  368. char *transport;
  369. int transport_len;
  370. tmp_line[0] = '\0';
  371. if (strpbrk(mode, "r+")) {
  372. read_write = 1; /* Open for reading */
  373. }
  374. if (strpbrk(mode, "wa+")) {
  375. if (read_write) {
  376. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "FTP does not support simultaneous read/write connections");
  377. return NULL;
  378. }
  379. if (strchr(mode, 'a')) {
  380. read_write = 3; /* Open for Appending */
  381. } else {
  382. read_write = 2; /* Open for writing */
  383. }
  384. }
  385. if (!read_write) {
  386. /* No mode specified? */
  387. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Unknown file open mode");
  388. return NULL;
  389. }
  390. if (context &&
  391. php_stream_context_get_option(context, "ftp", "proxy", &tmpzval) == SUCCESS) {
  392. if (read_write == 1) {
  393. /* Use http wrapper to proxy ftp request */
  394. return php_stream_url_wrap_http(wrapper, path, mode, options, opened_path, context STREAMS_CC TSRMLS_CC);
  395. } else {
  396. /* ftp proxy is read-only */
  397. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "FTP proxy may only be used in read mode");
  398. return NULL;
  399. }
  400. }
  401. stream = php_ftp_fopen_connect(wrapper, path, mode, options, opened_path, context, &reuseid, &resource, &use_ssl, &use_ssl_on_data TSRMLS_CC);
  402. if (!stream) {
  403. goto errexit;
  404. }
  405. /* set the connection to be binary */
  406. php_stream_write_string(stream, "TYPE I\r\n");
  407. result = GET_FTP_RESULT(stream);
  408. if (result > 299 || result < 200)
  409. goto errexit;
  410. /* find out the size of the file (verifying it exists) */
  411. php_stream_printf(stream TSRMLS_CC, "SIZE %s\r\n", resource->path);
  412. /* read the response */
  413. result = GET_FTP_RESULT(stream);
  414. if (read_write == 1) {
  415. /* Read Mode */
  416. char *sizestr;
  417. /* when reading file, it must exist */
  418. if (result > 299 || result < 200) {
  419. errno = ENOENT;
  420. goto errexit;
  421. }
  422. sizestr = strchr(tmp_line, ' ');
  423. if (sizestr) {
  424. sizestr++;
  425. file_size = atoi(sizestr);
  426. php_stream_notify_file_size(context, file_size, tmp_line, result);
  427. }
  428. } else if (read_write == 2) {
  429. /* when writing file (but not appending), it must NOT exist, unless a context option exists which allows it */
  430. if (context && php_stream_context_get_option(context, "ftp", "overwrite", &tmpzval) == SUCCESS) {
  431. allow_overwrite = Z_LVAL_PP(tmpzval);
  432. }
  433. if (result <= 299 && result >= 200) {
  434. if (allow_overwrite) {
  435. /* Context permits overwriting file,
  436. so we just delete whatever's there in preparation */
  437. php_stream_printf(stream TSRMLS_CC, "DELE %s\r\n", resource->path);
  438. result = GET_FTP_RESULT(stream);
  439. if (result >= 300 || result <= 199) {
  440. goto errexit;
  441. }
  442. } else {
  443. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Remote file already exists and overwrite context option not specified");
  444. errno = EEXIST;
  445. goto errexit;
  446. }
  447. }
  448. }
  449. /* set up the passive connection */
  450. portno = php_fopen_do_pasv(stream, ip, sizeof(ip), &hoststart TSRMLS_CC);
  451. if (!portno) {
  452. goto errexit;
  453. }
  454. /* Send RETR/STOR command */
  455. if (read_write == 1) {
  456. /* set resume position if applicable */
  457. if (context &&
  458. php_stream_context_get_option(context, "ftp", "resume_pos", &tmpzval) == SUCCESS &&
  459. Z_TYPE_PP(tmpzval) == IS_LONG &&
  460. Z_LVAL_PP(tmpzval) > 0) {
  461. php_stream_printf(stream TSRMLS_CC, "REST %ld\r\n", Z_LVAL_PP(tmpzval));
  462. result = GET_FTP_RESULT(stream);
  463. if (result < 300 || result > 399) {
  464. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Unable to resume from offset %ld", Z_LVAL_PP(tmpzval));
  465. goto errexit;
  466. }
  467. }
  468. /* retrieve file */
  469. memcpy(tmp_line, "RETR", sizeof("RETR"));
  470. } else if (read_write == 2) {
  471. /* Write new file */
  472. memcpy(tmp_line, "STOR", sizeof("STOR"));
  473. } else {
  474. /* Append */
  475. memcpy(tmp_line, "APPE", sizeof("APPE"));
  476. }
  477. php_stream_printf(stream TSRMLS_CC, "%s %s\r\n", tmp_line, (resource->path != NULL ? resource->path : "/"));
  478. /* open the data channel */
  479. if (hoststart == NULL) {
  480. hoststart = resource->host;
  481. }
  482. transport_len = spprintf(&transport, 0, "tcp://%s:%d", hoststart, portno);
  483. datastream = php_stream_xport_create(transport, transport_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, NULL, NULL, context, NULL, NULL);
  484. efree(transport);
  485. if (datastream == NULL) {
  486. goto errexit;
  487. }
  488. result = GET_FTP_RESULT(stream);
  489. if (result != 150 && result != 125) {
  490. /* Could not retrieve or send the file
  491. * this data will only be sent to us after connection on the data port was initiated.
  492. */
  493. php_stream_close(datastream);
  494. datastream = NULL;
  495. goto errexit;
  496. }
  497. php_stream_context_set(datastream, context);
  498. php_stream_notify_progress_init(context, 0, file_size);
  499. if (use_ssl_on_data && (php_stream_xport_crypto_setup(datastream,
  500. STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL TSRMLS_CC) < 0 ||
  501. php_stream_xport_crypto_enable(datastream, 1 TSRMLS_CC) < 0)) {
  502. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Unable to activate SSL mode");
  503. php_stream_close(datastream);
  504. datastream = NULL;
  505. goto errexit;
  506. }
  507. /* remember control stream */
  508. datastream->wrapperthis = stream;
  509. php_url_free(resource);
  510. return datastream;
  511. errexit:
  512. if (resource) {
  513. php_url_free(resource);
  514. }
  515. if (stream) {
  516. php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE, tmp_line, result);
  517. php_stream_close(stream);
  518. }
  519. if (tmp_line[0] != '\0')
  520. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "FTP server reports %s", tmp_line);
  521. return NULL;
  522. }
  523. /* }}} */
  524. /* {{{ php_ftp_dirsteam_read
  525. */
  526. static size_t php_ftp_dirstream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
  527. {
  528. php_stream_dirent *ent = (php_stream_dirent *)buf;
  529. php_stream *innerstream;
  530. size_t tmp_len;
  531. char *basename;
  532. size_t basename_len;
  533. innerstream = ((php_ftp_dirstream_data *)stream->abstract)->datastream;
  534. if (count != sizeof(php_stream_dirent)) {
  535. return 0;
  536. }
  537. if (php_stream_eof(innerstream)) {
  538. return 0;
  539. }
  540. if (!php_stream_get_line(innerstream, ent->d_name, sizeof(ent->d_name), &tmp_len)) {
  541. return 0;
  542. }
  543. php_basename(ent->d_name, tmp_len, NULL, 0, &basename, &basename_len TSRMLS_CC);
  544. if (!basename) {
  545. return 0;
  546. }
  547. if (!basename_len) {
  548. efree(basename);
  549. return 0;
  550. }
  551. tmp_len = MIN(sizeof(ent->d_name), basename_len - 1);
  552. memcpy(ent->d_name, basename, tmp_len);
  553. ent->d_name[tmp_len - 1] = '\0';
  554. efree(basename);
  555. /* Trim off trailing whitespace characters */
  556. while (tmp_len > 0 &&
  557. (ent->d_name[tmp_len - 1] == '\n' || ent->d_name[tmp_len - 1] == '\r' ||
  558. ent->d_name[tmp_len - 1] == '\t' || ent->d_name[tmp_len - 1] == ' ')) {
  559. ent->d_name[--tmp_len] = '\0';
  560. }
  561. return sizeof(php_stream_dirent);
  562. }
  563. /* }}} */
  564. /* {{{ php_ftp_dirstream_close
  565. */
  566. static int php_ftp_dirstream_close(php_stream *stream, int close_handle TSRMLS_DC)
  567. {
  568. php_ftp_dirstream_data *data = stream->abstract;
  569. /* close control connection */
  570. if (data->controlstream) {
  571. php_stream_close(data->controlstream);
  572. data->controlstream = NULL;
  573. }
  574. /* close data connection */
  575. php_stream_close(data->datastream);
  576. data->datastream = NULL;
  577. efree(data);
  578. stream->abstract = NULL;
  579. return 0;
  580. }
  581. /* }}} */
  582. /* ftp dirstreams only need to support read and close operations,
  583. They can't be rewound because the underlying ftp stream can't be rewound. */
  584. static php_stream_ops php_ftp_dirstream_ops = {
  585. NULL, /* write */
  586. php_ftp_dirstream_read, /* read */
  587. php_ftp_dirstream_close, /* close */
  588. NULL, /* flush */
  589. "ftpdir",
  590. NULL, /* rewind */
  591. NULL, /* cast */
  592. NULL, /* stat */
  593. NULL /* set option */
  594. };
  595. /* {{{ php_stream_ftp_opendir
  596. */
  597. php_stream * php_stream_ftp_opendir(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
  598. char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
  599. {
  600. php_stream *stream, *reuseid, *datastream = NULL;
  601. php_ftp_dirstream_data *dirsdata;
  602. php_url *resource = NULL;
  603. int result = 0, use_ssl, use_ssl_on_data = 0;
  604. char *hoststart = NULL, tmp_line[512];
  605. char ip[sizeof("123.123.123.123")];
  606. unsigned short portno;
  607. tmp_line[0] = '\0';
  608. stream = php_ftp_fopen_connect(wrapper, path, mode, options, opened_path, context, &reuseid, &resource, &use_ssl, &use_ssl_on_data TSRMLS_CC);
  609. if (!stream) {
  610. goto opendir_errexit;
  611. }
  612. /* set the connection to be ascii */
  613. php_stream_write_string(stream, "TYPE A\r\n");
  614. result = GET_FTP_RESULT(stream);
  615. if (result > 299 || result < 200)
  616. goto opendir_errexit;
  617. // tmp_line isn't relevant after the php_fopen_do_pasv().
  618. tmp_line[0] = '\0';
  619. /* set up the passive connection */
  620. portno = php_fopen_do_pasv(stream, ip, sizeof(ip), &hoststart TSRMLS_CC);
  621. if (!portno) {
  622. goto opendir_errexit;
  623. }
  624. /* open the data channel */
  625. if (hoststart == NULL) {
  626. hoststart = resource->host;
  627. }
  628. datastream = php_stream_sock_open_host(hoststart, portno, SOCK_STREAM, 0, 0);
  629. if (datastream == NULL) {
  630. goto opendir_errexit;
  631. }
  632. php_stream_printf(stream TSRMLS_CC, "NLST %s\r\n", (resource->path != NULL ? resource->path : "/"));
  633. result = GET_FTP_RESULT(stream);
  634. if (result != 150 && result != 125) {
  635. /* Could not retrieve or send the file
  636. * this data will only be sent to us after connection on the data port was initiated.
  637. */
  638. php_stream_close(datastream);
  639. datastream = NULL;
  640. goto opendir_errexit;
  641. }
  642. php_stream_context_set(datastream, context);
  643. if (use_ssl_on_data && (php_stream_xport_crypto_setup(datastream,
  644. STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL TSRMLS_CC) < 0 ||
  645. php_stream_xport_crypto_enable(datastream, 1 TSRMLS_CC) < 0)) {
  646. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Unable to activate SSL mode");
  647. php_stream_close(datastream);
  648. datastream = NULL;
  649. goto opendir_errexit;
  650. }
  651. php_url_free(resource);
  652. dirsdata = emalloc(sizeof *dirsdata);
  653. dirsdata->datastream = datastream;
  654. dirsdata->controlstream = stream;
  655. dirsdata->dirstream = php_stream_alloc(&php_ftp_dirstream_ops, dirsdata, 0, mode);
  656. return dirsdata->dirstream;
  657. opendir_errexit:
  658. if (resource) {
  659. php_url_free(resource);
  660. }
  661. if (stream) {
  662. php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE, tmp_line, result);
  663. php_stream_close(stream);
  664. }
  665. if (tmp_line[0] != '\0') {
  666. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "FTP server reports %s", tmp_line);
  667. }
  668. return NULL;
  669. }
  670. /* }}} */
  671. /* {{{ php_stream_ftp_url_stat
  672. */
  673. static int php_stream_ftp_url_stat(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context TSRMLS_DC)
  674. {
  675. php_stream *stream = NULL;
  676. php_url *resource = NULL;
  677. int result;
  678. char tmp_line[512];
  679. /* If ssb is NULL then someone is misbehaving */
  680. if (!ssb) return -1;
  681. stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL TSRMLS_CC);
  682. if (!stream) {
  683. goto stat_errexit;
  684. }
  685. ssb->sb.st_mode = 0644; /* FTP won't give us a valid mode, so aproximate one based on being readable */
  686. php_stream_printf(stream TSRMLS_CC, "CWD %s\r\n", (resource->path != NULL ? resource->path : "/")); /* If we can CWD to it, it's a directory (maybe a link, but we can't tell) */
  687. result = GET_FTP_RESULT(stream);
  688. if (result < 200 || result > 299) {
  689. ssb->sb.st_mode |= S_IFREG;
  690. } else {
  691. ssb->sb.st_mode |= S_IFDIR;
  692. }
  693. php_stream_write_string(stream, "TYPE I\r\n"); /* we need this since some servers refuse to accept SIZE command in ASCII mode */
  694. result = GET_FTP_RESULT(stream);
  695. if(result < 200 || result > 299) {
  696. goto stat_errexit;
  697. }
  698. php_stream_printf(stream TSRMLS_CC, "SIZE %s\r\n", (resource->path != NULL ? resource->path : "/"));
  699. result = GET_FTP_RESULT(stream);
  700. if (result < 200 || result > 299) {
  701. /* Failure either means it doesn't exist
  702. or it's a directory and this server
  703. fails on listing directory sizes */
  704. if (ssb->sb.st_mode & S_IFDIR) {
  705. ssb->sb.st_size = 0;
  706. } else {
  707. goto stat_errexit;
  708. }
  709. } else {
  710. ssb->sb.st_size = atoi(tmp_line + 4);
  711. }
  712. php_stream_printf(stream TSRMLS_CC, "MDTM %s\r\n", (resource->path != NULL ? resource->path : "/"));
  713. result = GET_FTP_RESULT(stream);
  714. if (result == 213) {
  715. char *p = tmp_line + 4;
  716. int n;
  717. struct tm tm, tmbuf, *gmt;
  718. time_t stamp;
  719. while (p - tmp_line < sizeof(tmp_line) && !isdigit(*p)) {
  720. p++;
  721. }
  722. if (p - tmp_line > sizeof(tmp_line)) {
  723. goto mdtm_error;
  724. }
  725. n = sscanf(p, "%4u%2u%2u%2u%2u%2u", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
  726. if (n != 6) {
  727. goto mdtm_error;
  728. }
  729. tm.tm_year -= 1900;
  730. tm.tm_mon--;
  731. tm.tm_isdst = -1;
  732. /* figure out the GMT offset */
  733. stamp = time(NULL);
  734. gmt = php_gmtime_r(&stamp, &tmbuf);
  735. if (!gmt) {
  736. goto mdtm_error;
  737. }
  738. gmt->tm_isdst = -1;
  739. /* apply the GMT offset */
  740. tm.tm_sec += stamp - mktime(gmt);
  741. tm.tm_isdst = gmt->tm_isdst;
  742. ssb->sb.st_mtime = mktime(&tm);
  743. } else {
  744. /* error or unsupported command */
  745. mdtm_error:
  746. ssb->sb.st_mtime = -1;
  747. }
  748. ssb->sb.st_ino = 0; /* Unknown values */
  749. ssb->sb.st_dev = 0;
  750. ssb->sb.st_uid = 0;
  751. ssb->sb.st_gid = 0;
  752. ssb->sb.st_atime = -1;
  753. ssb->sb.st_ctime = -1;
  754. ssb->sb.st_nlink = 1;
  755. ssb->sb.st_rdev = -1;
  756. #ifdef HAVE_ST_BLKSIZE
  757. ssb->sb.st_blksize = 4096; /* Guess since FTP won't expose this information */
  758. #ifdef HAVE_ST_BLOCKS
  759. ssb->sb.st_blocks = (int)((4095 + ssb->sb.st_size) / ssb->sb.st_blksize); /* emulate ceil */
  760. #endif
  761. #endif
  762. php_stream_close(stream);
  763. php_url_free(resource);
  764. return 0;
  765. stat_errexit:
  766. if (resource) {
  767. php_url_free(resource);
  768. }
  769. if (stream) {
  770. php_stream_close(stream);
  771. }
  772. return -1;
  773. }
  774. /* }}} */
  775. /* {{{ php_stream_ftp_unlink
  776. */
  777. static int php_stream_ftp_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context TSRMLS_DC)
  778. {
  779. php_stream *stream = NULL;
  780. php_url *resource = NULL;
  781. int result;
  782. char tmp_line[512];
  783. stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, NULL, NULL, &resource, NULL, NULL TSRMLS_CC);
  784. if (!stream) {
  785. if (options & REPORT_ERRORS) {
  786. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to %s", url);
  787. }
  788. goto unlink_errexit;
  789. }
  790. if (resource->path == NULL) {
  791. if (options & REPORT_ERRORS) {
  792. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid path provided in %s", url);
  793. }
  794. goto unlink_errexit;
  795. }
  796. /* Attempt to delete the file */
  797. php_stream_printf(stream TSRMLS_CC, "DELE %s\r\n", (resource->path != NULL ? resource->path : "/"));
  798. result = GET_FTP_RESULT(stream);
  799. if (result < 200 || result > 299) {
  800. if (options & REPORT_ERRORS) {
  801. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error Deleting file: %s", tmp_line);
  802. }
  803. goto unlink_errexit;
  804. }
  805. php_url_free(resource);
  806. php_stream_close(stream);
  807. return 1;
  808. unlink_errexit:
  809. if (resource) {
  810. php_url_free(resource);
  811. }
  812. if (stream) {
  813. php_stream_close(stream);
  814. }
  815. return 0;
  816. }
  817. /* }}} */
  818. /* {{{ php_stream_ftp_rename
  819. */
  820. static int php_stream_ftp_rename(php_stream_wrapper *wrapper, const char *url_from, const char *url_to, int options, php_stream_context *context TSRMLS_DC)
  821. {
  822. php_stream *stream = NULL;
  823. php_url *resource_from = NULL, *resource_to = NULL;
  824. int result;
  825. char tmp_line[512];
  826. resource_from = php_url_parse(url_from);
  827. resource_to = php_url_parse(url_to);
  828. /* Must be same scheme (ftp/ftp or ftps/ftps), same host, and same port
  829. (or a 21/0 0/21 combination which is also "same")
  830. Also require paths to/from */
  831. if (!resource_from ||
  832. !resource_to ||
  833. !resource_from->scheme ||
  834. !resource_to->scheme ||
  835. strcmp(resource_from->scheme, resource_to->scheme) ||
  836. !resource_from->host ||
  837. !resource_to->host ||
  838. strcmp(resource_from->host, resource_to->host) ||
  839. (resource_from->port != resource_to->port &&
  840. resource_from->port * resource_to->port != 0 &&
  841. resource_from->port + resource_to->port != 21) ||
  842. !resource_from->path ||
  843. !resource_to->path) {
  844. goto rename_errexit;
  845. }
  846. stream = php_ftp_fopen_connect(wrapper, url_from, "r", 0, NULL, NULL, NULL, NULL, NULL, NULL TSRMLS_CC);
  847. if (!stream) {
  848. if (options & REPORT_ERRORS) {
  849. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to %s", resource_from->host);
  850. }
  851. goto rename_errexit;
  852. }
  853. /* Rename FROM */
  854. php_stream_printf(stream TSRMLS_CC, "RNFR %s\r\n", (resource_from->path != NULL ? resource_from->path : "/"));
  855. result = GET_FTP_RESULT(stream);
  856. if (result < 300 || result > 399) {
  857. if (options & REPORT_ERRORS) {
  858. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error Renaming file: %s", tmp_line);
  859. }
  860. goto rename_errexit;
  861. }
  862. /* Rename TO */
  863. php_stream_printf(stream TSRMLS_CC, "RNTO %s\r\n", (resource_to->path != NULL ? resource_to->path : "/"));
  864. result = GET_FTP_RESULT(stream);
  865. if (result < 200 || result > 299) {
  866. if (options & REPORT_ERRORS) {
  867. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error Renaming file: %s", tmp_line);
  868. }
  869. goto rename_errexit;
  870. }
  871. php_url_free(resource_from);
  872. php_url_free(resource_to);
  873. php_stream_close(stream);
  874. return 1;
  875. rename_errexit:
  876. if (resource_from) {
  877. php_url_free(resource_from);
  878. }
  879. if (resource_to) {
  880. php_url_free(resource_to);
  881. }
  882. if (stream) {
  883. php_stream_close(stream);
  884. }
  885. return 0;
  886. }
  887. /* }}} */
  888. /* {{{ php_stream_ftp_mkdir
  889. */
  890. static int php_stream_ftp_mkdir(php_stream_wrapper *wrapper, const char *url, int mode, int options, php_stream_context *context TSRMLS_DC)
  891. {
  892. php_stream *stream = NULL;
  893. php_url *resource = NULL;
  894. int result, recursive = options & PHP_STREAM_MKDIR_RECURSIVE;
  895. char tmp_line[512];
  896. stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, NULL, NULL, &resource, NULL, NULL TSRMLS_CC);
  897. if (!stream) {
  898. if (options & REPORT_ERRORS) {
  899. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to %s", url);
  900. }
  901. goto mkdir_errexit;
  902. }
  903. if (resource->path == NULL) {
  904. if (options & REPORT_ERRORS) {
  905. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid path provided in %s", url);
  906. }
  907. goto mkdir_errexit;
  908. }
  909. if (!recursive) {
  910. php_stream_printf(stream TSRMLS_CC, "MKD %s\r\n", resource->path);
  911. result = GET_FTP_RESULT(stream);
  912. } else {
  913. /* we look for directory separator from the end of string, thus hopefuly reducing our work load */
  914. char *p, *e, *buf;
  915. buf = estrdup(resource->path);
  916. e = buf + strlen(buf);
  917. /* find a top level directory we need to create */
  918. while ((p = strrchr(buf, '/'))) {
  919. *p = '\0';
  920. php_stream_printf(stream TSRMLS_CC, "CWD %s\r\n", buf);
  921. result = GET_FTP_RESULT(stream);
  922. if (result >= 200 && result <= 299) {
  923. *p = '/';
  924. break;
  925. }
  926. }
  927. if (p == buf) {
  928. php_stream_printf(stream TSRMLS_CC, "MKD %s\r\n", resource->path);
  929. result = GET_FTP_RESULT(stream);
  930. } else {
  931. php_stream_printf(stream TSRMLS_CC, "MKD %s\r\n", buf);
  932. result = GET_FTP_RESULT(stream);
  933. if (result >= 200 && result <= 299) {
  934. if (!p) {
  935. p = buf;
  936. }
  937. /* create any needed directories if the creation of the 1st directory worked */
  938. while (++p != e) {
  939. if (*p == '\0' && *(p + 1) != '\0') {
  940. *p = '/';
  941. php_stream_printf(stream TSRMLS_CC, "MKD %s\r\n", buf);
  942. result = GET_FTP_RESULT(stream);
  943. if (result < 200 || result > 299) {
  944. if (options & REPORT_ERRORS) {
  945. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tmp_line);
  946. }
  947. break;
  948. }
  949. }
  950. }
  951. }
  952. }
  953. efree(buf);
  954. }
  955. php_url_free(resource);
  956. php_stream_close(stream);
  957. if (result < 200 || result > 299) {
  958. /* Failure */
  959. return 0;
  960. }
  961. return 1;
  962. mkdir_errexit:
  963. if (resource) {
  964. php_url_free(resource);
  965. }
  966. if (stream) {
  967. php_stream_close(stream);
  968. }
  969. return 0;
  970. }
  971. /* }}} */
  972. /* {{{ php_stream_ftp_rmdir
  973. */
  974. static int php_stream_ftp_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context TSRMLS_DC)
  975. {
  976. php_stream *stream = NULL;
  977. php_url *resource = NULL;
  978. int result;
  979. char tmp_line[512];
  980. stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, NULL, NULL, &resource, NULL, NULL TSRMLS_CC);
  981. if (!stream) {
  982. if (options & REPORT_ERRORS) {
  983. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to %s", url);
  984. }
  985. goto rmdir_errexit;
  986. }
  987. if (resource->path == NULL) {
  988. if (options & REPORT_ERRORS) {
  989. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid path provided in %s", url);
  990. }
  991. goto rmdir_errexit;
  992. }
  993. php_stream_printf(stream TSRMLS_CC, "RMD %s\r\n", resource->path);
  994. result = GET_FTP_RESULT(stream);
  995. if (result < 200 || result > 299) {
  996. if (options & REPORT_ERRORS) {
  997. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tmp_line);
  998. }
  999. goto rmdir_errexit;
  1000. }
  1001. php_url_free(resource);
  1002. php_stream_close(stream);
  1003. return 1;
  1004. rmdir_errexit:
  1005. if (resource) {
  1006. php_url_free(resource);
  1007. }
  1008. if (stream) {
  1009. php_stream_close(stream);
  1010. }
  1011. return 0;
  1012. }
  1013. /* }}} */
  1014. static php_stream_wrapper_ops ftp_stream_wops = {
  1015. php_stream_url_wrap_ftp,
  1016. php_stream_ftp_stream_close, /* stream_close */
  1017. php_stream_ftp_stream_stat,
  1018. php_stream_ftp_url_stat, /* stat_url */
  1019. php_stream_ftp_opendir, /* opendir */
  1020. "ftp",
  1021. php_stream_ftp_unlink, /* unlink */
  1022. php_stream_ftp_rename, /* rename */
  1023. php_stream_ftp_mkdir, /* mkdir */
  1024. php_stream_ftp_rmdir /* rmdir */
  1025. };
  1026. PHPAPI php_stream_wrapper php_stream_ftp_wrapper = {
  1027. &ftp_stream_wops,
  1028. NULL,
  1029. 1 /* is_url */
  1030. };
  1031. /*
  1032. * Local variables:
  1033. * tab-width: 4
  1034. * c-basic-offset: 4
  1035. * End:
  1036. * vim600: sw=4 ts=4 fdm=marker
  1037. * vim<600: sw=4 ts=4
  1038. */