]> granicus.if.org Git - php/commitdiff
MFH: - Changed open_basedir to allow tightening in runtime contexts. (Sara)
authorHannes Magnusson <bjori@php.net>
Tue, 9 Dec 2008 10:20:11 +0000 (10:20 +0000)
committerHannes Magnusson <bjori@php.net>
Tue, 9 Dec 2008 10:20:11 +0000 (10:20 +0000)
- Add test

NEWS
ext/standard/tests/directory/open_basedir_001.phpt [new file with mode: 0644]
main/fopen_wrappers.c
main/fopen_wrappers.h

diff --git a/NEWS b/NEWS
index bb57046ad60fb4266e419f8abe807a635eb00675..37536296844879b6e7eea9e81e96b5462145e22d 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@ PHP                                                                        NEWS
 ?? ??? 200?, PHP 5.3.0 Alpha 4
 - Changed opendir/dir/scandir to use default context
   when no context argument is passed. (Sara)
+- Changed open_basedir to allow tightening in runtime contexts. (Sara)
 
 - Fixed bug #46811 ini_set() doesn't return false on failure. (Hannes)
 
diff --git a/ext/standard/tests/directory/open_basedir_001.phpt b/ext/standard/tests/directory/open_basedir_001.phpt
new file mode 100644 (file)
index 0000000..092eac2
--- /dev/null
@@ -0,0 +1,17 @@
+--TEST--
+openbase_dir runtime tightning
+--INI--
+open_basedir=/usr/local
+--FILE--
+<?php
+var_dump(ini_set("open_basedir", "/usr/local/bin"));
+var_dump(ini_get("open_basedir"));
+var_dump(ini_set("open_basedir", "/usr"));
+var_dump(ini_get("open_basedir"));
+?>
+--EXPECT--
+string(10) "/usr/local"
+string(14) "/usr/local/bin"
+bool(false)
+string(14) "/usr/local/bin"
+
index feabfb85cc88ab311c12d4ceee11d7e883e47a09..b6b1f3a49dd5f55de16a89f59f4cf4e6f151330d 100644 (file)
 #endif
 /* }}} */
 
+/* {{{ OnUpdateBaseDir
+Allows any change to open_basedir setting in during Startup and Shutdown events,
+or a tightening during activation/runtime/deactivation */
+PHPAPI ZEND_INI_MH(OnUpdateBaseDir)
+{
+       char **p, *pathbuf, *ptr, *end;
+#ifndef ZTS
+       char *base = (char *) mh_arg2;
+#else
+       char *base = (char *) ts_resource(*((int *) mh_arg2));
+#endif
+
+       p = (char **) (base + (size_t) mh_arg1);
+
+       if (stage == PHP_INI_STAGE_STARTUP || stage == PHP_INI_STAGE_SHUTDOWN) {
+               /* We're in a PHP_INI_SYSTEM context, no restrictions */
+               *p = new_value;
+               return SUCCESS;
+       }
+
+       /* Otherwise we're in runtime */
+       if (!*p || !**p) {
+               /* open_basedir not set yet, go ahead and give it a value */
+               *p = new_value;
+               return SUCCESS;
+       }
+
+       /* Shortcut: When we have a open_basedir and someone tries to unset, we know it'll fail */
+       if (!new_value || !*new_value) {
+               return FAILURE;
+       }
+
+       /* Is the proposed open_basedir at least as restrictive as the current setting? */
+       ptr = pathbuf = estrdup(new_value);
+       while (ptr && *ptr) {
+               end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
+               if (end != NULL) {
+                       *end = '\0';
+                       end++;
+               }
+               if (php_check_open_basedir_ex(ptr, 0 TSRMLS_CC) != 0) {
+                       /* At least one portion of this open_basedir is less restrictive than the prior one, FAIL */
+                       efree(pathbuf);
+                       return FAILURE;
+               }
+               ptr = end;
+       }
+       efree(pathbuf);
+
+       /* Everything checks out, set it */
+       *p = new_value;
+
+       return SUCCESS;
+}
+/* }}} */
+
 /* {{{ php_check_specific_open_basedir
        When open_basedir is not NULL, check if the given filename is located in
        open_basedir. Returns -1 if error or not in the open_basedir, else 0.
index 1b8113c10cff768feb28e41c5d2618b65baf8f30..b58bc02ac97b8fd6838e9d58f056f2b619834dae 100644 (file)
@@ -23,6 +23,7 @@
 
 BEGIN_EXTERN_C()
 #include "php_globals.h"
+#include "php_ini.h"
 
 PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle TSRMLS_DC);
 PHPAPI char *expand_filepath(const char *filepath, char *real_path TSRMLS_DC);
@@ -39,6 +40,8 @@ PHPAPI char *php_resolve_path(const char *filename, int filename_len, const char
 PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path TSRMLS_DC);
 
 PHPAPI char *php_strip_url_passwd(char *path);
+
+PHPAPI ZEND_INI_MH(OnUpdateBaseDir);
 END_EXTERN_C()
 
 #endif