cvs: php4 /main fopen_wrappers.c fopen_wrappers.h

From: Date: Fri, 01 Feb 2002 20:04:15 +0000
Subject: cvs: php4 /main fopen_wrappers.c fopen_wrappers.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-9235@lists.php.net to get a copy of this message
jflemer Fri Feb 1 15:04:15 2002 EDT Modified files: /php4/main fopen_wrappers.c fopen_wrappers.h Log: Changed php.ini directive 'safe_mode_include_dir' to accept a (semi)colon separated path, rather than a single directory. Also moved checking of said path into a separate path for code readability. @- Changed php.ini directive 'safe_mode_include_dir' to accept a @ (semi)colon separated path (like 'include_path') rather than @ a single directory. (jflemer) Index: php4/main/fopen_wrappers.c diff -u php4/main/fopen_wrappers.c:1.140 php4/main/fopen_wrappers.c:1.141 --- php4/main/fopen_wrappers.c:1.140 Sat Dec 22 19:46:13 2001 +++ php4/main/fopen_wrappers.c Fri Feb 1 15:04:14 2002 @@ -16,7 +16,7 @@ | Jim Winstead <jimw@php.net> | +----------------------------------------------------------------------+ */ -/* $Id: fopen_wrappers.c,v 1.140 2001/12/23 00:46:13 alexwaugh Exp $ */ +/* $Id: fopen_wrappers.c,v 1.141 2002/02/01 20:04:14 jflemer Exp $ */ /* {{{ includes */ @@ -221,6 +221,57 @@ } /* }}} */ +/* {{{ php_check_safe_mode_include_dir + */ +PHPAPI int php_check_safe_mode_include_dir(char *path TSRMLS_DC) +{ + /* Only check when safe_mode on and safe_mode_include_dir is available */ + if (PG(safe_mode) && PG(safe_mode_include_dir) && + *PG(safe_mode_include_dir)) + { + char *pathbuf; + char *ptr; + char *end; + char resolved_name[MAXPATHLEN]; + + /* Resolve the real path into resolved_name */ + if (expand_filepath(path, resolved_name TSRMLS_CC) == NULL) + return -1; + + pathbuf = estrdup(PG(safe_mode_include_dir)); + + ptr = pathbuf; + + while (ptr && *ptr) { + end = strchr(ptr, DEFAULT_DIR_SEPARATOR); + if (end != NULL) { + *end = '\0'; + end++; + } + + /* Check the path */ +#ifdef PHP_WIN32 + if (strncasecmp(ptr, resolved_name, strlen(ptr)) == 0) +#else + if (strncmp(ptr, resolved_name, strlen(ptr)) == 0) +#endif + { + /* File is in the right directory */ + efree(pathbuf); + return 0; + } + + ptr = end; + } + efree(pathbuf); + return -1; + } + + /* Nothing to check... */ + return 0; +} +/* }}} */ + /* {{{ php_fopen_and_set_opened_path */ static FILE *php_fopen_and_set_opened_path(const char *path, char *mode, char **opened_path TSRMLS_DC) @@ -375,13 +426,10 @@ char *pathbuf, *ptr, *end; char *exec_fname; char trypath[MAXPATHLEN]; - char trydir[MAXPATHLEN]; - char safe_mode_include_dir[MAXPATHLEN]; struct stat sb; FILE *fp; int path_length; int filename_length; - int safe_mode_include_dir_length; int exec_fname_length; if (opened_path) { @@ -406,32 +454,16 @@ * files in safe_mode_include_dir (or subdir) are excluded from * safe mode GID/UID checks */ - *safe_mode_include_dir = 0; - safe_mode_include_dir_length = 0; - if(PG(safe_mode_include_dir) && VCWD_REALPATH(PG(safe_mode_include_dir), safe_mode_include_dir)) { - safe_mode_include_dir_length = strlen(safe_mode_include_dir); - } /* Absolute path open */ if (IS_ABSOLUTE_PATH(filename, filename_length)) { - /* Check to see if file is in safe_mode_include_dir (or subdir) */ - if (PG(safe_mode) && *safe_mode_include_dir && VCWD_REALPATH(filename, trypath)) { -#ifdef PHP_WIN32 - if (strncasecmp(safe_mode_include_dir, trypath, safe_mode_include_dir_length) == 0) -#else - if (strncmp(safe_mode_include_dir, trypath, safe_mode_include_dir_length) == 0) -#endif - { - /* absolute path matches safe_mode_include_dir */ - fp = php_fopen_and_set_opened_path(trypath, mode, opened_path TSRMLS_CC); - if (fp) { - return fp; - } - } - } - if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) { + if ((php_check_safe_mode_include_dir(filename)) == 0) + /* filename is in safe_mode_include_dir (or subdir) */ + return php_fopen_and_set_opened_path(filename, mode, opened_path TSRMLS_CC); + + if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) return NULL; - } + return php_fopen_and_set_opened_path(filename, mode, opened_path TSRMLS_CC); } @@ -476,26 +508,18 @@ end++; } snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename); - /* Check to see trypath is in safe_mode_include_dir (or subdir) */ - if (PG(safe_mode) && *safe_mode_include_dir && VCWD_REALPATH(trypath, trydir)) { -#ifdef PHP_WIN32 - if (strncasecmp(safe_mode_include_dir, trydir, safe_mode_include_dir_length) == 0) -#else - if (strncmp(safe_mode_include_dir, trydir, safe_mode_include_dir_length) == 0) -#endif - { - /* trypath is in safe_mode_include_dir */ - fp = php_fopen_and_set_opened_path(trydir, mode, opened_path TSRMLS_CC); - if (fp) { - efree(pathbuf); - return fp; - } - } - } if (PG(safe_mode)) { - if (VCWD_STAT(trypath, &sb) == 0 && (!php_checkuid(trypath, mode, CHECKUID_CHECK_MODE_PARAM))) { + if (VCWD_STAT(trypath, &sb) == 0) { + /* file exists ... check permission */ + if ((php_check_safe_mode_include_dir(trypath) == 0) || + php_checkuid(trypath, mode, CHECKUID_CHECK_MODE_PARAM)) + /* UID ok, or trypath is in safe_mode_include_dir */ + fp = php_fopen_and_set_opened_path(trypath, mode, opened_path TSRMLS_CC); + else + fp = NULL; + efree(pathbuf); - return NULL; + return fp; } } fp = php_fopen_and_set_opened_path(trypath, mode, opened_path TSRMLS_CC); Index: php4/main/fopen_wrappers.h diff -u php4/main/fopen_wrappers.h:1.31 php4/main/fopen_wrappers.h:1.32 --- php4/main/fopen_wrappers.h:1.31 Tue Dec 11 10:31:02 2001 +++ php4/main/fopen_wrappers.h Fri Feb 1 15:04:14 2002 @@ -15,7 +15,7 @@ | Authors: Jim Winstead <jimw@php.net> | +----------------------------------------------------------------------+ */ -/* $Id: fopen_wrappers.h,v 1.31 2001/12/11 15:31:02 sebastian Exp $ */ +/* $Id: fopen_wrappers.h,v 1.32 2002/02/01 20:04:14 jflemer Exp $ */ #ifndef FOPEN_WRAPPERS_H #define FOPEN_WRAPPERS_H @@ -73,6 +73,8 @@ PHPAPI int php_check_open_basedir(char *path TSRMLS_DC); PHPAPI int php_check_specific_open_basedir(char *basedir, char *path TSRMLS_DC); + +PHPAPI int php_check_safe_mode_include_dir(char *path TSRMLS_DC); PHPAPI FILE *php_fopen_with_path(char *filename, char *mode, char *path, char **opened_path TSRMLS_DC);

« previous php.cvs (#9235) next »