ftp_fopen_wrapper.c 32 KB

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