PageRenderTime 73ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/main/rfc1867.c

https://github.com/php/php-src
C | 1293 lines | 962 code | 187 blank | 144 comment | 276 complexity | e3731818ee7bb30a8ec67a2212db70bd MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Rasmus Lerdorf <rasmus@php.net> |
  14. | Jani Taskinen <jani@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. /*
  18. * This product includes software developed by the Apache Group
  19. * for use in the Apache HTTP server project (http://www.apache.org/).
  20. *
  21. */
  22. #include <stdio.h>
  23. #include "php.h"
  24. #include "php_open_temporary_file.h"
  25. #include "zend_globals.h"
  26. #include "php_globals.h"
  27. #include "php_variables.h"
  28. #include "rfc1867.h"
  29. #include "zend_smart_string.h"
  30. #ifndef DEBUG_FILE_UPLOAD
  31. # define DEBUG_FILE_UPLOAD 0
  32. #endif
  33. static int dummy_encoding_translation(void)
  34. {
  35. return 0;
  36. }
  37. static char *php_ap_getword(const zend_encoding *encoding, char **line, char stop);
  38. static char *php_ap_getword_conf(const zend_encoding *encoding, char *str);
  39. static php_rfc1867_encoding_translation_t php_rfc1867_encoding_translation = dummy_encoding_translation;
  40. static php_rfc1867_get_detect_order_t php_rfc1867_get_detect_order = NULL;
  41. static php_rfc1867_set_input_encoding_t php_rfc1867_set_input_encoding = NULL;
  42. static php_rfc1867_getword_t php_rfc1867_getword = php_ap_getword;
  43. static php_rfc1867_getword_conf_t php_rfc1867_getword_conf = php_ap_getword_conf;
  44. static php_rfc1867_basename_t php_rfc1867_basename = NULL;
  45. PHPAPI int (*php_rfc1867_callback)(unsigned int event, void *event_data, void **extra) = NULL;
  46. static void safe_php_register_variable(char *var, char *strval, size_t val_len, zval *track_vars_array, bool override_protection);
  47. /* The longest property name we use in an uploaded file array */
  48. #define MAX_SIZE_OF_INDEX sizeof("[full_path]")
  49. /* The longest anonymous name */
  50. #define MAX_SIZE_ANONNAME 33
  51. /* Errors */
  52. #define UPLOAD_ERROR_OK 0 /* File upload successful */
  53. #define UPLOAD_ERROR_A 1 /* Uploaded file exceeded upload_max_filesize */
  54. #define UPLOAD_ERROR_B 2 /* Uploaded file exceeded MAX_FILE_SIZE */
  55. #define UPLOAD_ERROR_C 3 /* Partially uploaded */
  56. #define UPLOAD_ERROR_D 4 /* No file uploaded */
  57. #define UPLOAD_ERROR_E 6 /* Missing /tmp or similar directory */
  58. #define UPLOAD_ERROR_F 7 /* Failed to write file to disk */
  59. #define UPLOAD_ERROR_X 8 /* File upload stopped by extension */
  60. void php_rfc1867_register_constants(void) /* {{{ */
  61. {
  62. REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_OK", UPLOAD_ERROR_OK, CONST_CS | CONST_PERSISTENT);
  63. REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_INI_SIZE", UPLOAD_ERROR_A, CONST_CS | CONST_PERSISTENT);
  64. REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_FORM_SIZE", UPLOAD_ERROR_B, CONST_CS | CONST_PERSISTENT);
  65. REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_PARTIAL", UPLOAD_ERROR_C, CONST_CS | CONST_PERSISTENT);
  66. REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_NO_FILE", UPLOAD_ERROR_D, CONST_CS | CONST_PERSISTENT);
  67. REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_NO_TMP_DIR", UPLOAD_ERROR_E, CONST_CS | CONST_PERSISTENT);
  68. REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_CANT_WRITE", UPLOAD_ERROR_F, CONST_CS | CONST_PERSISTENT);
  69. REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_EXTENSION", UPLOAD_ERROR_X, CONST_CS | CONST_PERSISTENT);
  70. }
  71. /* }}} */
  72. static void normalize_protected_variable(char *varname) /* {{{ */
  73. {
  74. char *s = varname, *index = NULL, *indexend = NULL, *p;
  75. /* skip leading space */
  76. while (*s == ' ') {
  77. s++;
  78. }
  79. /* and remove it */
  80. if (s != varname) {
  81. memmove(varname, s, strlen(s)+1);
  82. }
  83. for (p = varname; *p && *p != '['; p++) {
  84. switch(*p) {
  85. case ' ':
  86. case '.':
  87. *p = '_';
  88. break;
  89. }
  90. }
  91. /* find index */
  92. index = strchr(varname, '[');
  93. if (index) {
  94. index++;
  95. s = index;
  96. } else {
  97. return;
  98. }
  99. /* done? */
  100. while (index) {
  101. while (*index == ' ' || *index == '\r' || *index == '\n' || *index=='\t') {
  102. index++;
  103. }
  104. indexend = strchr(index, ']');
  105. indexend = indexend ? indexend + 1 : index + strlen(index);
  106. if (s != index) {
  107. memmove(s, index, strlen(index)+1);
  108. s += indexend-index;
  109. } else {
  110. s = indexend;
  111. }
  112. if (*s == '[') {
  113. s++;
  114. index = s;
  115. } else {
  116. index = NULL;
  117. }
  118. }
  119. *s = '\0';
  120. }
  121. /* }}} */
  122. static void add_protected_variable(char *varname) /* {{{ */
  123. {
  124. normalize_protected_variable(varname);
  125. zend_hash_str_add_empty_element(&PG(rfc1867_protected_variables), varname, strlen(varname));
  126. }
  127. /* }}} */
  128. static bool is_protected_variable(char *varname) /* {{{ */
  129. {
  130. normalize_protected_variable(varname);
  131. return zend_hash_str_exists(&PG(rfc1867_protected_variables), varname, strlen(varname));
  132. }
  133. /* }}} */
  134. static void safe_php_register_variable(char *var, char *strval, size_t val_len, zval *track_vars_array, bool override_protection) /* {{{ */
  135. {
  136. if (override_protection || !is_protected_variable(var)) {
  137. php_register_variable_safe(var, strval, val_len, track_vars_array);
  138. }
  139. }
  140. /* }}} */
  141. static void safe_php_register_variable_ex(char *var, zval *val, zval *track_vars_array, bool override_protection) /* {{{ */
  142. {
  143. if (override_protection || !is_protected_variable(var)) {
  144. php_register_variable_ex(var, val, track_vars_array);
  145. }
  146. }
  147. /* }}} */
  148. static void register_http_post_files_variable(char *strvar, char *val, zval *http_post_files, bool override_protection) /* {{{ */
  149. {
  150. safe_php_register_variable(strvar, val, strlen(val), http_post_files, override_protection);
  151. }
  152. /* }}} */
  153. static void register_http_post_files_variable_ex(char *var, zval *val, zval *http_post_files, bool override_protection) /* {{{ */
  154. {
  155. safe_php_register_variable_ex(var, val, http_post_files, override_protection);
  156. }
  157. /* }}} */
  158. static void free_filename(zval *el) {
  159. zend_string *filename = Z_STR_P(el);
  160. zend_string_release_ex(filename, 0);
  161. }
  162. PHPAPI void destroy_uploaded_files_hash(void) /* {{{ */
  163. {
  164. zval *el;
  165. ZEND_HASH_MAP_FOREACH_VAL(SG(rfc1867_uploaded_files), el) {
  166. zend_string *filename = Z_STR_P(el);
  167. VCWD_UNLINK(ZSTR_VAL(filename));
  168. } ZEND_HASH_FOREACH_END();
  169. zend_hash_destroy(SG(rfc1867_uploaded_files));
  170. FREE_HASHTABLE(SG(rfc1867_uploaded_files));
  171. }
  172. /* }}} */
  173. /* {{{ Following code is based on apache_multipart_buffer.c from libapreq-0.33 package. */
  174. #define FILLUNIT (1024 * 5)
  175. typedef struct {
  176. /* read buffer */
  177. char *buffer;
  178. char *buf_begin;
  179. int bufsize;
  180. int bytes_in_buffer;
  181. /* boundary info */
  182. char *boundary;
  183. char *boundary_next;
  184. int boundary_next_len;
  185. const zend_encoding *input_encoding;
  186. const zend_encoding **detect_order;
  187. size_t detect_order_size;
  188. } multipart_buffer;
  189. typedef struct {
  190. char *key;
  191. char *value;
  192. } mime_header_entry;
  193. /*
  194. * Fill up the buffer with client data.
  195. * Returns number of bytes added to buffer.
  196. */
  197. static int fill_buffer(multipart_buffer *self)
  198. {
  199. int bytes_to_read, total_read = 0, actual_read = 0;
  200. /* shift the existing data if necessary */
  201. if (self->bytes_in_buffer > 0 && self->buf_begin != self->buffer) {
  202. memmove(self->buffer, self->buf_begin, self->bytes_in_buffer);
  203. }
  204. self->buf_begin = self->buffer;
  205. /* calculate the free space in the buffer */
  206. bytes_to_read = self->bufsize - self->bytes_in_buffer;
  207. /* read the required number of bytes */
  208. while (bytes_to_read > 0) {
  209. char *buf = self->buffer + self->bytes_in_buffer;
  210. actual_read = (int)sapi_module.read_post(buf, bytes_to_read);
  211. /* update the buffer length */
  212. if (actual_read > 0) {
  213. self->bytes_in_buffer += actual_read;
  214. SG(read_post_bytes) += actual_read;
  215. total_read += actual_read;
  216. bytes_to_read -= actual_read;
  217. } else {
  218. break;
  219. }
  220. }
  221. return total_read;
  222. }
  223. /* eof if we are out of bytes, or if we hit the final boundary */
  224. static int multipart_buffer_eof(multipart_buffer *self)
  225. {
  226. return self->bytes_in_buffer == 0 && fill_buffer(self) < 1;
  227. }
  228. /* create new multipart_buffer structure */
  229. static multipart_buffer *multipart_buffer_new(char *boundary, int boundary_len)
  230. {
  231. multipart_buffer *self = (multipart_buffer *) ecalloc(1, sizeof(multipart_buffer));
  232. int minsize = boundary_len + 6;
  233. if (minsize < FILLUNIT) minsize = FILLUNIT;
  234. self->buffer = (char *) ecalloc(1, minsize + 1);
  235. self->bufsize = minsize;
  236. spprintf(&self->boundary, 0, "--%s", boundary);
  237. self->boundary_next_len = (int)spprintf(&self->boundary_next, 0, "\n--%s", boundary);
  238. self->buf_begin = self->buffer;
  239. self->bytes_in_buffer = 0;
  240. if (php_rfc1867_encoding_translation()) {
  241. php_rfc1867_get_detect_order(&self->detect_order, &self->detect_order_size);
  242. } else {
  243. self->detect_order = NULL;
  244. self->detect_order_size = 0;
  245. }
  246. self->input_encoding = NULL;
  247. return self;
  248. }
  249. /*
  250. * Gets the next CRLF terminated line from the input buffer.
  251. * If it doesn't find a CRLF, and the buffer isn't completely full, returns
  252. * NULL; otherwise, returns the beginning of the null-terminated line,
  253. * minus the CRLF.
  254. *
  255. * Note that we really just look for LF terminated lines. This works
  256. * around a bug in internet explorer for the macintosh which sends mime
  257. * boundaries that are only LF terminated when you use an image submit
  258. * button in a multipart/form-data form.
  259. */
  260. static char *next_line(multipart_buffer *self)
  261. {
  262. /* look for LF in the data */
  263. char* line = self->buf_begin;
  264. char* ptr = memchr(self->buf_begin, '\n', self->bytes_in_buffer);
  265. if (ptr) { /* LF found */
  266. /* terminate the string, remove CRLF */
  267. if ((ptr - line) > 0 && *(ptr-1) == '\r') {
  268. *(ptr-1) = 0;
  269. } else {
  270. *ptr = 0;
  271. }
  272. /* bump the pointer */
  273. self->buf_begin = ptr + 1;
  274. self->bytes_in_buffer -= (self->buf_begin - line);
  275. } else { /* no LF found */
  276. /* buffer isn't completely full, fail */
  277. if (self->bytes_in_buffer < self->bufsize) {
  278. return NULL;
  279. }
  280. /* return entire buffer as a partial line */
  281. line[self->bufsize] = 0;
  282. self->buf_begin = ptr;
  283. self->bytes_in_buffer = 0;
  284. }
  285. return line;
  286. }
  287. /* Returns the next CRLF terminated line from the client */
  288. static char *get_line(multipart_buffer *self)
  289. {
  290. char* ptr = next_line(self);
  291. if (!ptr) {
  292. fill_buffer(self);
  293. ptr = next_line(self);
  294. }
  295. return ptr;
  296. }
  297. /* Free header entry */
  298. static void php_free_hdr_entry(mime_header_entry *h)
  299. {
  300. if (h->key) {
  301. efree(h->key);
  302. }
  303. if (h->value) {
  304. efree(h->value);
  305. }
  306. }
  307. /* finds a boundary */
  308. static int find_boundary(multipart_buffer *self, char *boundary)
  309. {
  310. char *line;
  311. /* loop through lines */
  312. while( (line = get_line(self)) )
  313. {
  314. /* finished if we found the boundary */
  315. if (!strcmp(line, boundary)) {
  316. return 1;
  317. }
  318. }
  319. /* didn't find the boundary */
  320. return 0;
  321. }
  322. /* parse headers */
  323. static int multipart_buffer_headers(multipart_buffer *self, zend_llist *header)
  324. {
  325. char *line;
  326. mime_header_entry entry = {0};
  327. smart_string buf_value = {0};
  328. char *key = NULL;
  329. /* didn't find boundary, abort */
  330. if (!find_boundary(self, self->boundary)) {
  331. return 0;
  332. }
  333. /* get lines of text, or CRLF_CRLF */
  334. while ((line = get_line(self)) && line[0] != '\0') {
  335. /* add header to table */
  336. char *value = NULL;
  337. if (php_rfc1867_encoding_translation()) {
  338. self->input_encoding = zend_multibyte_encoding_detector((const unsigned char *) line, strlen(line), self->detect_order, self->detect_order_size);
  339. }
  340. /* space in the beginning means same header */
  341. if (!isspace(line[0])) {
  342. value = strchr(line, ':');
  343. }
  344. if (value) {
  345. if (buf_value.c && key) {
  346. /* new entry, add the old one to the list */
  347. smart_string_0(&buf_value);
  348. entry.key = key;
  349. entry.value = buf_value.c;
  350. zend_llist_add_element(header, &entry);
  351. buf_value.c = NULL;
  352. key = NULL;
  353. }
  354. *value = '\0';
  355. do { value++; } while (isspace(*value));
  356. key = estrdup(line);
  357. smart_string_appends(&buf_value, value);
  358. } else if (buf_value.c) { /* If no ':' on the line, add to previous line */
  359. smart_string_appends(&buf_value, line);
  360. } else {
  361. continue;
  362. }
  363. }
  364. if (buf_value.c && key) {
  365. /* add the last one to the list */
  366. smart_string_0(&buf_value);
  367. entry.key = key;
  368. entry.value = buf_value.c;
  369. zend_llist_add_element(header, &entry);
  370. }
  371. return 1;
  372. }
  373. static char *php_mime_get_hdr_value(zend_llist header, char *key)
  374. {
  375. mime_header_entry *entry;
  376. if (key == NULL) {
  377. return NULL;
  378. }
  379. entry = zend_llist_get_first(&header);
  380. while (entry) {
  381. if (!strcasecmp(entry->key, key)) {
  382. return entry->value;
  383. }
  384. entry = zend_llist_get_next(&header);
  385. }
  386. return NULL;
  387. }
  388. static char *php_ap_getword(const zend_encoding *encoding, char **line, char stop)
  389. {
  390. char *pos = *line, quote;
  391. char *res;
  392. while (*pos && *pos != stop) {
  393. if ((quote = *pos) == '"' || quote == '\'') {
  394. ++pos;
  395. while (*pos && *pos != quote) {
  396. if (*pos == '\\' && pos[1] && pos[1] == quote) {
  397. pos += 2;
  398. } else {
  399. ++pos;
  400. }
  401. }
  402. if (*pos) {
  403. ++pos;
  404. }
  405. } else ++pos;
  406. }
  407. if (*pos == '\0') {
  408. res = estrdup(*line);
  409. *line += strlen(*line);
  410. return res;
  411. }
  412. res = estrndup(*line, pos - *line);
  413. while (*pos == stop) {
  414. ++pos;
  415. }
  416. *line = pos;
  417. return res;
  418. }
  419. static char *substring_conf(char *start, int len, char quote)
  420. {
  421. char *result = emalloc(len + 1);
  422. char *resp = result;
  423. int i;
  424. for (i = 0; i < len && start[i] != quote; ++i) {
  425. if (start[i] == '\\' && (start[i + 1] == '\\' || (quote && start[i + 1] == quote))) {
  426. *resp++ = start[++i];
  427. } else {
  428. *resp++ = start[i];
  429. }
  430. }
  431. *resp = '\0';
  432. return result;
  433. }
  434. static char *php_ap_getword_conf(const zend_encoding *encoding, char *str)
  435. {
  436. while (*str && isspace(*str)) {
  437. ++str;
  438. }
  439. if (!*str) {
  440. return estrdup("");
  441. }
  442. if (*str == '"' || *str == '\'') {
  443. char quote = *str;
  444. str++;
  445. return substring_conf(str, (int)strlen(str), quote);
  446. } else {
  447. char *strend = str;
  448. while (*strend && !isspace(*strend)) {
  449. ++strend;
  450. }
  451. return substring_conf(str, strend - str, 0);
  452. }
  453. }
  454. static char *php_ap_basename(const zend_encoding *encoding, char *path)
  455. {
  456. char *s = strrchr(path, '\\');
  457. char *s2 = strrchr(path, '/');
  458. if (s && s2) {
  459. if (s > s2) {
  460. ++s;
  461. } else {
  462. s = ++s2;
  463. }
  464. return s;
  465. } else if (s) {
  466. return ++s;
  467. } else if (s2) {
  468. return ++s2;
  469. }
  470. return path;
  471. }
  472. /*
  473. * Search for a string in a fixed-length byte string.
  474. * If partial is true, partial matches are allowed at the end of the buffer.
  475. * Returns NULL if not found, or a pointer to the start of the first match.
  476. */
  477. static void *php_ap_memstr(char *haystack, int haystacklen, char *needle, int needlen, int partial)
  478. {
  479. int len = haystacklen;
  480. char *ptr = haystack;
  481. /* iterate through first character matches */
  482. while( (ptr = memchr(ptr, needle[0], len)) ) {
  483. /* calculate length after match */
  484. len = haystacklen - (ptr - (char *)haystack);
  485. /* done if matches up to capacity of buffer */
  486. if (memcmp(needle, ptr, needlen < len ? needlen : len) == 0 && (partial || len >= needlen)) {
  487. break;
  488. }
  489. /* next character */
  490. ptr++; len--;
  491. }
  492. return ptr;
  493. }
  494. /* read until a boundary condition */
  495. static size_t multipart_buffer_read(multipart_buffer *self, char *buf, size_t bytes, int *end)
  496. {
  497. size_t len, max;
  498. char *bound;
  499. /* fill buffer if needed */
  500. if (bytes > (size_t)self->bytes_in_buffer) {
  501. fill_buffer(self);
  502. }
  503. /* look for a potential boundary match, only read data up to that point */
  504. if ((bound = php_ap_memstr(self->buf_begin, self->bytes_in_buffer, self->boundary_next, self->boundary_next_len, 1))) {
  505. max = bound - self->buf_begin;
  506. if (end && php_ap_memstr(self->buf_begin, self->bytes_in_buffer, self->boundary_next, self->boundary_next_len, 0)) {
  507. *end = 1;
  508. }
  509. } else {
  510. max = self->bytes_in_buffer;
  511. }
  512. /* maximum number of bytes we are reading */
  513. len = max < bytes-1 ? max : bytes-1;
  514. /* if we read any data... */
  515. if (len > 0) {
  516. /* copy the data */
  517. memcpy(buf, self->buf_begin, len);
  518. buf[len] = 0;
  519. if (bound && len > 0 && buf[len-1] == '\r') {
  520. buf[--len] = 0;
  521. }
  522. /* update the buffer */
  523. self->bytes_in_buffer -= (int)len;
  524. self->buf_begin += len;
  525. }
  526. return len;
  527. }
  528. /*
  529. XXX: this is horrible memory-usage-wise, but we only expect
  530. to do this on small pieces of form data.
  531. */
  532. static char *multipart_buffer_read_body(multipart_buffer *self, size_t *len)
  533. {
  534. char buf[FILLUNIT], *out=NULL;
  535. size_t total_bytes=0, read_bytes=0;
  536. while((read_bytes = multipart_buffer_read(self, buf, sizeof(buf), NULL))) {
  537. out = erealloc(out, total_bytes + read_bytes + 1);
  538. memcpy(out + total_bytes, buf, read_bytes);
  539. total_bytes += read_bytes;
  540. }
  541. if (out) {
  542. out[total_bytes] = '\0';
  543. }
  544. *len = total_bytes;
  545. return out;
  546. }
  547. /* }}} */
  548. /*
  549. * The combined READER/HANDLER
  550. *
  551. */
  552. SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
  553. {
  554. char *boundary, *s = NULL, *boundary_end = NULL, *start_arr = NULL, *array_index = NULL;
  555. char *lbuf = NULL, *abuf = NULL;
  556. zend_string *temp_filename = NULL;
  557. int boundary_len = 0, cancel_upload = 0, is_arr_upload = 0;
  558. size_t array_len = 0;
  559. int64_t total_bytes = 0, max_file_size = 0;
  560. int skip_upload = 0, anonymous_index = 0;
  561. HashTable *uploaded_files = NULL;
  562. multipart_buffer *mbuff;
  563. zval *array_ptr = (zval *) arg;
  564. int fd = -1;
  565. zend_llist header;
  566. void *event_extra_data = NULL;
  567. unsigned int llen = 0;
  568. int upload_cnt = INI_INT("max_file_uploads");
  569. const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding();
  570. php_rfc1867_getword_t getword;
  571. php_rfc1867_getword_conf_t getword_conf;
  572. php_rfc1867_basename_t _basename;
  573. zend_long count = 0;
  574. if (php_rfc1867_encoding_translation() && internal_encoding) {
  575. getword = php_rfc1867_getword;
  576. getword_conf = php_rfc1867_getword_conf;
  577. _basename = php_rfc1867_basename;
  578. } else {
  579. getword = php_ap_getword;
  580. getword_conf = php_ap_getword_conf;
  581. _basename = php_ap_basename;
  582. }
  583. if (SG(post_max_size) > 0 && SG(request_info).content_length > SG(post_max_size)) {
  584. sapi_module.sapi_error(E_WARNING, "POST Content-Length of " ZEND_LONG_FMT " bytes exceeds the limit of " ZEND_LONG_FMT " bytes", SG(request_info).content_length, SG(post_max_size));
  585. return;
  586. }
  587. /* Get the boundary */
  588. boundary = strstr(content_type_dup, "boundary");
  589. if (!boundary) {
  590. int content_type_len = (int)strlen(content_type_dup);
  591. char *content_type_lcase = estrndup(content_type_dup, content_type_len);
  592. zend_str_tolower(content_type_lcase, content_type_len);
  593. boundary = strstr(content_type_lcase, "boundary");
  594. if (boundary) {
  595. boundary = content_type_dup + (boundary - content_type_lcase);
  596. }
  597. efree(content_type_lcase);
  598. }
  599. if (!boundary || !(boundary = strchr(boundary, '='))) {
  600. sapi_module.sapi_error(E_WARNING, "Missing boundary in multipart/form-data POST data");
  601. return;
  602. }
  603. boundary++;
  604. boundary_len = (int)strlen(boundary);
  605. if (boundary[0] == '"') {
  606. boundary++;
  607. boundary_end = strchr(boundary, '"');
  608. if (!boundary_end) {
  609. sapi_module.sapi_error(E_WARNING, "Invalid boundary in multipart/form-data POST data");
  610. return;
  611. }
  612. } else {
  613. /* search for the end of the boundary */
  614. boundary_end = strpbrk(boundary, ",;");
  615. }
  616. if (boundary_end) {
  617. boundary_end[0] = '\0';
  618. boundary_len = boundary_end-boundary;
  619. }
  620. /* Initialize the buffer */
  621. if (!(mbuff = multipart_buffer_new(boundary, boundary_len))) {
  622. sapi_module.sapi_error(E_WARNING, "Unable to initialize the input buffer");
  623. return;
  624. }
  625. /* Initialize $_FILES[] */
  626. zend_hash_init(&PG(rfc1867_protected_variables), 8, NULL, NULL, 0);
  627. ALLOC_HASHTABLE(uploaded_files);
  628. zend_hash_init(uploaded_files, 8, NULL, free_filename, 0);
  629. SG(rfc1867_uploaded_files) = uploaded_files;
  630. if (Z_TYPE(PG(http_globals)[TRACK_VARS_FILES]) != IS_ARRAY) {
  631. /* php_auto_globals_create_files() might have already done that */
  632. array_init(&PG(http_globals)[TRACK_VARS_FILES]);
  633. }
  634. zend_llist_init(&header, sizeof(mime_header_entry), (llist_dtor_func_t) php_free_hdr_entry, 0);
  635. if (php_rfc1867_callback != NULL) {
  636. multipart_event_start event_start;
  637. event_start.content_length = SG(request_info).content_length;
  638. if (php_rfc1867_callback(MULTIPART_EVENT_START, &event_start, &event_extra_data) == FAILURE) {
  639. goto fileupload_done;
  640. }
  641. }
  642. while (!multipart_buffer_eof(mbuff))
  643. {
  644. char buff[FILLUNIT];
  645. char *cd = NULL, *param = NULL, *filename = NULL, *tmp = NULL;
  646. size_t blen = 0, wlen = 0;
  647. zend_off_t offset;
  648. zend_llist_clean(&header);
  649. if (!multipart_buffer_headers(mbuff, &header)) {
  650. goto fileupload_done;
  651. }
  652. if ((cd = php_mime_get_hdr_value(header, "Content-Disposition"))) {
  653. char *pair = NULL;
  654. int end = 0;
  655. while (isspace(*cd)) {
  656. ++cd;
  657. }
  658. while (*cd && (pair = getword(mbuff->input_encoding, &cd, ';')))
  659. {
  660. char *key = NULL, *word = pair;
  661. while (isspace(*cd)) {
  662. ++cd;
  663. }
  664. if (strchr(pair, '=')) {
  665. key = getword(mbuff->input_encoding, &pair, '=');
  666. if (!strcasecmp(key, "name")) {
  667. if (param) {
  668. efree(param);
  669. }
  670. param = getword_conf(mbuff->input_encoding, pair);
  671. if (mbuff->input_encoding && internal_encoding) {
  672. unsigned char *new_param;
  673. size_t new_param_len;
  674. if ((size_t)-1 != zend_multibyte_encoding_converter(&new_param, &new_param_len, (unsigned char *)param, strlen(param), internal_encoding, mbuff->input_encoding)) {
  675. efree(param);
  676. param = (char *)new_param;
  677. }
  678. }
  679. } else if (!strcasecmp(key, "filename")) {
  680. if (filename) {
  681. efree(filename);
  682. }
  683. filename = getword_conf(mbuff->input_encoding, pair);
  684. if (mbuff->input_encoding && internal_encoding) {
  685. unsigned char *new_filename;
  686. size_t new_filename_len;
  687. if ((size_t)-1 != zend_multibyte_encoding_converter(&new_filename, &new_filename_len, (unsigned char *)filename, strlen(filename), internal_encoding, mbuff->input_encoding)) {
  688. efree(filename);
  689. filename = (char *)new_filename;
  690. }
  691. }
  692. }
  693. }
  694. if (key) {
  695. efree(key);
  696. }
  697. efree(word);
  698. }
  699. /* Normal form variable, safe to read all data into memory */
  700. if (!filename && param) {
  701. size_t value_len;
  702. char *value = multipart_buffer_read_body(mbuff, &value_len);
  703. size_t new_val_len; /* Dummy variable */
  704. if (!value) {
  705. value = estrdup("");
  706. value_len = 0;
  707. }
  708. if (mbuff->input_encoding && internal_encoding) {
  709. unsigned char *new_value;
  710. size_t new_value_len;
  711. if ((size_t)-1 != zend_multibyte_encoding_converter(&new_value, &new_value_len, (unsigned char *)value, value_len, internal_encoding, mbuff->input_encoding)) {
  712. efree(value);
  713. value = (char *)new_value;
  714. value_len = new_value_len;
  715. }
  716. }
  717. if (++count <= PG(max_input_vars) && sapi_module.input_filter(PARSE_POST, param, &value, value_len, &new_val_len)) {
  718. if (php_rfc1867_callback != NULL) {
  719. multipart_event_formdata event_formdata;
  720. size_t newlength = new_val_len;
  721. event_formdata.post_bytes_processed = SG(read_post_bytes);
  722. event_formdata.name = param;
  723. event_formdata.value = &value;
  724. event_formdata.length = new_val_len;
  725. event_formdata.newlength = &newlength;
  726. if (php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data) == FAILURE) {
  727. efree(param);
  728. efree(value);
  729. continue;
  730. }
  731. new_val_len = newlength;
  732. }
  733. safe_php_register_variable(param, value, new_val_len, array_ptr, 0);
  734. } else {
  735. if (count == PG(max_input_vars) + 1) {
  736. php_error_docref(NULL, E_WARNING, "Input variables exceeded " ZEND_LONG_FMT ". To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
  737. }
  738. if (php_rfc1867_callback != NULL) {
  739. multipart_event_formdata event_formdata;
  740. event_formdata.post_bytes_processed = SG(read_post_bytes);
  741. event_formdata.name = param;
  742. event_formdata.value = &value;
  743. event_formdata.length = value_len;
  744. event_formdata.newlength = NULL;
  745. php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data);
  746. }
  747. }
  748. if (!strcasecmp(param, "MAX_FILE_SIZE")) {
  749. max_file_size = strtoll(value, NULL, 10);
  750. }
  751. efree(param);
  752. efree(value);
  753. continue;
  754. }
  755. /* If file_uploads=off, skip the file part */
  756. if (!PG(file_uploads)) {
  757. skip_upload = 1;
  758. } else if (upload_cnt <= 0) {
  759. skip_upload = 1;
  760. sapi_module.sapi_error(E_WARNING, "Maximum number of allowable file uploads has been exceeded");
  761. }
  762. /* Return with an error if the posted data is garbled */
  763. if (!param && !filename) {
  764. sapi_module.sapi_error(E_WARNING, "File Upload Mime headers garbled");
  765. goto fileupload_done;
  766. }
  767. if (!param) {
  768. param = emalloc(MAX_SIZE_ANONNAME);
  769. snprintf(param, MAX_SIZE_ANONNAME, "%u", anonymous_index++);
  770. }
  771. /* New Rule: never repair potential malicious user input */
  772. if (!skip_upload) {
  773. long c = 0;
  774. tmp = param;
  775. while (*tmp) {
  776. if (*tmp == '[') {
  777. c++;
  778. } else if (*tmp == ']') {
  779. c--;
  780. if (tmp[1] && tmp[1] != '[') {
  781. skip_upload = 1;
  782. break;
  783. }
  784. }
  785. if (c < 0) {
  786. skip_upload = 1;
  787. break;
  788. }
  789. tmp++;
  790. }
  791. /* Brackets should always be closed */
  792. if(c != 0) {
  793. skip_upload = 1;
  794. }
  795. }
  796. total_bytes = cancel_upload = 0;
  797. temp_filename = NULL;
  798. fd = -1;
  799. if (!skip_upload && php_rfc1867_callback != NULL) {
  800. multipart_event_file_start event_file_start;
  801. event_file_start.post_bytes_processed = SG(read_post_bytes);
  802. event_file_start.name = param;
  803. event_file_start.filename = &filename;
  804. if (php_rfc1867_callback(MULTIPART_EVENT_FILE_START, &event_file_start, &event_extra_data) == FAILURE) {
  805. temp_filename = NULL;
  806. efree(param);
  807. efree(filename);
  808. continue;
  809. }
  810. }
  811. if (skip_upload) {
  812. efree(param);
  813. efree(filename);
  814. continue;
  815. }
  816. if (filename[0] == '\0') {
  817. #if DEBUG_FILE_UPLOAD
  818. sapi_module.sapi_error(E_NOTICE, "No file uploaded");
  819. #endif
  820. cancel_upload = UPLOAD_ERROR_D;
  821. }
  822. offset = 0;
  823. end = 0;
  824. if (!cancel_upload) {
  825. /* only bother to open temp file if we have data */
  826. blen = multipart_buffer_read(mbuff, buff, sizeof(buff), &end);
  827. #if DEBUG_FILE_UPLOAD
  828. if (blen > 0) {
  829. #else
  830. /* in non-debug mode we have no problem with 0-length files */
  831. {
  832. #endif
  833. fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_FALLBACK);
  834. upload_cnt--;
  835. if (fd == -1) {
  836. sapi_module.sapi_error(E_WARNING, "File upload error - unable to create a temporary file");
  837. cancel_upload = UPLOAD_ERROR_E;
  838. }
  839. }
  840. }
  841. while (!cancel_upload && (blen > 0))
  842. {
  843. if (php_rfc1867_callback != NULL) {
  844. multipart_event_file_data event_file_data;
  845. event_file_data.post_bytes_processed = SG(read_post_bytes);
  846. event_file_data.offset = offset;
  847. event_file_data.data = buff;
  848. event_file_data.length = blen;
  849. event_file_data.newlength = &blen;
  850. if (php_rfc1867_callback(MULTIPART_EVENT_FILE_DATA, &event_file_data, &event_extra_data) == FAILURE) {
  851. cancel_upload = UPLOAD_ERROR_X;
  852. continue;
  853. }
  854. }
  855. if (PG(upload_max_filesize) > 0 && (zend_long)(total_bytes+blen) > PG(upload_max_filesize)) {
  856. #if DEBUG_FILE_UPLOAD
  857. sapi_module.sapi_error(E_NOTICE, "upload_max_filesize of " ZEND_LONG_FMT " bytes exceeded - file [%s=%s] not saved", PG(upload_max_filesize), param, filename);
  858. #endif
  859. cancel_upload = UPLOAD_ERROR_A;
  860. } else if (max_file_size && ((zend_long)(total_bytes+blen) > max_file_size)) {
  861. #if DEBUG_FILE_UPLOAD
  862. sapi_module.sapi_error(E_NOTICE, "MAX_FILE_SIZE of %" PRId64 " bytes exceeded - file [%s=%s] not saved", max_file_size, param, filename);
  863. #endif
  864. cancel_upload = UPLOAD_ERROR_B;
  865. } else if (blen > 0) {
  866. #ifdef PHP_WIN32
  867. wlen = write(fd, buff, (unsigned int)blen);
  868. #else
  869. wlen = write(fd, buff, blen);
  870. #endif
  871. if (wlen == (size_t)-1) {
  872. /* write failed */
  873. #if DEBUG_FILE_UPLOAD
  874. sapi_module.sapi_error(E_NOTICE, "write() failed - %s", strerror(errno));
  875. #endif
  876. cancel_upload = UPLOAD_ERROR_F;
  877. } else if (wlen < blen) {
  878. #if DEBUG_FILE_UPLOAD
  879. sapi_module.sapi_error(E_NOTICE, "Only %zd bytes were written, expected to write %zd", wlen, blen);
  880. #endif
  881. cancel_upload = UPLOAD_ERROR_F;
  882. } else {
  883. total_bytes += wlen;
  884. }
  885. offset += wlen;
  886. }
  887. /* read data for next iteration */
  888. blen = multipart_buffer_read(mbuff, buff, sizeof(buff), &end);
  889. }
  890. if (fd != -1) { /* may not be initialized if file could not be created */
  891. close(fd);
  892. }
  893. if (!cancel_upload && !end) {
  894. #if DEBUG_FILE_UPLOAD
  895. sapi_module.sapi_error(E_NOTICE, "Missing mime boundary at the end of the data for file %s", filename[0] != '\0' ? filename : "");
  896. #endif
  897. cancel_upload = UPLOAD_ERROR_C;
  898. }
  899. #if DEBUG_FILE_UPLOAD
  900. if (filename[0] != '\0' && total_bytes == 0 && !cancel_upload) {
  901. sapi_module.sapi_error(E_WARNING, "Uploaded file size 0 - file [%s=%s] not saved", param, filename);
  902. cancel_upload = 5;
  903. }
  904. #endif
  905. if (php_rfc1867_callback != NULL) {
  906. multipart_event_file_end event_file_end;
  907. event_file_end.post_bytes_processed = SG(read_post_bytes);
  908. event_file_end.temp_filename = temp_filename ? ZSTR_VAL(temp_filename) : NULL;
  909. event_file_end.cancel_upload = cancel_upload;
  910. if (php_rfc1867_callback(MULTIPART_EVENT_FILE_END, &event_file_end, &event_extra_data) == FAILURE) {
  911. cancel_upload = UPLOAD_ERROR_X;
  912. }
  913. }
  914. if (cancel_upload) {
  915. if (temp_filename) {
  916. if (cancel_upload != UPLOAD_ERROR_E) { /* file creation failed */
  917. unlink(ZSTR_VAL(temp_filename));
  918. }
  919. zend_string_release_ex(temp_filename, 0);
  920. }
  921. temp_filename = NULL;
  922. } else {
  923. zend_hash_add_ptr(SG(rfc1867_uploaded_files), temp_filename, temp_filename);
  924. }
  925. /* is_arr_upload is true when name of file upload field
  926. * ends in [.*]
  927. * start_arr is set to point to 1st [ */
  928. is_arr_upload = (start_arr = strchr(param,'[')) && (param[strlen(param)-1] == ']');
  929. if (is_arr_upload) {
  930. array_len = strlen(start_arr);
  931. if (array_index) {
  932. efree(array_index);
  933. }
  934. array_index = estrndup(start_arr + 1, array_len - 2);
  935. }
  936. /* Add $foo_name */
  937. if (llen < strlen(param) + MAX_SIZE_OF_INDEX + 1) {
  938. llen = (int)strlen(param);
  939. lbuf = (char *) safe_erealloc(lbuf, llen, 1, MAX_SIZE_OF_INDEX + 1);
  940. llen += MAX_SIZE_OF_INDEX + 1;
  941. }
  942. if (is_arr_upload) {
  943. if (abuf) efree(abuf);
  944. abuf = estrndup(param, strlen(param)-array_len);
  945. snprintf(lbuf, llen, "%s_name[%s]", abuf, array_index);
  946. } else {
  947. snprintf(lbuf, llen, "%s_name", param);
  948. }
  949. /* Pursuant to RFC 7578, strip any path components in the
  950. * user-supplied file name:
  951. * > If a "filename" parameter is supplied ... do not use
  952. * > directory path information that may be present."
  953. */
  954. s = _basename(internal_encoding, filename);
  955. if (!s) {
  956. s = filename;
  957. }
  958. /* Add $foo[name] */
  959. if (is_arr_upload) {
  960. snprintf(lbuf, llen, "%s[name][%s]", abuf, array_index);
  961. } else {
  962. snprintf(lbuf, llen, "%s[name]", param);
  963. }
  964. register_http_post_files_variable(lbuf, s, &PG(http_globals)[TRACK_VARS_FILES], 0);
  965. s = NULL;
  966. /* Add full path of supplied file for folder uploads via
  967. * <input type="file" name="files" multiple webkitdirectory>
  968. */
  969. /* Add $foo[full_path] */
  970. if (is_arr_upload) {
  971. snprintf(lbuf, llen, "%s[full_path][%s]", abuf, array_index);
  972. } else {
  973. snprintf(lbuf, llen, "%s[full_path]", param);
  974. }
  975. register_http_post_files_variable(lbuf, filename, &PG(http_globals)[TRACK_VARS_FILES], 0);
  976. efree(filename);
  977. /* Possible Content-Type: */
  978. if (cancel_upload || !(cd = php_mime_get_hdr_value(header, "Content-Type"))) {
  979. cd = "";
  980. } else {
  981. /* fix for Opera 6.01 */
  982. s = strchr(cd, ';');
  983. if (s != NULL) {
  984. *s = '\0';
  985. }
  986. }
  987. /* Add $foo[type] */
  988. if (is_arr_upload) {
  989. snprintf(lbuf, llen, "%s[type][%s]", abuf, array_index);
  990. } else {
  991. snprintf(lbuf, llen, "%s[type]", param);
  992. }
  993. register_http_post_files_variable(lbuf, cd, &PG(http_globals)[TRACK_VARS_FILES], 0);
  994. /* Restore Content-Type Header */
  995. if (s != NULL) {
  996. *s = ';';
  997. }
  998. s = "";
  999. {
  1000. /* store temp_filename as-is (in case upload_tmp_dir
  1001. * contains escapable characters. escape only the variable name.) */
  1002. zval zfilename;
  1003. /* Initialize variables */
  1004. add_protected_variable(param);
  1005. /* Add $foo[tmp_name] */
  1006. if (is_arr_upload) {
  1007. snprintf(lbuf, llen, "%s[tmp_name][%s]", abuf, array_index);
  1008. } else {
  1009. snprintf(lbuf, llen, "%s[tmp_name]", param);
  1010. }
  1011. add_protected_variable(lbuf);
  1012. if (temp_filename) {
  1013. ZVAL_STR_COPY(&zfilename, temp_filename);
  1014. } else {
  1015. ZVAL_EMPTY_STRING(&zfilename);
  1016. }
  1017. register_http_post_files_variable_ex(lbuf, &zfilename, &PG(http_globals)[TRACK_VARS_FILES], 1);
  1018. }
  1019. {
  1020. zval file_size, error_type;
  1021. int size_overflow = 0;
  1022. char file_size_buf[65];
  1023. ZVAL_LONG(&error_type, cancel_upload);
  1024. /* Add $foo[error] */
  1025. if (cancel_upload) {
  1026. ZVAL_LONG(&file_size, 0);
  1027. } else {
  1028. if (total_bytes > ZEND_LONG_MAX) {
  1029. #ifdef PHP_WIN32
  1030. if (_i64toa_s(total_bytes, file_size_buf, 65, 10)) {
  1031. file_size_buf[0] = '0';
  1032. file_size_buf[1] = '\0';
  1033. }
  1034. #else
  1035. {
  1036. int __len = snprintf(file_size_buf, 65, "%" PRId64, total_bytes);
  1037. file_size_buf[__len] = '\0';
  1038. }
  1039. #endif
  1040. size_overflow = 1;
  1041. } else {
  1042. ZVAL_LONG(&file_size, total_bytes);
  1043. }
  1044. }
  1045. if (is_arr_upload) {
  1046. snprintf(lbuf, llen, "%s[error][%s]", abuf, array_index);
  1047. } else {
  1048. snprintf(lbuf, llen, "%s[error]", param);
  1049. }
  1050. register_http_post_files_variable_ex(lbuf, &error_type, &PG(http_globals)[TRACK_VARS_FILES], 0);
  1051. /* Add $foo[size] */
  1052. if (is_arr_upload) {
  1053. snprintf(lbuf, llen, "%s[size][%s]", abuf, array_index);
  1054. } else {
  1055. snprintf(lbuf, llen, "%s[size]", param);
  1056. }
  1057. if (size_overflow) {
  1058. ZVAL_STRING(&file_size, file_size_buf);
  1059. }
  1060. register_http_post_files_variable_ex(lbuf, &file_size, &PG(http_globals)[TRACK_VARS_FILES], size_overflow);
  1061. }
  1062. efree(param);
  1063. }
  1064. }
  1065. fileupload_done:
  1066. if (php_rfc1867_callback != NULL) {
  1067. multipart_event_end event_end;
  1068. event_end.post_bytes_processed = SG(read_post_bytes);
  1069. php_rfc1867_callback(MULTIPART_EVENT_END, &event_end, &event_extra_data);
  1070. }
  1071. if (lbuf) efree(lbuf);
  1072. if (abuf) efree(abuf);
  1073. if (array_index) efree(array_index);
  1074. zend_hash_destroy(&PG(rfc1867_protected_variables));
  1075. zend_llist_destroy(&header);
  1076. if (mbuff->boundary_next) efree(mbuff->boundary_next);
  1077. if (mbuff->boundary) efree(mbuff->boundary);
  1078. if (mbuff->buffer) efree(mbuff->buffer);
  1079. if (mbuff) efree(mbuff);
  1080. }
  1081. /* }}} */
  1082. SAPI_API void php_rfc1867_set_multibyte_callbacks(
  1083. php_rfc1867_encoding_translation_t encoding_translation,
  1084. php_rfc1867_get_detect_order_t get_detect_order,
  1085. php_rfc1867_set_input_encoding_t set_input_encoding,
  1086. php_rfc1867_getword_t getword,
  1087. php_rfc1867_getword_conf_t getword_conf,
  1088. php_rfc1867_basename_t basename) /* {{{ */
  1089. {
  1090. php_rfc1867_encoding_translation = encoding_translation;
  1091. php_rfc1867_get_detect_order = get_detect_order;
  1092. php_rfc1867_set_input_encoding = set_input_encoding;
  1093. php_rfc1867_getword = getword;
  1094. php_rfc1867_getword_conf = getword_conf;
  1095. php_rfc1867_basename = basename;
  1096. }
  1097. /* }}} */