php_fopen_wrapper.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. +----------------------------------------------------------------------+
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #if HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #include "php.h"
  26. #include "php_globals.h"
  27. #include "php_standard.h"
  28. #include "php_memory_streams.h"
  29. #include "php_fopen_wrappers.h"
  30. #include "SAPI.h"
  31. static size_t php_stream_output_write(php_stream *stream, const char *buf, size_t count) /* {{{ */
  32. {
  33. PHPWRITE(buf, count);
  34. return count;
  35. }
  36. /* }}} */
  37. static size_t php_stream_output_read(php_stream *stream, char *buf, size_t count) /* {{{ */
  38. {
  39. stream->eof = 1;
  40. return 0;
  41. }
  42. /* }}} */
  43. static int php_stream_output_close(php_stream *stream, int close_handle) /* {{{ */
  44. {
  45. return 0;
  46. }
  47. /* }}} */
  48. const php_stream_ops php_stream_output_ops = {
  49. php_stream_output_write,
  50. php_stream_output_read,
  51. php_stream_output_close,
  52. NULL, /* flush */
  53. "Output",
  54. NULL, /* seek */
  55. NULL, /* cast */
  56. NULL, /* stat */
  57. NULL /* set_option */
  58. };
  59. typedef struct php_stream_input { /* {{{ */
  60. php_stream *body;
  61. zend_off_t position;
  62. } php_stream_input_t;
  63. /* }}} */
  64. static size_t php_stream_input_write(php_stream *stream, const char *buf, size_t count) /* {{{ */
  65. {
  66. return -1;
  67. }
  68. /* }}} */
  69. static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count) /* {{{ */
  70. {
  71. php_stream_input_t *input = stream->abstract;
  72. size_t read;
  73. if (!SG(post_read) && SG(read_post_bytes) < (int64_t)(input->position + count)) {
  74. /* read requested data from SAPI */
  75. size_t read_bytes = sapi_read_post_block(buf, count);
  76. if (read_bytes > 0) {
  77. php_stream_seek(input->body, 0, SEEK_END);
  78. php_stream_write(input->body, buf, read_bytes);
  79. }
  80. }
  81. if (!input->body->readfilters.head) {
  82. /* If the input stream contains filters, it's not really seekable. The
  83. input->position is likely to be wrong for unfiltered data. */
  84. php_stream_seek(input->body, input->position, SEEK_SET);
  85. }
  86. read = php_stream_read(input->body, buf, count);
  87. if (!read || read == (size_t) -1) {
  88. stream->eof = 1;
  89. } else {
  90. input->position += read;
  91. }
  92. return read;
  93. }
  94. /* }}} */
  95. static int php_stream_input_close(php_stream *stream, int close_handle) /* {{{ */
  96. {
  97. efree(stream->abstract);
  98. stream->abstract = NULL;
  99. return 0;
  100. }
  101. /* }}} */
  102. static int php_stream_input_flush(php_stream *stream) /* {{{ */
  103. {
  104. return -1;
  105. }
  106. /* }}} */
  107. static int php_stream_input_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset) /* {{{ */
  108. {
  109. php_stream_input_t *input = stream->abstract;
  110. if (input->body) {
  111. int sought = php_stream_seek(input->body, offset, whence);
  112. *newoffset = input->position = (input->body)->position;
  113. return sought;
  114. }
  115. return -1;
  116. }
  117. /* }}} */
  118. const php_stream_ops php_stream_input_ops = {
  119. php_stream_input_write,
  120. php_stream_input_read,
  121. php_stream_input_close,
  122. php_stream_input_flush,
  123. "Input",
  124. php_stream_input_seek,
  125. NULL, /* cast */
  126. NULL, /* stat */
  127. NULL /* set_option */
  128. };
  129. static void php_stream_apply_filter_list(php_stream *stream, char *filterlist, int read_chain, int write_chain) /* {{{ */
  130. {
  131. char *p, *token = NULL;
  132. php_stream_filter *temp_filter;
  133. p = php_strtok_r(filterlist, "|", &token);
  134. while (p) {
  135. php_url_decode(p, strlen(p));
  136. if (read_chain) {
  137. if ((temp_filter = php_stream_filter_create(p, NULL, php_stream_is_persistent(stream)))) {
  138. php_stream_filter_append(&stream->readfilters, temp_filter);
  139. } else {
  140. php_error_docref(NULL, E_WARNING, "Unable to create filter (%s)", p);
  141. }
  142. }
  143. if (write_chain) {
  144. if ((temp_filter = php_stream_filter_create(p, NULL, php_stream_is_persistent(stream)))) {
  145. php_stream_filter_append(&stream->writefilters, temp_filter);
  146. } else {
  147. php_error_docref(NULL, E_WARNING, "Unable to create filter (%s)", p);
  148. }
  149. }
  150. p = php_strtok_r(NULL, "|", &token);
  151. }
  152. }
  153. /* }}} */
  154. php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
  155. zend_string **opened_path, php_stream_context *context STREAMS_DC) /* {{{ */
  156. {
  157. int fd = -1;
  158. int mode_rw = 0;
  159. php_stream * stream = NULL;
  160. char *p, *token, *pathdup;
  161. zend_long max_memory;
  162. FILE *file = NULL;
  163. #ifdef PHP_WIN32
  164. int pipe_requested = 0;
  165. #endif
  166. if (!strncasecmp(path, "php://", 6)) {
  167. path += 6;
  168. }
  169. if (!strncasecmp(path, "temp", 4)) {
  170. path += 4;
  171. max_memory = PHP_STREAM_MAX_MEM;
  172. if (!strncasecmp(path, "/maxmemory:", 11)) {
  173. path += 11;
  174. max_memory = ZEND_STRTOL(path, NULL, 10);
  175. if (max_memory < 0) {
  176. zend_throw_error(NULL, "Max memory must be >= 0");
  177. return NULL;
  178. }
  179. }
  180. mode_rw = php_stream_mode_from_str(mode);
  181. return php_stream_temp_create(mode_rw, max_memory);
  182. }
  183. if (!strcasecmp(path, "memory")) {
  184. mode_rw = php_stream_mode_from_str(mode);
  185. return php_stream_memory_create(mode_rw);
  186. }
  187. if (!strcasecmp(path, "output")) {
  188. return php_stream_alloc(&php_stream_output_ops, NULL, 0, "wb");
  189. }
  190. if (!strcasecmp(path, "input")) {
  191. php_stream_input_t *input;
  192. if ((options & STREAM_OPEN_FOR_INCLUDE) && !PG(allow_url_include) ) {
  193. if (options & REPORT_ERRORS) {
  194. php_error_docref(NULL, E_WARNING, "URL file-access is disabled in the server configuration");
  195. }
  196. return NULL;
  197. }
  198. input = ecalloc(1, sizeof(*input));
  199. if ((input->body = SG(request_info).request_body)) {
  200. php_stream_rewind(input->body);
  201. } else {
  202. input->body = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir));
  203. SG(request_info).request_body = input->body;
  204. }
  205. return php_stream_alloc(&php_stream_input_ops, input, 0, "rb");
  206. }
  207. if (!strcasecmp(path, "stdin")) {
  208. if ((options & STREAM_OPEN_FOR_INCLUDE) && !PG(allow_url_include) ) {
  209. if (options & REPORT_ERRORS) {
  210. php_error_docref(NULL, E_WARNING, "URL file-access is disabled in the server configuration");
  211. }
  212. return NULL;
  213. }
  214. if (!strcmp(sapi_module.name, "cli")) {
  215. static int cli_in = 0;
  216. fd = STDIN_FILENO;
  217. if (cli_in) {
  218. fd = dup(fd);
  219. } else {
  220. cli_in = 1;
  221. file = stdin;
  222. }
  223. } else {
  224. fd = dup(STDIN_FILENO);
  225. }
  226. #ifdef PHP_WIN32
  227. pipe_requested = 1;
  228. #endif
  229. } else if (!strcasecmp(path, "stdout")) {
  230. if (!strcmp(sapi_module.name, "cli")) {
  231. static int cli_out = 0;
  232. fd = STDOUT_FILENO;
  233. if (cli_out++) {
  234. fd = dup(fd);
  235. } else {
  236. cli_out = 1;
  237. file = stdout;
  238. }
  239. } else {
  240. fd = dup(STDOUT_FILENO);
  241. }
  242. #ifdef PHP_WIN32
  243. pipe_requested = 1;
  244. #endif
  245. } else if (!strcasecmp(path, "stderr")) {
  246. if (!strcmp(sapi_module.name, "cli")) {
  247. static int cli_err = 0;
  248. fd = STDERR_FILENO;
  249. if (cli_err++) {
  250. fd = dup(fd);
  251. } else {
  252. cli_err = 1;
  253. file = stderr;
  254. }
  255. } else {
  256. fd = dup(STDERR_FILENO);
  257. }
  258. #ifdef PHP_WIN32
  259. pipe_requested = 1;
  260. #endif
  261. } else if (!strncasecmp(path, "fd/", 3)) {
  262. const char *start;
  263. char *end;
  264. zend_long fildes_ori;
  265. int dtablesize;
  266. if (strcmp(sapi_module.name, "cli")) {
  267. if (options & REPORT_ERRORS) {
  268. php_error_docref(NULL, E_WARNING, "Direct access to file descriptors is only available from command-line PHP");
  269. }
  270. return NULL;
  271. }
  272. if ((options & STREAM_OPEN_FOR_INCLUDE) && !PG(allow_url_include) ) {
  273. if (options & REPORT_ERRORS) {
  274. php_error_docref(NULL, E_WARNING, "URL file-access is disabled in the server configuration");
  275. }
  276. return NULL;
  277. }
  278. start = &path[3];
  279. fildes_ori = ZEND_STRTOL(start, &end, 10);
  280. if (end == start || *end != '\0') {
  281. php_stream_wrapper_log_error(wrapper, options,
  282. "php://fd/ stream must be specified in the form php://fd/<orig fd>");
  283. return NULL;
  284. }
  285. #if HAVE_UNISTD_H
  286. dtablesize = getdtablesize();
  287. #else
  288. dtablesize = INT_MAX;
  289. #endif
  290. if (fildes_ori < 0 || fildes_ori >= dtablesize) {
  291. php_stream_wrapper_log_error(wrapper, options,
  292. "The file descriptors must be non-negative numbers smaller than %d", dtablesize);
  293. return NULL;
  294. }
  295. fd = dup((int)fildes_ori);
  296. if (fd == -1) {
  297. php_stream_wrapper_log_error(wrapper, options,
  298. "Error duping file descriptor " ZEND_LONG_FMT "; possibly it doesn't exist: "
  299. "[%d]: %s", fildes_ori, errno, strerror(errno));
  300. return NULL;
  301. }
  302. } else if (!strncasecmp(path, "filter/", 7)) {
  303. /* Save time/memory when chain isn't specified */
  304. if (strchr(mode, 'r') || strchr(mode, '+')) {
  305. mode_rw |= PHP_STREAM_FILTER_READ;
  306. }
  307. if (strchr(mode, 'w') || strchr(mode, '+') || strchr(mode, 'a')) {
  308. mode_rw |= PHP_STREAM_FILTER_WRITE;
  309. }
  310. pathdup = estrndup(path + 6, strlen(path + 6));
  311. p = strstr(pathdup, "/resource=");
  312. if (!p) {
  313. zend_throw_error(NULL, "No URL resource specified");
  314. efree(pathdup);
  315. return NULL;
  316. }
  317. if (!(stream = php_stream_open_wrapper(p + 10, mode, options, opened_path))) {
  318. efree(pathdup);
  319. return NULL;
  320. }
  321. *p = '\0';
  322. p = php_strtok_r(pathdup + 1, "/", &token);
  323. while (p) {
  324. if (!strncasecmp(p, "read=", 5)) {
  325. php_stream_apply_filter_list(stream, p + 5, 1, 0);
  326. } else if (!strncasecmp(p, "write=", 6)) {
  327. php_stream_apply_filter_list(stream, p + 6, 0, 1);
  328. } else {
  329. php_stream_apply_filter_list(stream, p, mode_rw & PHP_STREAM_FILTER_READ, mode_rw & PHP_STREAM_FILTER_WRITE);
  330. }
  331. p = php_strtok_r(NULL, "/", &token);
  332. }
  333. efree(pathdup);
  334. if (EG(exception)) {
  335. php_stream_close(stream);
  336. return NULL;
  337. }
  338. return stream;
  339. } else {
  340. /* invalid php://thingy */
  341. php_error_docref(NULL, E_WARNING, "Invalid php:// URL specified");
  342. return NULL;
  343. }
  344. /* must be stdin, stderr or stdout */
  345. if (fd == -1) {
  346. /* failed to dup */
  347. return NULL;
  348. }
  349. #if defined(S_IFSOCK) && !defined(PHP_WIN32)
  350. do {
  351. zend_stat_t st;
  352. memset(&st, 0, sizeof(st));
  353. if (zend_fstat(fd, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
  354. stream = php_stream_sock_open_from_socket(fd, NULL);
  355. if (stream) {
  356. stream->ops = &php_stream_socket_ops;
  357. return stream;
  358. }
  359. }
  360. } while (0);
  361. #endif
  362. if (file) {
  363. stream = php_stream_fopen_from_file(file, mode);
  364. } else {
  365. stream = php_stream_fopen_from_fd(fd, mode, NULL);
  366. if (stream == NULL) {
  367. close(fd);
  368. }
  369. }
  370. #ifdef PHP_WIN32
  371. if (pipe_requested && stream && context) {
  372. zval *blocking_pipes = php_stream_context_get_option(context, "pipe", "blocking");
  373. if (blocking_pipes) {
  374. php_stream_set_option(stream, PHP_STREAM_OPTION_PIPE_BLOCKING, zval_get_long(blocking_pipes), NULL);
  375. }
  376. }
  377. #endif
  378. return stream;
  379. }
  380. /* }}} */
  381. static const php_stream_wrapper_ops php_stdio_wops = {
  382. php_stream_url_wrap_php,
  383. NULL, /* close */
  384. NULL, /* fstat */
  385. NULL, /* stat */
  386. NULL, /* opendir */
  387. "PHP",
  388. NULL, /* unlink */
  389. NULL, /* rename */
  390. NULL, /* mkdir */
  391. NULL, /* rmdir */
  392. NULL
  393. };
  394. PHPAPI const php_stream_wrapper php_stream_php_wrapper = {
  395. &php_stdio_wops,
  396. NULL,
  397. 0, /* is_url */
  398. };
  399. /*
  400. * Local variables:
  401. * tab-width: 4
  402. * c-basic-offset: 4
  403. * End:
  404. * vim600: sw=4 ts=4 fdm=marker
  405. * vim<600: sw=4 ts=4
  406. */