xref: /PHP-8.1/main/streams/cast.c (revision 41e00819)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Wez Furlong <wez@thebrainroom.com>                          |
14    +----------------------------------------------------------------------+
15  */
16 
17 #define _GNU_SOURCE
18 #include "php.h"
19 #include "php_globals.h"
20 #include "php_network.h"
21 #include "php_open_temporary_file.h"
22 #include "ext/standard/file.h"
23 #include <stddef.h>
24 #include <fcntl.h>
25 
26 #include "php_streams_int.h"
27 
28 /* Under BSD, emulate fopencookie using funopen */
29 #if defined(HAVE_FUNOPEN) && !defined(HAVE_FOPENCOOKIE)
30 
31 /* NetBSD 6.0+ uses off_t instead of fpos_t in funopen */
32 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 600000000)
33 #  define PHP_FPOS_T off_t
34 # else
35 #  define PHP_FPOS_T fpos_t
36 # endif
37 
38 typedef struct {
39 	int (*reader)(void *, char *, int);
40 	int (*writer)(void *, const char *, int);
41 	PHP_FPOS_T (*seeker)(void *, PHP_FPOS_T, int);
42 	int (*closer)(void *);
43 } COOKIE_IO_FUNCTIONS_T;
44 
fopencookie(void * cookie,const char * mode,COOKIE_IO_FUNCTIONS_T * funcs)45 FILE *fopencookie(void *cookie, const char *mode, COOKIE_IO_FUNCTIONS_T *funcs)
46 {
47 	return funopen(cookie, funcs->reader, funcs->writer, funcs->seeker, funcs->closer);
48 }
49 # define HAVE_FOPENCOOKIE 1
50 # define PHP_EMULATE_FOPENCOOKIE 1
51 # define PHP_STREAM_COOKIE_FUNCTIONS	&stream_cookie_functions
52 #elif defined(HAVE_FOPENCOOKIE)
53 # define PHP_STREAM_COOKIE_FUNCTIONS	stream_cookie_functions
54 #endif
55 
56 /* {{{ STDIO with fopencookie */
57 #if defined(PHP_EMULATE_FOPENCOOKIE)
58 /* use our fopencookie emulation */
stream_cookie_reader(void * cookie,char * buffer,int size)59 static int stream_cookie_reader(void *cookie, char *buffer, int size)
60 {
61 	int ret;
62 
63 	ret = php_stream_read((php_stream*)cookie, buffer, size);
64 	return ret;
65 }
66 
stream_cookie_writer(void * cookie,const char * buffer,int size)67 static int stream_cookie_writer(void *cookie, const char *buffer, int size)
68 {
69 
70 	return php_stream_write((php_stream *)cookie, (char *)buffer, size);
71 }
72 
stream_cookie_seeker(void * cookie,zend_off_t position,int whence)73 static PHP_FPOS_T stream_cookie_seeker(void *cookie, zend_off_t position, int whence)
74 {
75 
76 	return (PHP_FPOS_T)php_stream_seek((php_stream *)cookie, position, whence);
77 }
78 
stream_cookie_closer(void * cookie)79 static int stream_cookie_closer(void *cookie)
80 {
81 	php_stream *stream = (php_stream*)cookie;
82 
83 	/* prevent recursion */
84 	stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
85 	return php_stream_free(stream,
86 		PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_KEEP_RSRC | PHP_STREAM_FREE_RSRC_DTOR);
87 }
88 #elif defined(HAVE_FOPENCOOKIE)
stream_cookie_reader(void * cookie,char * buffer,size_t size)89 static ssize_t stream_cookie_reader(void *cookie, char *buffer, size_t size)
90 {
91 	ssize_t ret;
92 
93 	ret = php_stream_read(((php_stream *)cookie), buffer, size);
94 	return ret;
95 }
96 
stream_cookie_writer(void * cookie,const char * buffer,size_t size)97 static ssize_t stream_cookie_writer(void *cookie, const char *buffer, size_t size)
98 {
99 
100 	return php_stream_write(((php_stream *)cookie), (char *)buffer, size);
101 }
102 
103 # ifdef COOKIE_SEEKER_USES_OFF64_T
stream_cookie_seeker(void * cookie,off64_t * position,int whence)104 static int stream_cookie_seeker(void *cookie, off64_t *position, int whence)
105 {
106 
107 	*position = php_stream_seek((php_stream *)cookie, (zend_off_t)*position, whence);
108 
109 	if (*position == -1) {
110 		return -1;
111 	}
112 	return 0;
113 }
114 # else
stream_cookie_seeker(void * cookie,zend_off_t position,int whence)115 static int stream_cookie_seeker(void *cookie, zend_off_t position, int whence)
116 {
117 
118 	return php_stream_seek((php_stream *)cookie, position, whence);
119 }
120 # endif
121 
stream_cookie_closer(void * cookie)122 static int stream_cookie_closer(void *cookie)
123 {
124 	php_stream *stream = (php_stream*)cookie;
125 
126 	/* prevent recursion */
127 	stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
128 	return php_stream_free(stream,
129 		PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_KEEP_RSRC | PHP_STREAM_FREE_RSRC_DTOR);
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 		if (show_err) {
297 			php_error_docref(NULL, E_WARNING, "Cannot cast a filtered stream on this system");
298 		}
299 		return FAILURE;
300 	} else if (stream->ops->cast && stream->ops->cast(stream, castas, ret) == SUCCESS) {
301 		goto exit_success;
302 	}
303 
304 	if (show_err) {
305 		/* these names depend on the values of the PHP_STREAM_AS_XXX defines in php_streams.h */
306 		static const char *cast_names[4] = {
307 			"STDIO FILE*",
308 			"File Descriptor",
309 			"Socket Descriptor",
310 			"select()able descriptor"
311 		};
312 
313 		php_error_docref(NULL, E_WARNING, "Cannot represent a stream of type %s as a %s", stream->ops->label, cast_names[castas]);
314 	}
315 
316 	return FAILURE;
317 
318 exit_success:
319 
320 	if ((stream->writepos - stream->readpos) > 0 &&
321 		stream->fclose_stdiocast != PHP_STREAM_FCLOSE_FOPENCOOKIE &&
322 		(flags & PHP_STREAM_CAST_INTERNAL) == 0
323 	) {
324 		/* the data we have buffered will be lost to the third party library that
325 		 * will be accessing the stream.  Emit a warning so that the end-user will
326 		 * know that they should try something else */
327 
328 		php_error_docref(NULL, E_WARNING, ZEND_LONG_FMT " bytes of buffered data lost during stream conversion!", (zend_long)(stream->writepos - stream->readpos));
329 	}
330 
331 	if (castas == PHP_STREAM_AS_STDIO && ret) {
332 		stream->stdiocast = *(FILE**)ret;
333 	}
334 
335 	if (flags & PHP_STREAM_CAST_RELEASE) {
336 		php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
337 	}
338 
339 	return SUCCESS;
340 
341 }
342 /* }}} */
343 
344 /* {{{ php_stream_open_wrapper_as_file */
_php_stream_open_wrapper_as_file(char * path,char * mode,int options,zend_string ** opened_path STREAMS_DC)345 PHPAPI FILE * _php_stream_open_wrapper_as_file(char *path, char *mode, int options, zend_string **opened_path STREAMS_DC)
346 {
347 	FILE *fp = NULL;
348 	php_stream *stream = NULL;
349 
350 	stream = php_stream_open_wrapper_rel(path, mode, options|STREAM_WILL_CAST, opened_path);
351 
352 	if (stream == NULL) {
353 		return NULL;
354 	}
355 
356 	if (php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_TRY_HARD|PHP_STREAM_CAST_RELEASE, (void**)&fp, REPORT_ERRORS) == FAILURE) {
357 		php_stream_close(stream);
358 		if (opened_path && *opened_path) {
359 			zend_string_release_ex(*opened_path, 0);
360 		}
361 		return NULL;
362 	}
363 	return fp;
364 }
365 /* }}} */
366 
367 /* {{{ php_stream_make_seekable */
_php_stream_make_seekable(php_stream * origstream,php_stream ** newstream,int flags STREAMS_DC)368 PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream, int flags STREAMS_DC)
369 {
370 	if (newstream == NULL) {
371 		return PHP_STREAM_FAILED;
372 	}
373 	*newstream = NULL;
374 
375 	if (((flags & PHP_STREAM_FORCE_CONVERSION) == 0) && origstream->ops->seek != NULL) {
376 		*newstream = origstream;
377 		return PHP_STREAM_UNCHANGED;
378 	}
379 
380 	/* Use a tmpfile and copy the old streams contents into it */
381 
382 	if (flags & PHP_STREAM_PREFER_STDIO) {
383 		*newstream = php_stream_fopen_tmpfile();
384 	} else {
385 		*newstream = php_stream_temp_new();
386 	}
387 
388 	if (*newstream == NULL) {
389 		return PHP_STREAM_FAILED;
390 	}
391 
392 #if ZEND_DEBUG
393 	(*newstream)->open_filename = origstream->open_filename;
394 	(*newstream)->open_lineno = origstream->open_lineno;
395 #endif
396 
397 	if (php_stream_copy_to_stream_ex(origstream, *newstream, PHP_STREAM_COPY_ALL, NULL) != SUCCESS) {
398 		php_stream_close(*newstream);
399 		*newstream = NULL;
400 		return PHP_STREAM_CRITICAL;
401 	}
402 
403 	php_stream_close(origstream);
404 	php_stream_seek(*newstream, 0, SEEK_SET);
405 
406 	return PHP_STREAM_RELEASED;
407 }
408 /* }}} */
409