From 586e08ac32984e09b238bd034cd0e5cd4cf67705 Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 5 Sep 2006 22:38:59 +0000 Subject: [PATCH] backported detection of potentially insecure dataroot location SC#295 --- lib/adminlib.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/adminlib.php b/lib/adminlib.php index e3c7bcbd4fb..56511b5e938 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -349,4 +349,35 @@ function print_progress($done, $total, $updatetime=5, $sleeptime=1, $donetext='' sleep($sleeptime); } } + +/** + * Try to verify that dataroot is not accessible from web. + * It is not 100% correct but might help to reduce number of vulnerable sites. + * + * Protection from httpd.conf and .htaccess is not detected properly. + */ +function is_dataroot_insecure() { + global $CFG; + + $siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround + + $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1); + $rp = strrev(trim($rp, '/')); + $rp = explode('/', $rp); + foreach($rp as $r) { + if (strpos($siteroot, '/'.$r.'/') === 0) { + $siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory + } else { + break; // probably alias root + } + } + + $siteroot = strrev($siteroot); + $dataroot = str_replace('\\', '/', $CFG->dataroot.'/'); + + if (strpos($dataroot, $siteroot) === 0) { + return true; + } + return false; +} ?>