Skip to content

Commit 2023a79

Browse files
committed
Improved folder protection.
1 parent f4bb181 commit 2023a79

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

server/plugins-available/apache2_plugin.inc.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ function onLoad() {
8282
$app->plugins->registerEvent('web_folder_user_update',$this->plugin_name,'web_folder_user');
8383
$app->plugins->registerEvent('web_folder_user_delete',$this->plugin_name,'web_folder_user');
8484

85+
$app->plugins->registerEvent('web_folder_update',$this->plugin_name,'web_folder_update');
8586
$app->plugins->registerEvent('web_folder_delete',$this->plugin_name,'web_folder_delete');
8687

8788
}
@@ -1295,6 +1296,79 @@ function web_folder_delete($event_name,$data) {
12951296
unlink($folder_path.'.htaccess');
12961297
$app->log('Removed file'.$folder_path.'.htaccess',LOGLEVEL_DEBUG);
12971298
}
1299+
}
1300+
1301+
//* Update folder protection, when path has been changed
1302+
function web_folder_update($event_name,$data) {
1303+
global $app, $conf;
1304+
1305+
$website = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".intval($data['new']['parent_domain_id']));
1306+
1307+
if(!is_array($website)) {
1308+
$app->log('Not able to retrieve folder or website record.',LOGLEVEL_DEBUG);
1309+
return false;
1310+
}
1311+
1312+
//* Get the folder path.
1313+
$old_folder_path = realpath($website['document_root'].'/web/'.$data['old']['path']);
1314+
if(substr($old_folder_path,-1 != '/')) $old_folder_path .= '/';
1315+
1316+
$new_folder_path = escapeshellcmd($website['document_root'].'/web/'.$data['new']['path']);
1317+
if(substr($new_folder_path,-1 != '/')) $new_folder_path .= '/';
1318+
1319+
//* Check if the resulting path is inside the docroot
1320+
if(stristr($new_folder_path,'..') || stristr($new_folder_path,'./') || stristr($new_folder_path,'\\')) {
1321+
$app->log('Folder path "'.$new_folder_path.'" contains .. or ./.',LOGLEVEL_DEBUG);
1322+
return false;
1323+
}
1324+
if(stristr($old_folder_path,'..') || stristr($old_folder_path,'./') || stristr($old_folder_path,'\\')) {
1325+
$app->log('Folder path "'.$old_folder_path.'" contains .. or ./.',LOGLEVEL_DEBUG);
1326+
return false;
1327+
}
1328+
1329+
//* Check if the resulting path is inside the docroot
1330+
if(substr($old_folder_path,0,strlen($website['document_root'])) != $website['document_root']) {
1331+
$app->log('Old folder path '.$old_folder_path.' is outside of docroot.',LOGLEVEL_DEBUG);
1332+
return false;
1333+
}
1334+
if(substr($new_folder_path,0,strlen($website['document_root'])) != $website['document_root']) {
1335+
$app->log('New folder path '.$new_folder_path.' is outside of docroot.',LOGLEVEL_DEBUG);
1336+
return false;
1337+
}
1338+
1339+
//* Create the folder path, if it does not exist
1340+
if(!is_dir($new_folder_path)) exec('mkdir -p '.$new_folder_path);
1341+
1342+
if($data['old']['path'] != $data['new']['path']) {
1343+
1344+
1345+
//* move .htpasswd file
1346+
if(is_file($old_folder_path.'.htpasswd')) {
1347+
rename($old_folder_path.'.htpasswd',$new_folder_path.'.htpasswd');
1348+
$app->log('Moved file'.$new_folder_path.'.htpasswd',LOGLEVEL_DEBUG);
1349+
}
1350+
1351+
//* move .htpasswd file
1352+
if(is_file($old_folder_path.'.htaccess')) {
1353+
rename($old_folder_path.'.htaccess',$new_folder_path.'.htaccess');
1354+
$app->log('Moved file'.$new_folder_path.'.htaccess',LOGLEVEL_DEBUG);
1355+
}
1356+
1357+
}
1358+
1359+
//* Create the .htaccess file
1360+
if($data['new']['active'] == 'y' && !is_file($new_folder_path.'.htaccess')) {
1361+
$ht_file = "AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$folder_path.".htpasswd\nrequire valid-user";
1362+
file_put_contents($new_folder_path.'.htaccess',$ht_file);
1363+
chmod($new_folder_path.'.htpasswd',0755);
1364+
$app->log('Created file'.$new_folder_path.'.htaccess',LOGLEVEL_DEBUG);
1365+
}
1366+
1367+
//* Remove .htaccess file
1368+
if($data['new']['active'] == 'n' && is_file($new_folder_path.'.htaccess')) {
1369+
unlink($new_folder_path.'.htaccess');
1370+
$app->log('Removed file'.$new_folder_path.'.htaccess',LOGLEVEL_DEBUG);
1371+
}
12981372

12991373

13001374
}

0 commit comments

Comments
 (0)