1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Wez Furlong <wez@thebrainroom.com> |
16 +----------------------------------------------------------------------+
17 */
18
19 #define _GNU_SOURCE
20 #include "php.h"
21 #include "php_globals.h"
22 #include "php_network.h"
23 #include "php_open_temporary_file.h"
24 #include "ext/standard/file.h"
25 #include <stddef.h>
26 #include <fcntl.h>
27
28 #include "php_streams_int.h"
29
30 /* Under BSD, emulate fopencookie using funopen */
31 #if defined(HAVE_FUNOPEN) && !defined(HAVE_FOPENCOOKIE)
32
33 /* NetBSD 6.0+ uses off_t instead of fpos_t in funopen */
34 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 600000000)
35 # define PHP_FPOS_T off_t
36 # else
37 # define PHP_FPOS_T fpos_t
38 # endif
39
40 typedef struct {
41 int (*reader)(void *, char *, int);
42 int (*writer)(void *, const char *, int);
43 PHP_FPOS_T (*seeker)(void *, PHP_FPOS_T, int);
44 int (*closer)(void *);
45 } COOKIE_IO_FUNCTIONS_T;
46
fopencookie(void * cookie,const char * mode,COOKIE_IO_FUNCTIONS_T * funcs)47 FILE *fopencookie(void *cookie, const char *mode, COOKIE_IO_FUNCTIONS_T *funcs)
48 {
49 return funopen(cookie, funcs->reader, funcs->writer, funcs->seeker, funcs->closer);
50 }
51 # define HAVE_FOPENCOOKIE 1
52 # define PHP_EMULATE_FOPENCOOKIE 1
53 # define PHP_STREAM_COOKIE_FUNCTIONS &stream_cookie_functions
54 #elif defined(HAVE_FOPENCOOKIE)
55 # define PHP_STREAM_COOKIE_FUNCTIONS stream_cookie_functions
56 #endif
57
58 /* {{{ STDIO with fopencookie */
59 #if defined(PHP_EMULATE_FOPENCOOKIE)
60 /* use our fopencookie emulation */
stream_cookie_reader(void * cookie,char * buffer,int size)61 static int stream_cookie_reader(void *cookie, char *buffer, int size)
62 {
63 int ret;
64
65 ret = php_stream_read((php_stream*)cookie, buffer, size);
66 return ret;
67 }
68
stream_cookie_writer(void * cookie,const char * buffer,int size)69 static int stream_cookie_writer(void *cookie, const char *buffer, int size)
70 {
71
72 return php_stream_write((php_stream *)cookie, (char *)buffer, size);
73 }
74
stream_cookie_seeker(void * cookie,zend_off_t position,int whence)75 static PHP_FPOS_T stream_cookie_seeker(void *cookie, zend_off_t position, int whence)
76 {
77
78 return (PHP_FPOS_T)php_stream_seek((php_stream *)cookie, position, whence);
79 }
80
stream_cookie_closer(void * cookie)81 static int stream_cookie_closer(void *cookie)
82 {
83 php_stream *stream = (php_stream*)cookie;
84
85 /* prevent recursion */
86 stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
87 return php_stream_free(stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_KEEP_RSRC);
88 }
89 #elif defined(HAVE_FOPENCOOKIE)
stream_cookie_reader(void * cookie,char * buffer,size_t size)90 static ssize_t stream_cookie_reader(void *cookie, char *buffer, size_t size)
91 {
92 ssize_t ret;
93
94 ret = php_stream_read(((php_stream *)cookie), buffer, size);
95 return ret;
96 }
97
stream_cookie_writer(void * cookie,const char * buffer,size_t size)98 static ssize_t stream_cookie_writer(void *cookie, const char *buffer, size_t size)
99 {
100
101 return php_stream_write(((php_stream *)cookie), (char *)buffer, size);
102 }
103
104 # ifdef COOKIE_SEEKER_USES_OFF64_T
stream_cookie_seeker(void * cookie,__off64_t * position,int whence)105 static int stream_cookie_seeker(void *cookie, __off64_t *position, int whence)
106 {
107
108 *position = php_stream_seek((php_stream *)cookie, (zend_off_t)*position, whence);
109
110 if (*position == -1) {
111 return -1;
112 }
113 return 0;
114 }
115 # else
stream_cookie_seeker(void * cookie,zend_off_t position,int whence)116 static int stream_cookie_seeker(void *cookie, zend_off_t position, int whence)
117 {
118
119 return php_stream_seek((php_stream *)cookie, position, whence);
120 }
121 # endif
122
stream_cookie_closer(void * cookie)123 static int stream_cookie_closer(void *cookie)
124 {
125 php_stream *stream = (php_stream*)cookie;
126
127 /* prevent recursion */
128 stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
129 return php_stream_free(stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_KEEP_RSRC);
130 }
131 #endif /* elif defined(HAVE_FOPENCOOKIE) */
132
133 #if HAVE_FOPENCOOKIE
134 static COOKIE_IO_FUNCTIONS_T stream_cookie_functions =
135 {
136 stream_cookie_reader, stream_cookie_writer,
137 stream_cookie_seeker, stream_cookie_closer
138 };
139 #else
140 /* TODO: use socketpair() to emulate fopencookie, as suggested by Hartmut ? */
141 #endif
142 /* }}} */
143
144 /* {{{ php_stream_mode_sanitize_fdopen_fopencookie
145 * Result should have at least size 5, e.g. to write wbx+\0 */
php_stream_mode_sanitize_fdopen_fopencookie(php_stream * stream,char * result)146 void php_stream_mode_sanitize_fdopen_fopencookie(php_stream *stream, char *result)
147 {
148 /* replace modes not supported by fdopen and fopencookie, but supported
149 * by PHP's fread(), so that their calls won't fail */
150 const char *cur_mode = stream->mode;
151 int has_plus = 0,
152 has_bin = 0,
153 i,
154 res_curs = 0;
155
156 if (cur_mode[0] == 'r' || cur_mode[0] == 'w' || cur_mode[0] == 'a') {
157 result[res_curs++] = cur_mode[0];
158 } else {
159 /* assume cur_mode[0] is 'c' or 'x'; substitute by 'w', which should not
160 * truncate anything in fdopen/fopencookie */
161 result[res_curs++] = 'w';
162
163 /* x is allowed (at least by glibc & compat), but not as the 1st mode
164 * as in PHP and in any case is (at best) ignored by fdopen and fopencookie */
165 }
166
167 /* assume current mode has at most length 4 (e.g. wbn+) */
168 for (i = 1; i < 4 && cur_mode[i] != '\0'; i++) {
169 if (cur_mode[i] == 'b') {
170 has_bin = 1;
171 } else if (cur_mode[i] == '+') {
172 has_plus = 1;
173 }
174 /* ignore 'n', 't' or other stuff */
175 }
176
177 if (has_bin) {
178 result[res_curs++] = 'b';
179 }
180 if (has_plus) {
181 result[res_curs++] = '+';
182 }
183
184 result[res_curs] = '\0';
185 }
186 /* }}} */
187
188 /* {{{ php_stream_cast */
_php_stream_cast(php_stream * stream,int castas,void ** ret,int show_err)189 PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show_err)
190 {
191 int flags = castas & PHP_STREAM_CAST_MASK;
192 castas &= ~PHP_STREAM_CAST_MASK;
193
194 /* synchronize our buffer (if possible) */
195 if (ret && castas != PHP_STREAM_AS_FD_FOR_SELECT) {
196 php_stream_flush(stream);
197 if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
198 zend_off_t dummy;
199
200 stream->ops->seek(stream, stream->position, SEEK_SET, &dummy);
201 stream->readpos = stream->writepos = 0;
202 }
203 }
204
205 /* filtered streams can only be cast as stdio, and only when fopencookie is present */
206
207 if (castas == PHP_STREAM_AS_STDIO) {
208 if (stream->stdiocast) {
209 if (ret) {
210 *(FILE**)ret = stream->stdiocast;
211 }
212 goto exit_success;
213 }
214
215 /* if the stream is a stdio stream let's give it a chance to respond
216 * first, to avoid doubling up the layers of stdio with an fopencookie */
217 if (php_stream_is(stream, PHP_STREAM_IS_STDIO) &&
218 stream->ops->cast &&
219 !php_stream_is_filtered(stream) &&
220 stream->ops->cast(stream, castas, ret) == SUCCESS
221 ) {
222 goto exit_success;
223 }
224
225 #if HAVE_FOPENCOOKIE
226 /* if just checking, say yes we can be a FILE*, but don't actually create it yet */
227 if (ret == NULL) {
228 goto exit_success;
229 }
230
231 {
232 char fixed_mode[5];
233 php_stream_mode_sanitize_fdopen_fopencookie(stream, fixed_mode);
234 *(FILE**)ret = fopencookie(stream, fixed_mode, PHP_STREAM_COOKIE_FUNCTIONS);
235 }
236
237 if (*ret != NULL) {
238 zend_off_t pos;
239
240 stream->fclose_stdiocast = PHP_STREAM_FCLOSE_FOPENCOOKIE;
241
242 /* If the stream position is not at the start, we need to force
243 * the stdio layer to believe it's real location. */
244 pos = php_stream_tell(stream);
245 if (pos > 0) {
246 zend_fseek(*ret, pos, SEEK_SET);
247 }
248
249 goto exit_success;
250 }
251
252 /* must be either:
253 a) programmer error
254 b) no memory
255 -> lets bail
256 */
257 php_error_docref(NULL, E_ERROR, "fopencookie failed");
258 return FAILURE;
259 #endif
260
261 if (!php_stream_is_filtered(stream) && stream->ops->cast && stream->ops->cast(stream, castas, NULL) == SUCCESS) {
262 if (FAILURE == stream->ops->cast(stream, castas, ret)) {
263 return FAILURE;
264 }
265 goto exit_success;
266 } else if (flags & PHP_STREAM_CAST_TRY_HARD) {
267 php_stream *newstream;
268
269 newstream = php_stream_fopen_tmpfile();
270 if (newstream) {
271 int retcopy = php_stream_copy_to_stream_ex(stream, newstream, PHP_STREAM_COPY_ALL, NULL);
272
273 if (retcopy != SUCCESS) {
274 php_stream_close(newstream);
275 } else {
276 int retcast = php_stream_cast(newstream, castas | flags, (void **)ret, show_err);
277
278 if (retcast == SUCCESS) {
279 rewind(*(FILE**)ret);
280 }
281
282 /* do some specialized cleanup */
283 if ((flags & PHP_STREAM_CAST_RELEASE)) {
284 php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
285 }
286
287 /* TODO: we probably should be setting .stdiocast and .fclose_stdiocast or
288 * we may be leaking the FILE*. Needs investigation, though. */
289 return retcast;
290 }
291 }
292 }
293 }
294
295 if (php_stream_is_filtered(stream)) {
296 php_error_docref(NULL, E_WARNING, "cannot cast a filtered stream on this system");
297 return FAILURE;
298 } else if (stream->ops->cast && stream->ops->cast(stream, castas, ret) == SUCCESS) {
299 goto exit_success;
300 }
301
302 if (show_err) {
303 /* these names depend on the values of the PHP_STREAM_AS_XXX defines in php_streams.h */
304 static const char *cast_names[4] = {
305 "STDIO FILE*",
306 "File Descriptor",
307 "Socket Descriptor",
308 "select()able descriptor"
309 };
310
311 php_error_docref(NULL, E_WARNING, "cannot represent a stream of type %s as a %s", stream->ops->label, cast_names[castas]);
312 }
313
314 return FAILURE;
315
316 exit_success:
317
318 if ((stream->writepos - stream->readpos) > 0 &&
319 stream->fclose_stdiocast != PHP_STREAM_FCLOSE_FOPENCOOKIE &&
320 (flags & PHP_STREAM_CAST_INTERNAL) == 0
321 ) {
322 /* the data we have buffered will be lost to the third party library that
323 * will be accessing the stream. Emit a warning so that the end-user will
324 * know that they should try something else */
325
326 php_error_docref(NULL, E_WARNING, ZEND_LONG_FMT " bytes of buffered data lost during stream conversion!", (zend_long)(stream->writepos - stream->readpos));
327 }
328
329 if (castas == PHP_STREAM_AS_STDIO && ret) {
330 stream->stdiocast = *(FILE**)ret;
331 }
332
333 if (flags & PHP_STREAM_CAST_RELEASE) {
334 php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
335 }
336
337 return SUCCESS;
338
339 }
340 /* }}} */
341
342 /* {{{ php_stream_open_wrapper_as_file */
_php_stream_open_wrapper_as_file(char * path,char * mode,int options,zend_string ** opened_path STREAMS_DC)343 PHPAPI FILE * _php_stream_open_wrapper_as_file(char *path, char *mode, int options, zend_string **opened_path STREAMS_DC)
344 {
345 FILE *fp = NULL;
346 php_stream *stream = NULL;
347
348 stream = php_stream_open_wrapper_rel(path, mode, options|STREAM_WILL_CAST, opened_path);
349
350 if (stream == NULL) {
351 return NULL;
352 }
353
354 if (php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_TRY_HARD|PHP_STREAM_CAST_RELEASE, (void**)&fp, REPORT_ERRORS) == FAILURE) {
355 php_stream_close(stream);
356 if (opened_path && *opened_path) {
357 zend_string_release_ex(*opened_path, 0);
358 }
359 return NULL;
360 }
361 return fp;
362 }
363 /* }}} */
364
365 /* {{{ php_stream_make_seekable */
_php_stream_make_seekable(php_stream * origstream,php_stream ** newstream,int flags STREAMS_DC)366 PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream, int flags STREAMS_DC)
367 {
368 if (newstream == NULL) {
369 return PHP_STREAM_FAILED;
370 }
371 *newstream = NULL;
372
373 if (((flags & PHP_STREAM_FORCE_CONVERSION) == 0) && origstream->ops->seek != NULL) {
374 *newstream = origstream;
375 return PHP_STREAM_UNCHANGED;
376 }
377
378 /* Use a tmpfile and copy the old streams contents into it */
379
380 if (flags & PHP_STREAM_PREFER_STDIO) {
381 *newstream = php_stream_fopen_tmpfile();
382 } else {
383 *newstream = php_stream_temp_new();
384 }
385
386 if (*newstream == NULL) {
387 return PHP_STREAM_FAILED;
388 }
389
390 #if ZEND_DEBUG
391 (*newstream)->open_filename = origstream->open_filename;
392 (*newstream)->open_lineno = origstream->open_lineno;
393 #endif
394
395 if (php_stream_copy_to_stream_ex(origstream, *newstream, PHP_STREAM_COPY_ALL, NULL) != SUCCESS) {
396 php_stream_close(*newstream);
397 *newstream = NULL;
398 return PHP_STREAM_CRITICAL;
399 }
400
401 php_stream_close(origstream);
402 php_stream_seek(*newstream, 0, SEEK_SET);
403
404 return PHP_STREAM_RELEASED;
405 }
406 /* }}} */
407
408 /*
409 * Local variables:
410 * tab-width: 4
411 * c-basic-offset: 4
412 * End:
413 * vim600: noet sw=4 ts=4 fdm=marker
414 * vim<600: noet sw=4 ts=4
415 */
416