1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
16 : | Jim Winstead <jimw@php.net> |
17 : +----------------------------------------------------------------------+
18 : */
19 :
20 : /* $Id: fopen_wrappers.c 288246 2009-09-10 16:19:42Z jani $ */
21 :
22 : /* {{{ includes
23 : */
24 : #include "php.h"
25 : #include "php_globals.h"
26 : #include "SAPI.h"
27 :
28 : #include <stdio.h>
29 : #include <stdlib.h>
30 : #include <errno.h>
31 : #include <sys/types.h>
32 : #include <sys/stat.h>
33 : #include <fcntl.h>
34 :
35 : #ifdef PHP_WIN32
36 : #define O_RDONLY _O_RDONLY
37 : #include "win32/param.h"
38 : #else
39 : #include <sys/param.h>
40 : #endif
41 :
42 : #include "ext/standard/head.h"
43 : #include "ext/standard/php_standard.h"
44 : #include "zend_compile.h"
45 : #include "php_network.h"
46 :
47 : #if HAVE_PWD_H
48 : #include <pwd.h>
49 : #endif
50 :
51 : #include <sys/types.h>
52 : #if HAVE_SYS_SOCKET_H
53 : #include <sys/socket.h>
54 : #endif
55 :
56 : #ifndef S_ISREG
57 : #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
58 : #endif
59 :
60 : #ifdef PHP_WIN32
61 : #include <winsock2.h>
62 : #elif defined(NETWARE) && defined(USE_WINSOCK)
63 : #include <novsock2.h>
64 : #else
65 : #include <netinet/in.h>
66 : #include <netdb.h>
67 : #if HAVE_ARPA_INET_H
68 : #include <arpa/inet.h>
69 : #endif
70 : #endif
71 :
72 : #if defined(PHP_WIN32) || defined(__riscos__) || defined(NETWARE)
73 : #undef AF_UNIX
74 : #endif
75 :
76 : #if defined(AF_UNIX)
77 : #include <sys/un.h>
78 : #endif
79 : /* }}} */
80 :
81 : /* {{{ OnUpdateBaseDir
82 : Allows any change to open_basedir setting in during Startup and Shutdown events,
83 : or a tightening during activation/runtime/deactivation */
84 : PHPAPI ZEND_INI_MH(OnUpdateBaseDir)
85 17010 : {
86 : char **p, *pathbuf, *ptr, *end;
87 : #ifndef ZTS
88 17010 : char *base = (char *) mh_arg2;
89 : #else
90 : char *base = (char *) ts_resource(*((int *) mh_arg2));
91 : #endif
92 :
93 17010 : p = (char **) (base + (size_t) mh_arg1);
94 :
95 17010 : if (stage == PHP_INI_STAGE_STARTUP || stage == PHP_INI_STAGE_SHUTDOWN || stage == PHP_INI_STAGE_ACTIVATE || stage == PHP_INI_STAGE_DEACTIVATE) {
96 : /* We're in a PHP_INI_SYSTEM context, no restrictions */
97 17008 : *p = new_value;
98 17008 : return SUCCESS;
99 : }
100 :
101 : /* Otherwise we're in runtime */
102 2 : if (!*p || !**p) {
103 : /* open_basedir not set yet, go ahead and give it a value */
104 0 : *p = new_value;
105 0 : return SUCCESS;
106 : }
107 :
108 : /* Shortcut: When we have a open_basedir and someone tries to unset, we know it'll fail */
109 2 : if (!new_value || !*new_value) {
110 0 : return FAILURE;
111 : }
112 :
113 : /* Is the proposed open_basedir at least as restrictive as the current setting? */
114 2 : ptr = pathbuf = estrdup(new_value);
115 5 : while (ptr && *ptr) {
116 2 : end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
117 2 : if (end != NULL) {
118 0 : *end = '\0';
119 0 : end++;
120 : }
121 2 : if (php_check_open_basedir_ex(ptr, 0 TSRMLS_CC) != 0) {
122 : /* At least one portion of this open_basedir is less restrictive than the prior one, FAIL */
123 1 : efree(pathbuf);
124 1 : return FAILURE;
125 : }
126 1 : ptr = end;
127 : }
128 1 : efree(pathbuf);
129 :
130 : /* Everything checks out, set it */
131 1 : *p = new_value;
132 :
133 1 : return SUCCESS;
134 : }
135 : /* }}} */
136 :
137 :
138 : /* {{{ php_check_specific_open_basedir
139 : When open_basedir is not NULL, check if the given filename is located in
140 : open_basedir. Returns -1 if error or not in the open_basedir, else 0.
141 : When open_basedir is NULL, always return 0.
142 : */
143 : PHPAPI int php_check_specific_open_basedir(const char *basedir, const char *path TSRMLS_DC)
144 1057 : {
145 : char resolved_name[MAXPATHLEN];
146 : char resolved_basedir[MAXPATHLEN];
147 : char local_open_basedir[MAXPATHLEN];
148 : char path_tmp[MAXPATHLEN];
149 : char *path_file;
150 : int resolved_basedir_len;
151 : int resolved_name_len;
152 : int path_len;
153 1057 : int nesting_level = 0;
154 :
155 : /* Special case basedir==".": Use script-directory */
156 1057 : if (strcmp(basedir, ".") || !VCWD_GETCWD(local_open_basedir, MAXPATHLEN)) {
157 : /* Else use the unmodified path */
158 4 : strlcpy(local_open_basedir, basedir, sizeof(local_open_basedir));
159 : }
160 :
161 1057 : path_len = strlen(path);
162 1057 : if (path_len > (MAXPATHLEN - 1)) {
163 : /* empty and too long paths are invalid */
164 0 : return -1;
165 : }
166 :
167 : /* normalize and expand path */
168 1057 : if (expand_filepath(path, resolved_name TSRMLS_CC) == NULL) {
169 1 : return -1;
170 : }
171 :
172 1056 : path_len = strlen(resolved_name);
173 1056 : memcpy(path_tmp, resolved_name, path_len + 1); /* safe */
174 :
175 2442 : while (VCWD_REALPATH(path_tmp, resolved_name) == NULL) {
176 : #ifdef HAVE_SYMLINK
177 330 : if (nesting_level == 0) {
178 : int ret;
179 : char buf[MAXPATHLEN];
180 :
181 330 : ret = readlink(path_tmp, buf, MAXPATHLEN - 1);
182 330 : if (ret < 0) {
183 : /* not a broken symlink, move along.. */
184 : } else {
185 : /* put the real path into the path buffer */
186 0 : memcpy(path_tmp, buf, ret);
187 0 : path_tmp[ret] = '\0';
188 : }
189 : }
190 : #endif
191 :
192 : #if defined(PHP_WIN32) || defined(NETWARE)
193 : path_file = strrchr(path_tmp, DEFAULT_SLASH);
194 : if (!path_file) {
195 : path_file = strrchr(path_tmp, '/');
196 : }
197 : #else
198 330 : path_file = strrchr(path_tmp, DEFAULT_SLASH);
199 : #endif
200 330 : if (!path_file) {
201 : /* none of the path components exist. definitely not in open_basedir.. */
202 0 : return -1;
203 : } else {
204 330 : path_len = path_file - path_tmp + 1;
205 : #if defined(PHP_WIN32) || defined(NETWARE)
206 : if (path_len > 1 && path_tmp[path_len - 2] == ':') {
207 : if (path_len != 3) {
208 : return -1;
209 : }
210 : /* this is c:\ */
211 : path_tmp[path_len] = '\0';
212 : } else {
213 : path_tmp[path_len - 1] = '\0';
214 : }
215 : #else
216 330 : path_tmp[path_len - 1] = '\0';
217 : #endif
218 : }
219 330 : nesting_level++;
220 : }
221 :
222 : /* Resolve open_basedir to resolved_basedir */
223 1056 : if (expand_filepath(local_open_basedir, resolved_basedir TSRMLS_CC) != NULL) {
224 : /* Handler for basedirs that end with a / */
225 1056 : resolved_basedir_len = strlen(resolved_basedir);
226 1056 : if (basedir[strlen(basedir) - 1] == PHP_DIR_SEPARATOR) {
227 1 : if (resolved_basedir[resolved_basedir_len - 1] != PHP_DIR_SEPARATOR) {
228 0 : resolved_basedir[resolved_basedir_len] = PHP_DIR_SEPARATOR;
229 0 : resolved_basedir[++resolved_basedir_len] = '\0';
230 : }
231 : }
232 :
233 1056 : resolved_name_len = strlen(resolved_name);
234 1056 : if (path_tmp[path_len - 1] == PHP_DIR_SEPARATOR) {
235 68 : if (resolved_name[resolved_name_len - 1] != PHP_DIR_SEPARATOR) {
236 36 : resolved_name[resolved_name_len] = PHP_DIR_SEPARATOR;
237 36 : resolved_name[++resolved_name_len] = '\0';
238 : }
239 : }
240 :
241 : /* Check the path */
242 : #if defined(PHP_WIN32) || defined(NETWARE)
243 : if (strncasecmp(resolved_basedir, resolved_name, resolved_basedir_len) == 0) {
244 : #else
245 1056 : if (strncmp(resolved_basedir, resolved_name, resolved_basedir_len) == 0) {
246 : #endif
247 : /* File is in the right directory */
248 727 : return 0;
249 : } else {
250 : /* /openbasedir/ and /openbasedir are the same directory */
251 329 : if (resolved_basedir_len == (resolved_name_len + 1) && resolved_basedir[resolved_basedir_len - 1] == PHP_DIR_SEPARATOR) {
252 : #if defined(PHP_WIN32) || defined(NETWARE)
253 : if (strncasecmp(resolved_basedir, resolved_name, resolved_name_len) == 0) {
254 : #else
255 0 : if (strncmp(resolved_basedir, resolved_name, resolved_name_len) == 0) {
256 : #endif
257 0 : return 0;
258 : }
259 : }
260 329 : return -1;
261 : }
262 : } else {
263 : /* Unable to resolve the real path, return -1 */
264 0 : return -1;
265 : }
266 : }
267 : /* }}} */
268 :
269 : PHPAPI int php_check_open_basedir(const char *path TSRMLS_DC)
270 436566 : {
271 436566 : return php_check_open_basedir_ex(path, 1 TSRMLS_CC);
272 : }
273 :
274 : /* {{{ php_check_open_basedir
275 : */
276 : PHPAPI int php_check_open_basedir_ex(const char *path, int warn TSRMLS_DC)
277 519637 : {
278 : /* Only check when open_basedir is available */
279 519637 : if (PG(open_basedir) && *PG(open_basedir)) {
280 : char *pathbuf;
281 : char *ptr;
282 : char *end;
283 :
284 1057 : pathbuf = estrdup(PG(open_basedir));
285 :
286 1057 : ptr = pathbuf;
287 :
288 2444 : while (ptr && *ptr) {
289 1057 : end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
290 1057 : if (end != NULL) {
291 0 : *end = '\0';
292 0 : end++;
293 : }
294 :
295 1057 : if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {
296 727 : efree(pathbuf);
297 727 : return 0;
298 : }
299 :
300 330 : ptr = end;
301 : }
302 330 : if (warn) {
303 311 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "open_basedir restriction in effect. File(%s) is not within the allowed path(s): (%s)", path, PG(open_basedir));
304 : }
305 330 : efree(pathbuf);
306 330 : errno = EPERM; /* we deny permission to open it */
307 330 : return -1;
308 : }
309 :
310 : /* Nothing to check... */
311 518580 : return 0;
312 : }
313 : /* }}} */
314 :
315 :
316 : /* {{{ php_fopen_and_set_opened_path
317 : */
318 : static FILE *php_fopen_and_set_opened_path(const char *path, const char *mode, char **opened_path TSRMLS_DC)
319 68158 : {
320 : FILE *fp;
321 :
322 68158 : if (php_check_open_basedir((char *)path TSRMLS_CC)) {
323 0 : return NULL;
324 : }
325 68158 : fp = VCWD_FOPEN(path, mode);
326 68158 : if (fp && opened_path) {
327 16894 : *opened_path = expand_filepath(path, NULL TSRMLS_CC);
328 : }
329 68158 : return fp;
330 : }
331 : /* }}} */
332 :
333 : /* {{{ php_fopen_primary_script
334 : */
335 : PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle TSRMLS_DC)
336 322 : {
337 : FILE *fp;
338 : #ifndef PHP_WIN32
339 : struct stat st;
340 : #endif
341 : char *path_info, *filename;
342 : int length;
343 :
344 322 : filename = SG(request_info).path_translated;
345 322 : path_info = SG(request_info).request_uri;
346 : #if HAVE_PWD_H
347 322 : if (PG(user_dir) && *PG(user_dir) && path_info && '/' == path_info[0] && '~' == path_info[1]) {
348 0 : char *s = strchr(path_info + 2, '/');
349 :
350 0 : filename = NULL; /* discard the original filename, it must not be used */
351 0 : if (s) { /* if there is no path name after the file, do not bother */
352 : char user[32]; /* to try open the directory */
353 : struct passwd *pw;
354 : #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
355 : struct passwd pwstruc;
356 : long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
357 : char *pwbuf;
358 :
359 : if (pwbuflen < 1) {
360 : return FAILURE;
361 : }
362 :
363 : pwbuf = emalloc(pwbuflen);
364 : #endif
365 0 : length = s - (path_info + 2);
366 0 : if (length > (int)sizeof(user) - 1) {
367 0 : length = sizeof(user) - 1;
368 : }
369 0 : memcpy(user, path_info + 2, length);
370 0 : user[length] = '\0';
371 : #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
372 : if (getpwnam_r(user, &pwstruc, pwbuf, pwbuflen, &pw)) {
373 : efree(pwbuf);
374 : return FAILURE;
375 : }
376 : #else
377 0 : pw = getpwnam(user);
378 : #endif
379 0 : if (pw && pw->pw_dir) {
380 0 : spprintf(&filename, 0, "%s%c%s%c%s", pw->pw_dir, PHP_DIR_SEPARATOR, PG(user_dir), PHP_DIR_SEPARATOR, s + 1); /* Safe */
381 0 : STR_FREE(SG(request_info).path_translated);
382 0 : SG(request_info).path_translated = filename;
383 : }
384 : #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
385 : efree(pwbuf);
386 : #endif
387 : }
388 : } else
389 : #endif
390 322 : if (PG(doc_root) && path_info) {
391 0 : length = strlen(PG(doc_root));
392 0 : if (IS_ABSOLUTE_PATH(PG(doc_root), length)) {
393 0 : filename = emalloc(length + strlen(path_info) + 2);
394 0 : if (filename) {
395 0 : memcpy(filename, PG(doc_root), length);
396 0 : if (!IS_SLASH(filename[length - 1])) { /* length is never 0 */
397 0 : filename[length++] = PHP_DIR_SEPARATOR;
398 : }
399 0 : if (IS_SLASH(path_info[0])) {
400 0 : length--;
401 : }
402 0 : strcpy(filename + length, path_info);
403 0 : STR_FREE(SG(request_info).path_translated);
404 0 : SG(request_info).path_translated = filename;
405 : }
406 : }
407 : } /* if doc_root && path_info */
408 :
409 322 : if (filename) {
410 322 : filename = zend_resolve_path(filename, strlen(filename) TSRMLS_CC);
411 : }
412 :
413 322 : if (!filename) {
414 : /* we have to free SG(request_info).path_translated here because
415 : * php_destroy_request_info assumes that it will get
416 : * freed when the include_names hash is emptied, but
417 : * we're not adding it in this case */
418 7 : STR_FREE(SG(request_info).path_translated);
419 7 : SG(request_info).path_translated = NULL;
420 7 : return FAILURE;
421 : }
422 315 : fp = VCWD_FOPEN(filename, "rb");
423 :
424 : #ifndef PHP_WIN32
425 : /* refuse to open anything that is not a regular file */
426 315 : if (fp && (0 > fstat(fileno(fp), &st) || !S_ISREG(st.st_mode))) {
427 0 : fclose(fp);
428 0 : fp = NULL;
429 : }
430 : #endif
431 :
432 315 : if (!fp) {
433 0 : STR_FREE(SG(request_info).path_translated); /* for same reason as above */
434 0 : SG(request_info).path_translated = NULL;
435 0 : return FAILURE;
436 : }
437 :
438 315 : file_handle->opened_path = expand_filepath(filename, NULL TSRMLS_CC);
439 :
440 315 : STR_FREE(SG(request_info).path_translated); /* for same reason as above */
441 315 : SG(request_info).path_translated = filename;
442 :
443 315 : file_handle->filename = SG(request_info).path_translated;
444 315 : file_handle->free_filename = 0;
445 315 : file_handle->handle.fp = fp;
446 315 : file_handle->type = ZEND_HANDLE_FP;
447 :
448 315 : return SUCCESS;
449 : }
450 : /* }}} */
451 :
452 : /* {{{ php_resolve_path
453 : * Returns the realpath for given filename according to include path
454 : */
455 : PHPAPI char *php_resolve_path(const char *filename, int filename_length, const char *path TSRMLS_DC)
456 13417 : {
457 : char resolved_path[MAXPATHLEN];
458 : char trypath[MAXPATHLEN];
459 : const char *ptr, *end, *p;
460 : char *actual_path;
461 : php_stream_wrapper *wrapper;
462 :
463 13417 : if (!filename) {
464 0 : return NULL;
465 : }
466 :
467 : /* Don't resolve paths which contain protocol (except of file://) */
468 13417 : for (p = filename; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++);
469 13417 : if ((*p == ':') && (p - filename > 1) && (p[1] == '/') && (p[2] == '/')) {
470 282 : wrapper = php_stream_locate_url_wrapper(filename, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
471 282 : if (wrapper == &php_plain_files_wrapper) {
472 4 : if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
473 2 : return estrdup(resolved_path);
474 : }
475 : }
476 280 : return NULL;
477 : }
478 :
479 13135 : if ((*filename == '.' &&
480 : (IS_SLASH(filename[1]) ||
481 : ((filename[1] == '.') && IS_SLASH(filename[2])))) ||
482 : IS_ABSOLUTE_PATH(filename, filename_length) ||
483 : !path ||
484 : !*path) {
485 7511 : if (tsrm_realpath(filename, resolved_path TSRMLS_CC)) {
486 7500 : return estrdup(resolved_path);
487 : } else {
488 11 : return NULL;
489 : }
490 : }
491 :
492 5624 : ptr = path;
493 16909 : while (ptr && *ptr) {
494 : /* Check for stream wrapper */
495 5750 : int is_stream_wrapper = 0;
496 :
497 5750 : for (p = ptr; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++);
498 5750 : if ((*p == ':') && (p - ptr > 1) && (p[1] == '/') && (p[2] == '/')) {
499 : /* .:// or ..:// is not a stream wrapper */
500 45 : if (p[-1] != '.' || p[-2] != '.' || p - 2 != ptr) {
501 45 : p += 3;
502 45 : is_stream_wrapper = 1;
503 : }
504 : }
505 5750 : end = strchr(p, DEFAULT_DIR_SEPARATOR);
506 5750 : if (end) {
507 211 : if ((end-ptr) + 1 + filename_length + 1 >= MAXPATHLEN) {
508 0 : ptr = end + 1;
509 0 : continue;
510 : }
511 211 : memcpy(trypath, ptr, end-ptr);
512 211 : trypath[end-ptr] = '/';
513 211 : memcpy(trypath+(end-ptr)+1, filename, filename_length+1);
514 211 : ptr = end+1;
515 : } else {
516 5539 : int len = strlen(ptr);
517 :
518 5539 : if (len + 1 + filename_length + 1 >= MAXPATHLEN) {
519 0 : break;
520 : }
521 5539 : memcpy(trypath, ptr, len);
522 5539 : trypath[len] = '/';
523 5539 : memcpy(trypath+len+1, filename, filename_length+1);
524 5539 : ptr = NULL;
525 : }
526 5750 : actual_path = trypath;
527 5750 : if (is_stream_wrapper) {
528 45 : wrapper = php_stream_locate_url_wrapper(trypath, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
529 45 : if (!wrapper) {
530 0 : continue;
531 45 : } else if (wrapper != &php_plain_files_wrapper) {
532 45 : if (wrapper->wops->url_stat) {
533 : php_stream_statbuf ssb;
534 :
535 45 : if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, 0, &ssb, NULL TSRMLS_CC)) {
536 33 : return estrdup(trypath);
537 : }
538 : }
539 12 : continue;
540 : }
541 : }
542 5705 : if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
543 56 : return estrdup(resolved_path);
544 : }
545 : } /* end provided path */
546 :
547 : /* check in calling scripts' current working directory as a fall back case
548 : */
549 5535 : if (zend_is_executing(TSRMLS_C)) {
550 5528 : char *exec_fname = zend_get_executed_filename(TSRMLS_C);
551 5528 : int exec_fname_length = strlen(exec_fname);
552 :
553 129908 : while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
554 5528 : if (exec_fname && exec_fname[0] != '[' &&
555 : exec_fname_length > 0 &&
556 : exec_fname_length + 1 + filename_length + 1 < MAXPATHLEN) {
557 5528 : memcpy(trypath, exec_fname, exec_fname_length + 1);
558 5528 : memcpy(trypath+exec_fname_length + 1, filename, filename_length+1);
559 5528 : actual_path = trypath;
560 :
561 : /* Check for stream wrapper */
562 5528 : for (p = trypath; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++);
563 5528 : if ((*p == ':') && (p - trypath > 1) && (p[1] == '/') && (p[2] == '/')) {
564 4 : wrapper = php_stream_locate_url_wrapper(trypath, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
565 4 : if (!wrapper) {
566 0 : return NULL;
567 4 : } else if (wrapper != &php_plain_files_wrapper) {
568 4 : if (wrapper->wops->url_stat) {
569 : php_stream_statbuf ssb;
570 :
571 4 : if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, 0, &ssb, NULL TSRMLS_CC)) {
572 2 : return estrdup(trypath);
573 : }
574 : }
575 2 : return NULL;
576 : }
577 : }
578 :
579 5524 : if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
580 5449 : return estrdup(resolved_path);
581 : }
582 : }
583 : }
584 :
585 82 : return NULL;
586 : }
587 : /* }}} */
588 :
589 : /* {{{ php_fopen_with_path
590 : * Tries to open a file with a PATH-style list of directories.
591 : * If the filename starts with "." or "/", the path is ignored.
592 : */
593 : PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path TSRMLS_DC)
594 33788 : {
595 : char *pathbuf, *ptr, *end;
596 : char *exec_fname;
597 : char trypath[MAXPATHLEN];
598 : FILE *fp;
599 : int path_length;
600 : int filename_length;
601 : int exec_fname_length;
602 :
603 33788 : if (opened_path) {
604 33788 : *opened_path = NULL;
605 : }
606 :
607 33788 : if (!filename) {
608 0 : return NULL;
609 : }
610 :
611 33788 : filename_length = strlen(filename);
612 :
613 : /* Relative path open */
614 33788 : if (*filename == '.') {
615 0 : return php_fopen_and_set_opened_path(filename, mode, opened_path TSRMLS_CC);
616 : }
617 :
618 : /* Absolute path open */
619 : /* FIXME: Andi - Do we actually need the if ()? */
620 33788 : if (IS_ABSOLUTE_PATH(filename, filename_length) || (!path || (path && !*path))) {
621 0 : return php_fopen_and_set_opened_path(filename, mode, opened_path TSRMLS_CC);
622 : }
623 :
624 : /* check in provided path */
625 : /* append the calling scripts' current working directory
626 : * as a fall back case
627 : */
628 33788 : if (zend_is_executing(TSRMLS_C)) {
629 0 : exec_fname = zend_get_executed_filename(TSRMLS_C);
630 0 : exec_fname_length = strlen(exec_fname);
631 0 : path_length = strlen(path);
632 :
633 0 : while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
634 0 : if ((exec_fname && exec_fname[0] == '[') || exec_fname_length <= 0) {
635 : /* [no active file] or no path */
636 0 : pathbuf = estrdup(path);
637 : } else {
638 0 : pathbuf = (char *) emalloc(exec_fname_length + path_length + 1 + 1);
639 0 : memcpy(pathbuf, path, path_length);
640 0 : pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;
641 0 : memcpy(pathbuf + path_length + 1, exec_fname, exec_fname_length);
642 0 : pathbuf[path_length + exec_fname_length + 1] = '\0';
643 : }
644 : } else {
645 33788 : pathbuf = estrdup(path);
646 : }
647 :
648 33788 : ptr = pathbuf;
649 :
650 118840 : while (ptr && *ptr) {
651 68158 : end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
652 68158 : if (end != NULL) {
653 34370 : *end = '\0';
654 34370 : end++;
655 : }
656 68158 : if (snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename) >= MAXPATHLEN) {
657 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s/%s path was truncated to %d", ptr, filename, MAXPATHLEN);
658 : }
659 :
660 68158 : fp = php_fopen_and_set_opened_path(trypath, mode, opened_path TSRMLS_CC);
661 68158 : if (fp) {
662 16894 : efree(pathbuf);
663 16894 : return fp;
664 : }
665 51264 : ptr = end;
666 : } /* end provided path */
667 :
668 16894 : efree(pathbuf);
669 16894 : return NULL;
670 : }
671 : /* }}} */
672 :
673 : /* {{{ php_strip_url_passwd
674 : */
675 : PHPAPI char *php_strip_url_passwd(char *url)
676 576 : {
677 : register char *p, *url_start;
678 :
679 576 : if (url == NULL) {
680 0 : return "";
681 : }
682 :
683 576 : p = url;
684 :
685 15586 : while (*p) {
686 14558 : if (*p == ':' && *(p + 1) == '/' && *(p + 2) == '/') {
687 : /* found protocol */
688 124 : url_start = p = p + 3;
689 :
690 6548 : while (*p) {
691 6302 : if (*p == '@') {
692 : int i;
693 :
694 8 : for (i = 0; i < 3 && url_start < p; i++, url_start++) {
695 6 : *url_start = '.';
696 : }
697 24 : for (; *p; p++) {
698 22 : *url_start++ = *p;
699 : }
700 2 : *url_start=0;
701 2 : break;
702 : }
703 6300 : p++;
704 : }
705 124 : return url;
706 : }
707 14434 : p++;
708 : }
709 452 : return url;
710 : }
711 : /* }}} */
712 :
713 : /* {{{ expand_filepath
714 : */
715 : PHPAPI char *expand_filepath(const char *filepath, char *real_path TSRMLS_DC)
716 85463 : {
717 85463 : return expand_filepath_ex(filepath, real_path, NULL, 0 TSRMLS_CC);
718 : }
719 : /* }}} */
720 :
721 : /* {{{ expand_filepath_ex
722 : */
723 : PHPAPI char *expand_filepath_ex(const char *filepath, char *real_path, const char *relative_to, size_t relative_to_len TSRMLS_DC)
724 85635 : {
725 : cwd_state new_state;
726 : char cwd[MAXPATHLEN];
727 : int copy_len;
728 :
729 85635 : if (!filepath[0]) {
730 16 : return NULL;
731 85619 : } else if (IS_ABSOLUTE_PATH(filepath, strlen(filepath))) {
732 84463 : cwd[0] = '\0';
733 : } else {
734 1156 : const char *iam = SG(request_info).path_translated;
735 : const char *result;
736 1156 : if (relative_to) {
737 32 : if (relative_to_len > MAXPATHLEN-1U) {
738 0 : return NULL;
739 : }
740 32 : result = relative_to;
741 32 : memcpy(cwd, relative_to, relative_to_len+1U);
742 : } else {
743 1124 : result = VCWD_GETCWD(cwd, MAXPATHLEN);
744 : }
745 :
746 1156 : if (!result && (iam != filepath)) {
747 0 : int fdtest = -1;
748 :
749 0 : fdtest = VCWD_OPEN(filepath, O_RDONLY);
750 0 : if (fdtest != -1) {
751 : /* return a relative file path if for any reason
752 : * we cannot cannot getcwd() and the requested,
753 : * relatively referenced file is accessible */
754 0 : copy_len = strlen(filepath) > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : strlen(filepath);
755 0 : real_path = estrndup(filepath, copy_len);
756 0 : close(fdtest);
757 0 : return real_path;
758 : } else {
759 0 : cwd[0] = '\0';
760 : }
761 1156 : } else if (!result) {
762 0 : cwd[0] = '\0';
763 : }
764 : }
765 :
766 85619 : new_state.cwd = strdup(cwd);
767 85619 : new_state.cwd_length = strlen(cwd);
768 :
769 85619 : if (virtual_file_ex(&new_state, filepath, NULL, CWD_FILEPATH)) {
770 3 : free(new_state.cwd);
771 3 : return NULL;
772 : }
773 :
774 85616 : if (real_path) {
775 2769 : copy_len = new_state.cwd_length > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : new_state.cwd_length;
776 2769 : memcpy(real_path, new_state.cwd, copy_len);
777 2769 : real_path[copy_len] = '\0';
778 : } else {
779 82847 : real_path = estrndup(new_state.cwd, new_state.cwd_length);
780 : }
781 85616 : free(new_state.cwd);
782 :
783 85616 : return real_path;
784 : }
785 : /* }}} */
786 :
787 : /*
788 : * Local variables:
789 : * tab-width: 4
790 : * c-basic-offset: 4
791 : * End:
792 : * vim600: sw=4 ts=4 fdm=marker
793 : * vim<600: sw=4 ts=4
794 : */
|