/src/php-src/main/fopen_wrappers.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> | |
12 | | | Jim Winstead <jimw@php.net> | |
13 | | +----------------------------------------------------------------------+ |
14 | | */ |
15 | | |
16 | | /* {{{ includes */ |
17 | | #include "php.h" |
18 | | #include "php_globals.h" |
19 | | #include "SAPI.h" |
20 | | |
21 | | #include <stdio.h> |
22 | | #include <stdlib.h> |
23 | | #include <errno.h> |
24 | | #include <sys/types.h> |
25 | | #include <sys/stat.h> |
26 | | #include <fcntl.h> |
27 | | |
28 | | #ifdef PHP_WIN32 |
29 | | #define O_RDONLY _O_RDONLY |
30 | | #include "win32/param.h" |
31 | | #else |
32 | | #include <sys/param.h> |
33 | | #endif |
34 | | |
35 | | #include "ext/standard/head.h" |
36 | | #include "ext/standard/php_standard.h" |
37 | | #include "zend_compile.h" |
38 | | #include "php_network.h" |
39 | | #include "zend_smart_str.h" |
40 | | |
41 | | #ifdef HAVE_PWD_H |
42 | | #include <pwd.h> |
43 | | #endif |
44 | | |
45 | | #ifdef HAVE_SYS_SOCKET_H |
46 | | #include <sys/socket.h> |
47 | | #endif |
48 | | |
49 | | #ifdef PHP_WIN32 |
50 | | #include <winsock2.h> |
51 | | #else |
52 | | #include <netinet/in.h> |
53 | | #include <netdb.h> |
54 | | #ifdef HAVE_ARPA_INET_H |
55 | | #include <arpa/inet.h> |
56 | | #endif |
57 | | #endif |
58 | | |
59 | | #if defined(PHP_WIN32) || defined(__riscos__) |
60 | | #undef AF_UNIX |
61 | | #endif |
62 | | |
63 | | #if defined(AF_UNIX) |
64 | | #include <sys/un.h> |
65 | | #endif |
66 | | /* }}} */ |
67 | | |
68 | | /* {{{ OnUpdateBaseDir |
69 | | Allows any change to open_basedir setting in during Startup and Shutdown events, |
70 | | or a tightening during activation/runtime/deactivation */ |
71 | | PHPAPI ZEND_INI_MH(OnUpdateBaseDir) |
72 | 28 | { |
73 | 28 | char **p = ZEND_INI_GET_ADDR(); |
74 | 28 | char *pathbuf, *ptr, *end; |
75 | | |
76 | 28 | if (stage == PHP_INI_STAGE_STARTUP || stage == PHP_INI_STAGE_SHUTDOWN || stage == PHP_INI_STAGE_ACTIVATE || stage == PHP_INI_STAGE_DEACTIVATE) { |
77 | 22 | if (PG(open_basedir_modified)) { |
78 | 6 | efree(*p); |
79 | 6 | } |
80 | | /* We're in a PHP_INI_SYSTEM context, no restrictions */ |
81 | 22 | *p = new_value ? ZSTR_VAL(new_value) : NULL; |
82 | 22 | PG(open_basedir_modified) = false; |
83 | 22 | return SUCCESS; |
84 | 22 | } |
85 | | |
86 | | /* Shortcut: When we have a open_basedir and someone tries to unset, we know it'll fail */ |
87 | 6 | if (!new_value || !*ZSTR_VAL(new_value)) { |
88 | 0 | return FAILURE; |
89 | 0 | } |
90 | | |
91 | | /* Is the proposed open_basedir at least as restrictive as the current setting? */ |
92 | 6 | smart_str buf = {0}; |
93 | 6 | ptr = pathbuf = estrdup(ZSTR_VAL(new_value)); |
94 | 12 | while (ptr && *ptr) { |
95 | 6 | end = strchr(ptr, DEFAULT_DIR_SEPARATOR); |
96 | 6 | if (end != NULL) { |
97 | 0 | *end = '\0'; |
98 | 0 | end++; |
99 | 0 | } |
100 | 6 | char resolved_name[MAXPATHLEN + 1]; |
101 | 6 | if (expand_filepath(ptr, resolved_name) == NULL) { |
102 | 0 | efree(pathbuf); |
103 | 0 | smart_str_free(&buf); |
104 | 0 | return FAILURE; |
105 | 0 | } |
106 | 6 | if (php_check_open_basedir_ex(resolved_name, 0) != 0) { |
107 | | /* At least one portion of this open_basedir is less restrictive than the prior one, FAIL */ |
108 | 0 | efree(pathbuf); |
109 | 0 | smart_str_free(&buf); |
110 | 0 | return FAILURE; |
111 | 0 | } |
112 | 6 | if (smart_str_get_len(&buf) != 0) { |
113 | 0 | smart_str_appendc(&buf, DEFAULT_DIR_SEPARATOR); |
114 | 0 | } |
115 | 6 | smart_str_appends(&buf, resolved_name); |
116 | 6 | ptr = end; |
117 | 6 | } |
118 | 6 | efree(pathbuf); |
119 | | |
120 | | /* Everything checks out, set it */ |
121 | 6 | zend_string *tmp = smart_str_extract(&buf); |
122 | 6 | char *result = estrdup(ZSTR_VAL(tmp)); |
123 | 6 | if (PG(open_basedir_modified)) { |
124 | 0 | efree(*p); |
125 | 0 | } |
126 | 6 | *p = result; |
127 | 6 | PG(open_basedir_modified) = true; |
128 | 6 | zend_string_release(tmp); |
129 | | |
130 | 6 | return SUCCESS; |
131 | 6 | } |
132 | | /* }}} */ |
133 | | |
134 | | /* {{{ php_check_specific_open_basedir |
135 | | When open_basedir is not NULL, check if the given filename is located in |
136 | | open_basedir. Returns -1 if error or not in the open_basedir, else 0. |
137 | | When open_basedir is NULL, always return 0. |
138 | | */ |
139 | | PHPAPI int php_check_specific_open_basedir(const char *basedir, const char *path) |
140 | 297 | { |
141 | 297 | char resolved_name[MAXPATHLEN + 1]; |
142 | 297 | char resolved_basedir[MAXPATHLEN + 1]; |
143 | 297 | char local_open_basedir[MAXPATHLEN]; |
144 | 297 | char path_tmp[MAXPATHLEN + 1]; |
145 | 297 | char *path_file; |
146 | 297 | size_t resolved_basedir_len; |
147 | 297 | size_t resolved_name_len; |
148 | 297 | size_t path_len; |
149 | 297 | int nesting_level = 0; |
150 | | |
151 | | #ifdef PHP_WIN32 |
152 | | /* Preserve the working-directory permission check for bare NUL. */ |
153 | | if ((strcasecmp(path, "NUL") == 0 || strcasecmp(path, "NUL:") == 0) |
154 | | && php_check_specific_open_basedir(basedir, ".") == 0) { |
155 | | return 0; |
156 | | } |
157 | | #endif |
158 | | |
159 | | /* Special case basedir==".": Use script-directory */ |
160 | 297 | if (strcmp(basedir, ".") || !VCWD_GETCWD(local_open_basedir, MAXPATHLEN)) { |
161 | | /* Else use the unmodified path */ |
162 | 297 | strlcpy(local_open_basedir, basedir, sizeof(local_open_basedir)); |
163 | 297 | } |
164 | | |
165 | 297 | path_len = strlen(path); |
166 | 297 | if (path_len > (MAXPATHLEN - 1)) { |
167 | | /* empty and too long paths are invalid */ |
168 | 0 | return -1; |
169 | 0 | } |
170 | | |
171 | | /* normalize and expand path */ |
172 | 297 | if (expand_filepath(path, resolved_name) == NULL) { |
173 | 0 | return -1; |
174 | 0 | } |
175 | | |
176 | 297 | path_len = strlen(resolved_name); |
177 | 297 | memcpy(path_tmp, resolved_name, path_len + 1); /* safe */ |
178 | | |
179 | 811 | while (VCWD_REALPATH(path_tmp, resolved_name) == NULL) { |
180 | 538 | #if defined(PHP_WIN32) || defined(HAVE_SYMLINK) |
181 | 538 | if (nesting_level == 0) { |
182 | 288 | ssize_t ret; |
183 | 288 | char buf[MAXPATHLEN]; |
184 | | |
185 | 288 | ret = php_sys_readlink(path_tmp, buf, MAXPATHLEN - 1); |
186 | 288 | if (ret == -1) { |
187 | | /* not a broken symlink, move along.. */ |
188 | 288 | } else { |
189 | | /* put the real path into the path buffer */ |
190 | 0 | memcpy(path_tmp, buf, ret); |
191 | 0 | path_tmp[ret] = '\0'; |
192 | 0 | } |
193 | 288 | } |
194 | 538 | #endif |
195 | | |
196 | | #ifdef PHP_WIN32 |
197 | | path_file = strrchr(path_tmp, DEFAULT_SLASH); |
198 | | if (!path_file) { |
199 | | path_file = strrchr(path_tmp, '/'); |
200 | | } |
201 | | #else |
202 | 538 | path_file = strrchr(path_tmp, DEFAULT_SLASH); |
203 | 538 | #endif |
204 | 538 | if (!path_file) { |
205 | | /* none of the path components exist. definitely not in open_basedir.. */ |
206 | 0 | return -1; |
207 | 538 | } else { |
208 | 538 | path_len = path_file - path_tmp + 1; |
209 | | #ifdef PHP_WIN32 |
210 | | if (path_len > 1 && path_tmp[path_len - 2] == ':') { |
211 | | if (path_len != 3) { |
212 | | return -1; |
213 | | } |
214 | | /* this is c:\ */ |
215 | | path_tmp[path_len] = '\0'; |
216 | | } else { |
217 | | path_tmp[path_len - 1] = '\0'; |
218 | | } |
219 | | #else |
220 | 538 | path_tmp[path_len - 1] = '\0'; |
221 | 538 | #endif |
222 | 538 | } |
223 | 538 | if (*path_tmp == '\0') { |
224 | | /* Do not pass an empty string to realpath(), as this will resolve to CWD. */ |
225 | 24 | break; |
226 | 24 | } |
227 | 514 | nesting_level++; |
228 | 514 | } |
229 | | |
230 | | /* Resolve open_basedir to resolved_basedir */ |
231 | 297 | if (expand_filepath(local_open_basedir, resolved_basedir) != NULL) { |
232 | 297 | size_t basedir_len = strlen(basedir); |
233 | | /* Handler for basedirs that end with a / */ |
234 | 297 | resolved_basedir_len = strlen(resolved_basedir); |
235 | | #ifdef PHP_WIN32 |
236 | | if (basedir[basedir_len - 1] == PHP_DIR_SEPARATOR || basedir[basedir_len - 1] == '/') { |
237 | | #else |
238 | 297 | if (basedir[basedir_len - 1] == PHP_DIR_SEPARATOR) { |
239 | 0 | #endif |
240 | 0 | if (resolved_basedir[resolved_basedir_len - 1] != PHP_DIR_SEPARATOR) { |
241 | 0 | resolved_basedir[resolved_basedir_len] = PHP_DIR_SEPARATOR; |
242 | 0 | resolved_basedir[++resolved_basedir_len] = '\0'; |
243 | 0 | } |
244 | 297 | } else { |
245 | 297 | resolved_basedir[resolved_basedir_len++] = PHP_DIR_SEPARATOR; |
246 | 297 | resolved_basedir[resolved_basedir_len] = '\0'; |
247 | 297 | } |
248 | | |
249 | 297 | resolved_name_len = strlen(resolved_name); |
250 | 297 | if (path_tmp[path_len - 1] == PHP_DIR_SEPARATOR) { |
251 | 0 | if (resolved_name[resolved_name_len - 1] != PHP_DIR_SEPARATOR) { |
252 | 0 | resolved_name[resolved_name_len] = PHP_DIR_SEPARATOR; |
253 | 0 | resolved_name[++resolved_name_len] = '\0'; |
254 | 0 | } |
255 | 0 | } |
256 | | |
257 | | /* Check the path */ |
258 | | #ifdef PHP_WIN32 |
259 | | if (strncasecmp(resolved_basedir, resolved_name, resolved_basedir_len) == 0) { |
260 | | #else |
261 | 297 | if (strncmp(resolved_basedir, resolved_name, resolved_basedir_len) == 0) { |
262 | 2 | #endif |
263 | 2 | if (resolved_name_len > resolved_basedir_len && |
264 | 2 | resolved_name[resolved_basedir_len - 1] != PHP_DIR_SEPARATOR) { |
265 | 0 | return -1; |
266 | 2 | } else { |
267 | | /* File is in the right directory */ |
268 | 2 | return 0; |
269 | 2 | } |
270 | 295 | } else { |
271 | | /* /openbasedir/ and /openbasedir are the same directory */ |
272 | 295 | if (resolved_basedir_len == (resolved_name_len + 1) && resolved_basedir[resolved_basedir_len - 1] == PHP_DIR_SEPARATOR) { |
273 | | #ifdef PHP_WIN32 |
274 | | if (strncasecmp(resolved_basedir, resolved_name, resolved_name_len) == 0) { |
275 | | #else |
276 | 20 | if (strncmp(resolved_basedir, resolved_name, resolved_name_len) == 0) { |
277 | 15 | #endif |
278 | 15 | return 0; |
279 | 15 | } |
280 | 20 | } |
281 | 280 | return -1; |
282 | 295 | } |
283 | 297 | } else { |
284 | | /* Unable to resolve the real path, return -1 */ |
285 | 0 | return -1; |
286 | 0 | } |
287 | 297 | } |
288 | | /* }}} */ |
289 | | |
290 | | PHPAPI int php_check_open_basedir(const char *path) |
291 | 355 | { |
292 | 355 | return php_check_open_basedir_ex(path, 1); |
293 | 355 | } |
294 | | |
295 | | /* {{{ php_check_open_basedir */ |
296 | | PHPAPI int php_check_open_basedir_ex(const char *path, int warn) |
297 | 361 | { |
298 | | /* Only check when open_basedir is available */ |
299 | 361 | if (PG(open_basedir) && *PG(open_basedir)) { |
300 | 297 | char *pathbuf; |
301 | 297 | char *ptr; |
302 | 297 | char *end; |
303 | | |
304 | | /* Check if the path is too long so we can give a more useful error |
305 | | * message. */ |
306 | 297 | if (strlen(path) > (MAXPATHLEN - 1)) { |
307 | 0 | php_error_docref(NULL, E_WARNING, "File name is longer than the maximum allowed path length on this platform (%d): %s", MAXPATHLEN, path); |
308 | 0 | errno = EINVAL; |
309 | 0 | return -1; |
310 | 0 | } |
311 | | |
312 | 297 | pathbuf = estrdup(PG(open_basedir)); |
313 | | |
314 | 297 | ptr = pathbuf; |
315 | | |
316 | 577 | while (ptr && *ptr) { |
317 | 297 | end = strchr(ptr, DEFAULT_DIR_SEPARATOR); |
318 | 297 | if (end != NULL) { |
319 | 0 | *end = '\0'; |
320 | 0 | end++; |
321 | 0 | } |
322 | | |
323 | 297 | if (php_check_specific_open_basedir(ptr, path) == 0) { |
324 | 17 | efree(pathbuf); |
325 | 17 | return 0; |
326 | 17 | } |
327 | | |
328 | 280 | ptr = end; |
329 | 280 | } |
330 | 280 | if (warn) { |
331 | 280 | php_error_docref(NULL, E_WARNING, "open_basedir restriction in effect. File(%s) is not within the allowed path(s): (%s)", path, PG(open_basedir)); |
332 | 280 | } |
333 | 280 | efree(pathbuf); |
334 | 280 | errno = EPERM; /* we deny permission to open it */ |
335 | 280 | return -1; |
336 | 297 | } |
337 | | |
338 | | /* Nothing to check... */ |
339 | 64 | return 0; |
340 | 361 | } |
341 | | /* }}} */ |
342 | | |
343 | | /* {{{ php_fopen_and_set_opened_path */ |
344 | | static FILE *php_fopen_and_set_opened_path(const char *path, const char *mode, zend_string **opened_path) |
345 | 64 | { |
346 | 64 | FILE *fp; |
347 | | |
348 | 64 | if (php_check_open_basedir((char *)path)) { |
349 | 0 | return NULL; |
350 | 0 | } |
351 | 64 | fp = VCWD_FOPEN(path, mode); |
352 | 64 | if (fp && opened_path) { |
353 | | //TODO :avoid reallocation |
354 | 0 | char *tmp = expand_filepath_with_mode(path, NULL, NULL, 0, CWD_EXPAND); |
355 | 0 | if (tmp) { |
356 | 0 | *opened_path = zend_string_init(tmp, strlen(tmp), 0); |
357 | 0 | efree(tmp); |
358 | 0 | } |
359 | 0 | } |
360 | 64 | return fp; |
361 | 64 | } |
362 | | /* }}} */ |
363 | | |
364 | | /* {{{ php_fopen_primary_script */ |
365 | | PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle) |
366 | 0 | { |
367 | 0 | char *path_info; |
368 | 0 | zend_string *filename = NULL; |
369 | 0 | zend_string *resolved_path = NULL; |
370 | 0 | bool orig_display_errors; |
371 | |
|
372 | 0 | memset(file_handle, 0, sizeof(zend_file_handle)); |
373 | |
|
374 | 0 | path_info = SG(request_info).request_uri; |
375 | 0 | #ifdef HAVE_PWD_H |
376 | 0 | if (PG(user_dir) && *PG(user_dir) && path_info && '/' == path_info[0] && '~' == path_info[1]) { |
377 | 0 | char *s = strchr(path_info + 2, '/'); |
378 | |
|
379 | 0 | if (s) { /* if there is no path name after the file, do not bother */ |
380 | 0 | char user[32]; /* to try open the directory */ |
381 | |
|
382 | 0 | size_t length = s - (path_info + 2); |
383 | 0 | if (length > sizeof(user) - 1) { |
384 | 0 | length = sizeof(user) - 1; |
385 | 0 | } |
386 | 0 | memcpy(user, path_info + 2, length); |
387 | 0 | user[length] = '\0'; |
388 | |
|
389 | 0 | struct passwd *pw; |
390 | | #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX) |
391 | | struct passwd pwstruc; |
392 | | long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
393 | | char *pwbuf; |
394 | | int err; |
395 | | |
396 | | if (pwbuflen < 1) { |
397 | | pwbuflen = 1024; |
398 | | } |
399 | | # if ZEND_DEBUG |
400 | | /* Test retry logic */ |
401 | | pwbuflen = 1; |
402 | | # endif |
403 | | pwbuf = emalloc(pwbuflen); |
404 | | |
405 | | try_again: |
406 | | err = getpwnam_r(user, &pwstruc, pwbuf, pwbuflen, &pw); |
407 | | if (err) { |
408 | | if (err == ERANGE) { |
409 | | pwbuflen *= 2; |
410 | | pwbuf = erealloc(pwbuf, pwbuflen); |
411 | | goto try_again; |
412 | | } |
413 | | efree(pwbuf); |
414 | | return FAILURE; |
415 | | } |
416 | | #else |
417 | 0 | pw = getpwnam(user); |
418 | 0 | #endif |
419 | 0 | if (pw && pw->pw_dir) { |
420 | 0 | filename = zend_strpprintf(0, "%s%c%s%c%s", pw->pw_dir, PHP_DIR_SEPARATOR, PG(user_dir), PHP_DIR_SEPARATOR, s + 1); /* Safe */ |
421 | 0 | } else if (SG(request_info).path_translated) { |
422 | 0 | filename = zend_string_init(SG(request_info).path_translated, |
423 | 0 | strlen(SG(request_info).path_translated), 0); |
424 | 0 | } |
425 | | #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX) |
426 | | efree(pwbuf); |
427 | | #endif |
428 | 0 | } |
429 | 0 | } else |
430 | 0 | #endif |
431 | 0 | if (PG(doc_root) && path_info && IS_ABSOLUTE_PATH(ZSTR_VAL(PG(doc_root)), ZSTR_LEN(PG(doc_root)))) { |
432 | 0 | const size_t path_len = strlen(path_info); |
433 | | |
434 | | /* We need to concatenate two paths together, there are 3 situations: |
435 | | * - No trailing slash AND no leading slash |
436 | | * - Trailing slash AND leading slash |
437 | | * - Either a trailing slash OR a leading slash |
438 | | * In the first case we need to add a slash, in the second one we need to skip the leading slash, |
439 | | * and in the third we can just concatenate them together */ |
440 | 0 | const unsigned int nb_slashes = IS_SLASH(ZSTR_VAL(PG(doc_root))[ZSTR_LEN(PG(doc_root)) - 1]) + IS_SLASH(path_info[0]); |
441 | 0 | switch (nb_slashes) { |
442 | 0 | case 0: |
443 | 0 | filename = zend_string_concat3( |
444 | 0 | ZSTR_VAL(PG(doc_root)), ZSTR_LEN(PG(doc_root)), |
445 | 0 | ZEND_STRL("/"), |
446 | 0 | path_info, path_len |
447 | 0 | ); |
448 | 0 | break; |
449 | 0 | case 1: |
450 | 0 | filename = zend_string_concat2( |
451 | 0 | ZSTR_VAL(PG(doc_root)), ZSTR_LEN(PG(doc_root)), |
452 | 0 | path_info, path_len |
453 | 0 | ); |
454 | 0 | break; |
455 | 0 | case 2: |
456 | 0 | filename = zend_string_concat2( |
457 | 0 | ZSTR_VAL(PG(doc_root)), ZSTR_LEN(PG(doc_root)), |
458 | 0 | path_info + 1, path_len -1 |
459 | 0 | ); |
460 | 0 | break; |
461 | 0 | } |
462 | 0 | } else if (SG(request_info).path_translated) { |
463 | 0 | filename = zend_string_init(SG(request_info).path_translated, |
464 | 0 | strlen(SG(request_info).path_translated), 0); |
465 | 0 | } |
466 | | |
467 | | |
468 | 0 | if (filename) { |
469 | 0 | resolved_path = zend_resolve_path(filename); |
470 | 0 | } |
471 | |
|
472 | 0 | if (!resolved_path) { |
473 | 0 | if (filename) { |
474 | 0 | zend_string_release(filename); |
475 | 0 | } |
476 | | /* we have to free SG(request_info).path_translated here because |
477 | | * php_destroy_request_info assumes that it will get |
478 | | * freed when the include_names hash is emptied, but |
479 | | * we're not adding it in this case */ |
480 | 0 | if (SG(request_info).path_translated) { |
481 | 0 | efree(SG(request_info).path_translated); |
482 | 0 | SG(request_info).path_translated = NULL; |
483 | 0 | } |
484 | 0 | return FAILURE; |
485 | 0 | } |
486 | 0 | zend_string_release_ex(resolved_path, 0); |
487 | |
|
488 | 0 | orig_display_errors = PG(display_errors); |
489 | 0 | PG(display_errors) = 0; |
490 | 0 | zend_stream_init_filename_ex(file_handle, filename); |
491 | 0 | file_handle->primary_script = 1; |
492 | 0 | if (filename) { |
493 | 0 | zend_string_delref(filename); |
494 | 0 | } |
495 | 0 | if (zend_stream_open(file_handle) == FAILURE) { |
496 | 0 | PG(display_errors) = orig_display_errors; |
497 | 0 | if (SG(request_info).path_translated) { |
498 | 0 | efree(SG(request_info).path_translated); |
499 | 0 | SG(request_info).path_translated = NULL; |
500 | 0 | } |
501 | 0 | return FAILURE; |
502 | 0 | } |
503 | 0 | PG(display_errors) = orig_display_errors; |
504 | |
|
505 | 0 | return SUCCESS; |
506 | 0 | } |
507 | | /* }}} */ |
508 | | |
509 | 44.9k | static zend_string *tsrm_realpath_str(const char *path) { |
510 | 44.9k | char *realpath = tsrm_realpath(path, NULL); |
511 | 44.9k | if (!realpath) { |
512 | 546 | return NULL; |
513 | 546 | } |
514 | 44.3k | zend_string *realpath_str = zend_string_init(realpath, strlen(realpath), 0); |
515 | 44.3k | efree(realpath); |
516 | 44.3k | return realpath_str; |
517 | 44.9k | } |
518 | | |
519 | | /* {{{ php_resolve_path |
520 | | * Returns the realpath for given filename according to include path |
521 | | */ |
522 | | PHPAPI zend_string *php_resolve_path(const char *filename, size_t filename_length, const char *path) |
523 | 46.6k | { |
524 | 46.6k | zend_string *resolved_path; |
525 | 46.6k | char trypath[MAXPATHLEN]; |
526 | 46.6k | const char *ptr, *end, *p; |
527 | 46.6k | const char *actual_path; |
528 | 46.6k | php_stream_wrapper *wrapper; |
529 | 46.6k | zend_string *exec_filename; |
530 | | |
531 | 46.6k | if (!filename || zend_char_has_nul_byte(filename, filename_length)) { |
532 | 0 | return NULL; |
533 | 0 | } |
534 | | |
535 | | /* Don't resolve paths which contain protocol (except of file://) */ |
536 | 48.0k | for (p = filename; isalnum((unsigned char)*p) || *p == '+' || *p == '-' || *p == '.'; p++); |
537 | 46.6k | if ((*p == ':') && (p - filename > 1) && (p[1] == '/') && (p[2] == '/')) { |
538 | 17 | wrapper = php_stream_locate_url_wrapper(filename, &actual_path, STREAM_OPEN_FOR_INCLUDE); |
539 | 17 | if (wrapper == &php_plain_files_wrapper) { |
540 | 10 | if ((resolved_path = tsrm_realpath_str(actual_path))) { |
541 | 0 | return resolved_path; |
542 | 0 | } |
543 | 10 | } |
544 | 17 | return NULL; |
545 | 17 | } |
546 | | |
547 | 46.6k | if ((*filename == '.' && |
548 | 20 | (IS_SLASH(filename[1]) || |
549 | 17 | ((filename[1] == '.') && IS_SLASH(filename[2])))) || |
550 | 46.6k | IS_ABSOLUTE_PATH(filename, filename_length) || |
551 | | #ifdef PHP_WIN32 |
552 | | /* This should count as an absolute local path as well, however |
553 | | IS_ABSOLUTE_PATH doesn't care about this path form till now. It |
554 | | might be a big thing to extend, thus just a local handling for |
555 | | now. */ |
556 | | (filename_length >=2 && IS_SLASH(filename[0]) && !IS_SLASH(filename[1])) || |
557 | | #endif |
558 | 2.22k | !path || |
559 | 44.4k | !*path) { |
560 | 44.4k | return tsrm_realpath_str(filename); |
561 | 44.4k | } |
562 | | |
563 | 2.22k | ptr = path; |
564 | 4.45k | while (ptr && *ptr) { |
565 | | /* Check for stream wrapper */ |
566 | 2.23k | int is_stream_wrapper = 0; |
567 | | |
568 | 10.3k | for (p = ptr; isalnum((unsigned char)*p) || *p == '+' || *p == '-' || *p == '.'; p++); |
569 | 2.23k | if ((*p == ':') && (p - ptr > 1) && (p[1] == '/') && (p[2] == '/')) { |
570 | | /* .:// or ..:// is not a stream wrapper */ |
571 | 1.98k | if (p[-1] != '.' || p[-2] != '.' || p - 2 != ptr) { |
572 | 1.98k | p += 3; |
573 | 1.98k | is_stream_wrapper = 1; |
574 | 1.98k | } |
575 | 1.98k | } |
576 | 2.23k | end = strchr(p, DEFAULT_DIR_SEPARATOR); |
577 | 2.23k | if (end) { |
578 | 249 | if (filename_length > (MAXPATHLEN - 2) || (end-ptr) > MAXPATHLEN || (end-ptr) + 1 + filename_length + 1 >= MAXPATHLEN) { |
579 | 0 | ptr = end + 1; |
580 | 0 | continue; |
581 | 0 | } |
582 | 249 | memcpy(trypath, ptr, end-ptr); |
583 | 249 | trypath[end-ptr] = '/'; |
584 | 249 | memcpy(trypath+(end-ptr)+1, filename, filename_length+1); |
585 | 249 | ptr = end+1; |
586 | 1.98k | } else { |
587 | 1.98k | size_t len = strlen(ptr); |
588 | | |
589 | 1.98k | if (filename_length > (MAXPATHLEN - 2) || len > MAXPATHLEN || len + 1 + filename_length + 1 >= MAXPATHLEN) { |
590 | 0 | break; |
591 | 0 | } |
592 | 1.98k | memcpy(trypath, ptr, len); |
593 | 1.98k | trypath[len] = '/'; |
594 | 1.98k | memcpy(trypath+len+1, filename, filename_length+1); |
595 | 1.98k | ptr = NULL; |
596 | 1.98k | } |
597 | 2.23k | actual_path = trypath; |
598 | 2.23k | if (is_stream_wrapper) { |
599 | 1.98k | wrapper = php_stream_locate_url_wrapper(trypath, &actual_path, STREAM_OPEN_FOR_INCLUDE); |
600 | 1.98k | if (!wrapper) { |
601 | 0 | continue; |
602 | 1.98k | } else if (wrapper != &php_plain_files_wrapper) { |
603 | 1.98k | if (wrapper->wops->url_stat) { |
604 | 1.98k | php_stream_statbuf ssb; |
605 | | |
606 | 1.98k | if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, PHP_STREAM_URL_STAT_QUIET, &ssb, NULL)) { |
607 | 0 | return zend_string_init(trypath, strlen(trypath), 0); |
608 | 0 | } |
609 | 1.98k | if (EG(exception)) { |
610 | 0 | return NULL; |
611 | 0 | } |
612 | 1.98k | } |
613 | 1.98k | continue; |
614 | 1.98k | } |
615 | 1.98k | } |
616 | 251 | if ((resolved_path = tsrm_realpath_str(actual_path))) { |
617 | 1 | return resolved_path; |
618 | 1 | } |
619 | 251 | } /* end provided path */ |
620 | | |
621 | | /* check in calling scripts' current working directory as a fallback case |
622 | | */ |
623 | 2.22k | if (zend_is_executing() && |
624 | 247 | (exec_filename = zend_get_executed_filename_ex()) != NULL) { |
625 | 247 | const char *exec_fname = ZSTR_VAL(exec_filename); |
626 | 247 | size_t exec_fname_length = ZSTR_LEN(exec_filename); |
627 | | |
628 | 2.71k | while (exec_fname_length > 0) { |
629 | 2.71k | --exec_fname_length; |
630 | 2.71k | if (IS_SLASH(exec_fname[exec_fname_length])) { |
631 | 247 | break; |
632 | 247 | } |
633 | 2.71k | } |
634 | | |
635 | 247 | if (exec_fname_length > 0 && |
636 | 247 | filename_length < (MAXPATHLEN - 2) && |
637 | 247 | exec_fname_length + 1 + filename_length + 1 < MAXPATHLEN) { |
638 | 247 | memcpy(trypath, exec_fname, exec_fname_length + 1); |
639 | 247 | memcpy(trypath+exec_fname_length + 1, filename, filename_length+1); |
640 | 247 | actual_path = trypath; |
641 | | |
642 | | /* Check for stream wrapper */ |
643 | 247 | for (p = trypath; isalnum((unsigned char)*p) || *p == '+' || *p == '-' || *p == '.'; p++); |
644 | 247 | if ((*p == ':') && (p - trypath > 1) && (p[1] == '/') && (p[2] == '/')) { |
645 | 0 | wrapper = php_stream_locate_url_wrapper(trypath, &actual_path, STREAM_OPEN_FOR_INCLUDE); |
646 | 0 | if (!wrapper) { |
647 | 0 | return NULL; |
648 | 0 | } else if (wrapper != &php_plain_files_wrapper) { |
649 | 0 | if (wrapper->wops->url_stat) { |
650 | 0 | php_stream_statbuf ssb; |
651 | |
|
652 | 0 | if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, PHP_STREAM_URL_STAT_QUIET, &ssb, NULL)) { |
653 | 0 | return zend_string_init(trypath, strlen(trypath), 0); |
654 | 0 | } |
655 | 0 | if (EG(exception)) { |
656 | 0 | return NULL; |
657 | 0 | } |
658 | 0 | } |
659 | 0 | return NULL; |
660 | 0 | } |
661 | 0 | } |
662 | | |
663 | 247 | return tsrm_realpath_str(actual_path); |
664 | 247 | } |
665 | 247 | } |
666 | | |
667 | 1.98k | return NULL; |
668 | 2.22k | } |
669 | | /* }}} */ |
670 | | |
671 | | /* {{{ php_fopen_with_path |
672 | | * Tries to open a file with a PATH-style list of directories. |
673 | | * If the filename starts with "." or "/", the path is ignored. |
674 | | */ |
675 | | PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, zend_string **opened_path) |
676 | 32 | { |
677 | 32 | char *pathbuf, *ptr, *end; |
678 | 32 | char trypath[MAXPATHLEN]; |
679 | 32 | FILE *fp; |
680 | 32 | size_t filename_length; |
681 | 32 | zend_string *exec_filename; |
682 | | |
683 | 32 | if (opened_path) { |
684 | 32 | *opened_path = NULL; |
685 | 32 | } |
686 | | |
687 | 32 | if (!filename) { |
688 | 0 | return NULL; |
689 | 0 | } |
690 | | |
691 | 32 | filename_length = strlen(filename); |
692 | 32 | #ifndef PHP_WIN32 |
693 | 32 | (void) filename_length; |
694 | 32 | #endif |
695 | | |
696 | | /* Relative path open */ |
697 | 32 | if ((*filename == '.') |
698 | | /* Absolute path open */ |
699 | 32 | || IS_ABSOLUTE_PATH(filename, filename_length) |
700 | 32 | || (!path || !*path) |
701 | 32 | ) { |
702 | 0 | return php_fopen_and_set_opened_path(filename, mode, opened_path); |
703 | 0 | } |
704 | | |
705 | | /* check in provided path */ |
706 | | /* append the calling scripts' current working directory |
707 | | * as a fallback case |
708 | | */ |
709 | 32 | if (zend_is_executing() && |
710 | 0 | (exec_filename = zend_get_executed_filename_ex()) != NULL) { |
711 | 0 | const char *exec_fname = ZSTR_VAL(exec_filename); |
712 | 0 | size_t exec_fname_length = ZSTR_LEN(exec_filename); |
713 | |
|
714 | 0 | while ((--exec_fname_length < SIZE_MAX) && !IS_SLASH(exec_fname[exec_fname_length])); |
715 | 0 | if ((exec_fname && exec_fname[0] == '[') || exec_fname_length <= 0) { |
716 | | /* [no active file] or no path */ |
717 | 0 | pathbuf = estrdup(path); |
718 | 0 | } else { |
719 | 0 | size_t path_length = strlen(path); |
720 | |
|
721 | 0 | pathbuf = (char *) emalloc(exec_fname_length + path_length + 1 + 1); |
722 | 0 | memcpy(pathbuf, path, path_length); |
723 | 0 | pathbuf[path_length] = DEFAULT_DIR_SEPARATOR; |
724 | 0 | memcpy(pathbuf + path_length + 1, exec_fname, exec_fname_length); |
725 | 0 | pathbuf[path_length + exec_fname_length + 1] = '\0'; |
726 | 0 | } |
727 | 32 | } else { |
728 | 32 | pathbuf = estrdup(path); |
729 | 32 | } |
730 | | |
731 | 32 | ptr = pathbuf; |
732 | | |
733 | 96 | while (ptr && *ptr) { |
734 | 64 | end = strchr(ptr, DEFAULT_DIR_SEPARATOR); |
735 | 64 | if (end != NULL) { |
736 | 32 | *end = '\0'; |
737 | 32 | end++; |
738 | 32 | } |
739 | 64 | if (snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename) >= MAXPATHLEN) { |
740 | 0 | php_error_docref(NULL, E_NOTICE, "%s/%s path was truncated to %d", ptr, filename, MAXPATHLEN); |
741 | 0 | } |
742 | 64 | fp = php_fopen_and_set_opened_path(trypath, mode, opened_path); |
743 | 64 | if (fp) { |
744 | 0 | efree(pathbuf); |
745 | 0 | return fp; |
746 | 0 | } |
747 | 64 | ptr = end; |
748 | 64 | } /* end provided path */ |
749 | | |
750 | 32 | efree(pathbuf); |
751 | 32 | return NULL; |
752 | 32 | } |
753 | | /* }}} */ |
754 | | |
755 | | /* {{{ php_strip_url_passwd */ |
756 | | PHPAPI char *php_strip_url_passwd(char *url) |
757 | 339 | { |
758 | 339 | char *p, *url_start; |
759 | | |
760 | 339 | if (url == NULL) { |
761 | 0 | return ""; |
762 | 0 | } |
763 | | |
764 | 339 | p = url; |
765 | | |
766 | 4.04k | while (*p) { |
767 | 3.77k | if (*p == ':' && *(p + 1) == '/' && *(p + 2) == '/') { |
768 | | /* found protocol */ |
769 | 75 | url_start = p = p + 3; |
770 | | |
771 | 455 | while (*p) { |
772 | 425 | if (*p == '@') { |
773 | 45 | int i; |
774 | | |
775 | 56 | for (i = 0; i < 3 && url_start < p; i++, url_start++) { |
776 | 11 | *url_start = '.'; |
777 | 11 | } |
778 | 211 | for (; *p; p++) { |
779 | 166 | *url_start++ = *p; |
780 | 166 | } |
781 | 45 | *url_start=0; |
782 | 45 | break; |
783 | 45 | } |
784 | 380 | p++; |
785 | 380 | } |
786 | 75 | return url; |
787 | 75 | } |
788 | 3.70k | p++; |
789 | 3.70k | } |
790 | 264 | return url; |
791 | 339 | } |
792 | | /* }}} */ |
793 | | |
794 | | /* {{{ expand_filepath */ |
795 | | PHPAPI char *expand_filepath(const char *filepath, char *real_path) |
796 | 609 | { |
797 | 609 | return expand_filepath_ex(filepath, real_path, NULL, 0); |
798 | 609 | } |
799 | | /* }}} */ |
800 | | |
801 | | /* {{{ expand_filepath_ex */ |
802 | | PHPAPI char *expand_filepath_ex(const char *filepath, char *real_path, const char *relative_to, size_t relative_to_len) |
803 | 609 | { |
804 | 609 | return expand_filepath_with_mode(filepath, real_path, relative_to, relative_to_len, CWD_FILEPATH); |
805 | 609 | } |
806 | | /* }}} */ |
807 | | |
808 | | /* {{{ expand_filepath_use_realpath */ |
809 | | PHPAPI char *expand_filepath_with_mode(const char *filepath, char *real_path, const char *relative_to, size_t relative_to_len, int realpath_mode) |
810 | 609 | { |
811 | 609 | cwd_state new_state; |
812 | 609 | char cwd[MAXPATHLEN]; |
813 | 609 | size_t copy_len; |
814 | 609 | size_t path_len; |
815 | | |
816 | 609 | if (!filepath[0]) { |
817 | 0 | return NULL; |
818 | 0 | } |
819 | | |
820 | 609 | path_len = strlen(filepath); |
821 | | |
822 | 609 | if (IS_ABSOLUTE_PATH(filepath, path_len)) { |
823 | 354 | cwd[0] = '\0'; |
824 | 354 | } else { |
825 | 255 | const char *iam = SG(request_info).path_translated; |
826 | 255 | const char *result; |
827 | 255 | if (relative_to) { |
828 | 0 | if (relative_to_len > MAXPATHLEN-1U) { |
829 | 0 | return NULL; |
830 | 0 | } |
831 | 0 | result = relative_to; |
832 | 0 | memcpy(cwd, relative_to, relative_to_len+1U); |
833 | 255 | } else { |
834 | 255 | result = VCWD_GETCWD(cwd, MAXPATHLEN); |
835 | 255 | } |
836 | | |
837 | 255 | if (!result && (iam != filepath)) { |
838 | 0 | int fdtest = -1; |
839 | |
|
840 | 0 | fdtest = VCWD_OPEN(filepath, O_RDONLY); |
841 | 0 | if (fdtest != -1) { |
842 | | /* return a relative file path if for any reason |
843 | | * we cannot getcwd() and the requested, |
844 | | * relatively referenced file is accessible */ |
845 | 0 | copy_len = path_len > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : path_len; |
846 | 0 | if (real_path) { |
847 | 0 | memcpy(real_path, filepath, copy_len); |
848 | 0 | real_path[copy_len] = '\0'; |
849 | 0 | } else { |
850 | 0 | real_path = estrndup(filepath, copy_len); |
851 | 0 | } |
852 | 0 | close(fdtest); |
853 | 0 | return real_path; |
854 | 0 | } else { |
855 | 0 | cwd[0] = '\0'; |
856 | 0 | } |
857 | 255 | } else if (!result) { |
858 | 0 | cwd[0] = '\0'; |
859 | 0 | } |
860 | 255 | } |
861 | | |
862 | 609 | new_state.cwd = estrdup(cwd); |
863 | 609 | new_state.cwd_length = strlen(cwd); |
864 | | |
865 | 609 | if (virtual_file_ex(&new_state, filepath, NULL, realpath_mode)) { |
866 | 0 | efree(new_state.cwd); |
867 | 0 | return NULL; |
868 | 0 | } |
869 | | |
870 | 609 | if (real_path) { |
871 | 609 | copy_len = new_state.cwd_length > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : new_state.cwd_length; |
872 | 609 | memcpy(real_path, new_state.cwd, copy_len); |
873 | 609 | real_path[copy_len] = '\0'; |
874 | 609 | } else { |
875 | 0 | real_path = estrndup(new_state.cwd, new_state.cwd_length); |
876 | 0 | } |
877 | 609 | efree(new_state.cwd); |
878 | | |
879 | 609 | return real_path; |
880 | 609 | } |
881 | | /* }}} */ |