1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2013 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: Wez Furlong <wez@thebrainroom.com> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 #include "php.h"
22 #include "php_globals.h"
23 #include "php_network.h"
24 #include "php_open_temporary_file.h"
25 #include "ext/standard/file.h"
26 #include "ext/standard/flock_compat.h"
27 #include "ext/standard/php_filestat.h"
28 #include <stddef.h>
29 #include <fcntl.h>
30 #if HAVE_SYS_WAIT_H
31 #include <sys/wait.h>
32 #endif
33 #if HAVE_SYS_FILE_H
34 #include <sys/file.h>
35 #endif
36 #ifdef HAVE_SYS_MMAN_H
37 #include <sys/mman.h>
38 #endif
39 #include "SAPI.h"
40
41 #include "php_streams_int.h"
42 #ifdef PHP_WIN32
43 # include "win32/winutil.h"
44 #endif
45
46 #define php_stream_fopen_from_fd_int(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_CC TSRMLS_CC)
47 #define php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_REL_CC TSRMLS_CC)
48 #define php_stream_fopen_from_file_int(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_CC TSRMLS_CC)
49 #define php_stream_fopen_from_file_int_rel(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_REL_CC TSRMLS_CC)
50
51 /* parse standard "fopen" modes into open() flags */
php_stream_parse_fopen_modes(const char * mode,int * open_flags)52 PHPAPI int php_stream_parse_fopen_modes(const char *mode, int *open_flags)
53 {
54 int flags;
55
56 switch (mode[0]) {
57 case 'r':
58 flags = 0;
59 break;
60 case 'w':
61 flags = O_TRUNC|O_CREAT;
62 break;
63 case 'a':
64 flags = O_CREAT|O_APPEND;
65 break;
66 case 'x':
67 flags = O_CREAT|O_EXCL;
68 break;
69 case 'c':
70 flags = O_CREAT;
71 break;
72 default:
73 /* unknown mode */
74 return FAILURE;
75 }
76 #if defined(O_NONBLOCK)
77 if (strchr(mode, 'n')) {
78 flags |= O_NONBLOCK;
79 }
80 #endif
81 if (strchr(mode, '+')) {
82 flags |= O_RDWR;
83 } else if (flags) {
84 flags |= O_WRONLY;
85 } else {
86 flags |= O_RDONLY;
87 }
88
89 #if defined(_O_TEXT) && defined(O_BINARY)
90 if (strchr(mode, 't')) {
91 flags |= _O_TEXT;
92 } else {
93 flags |= O_BINARY;
94 }
95 #endif
96
97 *open_flags = flags;
98 return SUCCESS;
99 }
100
101
102 /* {{{ ------- STDIO stream implementation -------*/
103
104 typedef struct {
105 FILE *file;
106 int fd; /* underlying file descriptor */
107 unsigned is_process_pipe:1; /* use pclose instead of fclose */
108 unsigned is_pipe:1; /* don't try and seek */
109 unsigned cached_fstat:1; /* sb is valid */
110 unsigned _reserved:29;
111
112 int lock_flag; /* stores the lock state */
113 char *temp_file_name; /* if non-null, this is the path to a temporary file that
114 * is to be deleted when the stream is closed */
115 #if HAVE_FLUSHIO
116 char last_op;
117 #endif
118
119 #if HAVE_MMAP
120 char *last_mapped_addr;
121 size_t last_mapped_len;
122 #endif
123 #ifdef PHP_WIN32
124 char *last_mapped_addr;
125 HANDLE file_mapping;
126 #endif
127
128 struct stat sb;
129 } php_stdio_stream_data;
130 #define PHP_STDIOP_GET_FD(anfd, data) anfd = (data)->file ? fileno((data)->file) : (data)->fd
131
do_fstat(php_stdio_stream_data * d,int force)132 static int do_fstat(php_stdio_stream_data *d, int force)
133 {
134 if (!d->cached_fstat || force) {
135 int fd;
136 int r;
137
138 PHP_STDIOP_GET_FD(fd, d);
139 r = fstat(fd, &d->sb);
140 d->cached_fstat = r == 0;
141
142 return r;
143 }
144 return 0;
145 }
146
_php_stream_fopen_from_fd_int(int fd,const char * mode,const char * persistent_id STREAMS_DC TSRMLS_DC)147 static php_stream *_php_stream_fopen_from_fd_int(int fd, const char *mode, const char *persistent_id STREAMS_DC TSRMLS_DC)
148 {
149 php_stdio_stream_data *self;
150
151 self = pemalloc_rel_orig(sizeof(*self), persistent_id);
152 memset(self, 0, sizeof(*self));
153 self->file = NULL;
154 self->is_pipe = 0;
155 self->lock_flag = LOCK_UN;
156 self->is_process_pipe = 0;
157 self->temp_file_name = NULL;
158 self->fd = fd;
159
160 return php_stream_alloc_rel(&php_stream_stdio_ops, self, persistent_id, mode);
161 }
162
_php_stream_fopen_from_file_int(FILE * file,const char * mode STREAMS_DC TSRMLS_DC)163 static php_stream *_php_stream_fopen_from_file_int(FILE *file, const char *mode STREAMS_DC TSRMLS_DC)
164 {
165 php_stdio_stream_data *self;
166
167 self = emalloc_rel_orig(sizeof(*self));
168 memset(self, 0, sizeof(*self));
169 self->file = file;
170 self->is_pipe = 0;
171 self->lock_flag = LOCK_UN;
172 self->is_process_pipe = 0;
173 self->temp_file_name = NULL;
174 self->fd = fileno(file);
175
176 return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
177 }
178
_php_stream_fopen_temporary_file(const char * dir,const char * pfx,char ** opened_path STREAMS_DC TSRMLS_DC)179 PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path STREAMS_DC TSRMLS_DC)
180 {
181 int fd = php_open_temporary_fd(dir, pfx, opened_path TSRMLS_CC);
182
183 if (fd != -1) {
184 php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL);
185 if (stream) {
186 return stream;
187 }
188 close(fd);
189
190 php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to allocate stream");
191
192 return NULL;
193 }
194 return NULL;
195 }
196
_php_stream_fopen_tmpfile(int dummy STREAMS_DC TSRMLS_DC)197 PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC TSRMLS_DC)
198 {
199 char *opened_path = NULL;
200 int fd = php_open_temporary_fd(NULL, "php", &opened_path TSRMLS_CC);
201
202 if (fd != -1) {
203 php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL);
204 if (stream) {
205 php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
206 stream->wrapper = &php_plain_files_wrapper;
207 stream->orig_path = estrdup(opened_path);
208
209 self->temp_file_name = opened_path;
210 self->lock_flag = LOCK_UN;
211
212 return stream;
213 }
214 close(fd);
215
216 php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to allocate stream");
217
218 return NULL;
219 }
220 return NULL;
221 }
222
_php_stream_fopen_from_fd(int fd,const char * mode,const char * persistent_id STREAMS_DC TSRMLS_DC)223 PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const char *persistent_id STREAMS_DC TSRMLS_DC)
224 {
225 php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);
226
227 if (stream) {
228 php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
229
230 #ifdef S_ISFIFO
231 /* detect if this is a pipe */
232 if (self->fd >= 0) {
233 self->is_pipe = (do_fstat(self, 0) == 0 && S_ISFIFO(self->sb.st_mode)) ? 1 : 0;
234 }
235 #elif defined(PHP_WIN32)
236 {
237 zend_uintptr_t handle = _get_osfhandle(self->fd);
238
239 if (handle != (zend_uintptr_t)INVALID_HANDLE_VALUE) {
240 self->is_pipe = GetFileType((HANDLE)handle) == FILE_TYPE_PIPE;
241 }
242 }
243 #endif
244
245 if (self->is_pipe) {
246 stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
247 } else {
248 stream->position = lseek(self->fd, 0, SEEK_CUR);
249 #ifdef ESPIPE
250 if (stream->position == (off_t)-1 && errno == ESPIPE) {
251 stream->position = 0;
252 stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
253 self->is_pipe = 1;
254 }
255 #endif
256 }
257 }
258
259 return stream;
260 }
261
_php_stream_fopen_from_file(FILE * file,const char * mode STREAMS_DC TSRMLS_DC)262 PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC TSRMLS_DC)
263 {
264 php_stream *stream = php_stream_fopen_from_file_int_rel(file, mode);
265
266 if (stream) {
267 php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
268
269 #ifdef S_ISFIFO
270 /* detect if this is a pipe */
271 if (self->fd >= 0) {
272 self->is_pipe = (do_fstat(self, 0) == 0 && S_ISFIFO(self->sb.st_mode)) ? 1 : 0;
273 }
274 #elif defined(PHP_WIN32)
275 {
276 zend_uintptr_t handle = _get_osfhandle(self->fd);
277
278 if (handle != (zend_uintptr_t)INVALID_HANDLE_VALUE) {
279 self->is_pipe = GetFileType((HANDLE)handle) == FILE_TYPE_PIPE;
280 }
281 }
282 #endif
283
284 if (self->is_pipe) {
285 stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
286 } else {
287 stream->position = ftell(file);
288 }
289 }
290
291 return stream;
292 }
293
_php_stream_fopen_from_pipe(FILE * file,const char * mode STREAMS_DC TSRMLS_DC)294 PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STREAMS_DC TSRMLS_DC)
295 {
296 php_stdio_stream_data *self;
297 php_stream *stream;
298
299 self = emalloc_rel_orig(sizeof(*self));
300 memset(self, 0, sizeof(*self));
301 self->file = file;
302 self->is_pipe = 1;
303 self->lock_flag = LOCK_UN;
304 self->is_process_pipe = 1;
305 self->fd = fileno(file);
306 self->temp_file_name = NULL;
307
308 stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
309 stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
310 return stream;
311 }
312
php_stdiop_write(php_stream * stream,const char * buf,size_t count TSRMLS_DC)313 static size_t php_stdiop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
314 {
315 php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
316
317 assert(data != NULL);
318
319 if (data->fd >= 0) {
320 int bytes_written = write(data->fd, buf, count);
321 if (bytes_written < 0) return 0;
322 return (size_t) bytes_written;
323 } else {
324
325 #if HAVE_FLUSHIO
326 if (!data->is_pipe && data->last_op == 'r') {
327 fseek(data->file, 0, SEEK_CUR);
328 }
329 data->last_op = 'w';
330 #endif
331
332 return fwrite(buf, 1, count, data->file);
333 }
334 }
335
php_stdiop_read(php_stream * stream,char * buf,size_t count TSRMLS_DC)336 static size_t php_stdiop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
337 {
338 php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
339 size_t ret;
340
341 assert(data != NULL);
342
343 if (data->fd >= 0) {
344 ret = read(data->fd, buf, count);
345
346 if (ret == (size_t)-1 && errno == EINTR) {
347 /* Read was interrupted, retry once,
348 If read still fails, giveup with feof==0
349 so script can retry if desired */
350 ret = read(data->fd, buf, count);
351 }
352
353 stream->eof = (ret == 0 || (ret == (size_t)-1 && errno != EWOULDBLOCK && errno != EINTR && errno != EBADF));
354
355 } else {
356 #if HAVE_FLUSHIO
357 if (!data->is_pipe && data->last_op == 'w')
358 fseek(data->file, 0, SEEK_CUR);
359 data->last_op = 'r';
360 #endif
361
362 ret = fread(buf, 1, count, data->file);
363
364 stream->eof = feof(data->file);
365 }
366 return ret;
367 }
368
php_stdiop_close(php_stream * stream,int close_handle TSRMLS_DC)369 static int php_stdiop_close(php_stream *stream, int close_handle TSRMLS_DC)
370 {
371 int ret;
372 php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
373
374 assert(data != NULL);
375
376 #if HAVE_MMAP
377 if (data->last_mapped_addr) {
378 munmap(data->last_mapped_addr, data->last_mapped_len);
379 data->last_mapped_addr = NULL;
380 }
381 #elif defined(PHP_WIN32)
382 if (data->last_mapped_addr) {
383 UnmapViewOfFile(data->last_mapped_addr);
384 data->last_mapped_addr = NULL;
385 }
386 if (data->file_mapping) {
387 CloseHandle(data->file_mapping);
388 data->file_mapping = NULL;
389 }
390 #endif
391
392 if (close_handle) {
393 if (data->file) {
394 if (data->is_process_pipe) {
395 errno = 0;
396 ret = pclose(data->file);
397
398 #if HAVE_SYS_WAIT_H
399 if (WIFEXITED(ret)) {
400 ret = WEXITSTATUS(ret);
401 }
402 #endif
403 } else {
404 ret = fclose(data->file);
405 data->file = NULL;
406 }
407 } else if (data->fd != -1) {
408 ret = close(data->fd);
409 data->fd = -1;
410 } else {
411 return 0; /* everything should be closed already -> success */
412 }
413 if (data->temp_file_name) {
414 unlink(data->temp_file_name);
415 /* temporary streams are never persistent */
416 efree(data->temp_file_name);
417 data->temp_file_name = NULL;
418 }
419 } else {
420 ret = 0;
421 data->file = NULL;
422 data->fd = -1;
423 }
424
425 pefree(data, stream->is_persistent);
426
427 return ret;
428 }
429
php_stdiop_flush(php_stream * stream TSRMLS_DC)430 static int php_stdiop_flush(php_stream *stream TSRMLS_DC)
431 {
432 php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
433
434 assert(data != NULL);
435
436 /*
437 * stdio buffers data in user land. By calling fflush(3), this
438 * data is send to the kernel using write(2). fsync'ing is
439 * something completely different.
440 */
441 if (data->file) {
442 return fflush(data->file);
443 }
444 return 0;
445 }
446
php_stdiop_seek(php_stream * stream,off_t offset,int whence,off_t * newoffset TSRMLS_DC)447 static int php_stdiop_seek(php_stream *stream, off_t offset, int whence, off_t *newoffset TSRMLS_DC)
448 {
449 php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
450 int ret;
451
452 assert(data != NULL);
453
454 if (data->is_pipe) {
455 php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot seek on a pipe");
456 return -1;
457 }
458
459 if (data->fd >= 0) {
460 off_t result;
461
462 result = lseek(data->fd, offset, whence);
463 if (result == (off_t)-1)
464 return -1;
465
466 *newoffset = result;
467 return 0;
468
469 } else {
470 ret = fseek(data->file, offset, whence);
471 *newoffset = ftell(data->file);
472 return ret;
473 }
474 }
475
php_stdiop_cast(php_stream * stream,int castas,void ** ret TSRMLS_DC)476 static int php_stdiop_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
477 {
478 int fd;
479 php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
480
481 assert(data != NULL);
482
483 /* as soon as someone touches the stdio layer, buffering may ensue,
484 * so we need to stop using the fd directly in that case */
485
486 switch (castas) {
487 case PHP_STREAM_AS_STDIO:
488 if (ret) {
489
490 if (data->file == NULL) {
491 /* we were opened as a plain file descriptor, so we
492 * need fdopen now */
493 char fixed_mode[5];
494 php_stream_mode_sanitize_fdopen_fopencookie(stream, fixed_mode);
495 data->file = fdopen(data->fd, fixed_mode);
496 if (data->file == NULL) {
497 return FAILURE;
498 }
499 }
500
501 *(FILE**)ret = data->file;
502 data->fd = -1;
503 }
504 return SUCCESS;
505
506 case PHP_STREAM_AS_FD_FOR_SELECT:
507 PHP_STDIOP_GET_FD(fd, data);
508 if (fd < 0) {
509 return FAILURE;
510 }
511 if (ret) {
512 *(int*)ret = fd;
513 }
514 return SUCCESS;
515
516 case PHP_STREAM_AS_FD:
517 PHP_STDIOP_GET_FD(fd, data);
518
519 if (fd < 0) {
520 return FAILURE;
521 }
522 if (data->file) {
523 fflush(data->file);
524 }
525 if (ret) {
526 *(int*)ret = fd;
527 }
528 return SUCCESS;
529 default:
530 return FAILURE;
531 }
532 }
533
php_stdiop_stat(php_stream * stream,php_stream_statbuf * ssb TSRMLS_DC)534 static int php_stdiop_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
535 {
536 int ret;
537 php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
538
539 assert(data != NULL);
540
541 ret = do_fstat(data, 1);
542 memcpy(&ssb->sb, &data->sb, sizeof(ssb->sb));
543 return ret;
544 }
545
php_stdiop_set_option(php_stream * stream,int option,int value,void * ptrparam TSRMLS_DC)546 static int php_stdiop_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC)
547 {
548 php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
549 size_t size;
550 int fd;
551 #ifdef O_NONBLOCK
552 /* FIXME: make this work for win32 */
553 int flags;
554 int oldval;
555 #endif
556
557 PHP_STDIOP_GET_FD(fd, data);
558
559 switch(option) {
560 case PHP_STREAM_OPTION_BLOCKING:
561 if (fd == -1)
562 return -1;
563 #ifdef O_NONBLOCK
564 flags = fcntl(fd, F_GETFL, 0);
565 oldval = (flags & O_NONBLOCK) ? 0 : 1;
566 if (value)
567 flags &= ~O_NONBLOCK;
568 else
569 flags |= O_NONBLOCK;
570
571 if (-1 == fcntl(fd, F_SETFL, flags))
572 return -1;
573 return oldval;
574 #else
575 return -1; /* not yet implemented */
576 #endif
577
578 case PHP_STREAM_OPTION_WRITE_BUFFER:
579
580 if (data->file == NULL) {
581 return -1;
582 }
583
584 if (ptrparam)
585 size = *(size_t *)ptrparam;
586 else
587 size = BUFSIZ;
588
589 switch(value) {
590 case PHP_STREAM_BUFFER_NONE:
591 stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
592 return setvbuf(data->file, NULL, _IONBF, 0);
593
594 case PHP_STREAM_BUFFER_LINE:
595 stream->flags ^= PHP_STREAM_FLAG_NO_BUFFER;
596 return setvbuf(data->file, NULL, _IOLBF, size);
597
598 case PHP_STREAM_BUFFER_FULL:
599 stream->flags ^= PHP_STREAM_FLAG_NO_BUFFER;
600 return setvbuf(data->file, NULL, _IOFBF, size);
601
602 default:
603 return -1;
604 }
605 break;
606
607 case PHP_STREAM_OPTION_LOCKING:
608 if (fd == -1) {
609 return -1;
610 }
611
612 if ((zend_uintptr_t) ptrparam == PHP_STREAM_LOCK_SUPPORTED) {
613 return 0;
614 }
615
616 if (!flock(fd, value)) {
617 data->lock_flag = value;
618 return 0;
619 } else {
620 return -1;
621 }
622 break;
623
624 case PHP_STREAM_OPTION_MMAP_API:
625 #if HAVE_MMAP
626 {
627 php_stream_mmap_range *range = (php_stream_mmap_range*)ptrparam;
628 int prot, flags;
629
630 switch (value) {
631 case PHP_STREAM_MMAP_SUPPORTED:
632 return fd == -1 ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
633
634 case PHP_STREAM_MMAP_MAP_RANGE:
635 do_fstat(data, 1);
636 if (range->length == 0 && range->offset > 0 && range->offset < data->sb.st_size) {
637 range->length = data->sb.st_size - range->offset;
638 }
639 if (range->length == 0 || range->length > data->sb.st_size) {
640 range->length = data->sb.st_size;
641 }
642 if (range->offset >= data->sb.st_size) {
643 range->offset = data->sb.st_size;
644 range->length = 0;
645 }
646 switch (range->mode) {
647 case PHP_STREAM_MAP_MODE_READONLY:
648 prot = PROT_READ;
649 flags = MAP_PRIVATE;
650 break;
651 case PHP_STREAM_MAP_MODE_READWRITE:
652 prot = PROT_READ | PROT_WRITE;
653 flags = MAP_PRIVATE;
654 break;
655 case PHP_STREAM_MAP_MODE_SHARED_READONLY:
656 prot = PROT_READ;
657 flags = MAP_SHARED;
658 break;
659 case PHP_STREAM_MAP_MODE_SHARED_READWRITE:
660 prot = PROT_READ | PROT_WRITE;
661 flags = MAP_SHARED;
662 break;
663 default:
664 return PHP_STREAM_OPTION_RETURN_ERR;
665 }
666 range->mapped = (char*)mmap(NULL, range->length, prot, flags, fd, range->offset);
667 if (range->mapped == (char*)MAP_FAILED) {
668 range->mapped = NULL;
669 return PHP_STREAM_OPTION_RETURN_ERR;
670 }
671 /* remember the mapping */
672 data->last_mapped_addr = range->mapped;
673 data->last_mapped_len = range->length;
674 return PHP_STREAM_OPTION_RETURN_OK;
675
676 case PHP_STREAM_MMAP_UNMAP:
677 if (data->last_mapped_addr) {
678 munmap(data->last_mapped_addr, data->last_mapped_len);
679 data->last_mapped_addr = NULL;
680
681 return PHP_STREAM_OPTION_RETURN_OK;
682 }
683 return PHP_STREAM_OPTION_RETURN_ERR;
684 }
685 }
686 #elif defined(PHP_WIN32)
687 {
688 php_stream_mmap_range *range = (php_stream_mmap_range*)ptrparam;
689 HANDLE hfile = (HANDLE)_get_osfhandle(fd);
690 DWORD prot, acc, loffs = 0, delta = 0;
691
692 switch (value) {
693 case PHP_STREAM_MMAP_SUPPORTED:
694 return hfile == INVALID_HANDLE_VALUE ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
695
696 case PHP_STREAM_MMAP_MAP_RANGE:
697 switch (range->mode) {
698 case PHP_STREAM_MAP_MODE_READONLY:
699 prot = PAGE_READONLY;
700 acc = FILE_MAP_READ;
701 break;
702 case PHP_STREAM_MAP_MODE_READWRITE:
703 prot = PAGE_READWRITE;
704 acc = FILE_MAP_READ | FILE_MAP_WRITE;
705 break;
706 case PHP_STREAM_MAP_MODE_SHARED_READONLY:
707 prot = PAGE_READONLY;
708 acc = FILE_MAP_READ;
709 /* TODO: we should assign a name for the mapping */
710 break;
711 case PHP_STREAM_MAP_MODE_SHARED_READWRITE:
712 prot = PAGE_READWRITE;
713 acc = FILE_MAP_READ | FILE_MAP_WRITE;
714 /* TODO: we should assign a name for the mapping */
715 break;
716 default:
717 return PHP_STREAM_OPTION_RETURN_ERR;
718 }
719
720 /* create a mapping capable of viewing the whole file (this costs no real resources) */
721 data->file_mapping = CreateFileMapping(hfile, NULL, prot, 0, 0, NULL);
722
723 if (data->file_mapping == NULL) {
724 return PHP_STREAM_OPTION_RETURN_ERR;
725 }
726
727 size = GetFileSize(hfile, NULL);
728 if (range->length == 0 && range->offset > 0 && range->offset < size) {
729 range->length = size - range->offset;
730 }
731 if (range->length == 0 || range->length > size) {
732 range->length = size;
733 }
734 if (range->offset >= size) {
735 range->offset = size;
736 range->length = 0;
737 }
738
739 /* figure out how big a chunk to map to be able to view the part that we need */
740 if (range->offset != 0) {
741 SYSTEM_INFO info;
742 DWORD gran;
743
744 GetSystemInfo(&info);
745 gran = info.dwAllocationGranularity;
746 loffs = (range->offset / gran) * gran;
747 delta = range->offset - loffs;
748 }
749
750 data->last_mapped_addr = MapViewOfFile(data->file_mapping, acc, 0, loffs, range->length + delta);
751
752 if (data->last_mapped_addr) {
753 /* give them back the address of the start offset they requested */
754 range->mapped = data->last_mapped_addr + delta;
755 return PHP_STREAM_OPTION_RETURN_OK;
756 }
757
758 CloseHandle(data->file_mapping);
759 data->file_mapping = NULL;
760
761 return PHP_STREAM_OPTION_RETURN_ERR;
762
763 case PHP_STREAM_MMAP_UNMAP:
764 if (data->last_mapped_addr) {
765 UnmapViewOfFile(data->last_mapped_addr);
766 data->last_mapped_addr = NULL;
767 CloseHandle(data->file_mapping);
768 data->file_mapping = NULL;
769 return PHP_STREAM_OPTION_RETURN_OK;
770 }
771 return PHP_STREAM_OPTION_RETURN_ERR;
772
773 default:
774 return PHP_STREAM_OPTION_RETURN_ERR;
775 }
776 }
777
778 #endif
779 return PHP_STREAM_OPTION_RETURN_NOTIMPL;
780
781 case PHP_STREAM_OPTION_TRUNCATE_API:
782 switch (value) {
783 case PHP_STREAM_TRUNCATE_SUPPORTED:
784 return fd == -1 ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
785
786 case PHP_STREAM_TRUNCATE_SET_SIZE: {
787 ptrdiff_t new_size = *(ptrdiff_t*)ptrparam;
788 if (new_size < 0) {
789 return PHP_STREAM_OPTION_RETURN_ERR;
790 }
791 return ftruncate(fd, new_size) == 0 ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
792 }
793 }
794
795 default:
796 return PHP_STREAM_OPTION_RETURN_NOTIMPL;
797 }
798 }
799
800 PHPAPI php_stream_ops php_stream_stdio_ops = {
801 php_stdiop_write, php_stdiop_read,
802 php_stdiop_close, php_stdiop_flush,
803 "STDIO",
804 php_stdiop_seek,
805 php_stdiop_cast,
806 php_stdiop_stat,
807 php_stdiop_set_option
808 };
809 /* }}} */
810
811 /* {{{ plain files opendir/readdir implementation */
php_plain_files_dirstream_read(php_stream * stream,char * buf,size_t count TSRMLS_DC)812 static size_t php_plain_files_dirstream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
813 {
814 DIR *dir = (DIR*)stream->abstract;
815 /* avoid libc5 readdir problems */
816 char entry[sizeof(struct dirent)+MAXPATHLEN];
817 struct dirent *result = (struct dirent *)&entry;
818 php_stream_dirent *ent = (php_stream_dirent*)buf;
819
820 /* avoid problems if someone mis-uses the stream */
821 if (count != sizeof(php_stream_dirent))
822 return 0;
823
824 if (php_readdir_r(dir, (struct dirent *)entry, &result) == 0 && result) {
825 PHP_STRLCPY(ent->d_name, result->d_name, sizeof(ent->d_name), strlen(result->d_name));
826 return sizeof(php_stream_dirent);
827 }
828 return 0;
829 }
830
php_plain_files_dirstream_close(php_stream * stream,int close_handle TSRMLS_DC)831 static int php_plain_files_dirstream_close(php_stream *stream, int close_handle TSRMLS_DC)
832 {
833 return closedir((DIR *)stream->abstract);
834 }
835
php_plain_files_dirstream_rewind(php_stream * stream,off_t offset,int whence,off_t * newoffs TSRMLS_DC)836 static int php_plain_files_dirstream_rewind(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
837 {
838 rewinddir((DIR *)stream->abstract);
839 return 0;
840 }
841
842 static php_stream_ops php_plain_files_dirstream_ops = {
843 NULL, php_plain_files_dirstream_read,
844 php_plain_files_dirstream_close, NULL,
845 "dir",
846 php_plain_files_dirstream_rewind,
847 NULL, /* cast */
848 NULL, /* stat */
849 NULL /* set_option */
850 };
851
php_plain_files_dir_opener(php_stream_wrapper * wrapper,char * path,char * mode,int options,char ** opened_path,php_stream_context * context STREAMS_DC TSRMLS_DC)852 static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, char *path, char *mode,
853 int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
854 {
855 DIR *dir = NULL;
856 php_stream *stream = NULL;
857
858 #ifdef HAVE_GLOB
859 if (options & STREAM_USE_GLOB_DIR_OPEN) {
860 return php_glob_stream_wrapper.wops->dir_opener(&php_glob_stream_wrapper, path, mode, options, opened_path, context STREAMS_REL_CC TSRMLS_CC);
861 }
862 #endif
863
864 if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path TSRMLS_CC)) {
865 return NULL;
866 }
867
868 if (PG(safe_mode) &&(!php_checkuid(path, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
869 return NULL;
870 }
871
872 dir = VCWD_OPENDIR(path);
873
874 #ifdef PHP_WIN32
875 if (!dir) {
876 php_win32_docref2_from_error(GetLastError(), path, path TSRMLS_CC);
877 }
878
879 if (dir && dir->finished) {
880 closedir(dir);
881 dir = NULL;
882 }
883 #endif
884 if (dir) {
885 stream = php_stream_alloc(&php_plain_files_dirstream_ops, dir, 0, mode);
886 if (stream == NULL)
887 closedir(dir);
888 }
889
890 return stream;
891 }
892 /* }}} */
893
894 /* {{{ php_stream_fopen */
_php_stream_fopen(const char * filename,const char * mode,char ** opened_path,int options STREAMS_DC TSRMLS_DC)895 PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, char **opened_path, int options STREAMS_DC TSRMLS_DC)
896 {
897 char *realpath = NULL;
898 int open_flags;
899 int fd;
900 php_stream *ret;
901 int persistent = options & STREAM_OPEN_PERSISTENT;
902 char *persistent_id = NULL;
903
904 if (FAILURE == php_stream_parse_fopen_modes(mode, &open_flags)) {
905 if (options & REPORT_ERRORS) {
906 php_error_docref(NULL TSRMLS_CC, E_WARNING, "`%s' is not a valid mode for fopen", mode);
907 }
908 return NULL;
909 }
910
911 if (options & STREAM_ASSUME_REALPATH) {
912 realpath = estrdup(filename);
913 } else {
914 if ((realpath = expand_filepath(filename, NULL TSRMLS_CC)) == NULL) {
915 return NULL;
916 }
917 }
918
919 if (persistent) {
920 spprintf(&persistent_id, 0, "streams_stdio_%d_%s", open_flags, realpath);
921 switch (php_stream_from_persistent_id(persistent_id, &ret TSRMLS_CC)) {
922 case PHP_STREAM_PERSISTENT_SUCCESS:
923 if (opened_path) {
924 *opened_path = realpath;
925 realpath = NULL;
926 }
927 /* fall through */
928
929 case PHP_STREAM_PERSISTENT_FAILURE:
930 if (realpath) {
931 efree(realpath);
932 }
933 efree(persistent_id);;
934 return ret;
935 }
936 }
937
938 fd = open(realpath, open_flags, 0666);
939
940 if (fd != -1) {
941
942 if (options & STREAM_OPEN_FOR_INCLUDE) {
943 ret = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);
944 } else {
945 ret = php_stream_fopen_from_fd_rel(fd, mode, persistent_id);
946 }
947
948 if (ret) {
949 if (opened_path) {
950 *opened_path = realpath;
951 realpath = NULL;
952 }
953 if (realpath) {
954 efree(realpath);
955 }
956 if (persistent_id) {
957 efree(persistent_id);
958 }
959
960 /* WIN32 always set ISREG flag */
961 #ifndef PHP_WIN32
962 /* sanity checks for include/require.
963 * We check these after opening the stream, so that we save
964 * on fstat() syscalls */
965 if (options & STREAM_OPEN_FOR_INCLUDE) {
966 php_stdio_stream_data *self = (php_stdio_stream_data*)ret->abstract;
967 int r;
968
969 r = do_fstat(self, 0);
970 if ((r == 0 && !S_ISREG(self->sb.st_mode))) {
971 if (opened_path) {
972 efree(*opened_path);
973 *opened_path = NULL;
974 }
975 php_stream_close(ret);
976 return NULL;
977 }
978 }
979 #endif
980
981 return ret;
982 }
983 close(fd);
984 }
985 efree(realpath);
986 if (persistent_id) {
987 efree(persistent_id);
988 }
989 return NULL;
990 }
991 /* }}} */
992
993
php_plain_files_stream_opener(php_stream_wrapper * wrapper,char * path,char * mode,int options,char ** opened_path,php_stream_context * context STREAMS_DC TSRMLS_DC)994 static php_stream *php_plain_files_stream_opener(php_stream_wrapper *wrapper, char *path, char *mode,
995 int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
996 {
997 if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path TSRMLS_CC)) {
998 return NULL;
999 }
1000
1001 if ((php_check_safe_mode_include_dir(path TSRMLS_CC)) == 0) {
1002 return php_stream_fopen_rel(path, mode, opened_path, options);
1003 }
1004
1005 if ((options & ENFORCE_SAFE_MODE) && PG(safe_mode) && (!php_checkuid(path, mode, CHECKUID_CHECK_MODE_PARAM)))
1006 return NULL;
1007
1008 return php_stream_fopen_rel(path, mode, opened_path, options);
1009 }
1010
php_plain_files_url_stater(php_stream_wrapper * wrapper,char * url,int flags,php_stream_statbuf * ssb,php_stream_context * context TSRMLS_DC)1011 static int php_plain_files_url_stater(php_stream_wrapper *wrapper, char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context TSRMLS_DC)
1012 {
1013 char *p;
1014
1015 if ((p = strstr(url, "://")) != NULL) {
1016 if (p < strchr(url, '/')) {
1017 url = p + 3;
1018 }
1019 }
1020
1021 if (PG(safe_mode) &&(!php_checkuid_ex(url, NULL, CHECKUID_CHECK_FILE_AND_DIR, (flags & PHP_STREAM_URL_STAT_QUIET) ? CHECKUID_NO_ERRORS : 0))) {
1022 return -1;
1023 }
1024
1025 if (php_check_open_basedir_ex(url, (flags & PHP_STREAM_URL_STAT_QUIET) ? 0 : 1 TSRMLS_CC)) {
1026 return -1;
1027 }
1028
1029 #ifdef PHP_WIN32
1030 if (EG(windows_version_info).dwMajorVersion >= 5) {
1031 if (flags & PHP_STREAM_URL_STAT_LINK) {
1032 return VCWD_LSTAT(url, &ssb->sb);
1033 }
1034 }
1035 #else
1036 # ifdef HAVE_SYMLINK
1037 if (flags & PHP_STREAM_URL_STAT_LINK) {
1038 return VCWD_LSTAT(url, &ssb->sb);
1039 } else
1040 # endif
1041 #endif
1042 return VCWD_STAT(url, &ssb->sb);
1043 }
1044
php_plain_files_unlink(php_stream_wrapper * wrapper,char * url,int options,php_stream_context * context TSRMLS_DC)1045 static int php_plain_files_unlink(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC)
1046 {
1047 char *p;
1048 int ret;
1049
1050 if ((p = strstr(url, "://")) != NULL) {
1051 if (p < strchr(url, '/')) {
1052 url = p + 3;
1053 }
1054 }
1055
1056 if (options & ENFORCE_SAFE_MODE) {
1057 if (PG(safe_mode) && !php_checkuid(url, NULL, CHECKUID_CHECK_FILE_AND_DIR)) {
1058 return 0;
1059 }
1060
1061 if (php_check_open_basedir(url TSRMLS_CC)) {
1062 return 0;
1063 }
1064 }
1065
1066 ret = VCWD_UNLINK(url);
1067 if (ret == -1) {
1068 if (options & REPORT_ERRORS) {
1069 php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "%s", strerror(errno));
1070 }
1071 return 0;
1072 }
1073
1074 /* Clear stat cache (and realpath cache) */
1075 php_clear_stat_cache(1, NULL, 0 TSRMLS_CC);
1076
1077 return 1;
1078 }
1079
php_plain_files_rename(php_stream_wrapper * wrapper,char * url_from,char * url_to,int options,php_stream_context * context TSRMLS_DC)1080 static int php_plain_files_rename(php_stream_wrapper *wrapper, char *url_from, char *url_to, int options, php_stream_context *context TSRMLS_DC)
1081 {
1082 char *p;
1083 int ret;
1084
1085 if (!url_from || !url_to) {
1086 return 0;
1087 }
1088
1089 #ifdef PHP_WIN32
1090 if (!php_win32_check_trailing_space(url_from, strlen(url_from))) {
1091 php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to TSRMLS_CC);
1092 return 0;
1093 }
1094 if (!php_win32_check_trailing_space(url_to, strlen(url_to))) {
1095 php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to TSRMLS_CC);
1096 return 0;
1097 }
1098 #endif
1099
1100 if ((p = strstr(url_from, "://")) != NULL) {
1101 if (p < strchr(url_from, '/')) {
1102 url_from = p + 3;
1103 }
1104 }
1105
1106 if ((p = strstr(url_to, "://")) != NULL) {
1107 if (p < strchr(url_to, '/')) {
1108 url_to = p + 3;
1109 }
1110 }
1111
1112 if (PG(safe_mode) && (!php_checkuid(url_from, NULL, CHECKUID_CHECK_FILE_AND_DIR) ||
1113 !php_checkuid(url_to, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
1114 return 0;
1115 }
1116
1117 if (php_check_open_basedir(url_from TSRMLS_CC) || php_check_open_basedir(url_to TSRMLS_CC)) {
1118 return 0;
1119 }
1120
1121 ret = VCWD_RENAME(url_from, url_to);
1122
1123 if (ret == -1) {
1124 #ifndef PHP_WIN32
1125 # ifdef EXDEV
1126 if (errno == EXDEV) {
1127 struct stat sb;
1128 if (php_copy_file(url_from, url_to TSRMLS_CC) == SUCCESS) {
1129 if (VCWD_STAT(url_from, &sb) == 0) {
1130 # if !defined(TSRM_WIN32) && !defined(NETWARE)
1131 if (VCWD_CHMOD(url_to, sb.st_mode)) {
1132 if (errno == EPERM) {
1133 php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
1134 VCWD_UNLINK(url_from);
1135 return 1;
1136 }
1137 php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
1138 return 0;
1139 }
1140 if (VCWD_CHOWN(url_to, sb.st_uid, sb.st_gid)) {
1141 if (errno == EPERM) {
1142 php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
1143 VCWD_UNLINK(url_from);
1144 return 1;
1145 }
1146 php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
1147 return 0;
1148 }
1149 # endif
1150 VCWD_UNLINK(url_from);
1151 return 1;
1152 }
1153 }
1154 php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
1155 return 0;
1156 }
1157 # endif
1158 #endif
1159
1160 #ifdef PHP_WIN32
1161 php_win32_docref2_from_error(GetLastError(), url_from, url_to TSRMLS_CC);
1162 #else
1163 php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
1164 #endif
1165 return 0;
1166 }
1167
1168 /* Clear stat cache (and realpath cache) */
1169 php_clear_stat_cache(1, NULL, 0 TSRMLS_CC);
1170
1171 return 1;
1172 }
1173
php_plain_files_mkdir(php_stream_wrapper * wrapper,char * dir,int mode,int options,php_stream_context * context TSRMLS_DC)1174 static int php_plain_files_mkdir(php_stream_wrapper *wrapper, char *dir, int mode, int options, php_stream_context *context TSRMLS_DC)
1175 {
1176 int ret, recursive = options & PHP_STREAM_MKDIR_RECURSIVE;
1177 char *p;
1178
1179 if ((p = strstr(dir, "://")) != NULL) {
1180 if (p < strchr(dir, '/')) {
1181 dir = p + 3;
1182 }
1183 }
1184
1185 if (!recursive) {
1186 ret = php_mkdir(dir, mode TSRMLS_CC);
1187 } else {
1188 /* we look for directory separator from the end of string, thus hopefuly reducing our work load */
1189 char *e, *buf;
1190 struct stat sb;
1191 int dir_len = strlen(dir);
1192 int offset = 0;
1193
1194 buf = estrndup(dir, dir_len);
1195
1196 #ifdef PHP_WIN32
1197 e = buf;
1198 while (*e) {
1199 if (*e == '/') {
1200 *e = DEFAULT_SLASH;
1201 }
1202 e++;
1203 }
1204 #else
1205 e = buf + dir_len;
1206 #endif
1207
1208 if ((p = memchr(buf, DEFAULT_SLASH, dir_len))) {
1209 offset = p - buf + 1;
1210 }
1211
1212 if (p && dir_len == 1) {
1213 /* buf == "DEFAULT_SLASH" */
1214 }
1215 else {
1216 /* find a top level directory we need to create */
1217 while ( (p = strrchr(buf + offset, DEFAULT_SLASH)) || (offset != 1 && (p = strrchr(buf, DEFAULT_SLASH))) ) {
1218 int n = 0;
1219
1220 *p = '\0';
1221 while (p > buf && *(p-1) == DEFAULT_SLASH) {
1222 ++n;
1223 --p;
1224 *p = '\0';
1225 }
1226 if (VCWD_STAT(buf, &sb) == 0) {
1227 while (1) {
1228 *p = DEFAULT_SLASH;
1229 if (!n) break;
1230 --n;
1231 ++p;
1232 }
1233 break;
1234 }
1235 }
1236 }
1237
1238 if (p == buf) {
1239 ret = php_mkdir(dir, mode TSRMLS_CC);
1240 } else if (!(ret = php_mkdir(buf, mode TSRMLS_CC))) {
1241 if (!p) {
1242 p = buf;
1243 }
1244 /* create any needed directories if the creation of the 1st directory worked */
1245 while (++p != e) {
1246 if (*p == '\0') {
1247 *p = DEFAULT_SLASH;
1248 if ((*(p+1) != '\0') &&
1249 (ret = VCWD_MKDIR(buf, (mode_t)mode)) < 0) {
1250 if (options & REPORT_ERRORS) {
1251 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
1252 }
1253 break;
1254 }
1255 }
1256 }
1257 }
1258 efree(buf);
1259 }
1260 if (ret < 0) {
1261 /* Failure */
1262 return 0;
1263 } else {
1264 /* Success */
1265 return 1;
1266 }
1267 }
1268
php_plain_files_rmdir(php_stream_wrapper * wrapper,char * url,int options,php_stream_context * context TSRMLS_DC)1269 static int php_plain_files_rmdir(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC)
1270 {
1271 #if PHP_WIN32
1272 int url_len = strlen(url);
1273 #endif
1274 if (PG(safe_mode) &&(!php_checkuid(url, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
1275 return 0;
1276 }
1277
1278 if (php_check_open_basedir(url TSRMLS_CC)) {
1279 return 0;
1280 }
1281
1282 #if PHP_WIN32
1283 if (!php_win32_check_trailing_space(url, url_len)) {
1284 php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "%s", strerror(ENOENT));
1285 return 0;
1286 }
1287 #endif
1288
1289 if (VCWD_RMDIR(url) < 0) {
1290 php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "%s", strerror(errno));
1291 return 0;
1292 }
1293
1294 /* Clear stat cache (and realpath cache) */
1295 php_clear_stat_cache(1, NULL, 0 TSRMLS_CC);
1296
1297 return 1;
1298 }
1299
1300 static php_stream_wrapper_ops php_plain_files_wrapper_ops = {
1301 php_plain_files_stream_opener,
1302 NULL,
1303 NULL,
1304 php_plain_files_url_stater,
1305 php_plain_files_dir_opener,
1306 "plainfile",
1307 php_plain_files_unlink,
1308 php_plain_files_rename,
1309 php_plain_files_mkdir,
1310 php_plain_files_rmdir
1311 };
1312
1313 php_stream_wrapper php_plain_files_wrapper = {
1314 &php_plain_files_wrapper_ops,
1315 NULL,
1316 0
1317 };
1318
1319 /* {{{ php_stream_fopen_with_path */
_php_stream_fopen_with_path(char * filename,char * mode,char * path,char ** opened_path,int options STREAMS_DC TSRMLS_DC)1320 PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char *path, char **opened_path, int options STREAMS_DC TSRMLS_DC)
1321 {
1322 /* code ripped off from fopen_wrappers.c */
1323 char *pathbuf, *ptr, *end;
1324 char *exec_fname;
1325 char trypath[MAXPATHLEN];
1326 php_stream *stream;
1327 int path_length;
1328 int filename_length;
1329 int exec_fname_length;
1330
1331 if (opened_path) {
1332 *opened_path = NULL;
1333 }
1334
1335 if(!filename) {
1336 return NULL;
1337 }
1338
1339 filename_length = strlen(filename);
1340
1341 /* Relative path open */
1342 if (*filename == '.' && (IS_SLASH(filename[1]) || filename[1] == '.')) {
1343 /* further checks, we could have ....... filenames */
1344 ptr = filename + 1;
1345 if (*ptr == '.') {
1346 while (*(++ptr) == '.');
1347 if (!IS_SLASH(*ptr)) { /* not a relative path after all */
1348 goto not_relative_path;
1349 }
1350 }
1351
1352
1353 if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename TSRMLS_CC)) {
1354 return NULL;
1355 }
1356
1357 if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
1358 return NULL;
1359 }
1360 return php_stream_fopen_rel(filename, mode, opened_path, options);
1361 }
1362
1363 /*
1364 * files in safe_mode_include_dir (or subdir) are excluded from
1365 * safe mode GID/UID checks
1366 */
1367
1368 not_relative_path:
1369
1370 /* Absolute path open */
1371 if (IS_ABSOLUTE_PATH(filename, filename_length)) {
1372
1373 if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename TSRMLS_CC)) {
1374 return NULL;
1375 }
1376
1377 if ((php_check_safe_mode_include_dir(filename TSRMLS_CC)) == 0)
1378 /* filename is in safe_mode_include_dir (or subdir) */
1379 return php_stream_fopen_rel(filename, mode, opened_path, options);
1380
1381 if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM)))
1382 return NULL;
1383
1384 return php_stream_fopen_rel(filename, mode, opened_path, options);
1385 }
1386
1387 #ifdef PHP_WIN32
1388 if (IS_SLASH(filename[0])) {
1389 size_t cwd_len;
1390 char *cwd;
1391 cwd = virtual_getcwd_ex(&cwd_len TSRMLS_CC);
1392 /* getcwd() will return always return [DRIVE_LETTER]:/) on windows. */
1393 *(cwd+3) = '\0';
1394
1395 if (snprintf(trypath, MAXPATHLEN, "%s%s", cwd, filename) >= MAXPATHLEN) {
1396 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s/%s path was truncated to %d", cwd, filename, MAXPATHLEN);
1397 }
1398
1399 free(cwd);
1400
1401 if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(trypath TSRMLS_CC)) {
1402 return NULL;
1403 }
1404 if ((php_check_safe_mode_include_dir(trypath TSRMLS_CC)) == 0) {
1405 return php_stream_fopen_rel(trypath, mode, opened_path, options);
1406 }
1407 if (PG(safe_mode) && (!php_checkuid(trypath, mode, CHECKUID_CHECK_MODE_PARAM))) {
1408 return NULL;
1409 }
1410
1411 return php_stream_fopen_rel(trypath, mode, opened_path, options);
1412 }
1413 #endif
1414
1415 if (!path || (path && !*path)) {
1416 if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
1417 return NULL;
1418 }
1419 return php_stream_fopen_rel(filename, mode, opened_path, options);
1420 }
1421
1422 /* check in provided path */
1423 /* append the calling scripts' current working directory
1424 * as a fall back case
1425 */
1426 if (zend_is_executing(TSRMLS_C)) {
1427 exec_fname = zend_get_executed_filename(TSRMLS_C);
1428 exec_fname_length = strlen(exec_fname);
1429 path_length = strlen(path);
1430
1431 while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
1432 if ((exec_fname && exec_fname[0] == '[')
1433 || exec_fname_length<=0) {
1434 /* [no active file] or no path */
1435 pathbuf = estrdup(path);
1436 } else {
1437 pathbuf = (char *) emalloc(exec_fname_length + path_length +1 +1);
1438 memcpy(pathbuf, path, path_length);
1439 pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;
1440 memcpy(pathbuf+path_length+1, exec_fname, exec_fname_length);
1441 pathbuf[path_length + exec_fname_length +1] = '\0';
1442 }
1443 } else {
1444 pathbuf = estrdup(path);
1445 }
1446
1447 ptr = pathbuf;
1448
1449 while (ptr && *ptr) {
1450 end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
1451 if (end != NULL) {
1452 *end = '\0';
1453 end++;
1454 }
1455 if (*ptr == '\0') {
1456 goto stream_skip;
1457 }
1458 if (snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename) >= MAXPATHLEN) {
1459 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s/%s path was truncated to %d", ptr, filename, MAXPATHLEN);
1460 }
1461
1462 if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir_ex(trypath, 0 TSRMLS_CC)) {
1463 goto stream_skip;
1464 }
1465
1466 if (PG(safe_mode)) {
1467 struct stat sb;
1468
1469 if (VCWD_STAT(trypath, &sb) == 0) {
1470 /* file exists ... check permission */
1471 if ((php_check_safe_mode_include_dir(trypath TSRMLS_CC) == 0) ||
1472 php_checkuid_ex(trypath, mode, CHECKUID_CHECK_MODE_PARAM, CHECKUID_NO_ERRORS)) {
1473 /* UID ok, or trypath is in safe_mode_include_dir */
1474 stream = php_stream_fopen_rel(trypath, mode, opened_path, options);
1475 goto stream_done;
1476 }
1477 }
1478 goto stream_skip;
1479 }
1480 stream = php_stream_fopen_rel(trypath, mode, opened_path, options);
1481 if (stream) {
1482 stream_done:
1483 efree(pathbuf);
1484 return stream;
1485 }
1486 stream_skip:
1487 ptr = end;
1488 } /* end provided path */
1489
1490 efree(pathbuf);
1491 return NULL;
1492
1493 }
1494 /* }}} */
1495
1496 /*
1497 * Local variables:
1498 * tab-width: 4
1499 * c-basic-offset: 4
1500 * End:
1501 * vim600: noet sw=4 ts=4 fdm=marker
1502 * vim<600: noet sw=4 ts=4
1503 */
1504