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