Fwd: [PHP-CVS] cvs: php4 /main fopen-wrappers.c fopen-wrappers.h

From: Date: Wed, 16 Aug 2000 19:35:01 +0000
Subject: Fwd: [PHP-CVS] cvs: php4 /main fopen-wrappers.c fopen-wrappers.h
Groups: php.dev 
Request: Send a blank email to php-dev+get-29177@lists.php.net to get a copy of this message
As I mention in my commit message I want to start cleaning up this mess. I think the next positive step is to move the safe-mode and base_dir checks to the new php_fopen_and_set_opened_path() function so that it will finally be completely centralized. This means less duplicate code (in a zillion places) and probably a safer safe-mode which isn't too safe these days as it will be in a central place. Does anyone see a problem with moving the safe_mode and base_dir stuff to this function and out of the rest of fopen-wrappers? I think nothing should be effected unless something was buggy before. (I'm not touching open_primary_script) right now. BTW, I would REALLY like to remove the duplicate code from fopen_url_wrapper() but if I do it quickly it would mean losing that warning at the end of the function if what you're opening isn't a URL. I think it's a good change and no biggy to lose that warning. It would make things a hell of a lot cleaner as in fopen_wrapper() if fopen_url_wrapper() returns NULL we could just continue. Andi
From: "Andi Gutmans" <andi@php.net> To: php-cvs@lists.php.net Date: Wed, 16 Aug 2000 19:26:21 -0000 Subject: [PHP-CVS] cvs: php4 /main fopen-wrappers.c fopen-wrappers.h
andi            Wed Aug 16 12:26:21 2000 EDT
Modified files:
    /php4/main  fopen-wrappers.c fopen-wrappers.h
Log: - The beginning of an attempt to cleanup fopen-wrappers. - I started with trying to localize the V_FOPEN's so that we can have a - version which won't really open the file for include_once/require_once to - work faster and have less chance of a race which would cause a fd leak. - What I did will, therefore, change but I want to do this step by step - because the code is extremley messy so first of all I want to make sure - that the isolating of the V_FOPEN code doesn't break anything. - How about moving URL stuff out of this file? - php_fopen_url_wrapper() copy and pasted the second part of - php_fopen_wrapper() (incorrectly). Please try not to copy&paste code but - centralize functionality. Need to think of a nice way to nuke one of the - copies and have both functions use the same one. Index: php4/main/fopen-wrappers.c diff -u php4/main/fopen-wrappers.c:1.75 php4/main/fopen-wrappers.c:1.76
--- php4/main/fopen-wrappers.c:1.75     Tue Aug  8 09:36:25 2000
+++ php4/main/fopen-wrappers.c Wed Aug 16 12:26:21 2000 @@ -16,7 +16,7 @@
    |          Jim Winstead <jimw@php.net>                                 |
    +----------------------------------------------------------------------+
*/ -/* $Id: fopen-wrappers.c,v 1.75 2000/08/08 16:36:25 stas Exp $ */ +/* $Id: fopen-wrappers.c,v 1.76 2000/08/16 19:26:21 andi Exp $ */ #include "php.h" #include "php_globals.h" @@ -247,6 +247,17 @@
        return 0;
} +FILE *php_fopen_and_set_opened_path(const char *path, char *mode, char **opened_path) +{
+               FILE *fp;
+
+               fp = V_FOPEN(path, mode);
+               if (fp && opened_path) {
+                       *opened_path = expand_filepath(path,NULL);
+               }
+               return fp;
+} + PHPAPI FILE *php_fopen_wrapper(char *path, char *mode, int options, int *issock, int *socketd, char **opened_path) {
        PLS_FETCH();
@@ -266,19 +277,13 @@
        if (options & USE_PATH && PG(include_path) != NULL) {
                return php_fopen_with_path(path, mode, PG(include_path), opened_path);
        } else {
-               FILE *fp;
-
                if (options & ENFORCE_SAFE_MODE && PG(safe_mode) && (!php_checkuid(path, mode, 0))) {
                        return NULL;
                }
                if (php_check_open_basedir(path)) {
                        return NULL;
-               }
-               fp = V_FOPEN(path, mode);
-               if (fp && opened_path) {
-                       *opened_path = expand_filepath(path,NULL);
                }
-               return fp;
+               return php_fopen_and_set_opened_path(path, mode, opened_path);
        }
} @@ -391,7 +396,6 @@
        char *pathbuf, *ptr, *end;
        char trypath[MAXPATHLEN + 1];
        struct stat sb;
-       FILE *fp;
        PLS_FETCH();
        if (opened_path) {
@@ -404,11 +408,8 @@
                        return NULL;
                }
                if (php_check_open_basedir(filename)) return NULL;
-               fp = V_FOPEN(filename, mode);
-               if (fp && opened_path) {
-                       *opened_path = expand_filepath(filename,NULL);
-               }
-               return fp;
+
+               return php_fopen_and_set_opened_path(filename, mode, opened_path);
        }
        /* Absolute path open - prepend document_root in safe mode */
#ifdef PHP_WIN32 @@ -426,20 +427,12 @@
                                return NULL;
                        }
                        if (php_check_open_basedir(trypath)) return NULL;
-                       fp = V_FOPEN(trypath, mode);
-                       if (fp && opened_path) {
-                               *opened_path = expand_filepath(trypath,NULL);
-                       }
-                       return fp;
+                       return php_fopen_and_set_opened_path(filename, mode, opened_path);
                } else {
                        if (php_check_open_basedir(filename)) {
                                return NULL;
                        }
-                       fp = V_FOPEN(filename, mode);
-                       if (fp && opened_path) {
-                               *opened_path = expand_filepath(filename,NULL);
-                       }
-                       return fp;
+                       return php_fopen_and_set_opened_path(filename, mode, opened_path);
                }
        }
        if (!path || (path && !*path)) {
@@ -448,12 +441,8 @@
                }
                if (php_check_open_basedir(filename)) {
                        return NULL;
-               }
-               fp = V_FOPEN(filename, mode);
-               if (fp && opened_path) {
-                       *opened_path = strdup(filename);
                }
-               return fp;
+               return php_fopen_and_set_opened_path(filename, mode, opened_path);
        }
        pathbuf = estrdup(path);
@@ -476,17 +465,9 @@
                                return NULL;
                        }
                }
-               if ((fp = V_FOPEN(trypath, mode)) != NULL) {
-                       if (php_check_open_basedir(trypath)) {
-                               fclose(fp);
-                               efree(pathbuf);
-                               return NULL;
-                       }
-                       if (opened_path) {
-                               *opened_path = expand_filepath(trypath,NULL);
-                       }
+               if (!php_check_open_basedir(trypath)) {
                        efree(pathbuf);
-                       return fp;
+                       return php_fopen_and_set_opened_path(trypath, mode, opened_path);
                }
                ptr = end;
        }
@@ -1036,12 +1017,10 @@
                                if (php_check_open_basedir((char *) path)) {
                                        fp = NULL;
                                } else {
-                                       fp = V_FOPEN(path, mode);
+                                       fp = php_fopen_and_set_opened_path(path, mode, opened_path);
                                }
                        }
                }
- -
                return (fp);
        }
@@ -1100,7 +1079,7 @@ #if 1 -PHPAPI char *expand_filepath(char *filepath, char *real_path) +PHPAPI char *expand_filepath(const char *filepath, char *real_path) {
        cwd_state new_state;
        char cwd[MAXPATHLEN+1];
Index: php4/main/fopen-wrappers.h diff -u php4/main/fopen-wrappers.h:1.21 php4/main/fopen-wrappers.h:1.22
--- php4/main/fopen-wrappers.h:1.21     Tue Aug  8 09:36:25 2000
+++ php4/main/fopen-wrappers.h Wed Aug 16 12:26:21 2000 @@ -15,16 +15,16 @@
    | Authors: Jim Winstead <jimw@php.net>                                 |
    +----------------------------------------------------------------------+
*/ -/* $Id: fopen-wrappers.h,v 1.21 2000/08/08 16:36:25 stas Exp $ */ +/* $Id: fopen-wrappers.h,v 1.22 2000/08/16 19:26:21 andi Exp $ */ #ifndef FOPEN_WRAPPERS_H #define FOPEN_WRAPPERS_H #include "php_globals.h"
-#define IGNORE_PATH    0
-#define USE_PATH       1
-#define IGNORE_URL     2
+#define IGNORE_PATH            0
+#define USE_PATH               1
+#define IGNORE_URL             2
/* There's no USE_URL. */ #ifdef PHP_WIN32 # define IGNORE_URL_WIN 2 @@ -76,7 +76,7 @@ PHPAPI int php_is_url(char *path); PHPAPI char *php_strip_url_passwd(char *path); -PHPAPI char *expand_filepath(char *filepath,char *real_path); +PHPAPI char *expand_filepath(const char *filepath,char *real_path); int php_init_fopen_wrappers(void); int php_shutdown_fopen_wrappers(void); -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-cvs-unsubscribe@lists.php.net For additional commands, e-mail: php-cvs-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net
--- Andi Gutmans <andi@zend.com> http://www.zend.com/

« previous php.dev (#29177) next »