Skip to content

Commit 05e629f

Browse files
committed
jailkit cleanup: remove broken symlinks
1 parent c5a58c0 commit 05e629f

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

server/lib/classes/system.inc.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -941,26 +941,26 @@ function move($file1, $file2) {
941941
return $return_var == 0 ? true : false;
942942
}
943943

944-
function rmdir($dir, $recursive=false) {
944+
function rmdir($path, $recursive=false) {
945945
// Disallow operating on root directory
946-
if(realpath($dir) == '/') {
947-
$app->log("rmdir: afraid I might delete root: $dir", LOGLEVEL_WARN);
946+
if(realpath($path) == '/') {
947+
$app->log("rmdir: afraid I might delete root: $path", LOGLEVEL_WARN);
948948
return false;
949949
}
950950

951-
$dir = rtrim($dir, '/');
952-
if (is_dir($dir)) {
953-
$objects = array_diff(scandir($dir), array('.', '..'));
951+
$path = rtrim($path, '/');
952+
if (is_dir($path)) {
953+
$objects = array_diff(scandir($path), array('.', '..'));
954954
foreach ($objects as $object) {
955955
if ($recursive) {
956-
if (is_dir("$dir/$object")) {
957-
$this->rmdir("$dir/$object", $recursive);
956+
if (is_dir("$path/$object")) {
957+
$this->rmdir("$path/$object", $recursive);
958958
} else {
959-
unlink ("$dir/$object");
959+
unlink ("$path/$object");
960960
}
961961
}
962962
}
963-
return rmdir($dir);
963+
return rmdir($path);
964964
}
965965
return false;
966966
}
@@ -1007,6 +1007,28 @@ public function create_relative_link($f, $t) {
10071007
return symlink($cfrom, $to);
10081008
}
10091009

1010+
function remove_broken_symlinks($path, $recursive=false) {
1011+
if ($path != '/') {
1012+
$path = rtrim($path, '/');
1013+
}
1014+
if (is_dir($path)) {
1015+
$objects = array_diff(scandir($path), array('.', '..'));
1016+
foreach ($objects as $object) {
1017+
if ($recursive) {
1018+
if (is_dir("$path/$object")) {
1019+
$this->remove_broken_symlinks("$path/$object", $recursive);
1020+
} elseif (is_link("$path/$object") && !file_exists("$path/$object")) {
1021+
$app->log("removing broken symlink $path/$object", LOGLEVEL_DEBUG);
1022+
unlink ("$path/$object");
1023+
}
1024+
}
1025+
}
1026+
} elseif (is_link("$path") && !file_exists("$path")) {
1027+
$app->log("removing broken symlink $path", LOGLEVEL_DEBUG);
1028+
unlink ("$path");
1029+
}
1030+
}
1031+
10101032
function checkpath($path) {
10111033
$path = trim($path);
10121034
//* We allow only absolute paths
@@ -2465,8 +2487,7 @@ public function update_jailkit_chroot($home_dir, $sections = array(), $programs
24652487
}
24662488
}
24672489

2468-
// remove dangling symlinks
2469-
$app->log("TODO: search for and remove dangling symlinks", LOGLEVEL_DEBUG);
2490+
$this->remove_broken_symlinks($dir, true);
24702491
}
24712492

24722493

@@ -2602,7 +2623,7 @@ public function delete_jailkit_chroot($home_dir) {
26022623
if (is_file('/etc/jailkit/jk_socketd.ini')) {
26032624
$jk_socketd_ini = $app->ini_parser->parse_ini_file('/etc/jailkit/jk_socketd.ini');
26042625
$log = $home . '/dev/log';
2605-
if (isset($jk_socketd_ini[$log]) {
2626+
if (isset($jk_socketd_ini[$log])) {
26062627
unset($jk_socketd_ini[$log]);
26072628
$app->log('delete_jailkit_chroot: writing /etc/jailkit/jk_socketd.ini', LOGLEVEL_DEBUG);
26082629
$app->ini_parse->write_ini_file($jk_socketd_ini, '/etc/jailkit/jk_socketd.ini');

0 commit comments

Comments
 (0)